scsi: lpfc: Fix panic when FW-log buffsize is not initialized
[linux-2.6-block.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_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
396         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
397 }
398
399 /**
400  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
401  * @q: The Event Queue that the host has completed processing for.
402  * @arm: Indicates whether the host wants to arms this CQ.
403  *
404  * This routine will mark all Event Queue Entries on @q, from the last
405  * known completed entry to the last entry that was processed, as completed
406  * by clearing the valid bit for each completion queue entry. Then it will
407  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
408  * The internal host index in the @q will be updated by this routine to indicate
409  * that the host has finished processing the entries. The @arm parameter
410  * indicates that the queue should be rearmed when ringing the doorbell.
411  *
412  * This function will return the number of EQEs that were popped.
413  **/
414 uint32_t
415 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
416 {
417         uint32_t released = 0;
418         struct lpfc_hba *phba;
419         struct lpfc_eqe *temp_eqe;
420         struct lpfc_register doorbell;
421
422         /* sanity check on queue memory */
423         if (unlikely(!q))
424                 return 0;
425         phba = q->phba;
426
427         /* while there are valid entries */
428         while (q->hba_index != q->host_index) {
429                 if (!phba->sli4_hba.pc_sli4_params.eqav) {
430                         temp_eqe = q->qe[q->host_index].eqe;
431                         bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
432                 }
433                 released++;
434                 q->host_index = ((q->host_index + 1) % q->entry_count);
435         }
436         if (unlikely(released == 0 && !arm))
437                 return 0;
438
439         /* ring doorbell for number popped */
440         doorbell.word0 = 0;
441         if (arm) {
442                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
443                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
444         }
445         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
446         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
447         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
448                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
449         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
450         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
451         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
452         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
453                 readl(q->phba->sli4_hba.EQDBregaddr);
454         return released;
455 }
456
457 /**
458  * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
459  * @q: The Event Queue that the host has completed processing for.
460  * @arm: Indicates whether the host wants to arms this CQ.
461  *
462  * This routine will mark all Event Queue Entries on @q, from the last
463  * known completed entry to the last entry that was processed, as completed
464  * by clearing the valid bit for each completion queue entry. Then it will
465  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
466  * The internal host index in the @q will be updated by this routine to indicate
467  * that the host has finished processing the entries. The @arm parameter
468  * indicates that the queue should be rearmed when ringing the doorbell.
469  *
470  * This function will return the number of EQEs that were popped.
471  **/
472 uint32_t
473 lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
474 {
475         uint32_t released = 0;
476         struct lpfc_hba *phba;
477         struct lpfc_eqe *temp_eqe;
478         struct lpfc_register doorbell;
479
480         /* sanity check on queue memory */
481         if (unlikely(!q))
482                 return 0;
483         phba = q->phba;
484
485         /* while there are valid entries */
486         while (q->hba_index != q->host_index) {
487                 if (!phba->sli4_hba.pc_sli4_params.eqav) {
488                         temp_eqe = q->qe[q->host_index].eqe;
489                         bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
490                 }
491                 released++;
492                 q->host_index = ((q->host_index + 1) % q->entry_count);
493         }
494         if (unlikely(released == 0 && !arm))
495                 return 0;
496
497         /* ring doorbell for number popped */
498         doorbell.word0 = 0;
499         if (arm)
500                 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
501         bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
502         bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
503         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
504         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
505         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
506                 readl(q->phba->sli4_hba.EQDBregaddr);
507         return released;
508 }
509
510 /**
511  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
512  * @q: The Completion Queue to get the first valid CQE from
513  *
514  * This routine will get the first valid Completion Queue Entry from @q, update
515  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
516  * the Queue (no more work to do), or the Queue is full of CQEs that have been
517  * processed, but not popped back to the HBA then this routine will return NULL.
518  **/
519 static struct lpfc_cqe *
520 lpfc_sli4_cq_get(struct lpfc_queue *q)
521 {
522         struct lpfc_hba *phba;
523         struct lpfc_cqe *cqe;
524         uint32_t idx;
525
526         /* sanity check on queue memory */
527         if (unlikely(!q))
528                 return NULL;
529         phba = q->phba;
530         cqe = q->qe[q->hba_index].cqe;
531
532         /* If the next CQE is not valid then we are done */
533         if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
534                 return NULL;
535         /* If the host has not yet processed the next entry then we are done */
536         idx = ((q->hba_index + 1) % q->entry_count);
537         if (idx == q->host_index)
538                 return NULL;
539
540         q->hba_index = idx;
541         /* if the index wrapped around, toggle the valid bit */
542         if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
543                 q->qe_valid = (q->qe_valid) ? 0 : 1;
544
545         /*
546          * insert barrier for instruction interlock : data from the hardware
547          * must have the valid bit checked before it can be copied and acted
548          * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
549          * instructions allowing action on content before valid bit checked,
550          * add barrier here as well. May not be needed as "content" is a
551          * single 32-bit entity here (vs multi word structure for cq's).
552          */
553         mb();
554         return cqe;
555 }
556
557 /**
558  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
559  * @q: The Completion Queue that the host has completed processing for.
560  * @arm: Indicates whether the host wants to arms this CQ.
561  *
562  * This routine will mark all Completion queue entries on @q, from the last
563  * known completed entry to the last entry that was processed, as completed
564  * by clearing the valid bit for each completion queue entry. Then it will
565  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
566  * The internal host index in the @q will be updated by this routine to indicate
567  * that the host has finished processing the entries. The @arm parameter
568  * indicates that the queue should be rearmed when ringing the doorbell.
569  *
570  * This function will return the number of CQEs that were released.
571  **/
572 uint32_t
573 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
574 {
575         uint32_t released = 0;
576         struct lpfc_hba *phba;
577         struct lpfc_cqe *temp_qe;
578         struct lpfc_register doorbell;
579
580         /* sanity check on queue memory */
581         if (unlikely(!q))
582                 return 0;
583         phba = q->phba;
584
585         /* while there are valid entries */
586         while (q->hba_index != q->host_index) {
587                 if (!phba->sli4_hba.pc_sli4_params.cqav) {
588                         temp_qe = q->qe[q->host_index].cqe;
589                         bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
590                 }
591                 released++;
592                 q->host_index = ((q->host_index + 1) % q->entry_count);
593         }
594         if (unlikely(released == 0 && !arm))
595                 return 0;
596
597         /* ring doorbell for number popped */
598         doorbell.word0 = 0;
599         if (arm)
600                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
601         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
602         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
603         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
604                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
605         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
606         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
607         return released;
608 }
609
610 /**
611  * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
612  * @q: The Completion Queue that the host has completed processing for.
613  * @arm: Indicates whether the host wants to arms this CQ.
614  *
615  * This routine will mark all Completion queue entries on @q, from the last
616  * known completed entry to the last entry that was processed, as completed
617  * by clearing the valid bit for each completion queue entry. Then it will
618  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
619  * The internal host index in the @q will be updated by this routine to indicate
620  * that the host has finished processing the entries. The @arm parameter
621  * indicates that the queue should be rearmed when ringing the doorbell.
622  *
623  * This function will return the number of CQEs that were released.
624  **/
625 uint32_t
626 lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
627 {
628         uint32_t released = 0;
629         struct lpfc_hba *phba;
630         struct lpfc_cqe *temp_qe;
631         struct lpfc_register doorbell;
632
633         /* sanity check on queue memory */
634         if (unlikely(!q))
635                 return 0;
636         phba = q->phba;
637
638         /* while there are valid entries */
639         while (q->hba_index != q->host_index) {
640                 if (!phba->sli4_hba.pc_sli4_params.cqav) {
641                         temp_qe = q->qe[q->host_index].cqe;
642                         bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
643                 }
644                 released++;
645                 q->host_index = ((q->host_index + 1) % q->entry_count);
646         }
647         if (unlikely(released == 0 && !arm))
648                 return 0;
649
650         /* ring doorbell for number popped */
651         doorbell.word0 = 0;
652         if (arm)
653                 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
654         bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
655         bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
656         writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
657         return released;
658 }
659
660 /**
661  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
662  * @q: The Header Receive Queue to operate on.
663  * @wqe: The Receive Queue Entry to put on the Receive queue.
664  *
665  * This routine will copy the contents of @wqe to the next available entry on
666  * the @q. This function will then ring the Receive Queue Doorbell to signal the
667  * HBA to start processing the Receive Queue Entry. This function returns the
668  * index that the rqe was copied to if successful. If no entries are available
669  * on @q then this function will return -ENOMEM.
670  * The caller is expected to hold the hbalock when calling this routine.
671  **/
672 int
673 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
674                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
675 {
676         struct lpfc_rqe *temp_hrqe;
677         struct lpfc_rqe *temp_drqe;
678         struct lpfc_register doorbell;
679         int hq_put_index;
680         int dq_put_index;
681
682         /* sanity check on queue memory */
683         if (unlikely(!hq) || unlikely(!dq))
684                 return -ENOMEM;
685         hq_put_index = hq->host_index;
686         dq_put_index = dq->host_index;
687         temp_hrqe = hq->qe[hq_put_index].rqe;
688         temp_drqe = dq->qe[dq_put_index].rqe;
689
690         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
691                 return -EINVAL;
692         if (hq_put_index != dq_put_index)
693                 return -EINVAL;
694         /* If the host has not yet processed the next entry then we are done */
695         if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
696                 return -EBUSY;
697         lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
698         lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
699
700         /* Update the host index to point to the next slot */
701         hq->host_index = ((hq_put_index + 1) % hq->entry_count);
702         dq->host_index = ((dq_put_index + 1) % dq->entry_count);
703         hq->RQ_buf_posted++;
704
705         /* Ring The Header Receive Queue Doorbell */
706         if (!(hq->host_index % hq->entry_repost)) {
707                 doorbell.word0 = 0;
708                 if (hq->db_format == LPFC_DB_RING_FORMAT) {
709                         bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
710                                hq->entry_repost);
711                         bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
712                 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
713                         bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
714                                hq->entry_repost);
715                         bf_set(lpfc_rq_db_list_fm_index, &doorbell,
716                                hq->host_index);
717                         bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
718                 } else {
719                         return -EINVAL;
720                 }
721                 writel(doorbell.word0, hq->db_regaddr);
722         }
723         return hq_put_index;
724 }
725
726 /**
727  * lpfc_sli4_rq_release - Updates internal hba index for RQ
728  * @q: The Header Receive Queue to operate on.
729  *
730  * This routine will update the HBA index of a queue to reflect consumption of
731  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
732  * consumed an entry the host calls this function to update the queue's
733  * internal pointers. This routine returns the number of entries that were
734  * consumed by the HBA.
735  **/
736 static uint32_t
737 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
738 {
739         /* sanity check on queue memory */
740         if (unlikely(!hq) || unlikely(!dq))
741                 return 0;
742
743         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
744                 return 0;
745         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
746         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
747         return 1;
748 }
749
750 /**
751  * lpfc_cmd_iocb - Get next command iocb entry in the ring
752  * @phba: Pointer to HBA context object.
753  * @pring: Pointer to driver SLI ring object.
754  *
755  * This function returns pointer to next command iocb entry
756  * in the command ring. The caller must hold hbalock to prevent
757  * other threads consume the next command iocb.
758  * SLI-2/SLI-3 provide different sized iocbs.
759  **/
760 static inline IOCB_t *
761 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
762 {
763         return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
764                            pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
765 }
766
767 /**
768  * lpfc_resp_iocb - Get next response iocb entry in the ring
769  * @phba: Pointer to HBA context object.
770  * @pring: Pointer to driver SLI ring object.
771  *
772  * This function returns pointer to next response iocb entry
773  * in the response ring. The caller must hold hbalock to make sure
774  * that no other thread consume the next response iocb.
775  * SLI-2/SLI-3 provide different sized iocbs.
776  **/
777 static inline IOCB_t *
778 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
779 {
780         return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
781                            pring->sli.sli3.rspidx * phba->iocb_rsp_size);
782 }
783
784 /**
785  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
786  * @phba: Pointer to HBA context object.
787  *
788  * This function is called with hbalock held. This function
789  * allocates a new driver iocb object from the iocb pool. If the
790  * allocation is successful, it returns pointer to the newly
791  * allocated iocb object else it returns NULL.
792  **/
793 struct lpfc_iocbq *
794 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
795 {
796         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
797         struct lpfc_iocbq * iocbq = NULL;
798
799         lockdep_assert_held(&phba->hbalock);
800
801         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
802         if (iocbq)
803                 phba->iocb_cnt++;
804         if (phba->iocb_cnt > phba->iocb_max)
805                 phba->iocb_max = phba->iocb_cnt;
806         return iocbq;
807 }
808
809 /**
810  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
811  * @phba: Pointer to HBA context object.
812  * @xritag: XRI value.
813  *
814  * This function clears the sglq pointer from the array of acive
815  * sglq's. The xritag that is passed in is used to index into the
816  * array. Before the xritag can be used it needs to be adjusted
817  * by subtracting the xribase.
818  *
819  * Returns sglq ponter = success, NULL = Failure.
820  **/
821 struct lpfc_sglq *
822 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
823 {
824         struct lpfc_sglq *sglq;
825
826         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
827         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
828         return sglq;
829 }
830
831 /**
832  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
833  * @phba: Pointer to HBA context object.
834  * @xritag: XRI value.
835  *
836  * This function returns the sglq pointer from the array of acive
837  * sglq's. The xritag that is passed in is used to index into the
838  * array. Before the xritag can be used it needs to be adjusted
839  * by subtracting the xribase.
840  *
841  * Returns sglq ponter = success, NULL = Failure.
842  **/
843 struct lpfc_sglq *
844 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
845 {
846         struct lpfc_sglq *sglq;
847
848         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
849         return sglq;
850 }
851
852 /**
853  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
854  * @phba: Pointer to HBA context object.
855  * @xritag: xri used in this exchange.
856  * @rrq: The RRQ to be cleared.
857  *
858  **/
859 void
860 lpfc_clr_rrq_active(struct lpfc_hba *phba,
861                     uint16_t xritag,
862                     struct lpfc_node_rrq *rrq)
863 {
864         struct lpfc_nodelist *ndlp = NULL;
865
866         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
867                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
868
869         /* The target DID could have been swapped (cable swap)
870          * we should use the ndlp from the findnode if it is
871          * available.
872          */
873         if ((!ndlp) && rrq->ndlp)
874                 ndlp = rrq->ndlp;
875
876         if (!ndlp)
877                 goto out;
878
879         if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
880                 rrq->send_rrq = 0;
881                 rrq->xritag = 0;
882                 rrq->rrq_stop_time = 0;
883         }
884 out:
885         mempool_free(rrq, phba->rrq_pool);
886 }
887
888 /**
889  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
890  * @phba: Pointer to HBA context object.
891  *
892  * This function is called with hbalock held. This function
893  * Checks if stop_time (ratov from setting rrq active) has
894  * been reached, if it has and the send_rrq flag is set then
895  * it will call lpfc_send_rrq. If the send_rrq flag is not set
896  * then it will just call the routine to clear the rrq and
897  * free the rrq resource.
898  * The timer is set to the next rrq that is going to expire before
899  * leaving the routine.
900  *
901  **/
902 void
903 lpfc_handle_rrq_active(struct lpfc_hba *phba)
904 {
905         struct lpfc_node_rrq *rrq;
906         struct lpfc_node_rrq *nextrrq;
907         unsigned long next_time;
908         unsigned long iflags;
909         LIST_HEAD(send_rrq);
910
911         spin_lock_irqsave(&phba->hbalock, iflags);
912         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
913         next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
914         list_for_each_entry_safe(rrq, nextrrq,
915                                  &phba->active_rrq_list, list) {
916                 if (time_after(jiffies, rrq->rrq_stop_time))
917                         list_move(&rrq->list, &send_rrq);
918                 else if (time_before(rrq->rrq_stop_time, next_time))
919                         next_time = rrq->rrq_stop_time;
920         }
921         spin_unlock_irqrestore(&phba->hbalock, iflags);
922         if ((!list_empty(&phba->active_rrq_list)) &&
923             (!(phba->pport->load_flag & FC_UNLOADING)))
924                 mod_timer(&phba->rrq_tmr, next_time);
925         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
926                 list_del(&rrq->list);
927                 if (!rrq->send_rrq)
928                         /* this call will free the rrq */
929                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
930                 else if (lpfc_send_rrq(phba, rrq)) {
931                         /* if we send the rrq then the completion handler
932                         *  will clear the bit in the xribitmap.
933                         */
934                         lpfc_clr_rrq_active(phba, rrq->xritag,
935                                             rrq);
936                 }
937         }
938 }
939
940 /**
941  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
942  * @vport: Pointer to vport context object.
943  * @xri: The xri used in the exchange.
944  * @did: The targets DID for this exchange.
945  *
946  * returns NULL = rrq not found in the phba->active_rrq_list.
947  *         rrq = rrq for this xri and target.
948  **/
949 struct lpfc_node_rrq *
950 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
951 {
952         struct lpfc_hba *phba = vport->phba;
953         struct lpfc_node_rrq *rrq;
954         struct lpfc_node_rrq *nextrrq;
955         unsigned long iflags;
956
957         if (phba->sli_rev != LPFC_SLI_REV4)
958                 return NULL;
959         spin_lock_irqsave(&phba->hbalock, iflags);
960         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
961                 if (rrq->vport == vport && rrq->xritag == xri &&
962                                 rrq->nlp_DID == did){
963                         list_del(&rrq->list);
964                         spin_unlock_irqrestore(&phba->hbalock, iflags);
965                         return rrq;
966                 }
967         }
968         spin_unlock_irqrestore(&phba->hbalock, iflags);
969         return NULL;
970 }
971
972 /**
973  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
974  * @vport: Pointer to vport context object.
975  * @ndlp: Pointer to the lpfc_node_list structure.
976  * If ndlp is NULL Remove all active RRQs for this vport from the
977  * phba->active_rrq_list and clear the rrq.
978  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
979  **/
980 void
981 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
982
983 {
984         struct lpfc_hba *phba = vport->phba;
985         struct lpfc_node_rrq *rrq;
986         struct lpfc_node_rrq *nextrrq;
987         unsigned long iflags;
988         LIST_HEAD(rrq_list);
989
990         if (phba->sli_rev != LPFC_SLI_REV4)
991                 return;
992         if (!ndlp) {
993                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
994                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
995         }
996         spin_lock_irqsave(&phba->hbalock, iflags);
997         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
998                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
999                         list_move(&rrq->list, &rrq_list);
1000         spin_unlock_irqrestore(&phba->hbalock, iflags);
1001
1002         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1003                 list_del(&rrq->list);
1004                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1005         }
1006 }
1007
1008 /**
1009  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
1010  * @phba: Pointer to HBA context object.
1011  * @ndlp: Targets nodelist pointer for this exchange.
1012  * @xritag the xri in the bitmap to test.
1013  *
1014  * This function is called with hbalock held. This function
1015  * returns 0 = rrq not active for this xri
1016  *         1 = rrq is valid for this xri.
1017  **/
1018 int
1019 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1020                         uint16_t  xritag)
1021 {
1022         lockdep_assert_held(&phba->hbalock);
1023         if (!ndlp)
1024                 return 0;
1025         if (!ndlp->active_rrqs_xri_bitmap)
1026                 return 0;
1027         if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1028                         return 1;
1029         else
1030                 return 0;
1031 }
1032
1033 /**
1034  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1035  * @phba: Pointer to HBA context object.
1036  * @ndlp: nodelist pointer for this target.
1037  * @xritag: xri used in this exchange.
1038  * @rxid: Remote Exchange ID.
1039  * @send_rrq: Flag used to determine if we should send rrq els cmd.
1040  *
1041  * This function takes the hbalock.
1042  * The active bit is always set in the active rrq xri_bitmap even
1043  * if there is no slot avaiable for the other rrq information.
1044  *
1045  * returns 0 rrq actived for this xri
1046  *         < 0 No memory or invalid ndlp.
1047  **/
1048 int
1049 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1050                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1051 {
1052         unsigned long iflags;
1053         struct lpfc_node_rrq *rrq;
1054         int empty;
1055
1056         if (!ndlp)
1057                 return -EINVAL;
1058
1059         if (!phba->cfg_enable_rrq)
1060                 return -EINVAL;
1061
1062         spin_lock_irqsave(&phba->hbalock, iflags);
1063         if (phba->pport->load_flag & FC_UNLOADING) {
1064                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1065                 goto out;
1066         }
1067
1068         /*
1069          * set the active bit even if there is no mem available.
1070          */
1071         if (NLP_CHK_FREE_REQ(ndlp))
1072                 goto out;
1073
1074         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1075                 goto out;
1076
1077         if (!ndlp->active_rrqs_xri_bitmap)
1078                 goto out;
1079
1080         if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1081                 goto out;
1082
1083         spin_unlock_irqrestore(&phba->hbalock, iflags);
1084         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1085         if (!rrq) {
1086                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1087                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1088                                 " DID:0x%x Send:%d\n",
1089                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
1090                 return -EINVAL;
1091         }
1092         if (phba->cfg_enable_rrq == 1)
1093                 rrq->send_rrq = send_rrq;
1094         else
1095                 rrq->send_rrq = 0;
1096         rrq->xritag = xritag;
1097         rrq->rrq_stop_time = jiffies +
1098                                 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1099         rrq->ndlp = ndlp;
1100         rrq->nlp_DID = ndlp->nlp_DID;
1101         rrq->vport = ndlp->vport;
1102         rrq->rxid = rxid;
1103         spin_lock_irqsave(&phba->hbalock, iflags);
1104         empty = list_empty(&phba->active_rrq_list);
1105         list_add_tail(&rrq->list, &phba->active_rrq_list);
1106         phba->hba_flag |= HBA_RRQ_ACTIVE;
1107         if (empty)
1108                 lpfc_worker_wake_up(phba);
1109         spin_unlock_irqrestore(&phba->hbalock, iflags);
1110         return 0;
1111 out:
1112         spin_unlock_irqrestore(&phba->hbalock, iflags);
1113         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1114                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1115                         " DID:0x%x Send:%d\n",
1116                         xritag, rxid, ndlp->nlp_DID, send_rrq);
1117         return -EINVAL;
1118 }
1119
1120 /**
1121  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1122  * @phba: Pointer to HBA context object.
1123  * @piocb: Pointer to the iocbq.
1124  *
1125  * This function is called with the ring lock held. This function
1126  * gets a new driver sglq object from the sglq list. If the
1127  * list is not empty then it is successful, it returns pointer to the newly
1128  * allocated sglq object else it returns NULL.
1129  **/
1130 static struct lpfc_sglq *
1131 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1132 {
1133         struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1134         struct lpfc_sglq *sglq = NULL;
1135         struct lpfc_sglq *start_sglq = NULL;
1136         struct lpfc_scsi_buf *lpfc_cmd;
1137         struct lpfc_nodelist *ndlp;
1138         int found = 0;
1139
1140         lockdep_assert_held(&phba->hbalock);
1141
1142         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
1143                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1144                 ndlp = lpfc_cmd->rdata->pnode;
1145         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
1146                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
1147                 ndlp = piocbq->context_un.ndlp;
1148         } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1149                 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1150                         ndlp = NULL;
1151                 else
1152                         ndlp = piocbq->context_un.ndlp;
1153         } else {
1154                 ndlp = piocbq->context1;
1155         }
1156
1157         spin_lock(&phba->sli4_hba.sgl_list_lock);
1158         list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1159         start_sglq = sglq;
1160         while (!found) {
1161                 if (!sglq)
1162                         break;
1163                 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1164                     test_bit(sglq->sli4_lxritag,
1165                     ndlp->active_rrqs_xri_bitmap)) {
1166                         /* This xri has an rrq outstanding for this DID.
1167                          * put it back in the list and get another xri.
1168                          */
1169                         list_add_tail(&sglq->list, lpfc_els_sgl_list);
1170                         sglq = NULL;
1171                         list_remove_head(lpfc_els_sgl_list, sglq,
1172                                                 struct lpfc_sglq, list);
1173                         if (sglq == start_sglq) {
1174                                 list_add_tail(&sglq->list, lpfc_els_sgl_list);
1175                                 sglq = NULL;
1176                                 break;
1177                         } else
1178                                 continue;
1179                 }
1180                 sglq->ndlp = ndlp;
1181                 found = 1;
1182                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1183                 sglq->state = SGL_ALLOCATED;
1184         }
1185         spin_unlock(&phba->sli4_hba.sgl_list_lock);
1186         return sglq;
1187 }
1188
1189 /**
1190  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1191  * @phba: Pointer to HBA context object.
1192  * @piocb: Pointer to the iocbq.
1193  *
1194  * This function is called with the sgl_list lock held. This function
1195  * gets a new driver sglq object from the sglq list. If the
1196  * list is not empty then it is successful, it returns pointer to the newly
1197  * allocated sglq object else it returns NULL.
1198  **/
1199 struct lpfc_sglq *
1200 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1201 {
1202         struct list_head *lpfc_nvmet_sgl_list;
1203         struct lpfc_sglq *sglq = NULL;
1204
1205         lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1206
1207         lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1208
1209         list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1210         if (!sglq)
1211                 return NULL;
1212         phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1213         sglq->state = SGL_ALLOCATED;
1214         return sglq;
1215 }
1216
1217 /**
1218  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1219  * @phba: Pointer to HBA context object.
1220  *
1221  * This function is called with no lock held. This function
1222  * allocates a new driver iocb object from the iocb pool. If the
1223  * allocation is successful, it returns pointer to the newly
1224  * allocated iocb object else it returns NULL.
1225  **/
1226 struct lpfc_iocbq *
1227 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1228 {
1229         struct lpfc_iocbq * iocbq = NULL;
1230         unsigned long iflags;
1231
1232         spin_lock_irqsave(&phba->hbalock, iflags);
1233         iocbq = __lpfc_sli_get_iocbq(phba);
1234         spin_unlock_irqrestore(&phba->hbalock, iflags);
1235         return iocbq;
1236 }
1237
1238 /**
1239  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1240  * @phba: Pointer to HBA context object.
1241  * @iocbq: Pointer to driver iocb object.
1242  *
1243  * This function is called with hbalock held to release driver
1244  * iocb object to the iocb pool. The iotag in the iocb object
1245  * does not change for each use of the iocb object. This function
1246  * clears all other fields of the iocb object when it is freed.
1247  * The sqlq structure that holds the xritag and phys and virtual
1248  * mappings for the scatter gather list is retrieved from the
1249  * active array of sglq. The get of the sglq pointer also clears
1250  * the entry in the array. If the status of the IO indiactes that
1251  * this IO was aborted then the sglq entry it put on the
1252  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1253  * IO has good status or fails for any other reason then the sglq
1254  * entry is added to the free list (lpfc_els_sgl_list).
1255  **/
1256 static void
1257 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1258 {
1259         struct lpfc_sglq *sglq;
1260         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1261         unsigned long iflag = 0;
1262         struct lpfc_sli_ring *pring;
1263
1264         lockdep_assert_held(&phba->hbalock);
1265
1266         if (iocbq->sli4_xritag == NO_XRI)
1267                 sglq = NULL;
1268         else
1269                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1270
1271
1272         if (sglq)  {
1273                 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1274                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1275                                           iflag);
1276                         sglq->state = SGL_FREED;
1277                         sglq->ndlp = NULL;
1278                         list_add_tail(&sglq->list,
1279                                       &phba->sli4_hba.lpfc_nvmet_sgl_list);
1280                         spin_unlock_irqrestore(
1281                                 &phba->sli4_hba.sgl_list_lock, iflag);
1282                         goto out;
1283                 }
1284
1285                 pring = phba->sli4_hba.els_wq->pring;
1286                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1287                         (sglq->state != SGL_XRI_ABORTED)) {
1288                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1289                                           iflag);
1290                         list_add(&sglq->list,
1291                                  &phba->sli4_hba.lpfc_abts_els_sgl_list);
1292                         spin_unlock_irqrestore(
1293                                 &phba->sli4_hba.sgl_list_lock, iflag);
1294                 } else {
1295                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1296                                           iflag);
1297                         sglq->state = SGL_FREED;
1298                         sglq->ndlp = NULL;
1299                         list_add_tail(&sglq->list,
1300                                       &phba->sli4_hba.lpfc_els_sgl_list);
1301                         spin_unlock_irqrestore(
1302                                 &phba->sli4_hba.sgl_list_lock, iflag);
1303
1304                         /* Check if TXQ queue needs to be serviced */
1305                         if (!list_empty(&pring->txq))
1306                                 lpfc_worker_wake_up(phba);
1307                 }
1308         }
1309
1310 out:
1311         /*
1312          * Clean all volatile data fields, preserve iotag and node struct.
1313          */
1314         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1315         iocbq->sli4_lxritag = NO_XRI;
1316         iocbq->sli4_xritag = NO_XRI;
1317         iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1318                               LPFC_IO_NVME_LS);
1319         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1320 }
1321
1322
1323 /**
1324  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1325  * @phba: Pointer to HBA context object.
1326  * @iocbq: Pointer to driver iocb object.
1327  *
1328  * This function is called with hbalock held to release driver
1329  * iocb object to the iocb pool. The iotag in the iocb object
1330  * does not change for each use of the iocb object. This function
1331  * clears all other fields of the iocb object when it is freed.
1332  **/
1333 static void
1334 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1335 {
1336         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1337
1338         lockdep_assert_held(&phba->hbalock);
1339
1340         /*
1341          * Clean all volatile data fields, preserve iotag and node struct.
1342          */
1343         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1344         iocbq->sli4_xritag = NO_XRI;
1345         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1346 }
1347
1348 /**
1349  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1350  * @phba: Pointer to HBA context object.
1351  * @iocbq: Pointer to driver iocb object.
1352  *
1353  * This function is called with hbalock held to release driver
1354  * iocb object to the iocb pool. The iotag in the iocb object
1355  * does not change for each use of the iocb object. This function
1356  * clears all other fields of the iocb object when it is freed.
1357  **/
1358 static void
1359 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1360 {
1361         lockdep_assert_held(&phba->hbalock);
1362
1363         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1364         phba->iocb_cnt--;
1365 }
1366
1367 /**
1368  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1369  * @phba: Pointer to HBA context object.
1370  * @iocbq: Pointer to driver iocb object.
1371  *
1372  * This function is called with no lock held to release the iocb to
1373  * iocb pool.
1374  **/
1375 void
1376 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1377 {
1378         unsigned long iflags;
1379
1380         /*
1381          * Clean all volatile data fields, preserve iotag and node struct.
1382          */
1383         spin_lock_irqsave(&phba->hbalock, iflags);
1384         __lpfc_sli_release_iocbq(phba, iocbq);
1385         spin_unlock_irqrestore(&phba->hbalock, iflags);
1386 }
1387
1388 /**
1389  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1390  * @phba: Pointer to HBA context object.
1391  * @iocblist: List of IOCBs.
1392  * @ulpstatus: ULP status in IOCB command field.
1393  * @ulpWord4: ULP word-4 in IOCB command field.
1394  *
1395  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1396  * on the list by invoking the complete callback function associated with the
1397  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1398  * fields.
1399  **/
1400 void
1401 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1402                       uint32_t ulpstatus, uint32_t ulpWord4)
1403 {
1404         struct lpfc_iocbq *piocb;
1405
1406         while (!list_empty(iocblist)) {
1407                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1408                 if (!piocb->iocb_cmpl)
1409                         lpfc_sli_release_iocbq(phba, piocb);
1410                 else {
1411                         piocb->iocb.ulpStatus = ulpstatus;
1412                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1413                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1414                 }
1415         }
1416         return;
1417 }
1418
1419 /**
1420  * lpfc_sli_iocb_cmd_type - Get the iocb type
1421  * @iocb_cmnd: iocb command code.
1422  *
1423  * This function is called by ring event handler function to get the iocb type.
1424  * This function translates the iocb command to an iocb command type used to
1425  * decide the final disposition of each completed IOCB.
1426  * The function returns
1427  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1428  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1429  * LPFC_ABORT_IOCB   if it is an abort iocb
1430  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1431  *
1432  * The caller is not required to hold any lock.
1433  **/
1434 static lpfc_iocb_type
1435 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1436 {
1437         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1438
1439         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1440                 return 0;
1441
1442         switch (iocb_cmnd) {
1443         case CMD_XMIT_SEQUENCE_CR:
1444         case CMD_XMIT_SEQUENCE_CX:
1445         case CMD_XMIT_BCAST_CN:
1446         case CMD_XMIT_BCAST_CX:
1447         case CMD_ELS_REQUEST_CR:
1448         case CMD_ELS_REQUEST_CX:
1449         case CMD_CREATE_XRI_CR:
1450         case CMD_CREATE_XRI_CX:
1451         case CMD_GET_RPI_CN:
1452         case CMD_XMIT_ELS_RSP_CX:
1453         case CMD_GET_RPI_CR:
1454         case CMD_FCP_IWRITE_CR:
1455         case CMD_FCP_IWRITE_CX:
1456         case CMD_FCP_IREAD_CR:
1457         case CMD_FCP_IREAD_CX:
1458         case CMD_FCP_ICMND_CR:
1459         case CMD_FCP_ICMND_CX:
1460         case CMD_FCP_TSEND_CX:
1461         case CMD_FCP_TRSP_CX:
1462         case CMD_FCP_TRECEIVE_CX:
1463         case CMD_FCP_AUTO_TRSP_CX:
1464         case CMD_ADAPTER_MSG:
1465         case CMD_ADAPTER_DUMP:
1466         case CMD_XMIT_SEQUENCE64_CR:
1467         case CMD_XMIT_SEQUENCE64_CX:
1468         case CMD_XMIT_BCAST64_CN:
1469         case CMD_XMIT_BCAST64_CX:
1470         case CMD_ELS_REQUEST64_CR:
1471         case CMD_ELS_REQUEST64_CX:
1472         case CMD_FCP_IWRITE64_CR:
1473         case CMD_FCP_IWRITE64_CX:
1474         case CMD_FCP_IREAD64_CR:
1475         case CMD_FCP_IREAD64_CX:
1476         case CMD_FCP_ICMND64_CR:
1477         case CMD_FCP_ICMND64_CX:
1478         case CMD_FCP_TSEND64_CX:
1479         case CMD_FCP_TRSP64_CX:
1480         case CMD_FCP_TRECEIVE64_CX:
1481         case CMD_GEN_REQUEST64_CR:
1482         case CMD_GEN_REQUEST64_CX:
1483         case CMD_XMIT_ELS_RSP64_CX:
1484         case DSSCMD_IWRITE64_CR:
1485         case DSSCMD_IWRITE64_CX:
1486         case DSSCMD_IREAD64_CR:
1487         case DSSCMD_IREAD64_CX:
1488                 type = LPFC_SOL_IOCB;
1489                 break;
1490         case CMD_ABORT_XRI_CN:
1491         case CMD_ABORT_XRI_CX:
1492         case CMD_CLOSE_XRI_CN:
1493         case CMD_CLOSE_XRI_CX:
1494         case CMD_XRI_ABORTED_CX:
1495         case CMD_ABORT_MXRI64_CN:
1496         case CMD_XMIT_BLS_RSP64_CX:
1497                 type = LPFC_ABORT_IOCB;
1498                 break;
1499         case CMD_RCV_SEQUENCE_CX:
1500         case CMD_RCV_ELS_REQ_CX:
1501         case CMD_RCV_SEQUENCE64_CX:
1502         case CMD_RCV_ELS_REQ64_CX:
1503         case CMD_ASYNC_STATUS:
1504         case CMD_IOCB_RCV_SEQ64_CX:
1505         case CMD_IOCB_RCV_ELS64_CX:
1506         case CMD_IOCB_RCV_CONT64_CX:
1507         case CMD_IOCB_RET_XRI64_CX:
1508                 type = LPFC_UNSOL_IOCB;
1509                 break;
1510         case CMD_IOCB_XMIT_MSEQ64_CR:
1511         case CMD_IOCB_XMIT_MSEQ64_CX:
1512         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1513         case CMD_IOCB_RCV_ELS_LIST64_CX:
1514         case CMD_IOCB_CLOSE_EXTENDED_CN:
1515         case CMD_IOCB_ABORT_EXTENDED_CN:
1516         case CMD_IOCB_RET_HBQE64_CN:
1517         case CMD_IOCB_FCP_IBIDIR64_CR:
1518         case CMD_IOCB_FCP_IBIDIR64_CX:
1519         case CMD_IOCB_FCP_ITASKMGT64_CX:
1520         case CMD_IOCB_LOGENTRY_CN:
1521         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1522                 printk("%s - Unhandled SLI-3 Command x%x\n",
1523                                 __func__, iocb_cmnd);
1524                 type = LPFC_UNKNOWN_IOCB;
1525                 break;
1526         default:
1527                 type = LPFC_UNKNOWN_IOCB;
1528                 break;
1529         }
1530
1531         return type;
1532 }
1533
1534 /**
1535  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1536  * @phba: Pointer to HBA context object.
1537  *
1538  * This function is called from SLI initialization code
1539  * to configure every ring of the HBA's SLI interface. The
1540  * caller is not required to hold any lock. This function issues
1541  * a config_ring mailbox command for each ring.
1542  * This function returns zero if successful else returns a negative
1543  * error code.
1544  **/
1545 static int
1546 lpfc_sli_ring_map(struct lpfc_hba *phba)
1547 {
1548         struct lpfc_sli *psli = &phba->sli;
1549         LPFC_MBOXQ_t *pmb;
1550         MAILBOX_t *pmbox;
1551         int i, rc, ret = 0;
1552
1553         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1554         if (!pmb)
1555                 return -ENOMEM;
1556         pmbox = &pmb->u.mb;
1557         phba->link_state = LPFC_INIT_MBX_CMDS;
1558         for (i = 0; i < psli->num_rings; i++) {
1559                 lpfc_config_ring(phba, i, pmb);
1560                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1561                 if (rc != MBX_SUCCESS) {
1562                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1563                                         "0446 Adapter failed to init (%d), "
1564                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1565                                         "ring %d\n",
1566                                         rc, pmbox->mbxCommand,
1567                                         pmbox->mbxStatus, i);
1568                         phba->link_state = LPFC_HBA_ERROR;
1569                         ret = -ENXIO;
1570                         break;
1571                 }
1572         }
1573         mempool_free(pmb, phba->mbox_mem_pool);
1574         return ret;
1575 }
1576
1577 /**
1578  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1579  * @phba: Pointer to HBA context object.
1580  * @pring: Pointer to driver SLI ring object.
1581  * @piocb: Pointer to the driver iocb object.
1582  *
1583  * This function is called with hbalock held. The function adds the
1584  * new iocb to txcmplq of the given ring. This function always returns
1585  * 0. If this function is called for ELS ring, this function checks if
1586  * there is a vport associated with the ELS command. This function also
1587  * starts els_tmofunc timer if this is an ELS command.
1588  **/
1589 static int
1590 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1591                         struct lpfc_iocbq *piocb)
1592 {
1593         lockdep_assert_held(&phba->hbalock);
1594
1595         BUG_ON(!piocb);
1596
1597         list_add_tail(&piocb->list, &pring->txcmplq);
1598         piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1599
1600         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1601            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1602            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1603                 BUG_ON(!piocb->vport);
1604                 if (!(piocb->vport->load_flag & FC_UNLOADING))
1605                         mod_timer(&piocb->vport->els_tmofunc,
1606                                   jiffies +
1607                                   msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1608         }
1609
1610         return 0;
1611 }
1612
1613 /**
1614  * lpfc_sli_ringtx_get - Get first element of the txq
1615  * @phba: Pointer to HBA context object.
1616  * @pring: Pointer to driver SLI ring object.
1617  *
1618  * This function is called with hbalock held to get next
1619  * iocb in txq of the given ring. If there is any iocb in
1620  * the txq, the function returns first iocb in the list after
1621  * removing the iocb from the list, else it returns NULL.
1622  **/
1623 struct lpfc_iocbq *
1624 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1625 {
1626         struct lpfc_iocbq *cmd_iocb;
1627
1628         lockdep_assert_held(&phba->hbalock);
1629
1630         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1631         return cmd_iocb;
1632 }
1633
1634 /**
1635  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1636  * @phba: Pointer to HBA context object.
1637  * @pring: Pointer to driver SLI ring object.
1638  *
1639  * This function is called with hbalock held and the caller must post the
1640  * iocb without releasing the lock. If the caller releases the lock,
1641  * iocb slot returned by the function is not guaranteed to be available.
1642  * The function returns pointer to the next available iocb slot if there
1643  * is available slot in the ring, else it returns NULL.
1644  * If the get index of the ring is ahead of the put index, the function
1645  * will post an error attention event to the worker thread to take the
1646  * HBA to offline state.
1647  **/
1648 static IOCB_t *
1649 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1650 {
1651         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1652         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1653
1654         lockdep_assert_held(&phba->hbalock);
1655
1656         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1657            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1658                 pring->sli.sli3.next_cmdidx = 0;
1659
1660         if (unlikely(pring->sli.sli3.local_getidx ==
1661                 pring->sli.sli3.next_cmdidx)) {
1662
1663                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1664
1665                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1666                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1667                                         "0315 Ring %d issue: portCmdGet %d "
1668                                         "is bigger than cmd ring %d\n",
1669                                         pring->ringno,
1670                                         pring->sli.sli3.local_getidx,
1671                                         max_cmd_idx);
1672
1673                         phba->link_state = LPFC_HBA_ERROR;
1674                         /*
1675                          * All error attention handlers are posted to
1676                          * worker thread
1677                          */
1678                         phba->work_ha |= HA_ERATT;
1679                         phba->work_hs = HS_FFER3;
1680
1681                         lpfc_worker_wake_up(phba);
1682
1683                         return NULL;
1684                 }
1685
1686                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1687                         return NULL;
1688         }
1689
1690         return lpfc_cmd_iocb(phba, pring);
1691 }
1692
1693 /**
1694  * lpfc_sli_next_iotag - Get an iotag for the iocb
1695  * @phba: Pointer to HBA context object.
1696  * @iocbq: Pointer to driver iocb object.
1697  *
1698  * This function gets an iotag for the iocb. If there is no unused iotag and
1699  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1700  * array and assigns a new iotag.
1701  * The function returns the allocated iotag if successful, else returns zero.
1702  * Zero is not a valid iotag.
1703  * The caller is not required to hold any lock.
1704  **/
1705 uint16_t
1706 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1707 {
1708         struct lpfc_iocbq **new_arr;
1709         struct lpfc_iocbq **old_arr;
1710         size_t new_len;
1711         struct lpfc_sli *psli = &phba->sli;
1712         uint16_t iotag;
1713
1714         spin_lock_irq(&phba->hbalock);
1715         iotag = psli->last_iotag;
1716         if(++iotag < psli->iocbq_lookup_len) {
1717                 psli->last_iotag = iotag;
1718                 psli->iocbq_lookup[iotag] = iocbq;
1719                 spin_unlock_irq(&phba->hbalock);
1720                 iocbq->iotag = iotag;
1721                 return iotag;
1722         } else if (psli->iocbq_lookup_len < (0xffff
1723                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1724                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1725                 spin_unlock_irq(&phba->hbalock);
1726                 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
1727                                   GFP_KERNEL);
1728                 if (new_arr) {
1729                         spin_lock_irq(&phba->hbalock);
1730                         old_arr = psli->iocbq_lookup;
1731                         if (new_len <= psli->iocbq_lookup_len) {
1732                                 /* highly unprobable case */
1733                                 kfree(new_arr);
1734                                 iotag = psli->last_iotag;
1735                                 if(++iotag < psli->iocbq_lookup_len) {
1736                                         psli->last_iotag = iotag;
1737                                         psli->iocbq_lookup[iotag] = iocbq;
1738                                         spin_unlock_irq(&phba->hbalock);
1739                                         iocbq->iotag = iotag;
1740                                         return iotag;
1741                                 }
1742                                 spin_unlock_irq(&phba->hbalock);
1743                                 return 0;
1744                         }
1745                         if (psli->iocbq_lookup)
1746                                 memcpy(new_arr, old_arr,
1747                                        ((psli->last_iotag  + 1) *
1748                                         sizeof (struct lpfc_iocbq *)));
1749                         psli->iocbq_lookup = new_arr;
1750                         psli->iocbq_lookup_len = new_len;
1751                         psli->last_iotag = iotag;
1752                         psli->iocbq_lookup[iotag] = iocbq;
1753                         spin_unlock_irq(&phba->hbalock);
1754                         iocbq->iotag = iotag;
1755                         kfree(old_arr);
1756                         return iotag;
1757                 }
1758         } else
1759                 spin_unlock_irq(&phba->hbalock);
1760
1761         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1762                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1763                         psli->last_iotag);
1764
1765         return 0;
1766 }
1767
1768 /**
1769  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1770  * @phba: Pointer to HBA context object.
1771  * @pring: Pointer to driver SLI ring object.
1772  * @iocb: Pointer to iocb slot in the ring.
1773  * @nextiocb: Pointer to driver iocb object which need to be
1774  *            posted to firmware.
1775  *
1776  * This function is called with hbalock held to post a new iocb to
1777  * the firmware. This function copies the new iocb to ring iocb slot and
1778  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1779  * a completion call back for this iocb else the function will free the
1780  * iocb object.
1781  **/
1782 static void
1783 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1784                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1785 {
1786         lockdep_assert_held(&phba->hbalock);
1787         /*
1788          * Set up an iotag
1789          */
1790         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1791
1792
1793         if (pring->ringno == LPFC_ELS_RING) {
1794                 lpfc_debugfs_slow_ring_trc(phba,
1795                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1796                         *(((uint32_t *) &nextiocb->iocb) + 4),
1797                         *(((uint32_t *) &nextiocb->iocb) + 6),
1798                         *(((uint32_t *) &nextiocb->iocb) + 7));
1799         }
1800
1801         /*
1802          * Issue iocb command to adapter
1803          */
1804         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1805         wmb();
1806         pring->stats.iocb_cmd++;
1807
1808         /*
1809          * If there is no completion routine to call, we can release the
1810          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1811          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1812          */
1813         if (nextiocb->iocb_cmpl)
1814                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1815         else
1816                 __lpfc_sli_release_iocbq(phba, nextiocb);
1817
1818         /*
1819          * Let the HBA know what IOCB slot will be the next one the
1820          * driver will put a command into.
1821          */
1822         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1823         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1824 }
1825
1826 /**
1827  * lpfc_sli_update_full_ring - Update the chip attention register
1828  * @phba: Pointer to HBA context object.
1829  * @pring: Pointer to driver SLI ring object.
1830  *
1831  * The caller is not required to hold any lock for calling this function.
1832  * This function updates the chip attention bits for the ring to inform firmware
1833  * that there are pending work to be done for this ring and requests an
1834  * interrupt when there is space available in the ring. This function is
1835  * called when the driver is unable to post more iocbs to the ring due
1836  * to unavailability of space in the ring.
1837  **/
1838 static void
1839 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1840 {
1841         int ringno = pring->ringno;
1842
1843         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1844
1845         wmb();
1846
1847         /*
1848          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1849          * The HBA will tell us when an IOCB entry is available.
1850          */
1851         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1852         readl(phba->CAregaddr); /* flush */
1853
1854         pring->stats.iocb_cmd_full++;
1855 }
1856
1857 /**
1858  * lpfc_sli_update_ring - Update chip attention register
1859  * @phba: Pointer to HBA context object.
1860  * @pring: Pointer to driver SLI ring object.
1861  *
1862  * This function updates the chip attention register bit for the
1863  * given ring to inform HBA that there is more work to be done
1864  * in this ring. The caller is not required to hold any lock.
1865  **/
1866 static void
1867 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1868 {
1869         int ringno = pring->ringno;
1870
1871         /*
1872          * Tell the HBA that there is work to do in this ring.
1873          */
1874         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1875                 wmb();
1876                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1877                 readl(phba->CAregaddr); /* flush */
1878         }
1879 }
1880
1881 /**
1882  * lpfc_sli_resume_iocb - Process iocbs in the txq
1883  * @phba: Pointer to HBA context object.
1884  * @pring: Pointer to driver SLI ring object.
1885  *
1886  * This function is called with hbalock held to post pending iocbs
1887  * in the txq to the firmware. This function is called when driver
1888  * detects space available in the ring.
1889  **/
1890 static void
1891 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1892 {
1893         IOCB_t *iocb;
1894         struct lpfc_iocbq *nextiocb;
1895
1896         lockdep_assert_held(&phba->hbalock);
1897
1898         /*
1899          * Check to see if:
1900          *  (a) there is anything on the txq to send
1901          *  (b) link is up
1902          *  (c) link attention events can be processed (fcp ring only)
1903          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1904          */
1905
1906         if (lpfc_is_link_up(phba) &&
1907             (!list_empty(&pring->txq)) &&
1908             (pring->ringno != LPFC_FCP_RING ||
1909              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1910
1911                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1912                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1913                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1914
1915                 if (iocb)
1916                         lpfc_sli_update_ring(phba, pring);
1917                 else
1918                         lpfc_sli_update_full_ring(phba, pring);
1919         }
1920
1921         return;
1922 }
1923
1924 /**
1925  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1926  * @phba: Pointer to HBA context object.
1927  * @hbqno: HBQ number.
1928  *
1929  * This function is called with hbalock held to get the next
1930  * available slot for the given HBQ. If there is free slot
1931  * available for the HBQ it will return pointer to the next available
1932  * HBQ entry else it will return NULL.
1933  **/
1934 static struct lpfc_hbq_entry *
1935 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1936 {
1937         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1938
1939         lockdep_assert_held(&phba->hbalock);
1940
1941         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1942             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1943                 hbqp->next_hbqPutIdx = 0;
1944
1945         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1946                 uint32_t raw_index = phba->hbq_get[hbqno];
1947                 uint32_t getidx = le32_to_cpu(raw_index);
1948
1949                 hbqp->local_hbqGetIdx = getidx;
1950
1951                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1952                         lpfc_printf_log(phba, KERN_ERR,
1953                                         LOG_SLI | LOG_VPORT,
1954                                         "1802 HBQ %d: local_hbqGetIdx "
1955                                         "%u is > than hbqp->entry_count %u\n",
1956                                         hbqno, hbqp->local_hbqGetIdx,
1957                                         hbqp->entry_count);
1958
1959                         phba->link_state = LPFC_HBA_ERROR;
1960                         return NULL;
1961                 }
1962
1963                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1964                         return NULL;
1965         }
1966
1967         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1968                         hbqp->hbqPutIdx;
1969 }
1970
1971 /**
1972  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1973  * @phba: Pointer to HBA context object.
1974  *
1975  * This function is called with no lock held to free all the
1976  * hbq buffers while uninitializing the SLI interface. It also
1977  * frees the HBQ buffers returned by the firmware but not yet
1978  * processed by the upper layers.
1979  **/
1980 void
1981 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1982 {
1983         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1984         struct hbq_dmabuf *hbq_buf;
1985         unsigned long flags;
1986         int i, hbq_count;
1987
1988         hbq_count = lpfc_sli_hbq_count();
1989         /* Return all memory used by all HBQs */
1990         spin_lock_irqsave(&phba->hbalock, flags);
1991         for (i = 0; i < hbq_count; ++i) {
1992                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1993                                 &phba->hbqs[i].hbq_buffer_list, list) {
1994                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1995                         list_del(&hbq_buf->dbuf.list);
1996                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1997                 }
1998                 phba->hbqs[i].buffer_count = 0;
1999         }
2000
2001         /* Mark the HBQs not in use */
2002         phba->hbq_in_use = 0;
2003         spin_unlock_irqrestore(&phba->hbalock, flags);
2004 }
2005
2006 /**
2007  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
2008  * @phba: Pointer to HBA context object.
2009  * @hbqno: HBQ number.
2010  * @hbq_buf: Pointer to HBQ buffer.
2011  *
2012  * This function is called with the hbalock held to post a
2013  * hbq buffer to the firmware. If the function finds an empty
2014  * slot in the HBQ, it will post the buffer. The function will return
2015  * pointer to the hbq entry if it successfully post the buffer
2016  * else it will return NULL.
2017  **/
2018 static int
2019 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
2020                          struct hbq_dmabuf *hbq_buf)
2021 {
2022         lockdep_assert_held(&phba->hbalock);
2023         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2024 }
2025
2026 /**
2027  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2028  * @phba: Pointer to HBA context object.
2029  * @hbqno: HBQ number.
2030  * @hbq_buf: Pointer to HBQ buffer.
2031  *
2032  * This function is called with the hbalock held to post a hbq buffer to the
2033  * firmware. If the function finds an empty slot in the HBQ, it will post the
2034  * buffer and place it on the hbq_buffer_list. The function will return zero if
2035  * it successfully post the buffer else it will return an error.
2036  **/
2037 static int
2038 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2039                             struct hbq_dmabuf *hbq_buf)
2040 {
2041         struct lpfc_hbq_entry *hbqe;
2042         dma_addr_t physaddr = hbq_buf->dbuf.phys;
2043
2044         lockdep_assert_held(&phba->hbalock);
2045         /* Get next HBQ entry slot to use */
2046         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2047         if (hbqe) {
2048                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2049
2050                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2051                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2052                 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2053                 hbqe->bde.tus.f.bdeFlags = 0;
2054                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2055                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2056                                 /* Sync SLIM */
2057                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2058                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2059                                 /* flush */
2060                 readl(phba->hbq_put + hbqno);
2061                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2062                 return 0;
2063         } else
2064                 return -ENOMEM;
2065 }
2066
2067 /**
2068  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2069  * @phba: Pointer to HBA context object.
2070  * @hbqno: HBQ number.
2071  * @hbq_buf: Pointer to HBQ buffer.
2072  *
2073  * This function is called with the hbalock held to post an RQE to the SLI4
2074  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2075  * the hbq_buffer_list and return zero, otherwise it will return an error.
2076  **/
2077 static int
2078 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2079                             struct hbq_dmabuf *hbq_buf)
2080 {
2081         int rc;
2082         struct lpfc_rqe hrqe;
2083         struct lpfc_rqe drqe;
2084         struct lpfc_queue *hrq;
2085         struct lpfc_queue *drq;
2086
2087         if (hbqno != LPFC_ELS_HBQ)
2088                 return 1;
2089         hrq = phba->sli4_hba.hdr_rq;
2090         drq = phba->sli4_hba.dat_rq;
2091
2092         lockdep_assert_held(&phba->hbalock);
2093         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2094         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2095         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2096         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2097         rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2098         if (rc < 0)
2099                 return rc;
2100         hbq_buf->tag = (rc | (hbqno << 16));
2101         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2102         return 0;
2103 }
2104
2105 /* HBQ for ELS and CT traffic. */
2106 static struct lpfc_hbq_init lpfc_els_hbq = {
2107         .rn = 1,
2108         .entry_count = 256,
2109         .mask_count = 0,
2110         .profile = 0,
2111         .ring_mask = (1 << LPFC_ELS_RING),
2112         .buffer_count = 0,
2113         .init_count = 40,
2114         .add_count = 40,
2115 };
2116
2117 /* Array of HBQs */
2118 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2119         &lpfc_els_hbq,
2120 };
2121
2122 /**
2123  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2124  * @phba: Pointer to HBA context object.
2125  * @hbqno: HBQ number.
2126  * @count: Number of HBQ buffers to be posted.
2127  *
2128  * This function is called with no lock held to post more hbq buffers to the
2129  * given HBQ. The function returns the number of HBQ buffers successfully
2130  * posted.
2131  **/
2132 static int
2133 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2134 {
2135         uint32_t i, posted = 0;
2136         unsigned long flags;
2137         struct hbq_dmabuf *hbq_buffer;
2138         LIST_HEAD(hbq_buf_list);
2139         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2140                 return 0;
2141
2142         if ((phba->hbqs[hbqno].buffer_count + count) >
2143             lpfc_hbq_defs[hbqno]->entry_count)
2144                 count = lpfc_hbq_defs[hbqno]->entry_count -
2145                                         phba->hbqs[hbqno].buffer_count;
2146         if (!count)
2147                 return 0;
2148         /* Allocate HBQ entries */
2149         for (i = 0; i < count; i++) {
2150                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2151                 if (!hbq_buffer)
2152                         break;
2153                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2154         }
2155         /* Check whether HBQ is still in use */
2156         spin_lock_irqsave(&phba->hbalock, flags);
2157         if (!phba->hbq_in_use)
2158                 goto err;
2159         while (!list_empty(&hbq_buf_list)) {
2160                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2161                                  dbuf.list);
2162                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2163                                       (hbqno << 16));
2164                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2165                         phba->hbqs[hbqno].buffer_count++;
2166                         posted++;
2167                 } else
2168                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2169         }
2170         spin_unlock_irqrestore(&phba->hbalock, flags);
2171         return posted;
2172 err:
2173         spin_unlock_irqrestore(&phba->hbalock, flags);
2174         while (!list_empty(&hbq_buf_list)) {
2175                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2176                                  dbuf.list);
2177                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2178         }
2179         return 0;
2180 }
2181
2182 /**
2183  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2184  * @phba: Pointer to HBA context object.
2185  * @qno: HBQ number.
2186  *
2187  * This function posts more buffers to the HBQ. This function
2188  * is called with no lock held. The function returns the number of HBQ entries
2189  * successfully allocated.
2190  **/
2191 int
2192 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2193 {
2194         if (phba->sli_rev == LPFC_SLI_REV4)
2195                 return 0;
2196         else
2197                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2198                                          lpfc_hbq_defs[qno]->add_count);
2199 }
2200
2201 /**
2202  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2203  * @phba: Pointer to HBA context object.
2204  * @qno:  HBQ queue number.
2205  *
2206  * This function is called from SLI initialization code path with
2207  * no lock held to post initial HBQ buffers to firmware. The
2208  * function returns the number of HBQ entries successfully allocated.
2209  **/
2210 static int
2211 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2212 {
2213         if (phba->sli_rev == LPFC_SLI_REV4)
2214                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2215                                         lpfc_hbq_defs[qno]->entry_count);
2216         else
2217                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2218                                          lpfc_hbq_defs[qno]->init_count);
2219 }
2220
2221 /**
2222  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2223  * @phba: Pointer to HBA context object.
2224  * @hbqno: HBQ number.
2225  *
2226  * This function removes the first hbq buffer on an hbq list and returns a
2227  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2228  **/
2229 static struct hbq_dmabuf *
2230 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2231 {
2232         struct lpfc_dmabuf *d_buf;
2233
2234         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2235         if (!d_buf)
2236                 return NULL;
2237         return container_of(d_buf, struct hbq_dmabuf, dbuf);
2238 }
2239
2240 /**
2241  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2242  * @phba: Pointer to HBA context object.
2243  * @hbqno: HBQ number.
2244  *
2245  * This function removes the first RQ buffer on an RQ buffer list and returns a
2246  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2247  **/
2248 static struct rqb_dmabuf *
2249 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2250 {
2251         struct lpfc_dmabuf *h_buf;
2252         struct lpfc_rqb *rqbp;
2253
2254         rqbp = hrq->rqbp;
2255         list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2256                          struct lpfc_dmabuf, list);
2257         if (!h_buf)
2258                 return NULL;
2259         rqbp->buffer_count--;
2260         return container_of(h_buf, struct rqb_dmabuf, hbuf);
2261 }
2262
2263 /**
2264  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2265  * @phba: Pointer to HBA context object.
2266  * @tag: Tag of the hbq buffer.
2267  *
2268  * This function searches for the hbq buffer associated with the given tag in
2269  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2270  * otherwise it returns NULL.
2271  **/
2272 static struct hbq_dmabuf *
2273 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2274 {
2275         struct lpfc_dmabuf *d_buf;
2276         struct hbq_dmabuf *hbq_buf;
2277         uint32_t hbqno;
2278
2279         hbqno = tag >> 16;
2280         if (hbqno >= LPFC_MAX_HBQS)
2281                 return NULL;
2282
2283         spin_lock_irq(&phba->hbalock);
2284         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2285                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2286                 if (hbq_buf->tag == tag) {
2287                         spin_unlock_irq(&phba->hbalock);
2288                         return hbq_buf;
2289                 }
2290         }
2291         spin_unlock_irq(&phba->hbalock);
2292         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2293                         "1803 Bad hbq tag. Data: x%x x%x\n",
2294                         tag, phba->hbqs[tag >> 16].buffer_count);
2295         return NULL;
2296 }
2297
2298 /**
2299  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2300  * @phba: Pointer to HBA context object.
2301  * @hbq_buffer: Pointer to HBQ buffer.
2302  *
2303  * This function is called with hbalock. This function gives back
2304  * the hbq buffer to firmware. If the HBQ does not have space to
2305  * post the buffer, it will free the buffer.
2306  **/
2307 void
2308 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2309 {
2310         uint32_t hbqno;
2311
2312         if (hbq_buffer) {
2313                 hbqno = hbq_buffer->tag >> 16;
2314                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2315                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2316         }
2317 }
2318
2319 /**
2320  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2321  * @mbxCommand: mailbox command code.
2322  *
2323  * This function is called by the mailbox event handler function to verify
2324  * that the completed mailbox command is a legitimate mailbox command. If the
2325  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2326  * and the mailbox event handler will take the HBA offline.
2327  **/
2328 static int
2329 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2330 {
2331         uint8_t ret;
2332
2333         switch (mbxCommand) {
2334         case MBX_LOAD_SM:
2335         case MBX_READ_NV:
2336         case MBX_WRITE_NV:
2337         case MBX_WRITE_VPARMS:
2338         case MBX_RUN_BIU_DIAG:
2339         case MBX_INIT_LINK:
2340         case MBX_DOWN_LINK:
2341         case MBX_CONFIG_LINK:
2342         case MBX_CONFIG_RING:
2343         case MBX_RESET_RING:
2344         case MBX_READ_CONFIG:
2345         case MBX_READ_RCONFIG:
2346         case MBX_READ_SPARM:
2347         case MBX_READ_STATUS:
2348         case MBX_READ_RPI:
2349         case MBX_READ_XRI:
2350         case MBX_READ_REV:
2351         case MBX_READ_LNK_STAT:
2352         case MBX_REG_LOGIN:
2353         case MBX_UNREG_LOGIN:
2354         case MBX_CLEAR_LA:
2355         case MBX_DUMP_MEMORY:
2356         case MBX_DUMP_CONTEXT:
2357         case MBX_RUN_DIAGS:
2358         case MBX_RESTART:
2359         case MBX_UPDATE_CFG:
2360         case MBX_DOWN_LOAD:
2361         case MBX_DEL_LD_ENTRY:
2362         case MBX_RUN_PROGRAM:
2363         case MBX_SET_MASK:
2364         case MBX_SET_VARIABLE:
2365         case MBX_UNREG_D_ID:
2366         case MBX_KILL_BOARD:
2367         case MBX_CONFIG_FARP:
2368         case MBX_BEACON:
2369         case MBX_LOAD_AREA:
2370         case MBX_RUN_BIU_DIAG64:
2371         case MBX_CONFIG_PORT:
2372         case MBX_READ_SPARM64:
2373         case MBX_READ_RPI64:
2374         case MBX_REG_LOGIN64:
2375         case MBX_READ_TOPOLOGY:
2376         case MBX_WRITE_WWN:
2377         case MBX_SET_DEBUG:
2378         case MBX_LOAD_EXP_ROM:
2379         case MBX_ASYNCEVT_ENABLE:
2380         case MBX_REG_VPI:
2381         case MBX_UNREG_VPI:
2382         case MBX_HEARTBEAT:
2383         case MBX_PORT_CAPABILITIES:
2384         case MBX_PORT_IOV_CONTROL:
2385         case MBX_SLI4_CONFIG:
2386         case MBX_SLI4_REQ_FTRS:
2387         case MBX_REG_FCFI:
2388         case MBX_UNREG_FCFI:
2389         case MBX_REG_VFI:
2390         case MBX_UNREG_VFI:
2391         case MBX_INIT_VPI:
2392         case MBX_INIT_VFI:
2393         case MBX_RESUME_RPI:
2394         case MBX_READ_EVENT_LOG_STATUS:
2395         case MBX_READ_EVENT_LOG:
2396         case MBX_SECURITY_MGMT:
2397         case MBX_AUTH_PORT:
2398         case MBX_ACCESS_VDATA:
2399                 ret = mbxCommand;
2400                 break;
2401         default:
2402                 ret = MBX_SHUTDOWN;
2403                 break;
2404         }
2405         return ret;
2406 }
2407
2408 /**
2409  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2410  * @phba: Pointer to HBA context object.
2411  * @pmboxq: Pointer to mailbox command.
2412  *
2413  * This is completion handler function for mailbox commands issued from
2414  * lpfc_sli_issue_mbox_wait function. This function is called by the
2415  * mailbox event handler function with no lock held. This function
2416  * will wake up thread waiting on the wait queue pointed by context1
2417  * of the mailbox.
2418  **/
2419 void
2420 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2421 {
2422         unsigned long drvr_flag;
2423         struct completion *pmbox_done;
2424
2425         /*
2426          * If pmbox_done is empty, the driver thread gave up waiting and
2427          * continued running.
2428          */
2429         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2430         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2431         pmbox_done = (struct completion *)pmboxq->context3;
2432         if (pmbox_done)
2433                 complete(pmbox_done);
2434         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2435         return;
2436 }
2437
2438
2439 /**
2440  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2441  * @phba: Pointer to HBA context object.
2442  * @pmb: Pointer to mailbox object.
2443  *
2444  * This function is the default mailbox completion handler. It
2445  * frees the memory resources associated with the completed mailbox
2446  * command. If the completed command is a REG_LOGIN mailbox command,
2447  * this function will issue a UREG_LOGIN to re-claim the RPI.
2448  **/
2449 void
2450 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2451 {
2452         struct lpfc_vport  *vport = pmb->vport;
2453         struct lpfc_dmabuf *mp;
2454         struct lpfc_nodelist *ndlp;
2455         struct Scsi_Host *shost;
2456         uint16_t rpi, vpi;
2457         int rc;
2458
2459         mp = (struct lpfc_dmabuf *) (pmb->context1);
2460
2461         if (mp) {
2462                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2463                 kfree(mp);
2464         }
2465
2466         /*
2467          * If a REG_LOGIN succeeded  after node is destroyed or node
2468          * is in re-discovery driver need to cleanup the RPI.
2469          */
2470         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2471             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2472             !pmb->u.mb.mbxStatus) {
2473                 rpi = pmb->u.mb.un.varWords[0];
2474                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2475                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2476                 pmb->vport = vport;
2477                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2478                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2479                 if (rc != MBX_NOT_FINISHED)
2480                         return;
2481         }
2482
2483         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2484                 !(phba->pport->load_flag & FC_UNLOADING) &&
2485                 !pmb->u.mb.mbxStatus) {
2486                 shost = lpfc_shost_from_vport(vport);
2487                 spin_lock_irq(shost->host_lock);
2488                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2489                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2490                 spin_unlock_irq(shost->host_lock);
2491         }
2492
2493         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2494                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2495                 lpfc_nlp_put(ndlp);
2496                 pmb->context2 = NULL;
2497         }
2498
2499         /* Check security permission status on INIT_LINK mailbox command */
2500         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2501             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2502                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2503                                 "2860 SLI authentication is required "
2504                                 "for INIT_LINK but has not done yet\n");
2505
2506         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2507                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2508         else
2509                 mempool_free(pmb, phba->mbox_mem_pool);
2510 }
2511  /**
2512  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2513  * @phba: Pointer to HBA context object.
2514  * @pmb: Pointer to mailbox object.
2515  *
2516  * This function is the unreg rpi mailbox completion handler. It
2517  * frees the memory resources associated with the completed mailbox
2518  * command. An additional refrenece is put on the ndlp to prevent
2519  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2520  * the unreg mailbox command completes, this routine puts the
2521  * reference back.
2522  *
2523  **/
2524 void
2525 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2526 {
2527         struct lpfc_vport  *vport = pmb->vport;
2528         struct lpfc_nodelist *ndlp;
2529
2530         ndlp = pmb->context1;
2531         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2532                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2533                     (bf_get(lpfc_sli_intf_if_type,
2534                      &phba->sli4_hba.sli_intf) >=
2535                      LPFC_SLI_INTF_IF_TYPE_2)) {
2536                         if (ndlp) {
2537                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2538                                                  "0010 UNREG_LOGIN vpi:%x "
2539                                                  "rpi:%x DID:%x map:%x %p\n",
2540                                                  vport->vpi, ndlp->nlp_rpi,
2541                                                  ndlp->nlp_DID,
2542                                                  ndlp->nlp_usg_map, ndlp);
2543                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2544                                 lpfc_nlp_put(ndlp);
2545                         }
2546                 }
2547         }
2548
2549         mempool_free(pmb, phba->mbox_mem_pool);
2550 }
2551
2552 /**
2553  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2554  * @phba: Pointer to HBA context object.
2555  *
2556  * This function is called with no lock held. This function processes all
2557  * the completed mailbox commands and gives it to upper layers. The interrupt
2558  * service routine processes mailbox completion interrupt and adds completed
2559  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2560  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2561  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2562  * function returns the mailbox commands to the upper layer by calling the
2563  * completion handler function of each mailbox.
2564  **/
2565 int
2566 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2567 {
2568         MAILBOX_t *pmbox;
2569         LPFC_MBOXQ_t *pmb;
2570         int rc;
2571         LIST_HEAD(cmplq);
2572
2573         phba->sli.slistat.mbox_event++;
2574
2575         /* Get all completed mailboxe buffers into the cmplq */
2576         spin_lock_irq(&phba->hbalock);
2577         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2578         spin_unlock_irq(&phba->hbalock);
2579
2580         /* Get a Mailbox buffer to setup mailbox commands for callback */
2581         do {
2582                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2583                 if (pmb == NULL)
2584                         break;
2585
2586                 pmbox = &pmb->u.mb;
2587
2588                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2589                         if (pmb->vport) {
2590                                 lpfc_debugfs_disc_trc(pmb->vport,
2591                                         LPFC_DISC_TRC_MBOX_VPORT,
2592                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2593                                         (uint32_t)pmbox->mbxCommand,
2594                                         pmbox->un.varWords[0],
2595                                         pmbox->un.varWords[1]);
2596                         }
2597                         else {
2598                                 lpfc_debugfs_disc_trc(phba->pport,
2599                                         LPFC_DISC_TRC_MBOX,
2600                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2601                                         (uint32_t)pmbox->mbxCommand,
2602                                         pmbox->un.varWords[0],
2603                                         pmbox->un.varWords[1]);
2604                         }
2605                 }
2606
2607                 /*
2608                  * It is a fatal error if unknown mbox command completion.
2609                  */
2610                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2611                     MBX_SHUTDOWN) {
2612                         /* Unknown mailbox command compl */
2613                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2614                                         "(%d):0323 Unknown Mailbox command "
2615                                         "x%x (x%x/x%x) Cmpl\n",
2616                                         pmb->vport ? pmb->vport->vpi : 0,
2617                                         pmbox->mbxCommand,
2618                                         lpfc_sli_config_mbox_subsys_get(phba,
2619                                                                         pmb),
2620                                         lpfc_sli_config_mbox_opcode_get(phba,
2621                                                                         pmb));
2622                         phba->link_state = LPFC_HBA_ERROR;
2623                         phba->work_hs = HS_FFER3;
2624                         lpfc_handle_eratt(phba);
2625                         continue;
2626                 }
2627
2628                 if (pmbox->mbxStatus) {
2629                         phba->sli.slistat.mbox_stat_err++;
2630                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2631                                 /* Mbox cmd cmpl error - RETRYing */
2632                                 lpfc_printf_log(phba, KERN_INFO,
2633                                         LOG_MBOX | LOG_SLI,
2634                                         "(%d):0305 Mbox cmd cmpl "
2635                                         "error - RETRYing Data: x%x "
2636                                         "(x%x/x%x) x%x x%x x%x\n",
2637                                         pmb->vport ? pmb->vport->vpi : 0,
2638                                         pmbox->mbxCommand,
2639                                         lpfc_sli_config_mbox_subsys_get(phba,
2640                                                                         pmb),
2641                                         lpfc_sli_config_mbox_opcode_get(phba,
2642                                                                         pmb),
2643                                         pmbox->mbxStatus,
2644                                         pmbox->un.varWords[0],
2645                                         pmb->vport->port_state);
2646                                 pmbox->mbxStatus = 0;
2647                                 pmbox->mbxOwner = OWN_HOST;
2648                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2649                                 if (rc != MBX_NOT_FINISHED)
2650                                         continue;
2651                         }
2652                 }
2653
2654                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2655                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2656                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2657                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2658                                 "x%x x%x x%x\n",
2659                                 pmb->vport ? pmb->vport->vpi : 0,
2660                                 pmbox->mbxCommand,
2661                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2662                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2663                                 pmb->mbox_cmpl,
2664                                 *((uint32_t *) pmbox),
2665                                 pmbox->un.varWords[0],
2666                                 pmbox->un.varWords[1],
2667                                 pmbox->un.varWords[2],
2668                                 pmbox->un.varWords[3],
2669                                 pmbox->un.varWords[4],
2670                                 pmbox->un.varWords[5],
2671                                 pmbox->un.varWords[6],
2672                                 pmbox->un.varWords[7],
2673                                 pmbox->un.varWords[8],
2674                                 pmbox->un.varWords[9],
2675                                 pmbox->un.varWords[10]);
2676
2677                 if (pmb->mbox_cmpl)
2678                         pmb->mbox_cmpl(phba,pmb);
2679         } while (1);
2680         return 0;
2681 }
2682
2683 /**
2684  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2685  * @phba: Pointer to HBA context object.
2686  * @pring: Pointer to driver SLI ring object.
2687  * @tag: buffer tag.
2688  *
2689  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2690  * is set in the tag the buffer is posted for a particular exchange,
2691  * the function will return the buffer without replacing the buffer.
2692  * If the buffer is for unsolicited ELS or CT traffic, this function
2693  * returns the buffer and also posts another buffer to the firmware.
2694  **/
2695 static struct lpfc_dmabuf *
2696 lpfc_sli_get_buff(struct lpfc_hba *phba,
2697                   struct lpfc_sli_ring *pring,
2698                   uint32_t tag)
2699 {
2700         struct hbq_dmabuf *hbq_entry;
2701
2702         if (tag & QUE_BUFTAG_BIT)
2703                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2704         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2705         if (!hbq_entry)
2706                 return NULL;
2707         return &hbq_entry->dbuf;
2708 }
2709
2710 /**
2711  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2712  * @phba: Pointer to HBA context object.
2713  * @pring: Pointer to driver SLI ring object.
2714  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2715  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2716  * @fch_type: the type for the first frame of the sequence.
2717  *
2718  * This function is called with no lock held. This function uses the r_ctl and
2719  * type of the received sequence to find the correct callback function to call
2720  * to process the sequence.
2721  **/
2722 static int
2723 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2724                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2725                          uint32_t fch_type)
2726 {
2727         int i;
2728
2729         switch (fch_type) {
2730         case FC_TYPE_NVME:
2731                 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
2732                 return 1;
2733         default:
2734                 break;
2735         }
2736
2737         /* unSolicited Responses */
2738         if (pring->prt[0].profile) {
2739                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2740                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2741                                                                         saveq);
2742                 return 1;
2743         }
2744         /* We must search, based on rctl / type
2745            for the right routine */
2746         for (i = 0; i < pring->num_mask; i++) {
2747                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2748                     (pring->prt[i].type == fch_type)) {
2749                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2750                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2751                                                 (phba, pring, saveq);
2752                         return 1;
2753                 }
2754         }
2755         return 0;
2756 }
2757
2758 /**
2759  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2760  * @phba: Pointer to HBA context object.
2761  * @pring: Pointer to driver SLI ring object.
2762  * @saveq: Pointer to the unsolicited iocb.
2763  *
2764  * This function is called with no lock held by the ring event handler
2765  * when there is an unsolicited iocb posted to the response ring by the
2766  * firmware. This function gets the buffer associated with the iocbs
2767  * and calls the event handler for the ring. This function handles both
2768  * qring buffers and hbq buffers.
2769  * When the function returns 1 the caller can free the iocb object otherwise
2770  * upper layer functions will free the iocb objects.
2771  **/
2772 static int
2773 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2774                             struct lpfc_iocbq *saveq)
2775 {
2776         IOCB_t           * irsp;
2777         WORD5            * w5p;
2778         uint32_t           Rctl, Type;
2779         struct lpfc_iocbq *iocbq;
2780         struct lpfc_dmabuf *dmzbuf;
2781
2782         irsp = &(saveq->iocb);
2783
2784         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2785                 if (pring->lpfc_sli_rcv_async_status)
2786                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2787                 else
2788                         lpfc_printf_log(phba,
2789                                         KERN_WARNING,
2790                                         LOG_SLI,
2791                                         "0316 Ring %d handler: unexpected "
2792                                         "ASYNC_STATUS iocb received evt_code "
2793                                         "0x%x\n",
2794                                         pring->ringno,
2795                                         irsp->un.asyncstat.evt_code);
2796                 return 1;
2797         }
2798
2799         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2800                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2801                 if (irsp->ulpBdeCount > 0) {
2802                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2803                                         irsp->un.ulpWord[3]);
2804                         lpfc_in_buf_free(phba, dmzbuf);
2805                 }
2806
2807                 if (irsp->ulpBdeCount > 1) {
2808                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2809                                         irsp->unsli3.sli3Words[3]);
2810                         lpfc_in_buf_free(phba, dmzbuf);
2811                 }
2812
2813                 if (irsp->ulpBdeCount > 2) {
2814                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2815                                 irsp->unsli3.sli3Words[7]);
2816                         lpfc_in_buf_free(phba, dmzbuf);
2817                 }
2818
2819                 return 1;
2820         }
2821
2822         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2823                 if (irsp->ulpBdeCount != 0) {
2824                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2825                                                 irsp->un.ulpWord[3]);
2826                         if (!saveq->context2)
2827                                 lpfc_printf_log(phba,
2828                                         KERN_ERR,
2829                                         LOG_SLI,
2830                                         "0341 Ring %d Cannot find buffer for "
2831                                         "an unsolicited iocb. tag 0x%x\n",
2832                                         pring->ringno,
2833                                         irsp->un.ulpWord[3]);
2834                 }
2835                 if (irsp->ulpBdeCount == 2) {
2836                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2837                                                 irsp->unsli3.sli3Words[7]);
2838                         if (!saveq->context3)
2839                                 lpfc_printf_log(phba,
2840                                         KERN_ERR,
2841                                         LOG_SLI,
2842                                         "0342 Ring %d Cannot find buffer for an"
2843                                         " unsolicited iocb. tag 0x%x\n",
2844                                         pring->ringno,
2845                                         irsp->unsli3.sli3Words[7]);
2846                 }
2847                 list_for_each_entry(iocbq, &saveq->list, list) {
2848                         irsp = &(iocbq->iocb);
2849                         if (irsp->ulpBdeCount != 0) {
2850                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2851                                                         irsp->un.ulpWord[3]);
2852                                 if (!iocbq->context2)
2853                                         lpfc_printf_log(phba,
2854                                                 KERN_ERR,
2855                                                 LOG_SLI,
2856                                                 "0343 Ring %d Cannot find "
2857                                                 "buffer for an unsolicited iocb"
2858                                                 ". tag 0x%x\n", pring->ringno,
2859                                                 irsp->un.ulpWord[3]);
2860                         }
2861                         if (irsp->ulpBdeCount == 2) {
2862                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2863                                                 irsp->unsli3.sli3Words[7]);
2864                                 if (!iocbq->context3)
2865                                         lpfc_printf_log(phba,
2866                                                 KERN_ERR,
2867                                                 LOG_SLI,
2868                                                 "0344 Ring %d Cannot find "
2869                                                 "buffer for an unsolicited "
2870                                                 "iocb. tag 0x%x\n",
2871                                                 pring->ringno,
2872                                                 irsp->unsli3.sli3Words[7]);
2873                         }
2874                 }
2875         }
2876         if (irsp->ulpBdeCount != 0 &&
2877             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2878              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2879                 int found = 0;
2880
2881                 /* search continue save q for same XRI */
2882                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2883                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2884                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
2885                                 list_add_tail(&saveq->list, &iocbq->list);
2886                                 found = 1;
2887                                 break;
2888                         }
2889                 }
2890                 if (!found)
2891                         list_add_tail(&saveq->clist,
2892                                       &pring->iocb_continue_saveq);
2893                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2894                         list_del_init(&iocbq->clist);
2895                         saveq = iocbq;
2896                         irsp = &(saveq->iocb);
2897                 } else
2898                         return 0;
2899         }
2900         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2901             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2902             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2903                 Rctl = FC_RCTL_ELS_REQ;
2904                 Type = FC_TYPE_ELS;
2905         } else {
2906                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2907                 Rctl = w5p->hcsw.Rctl;
2908                 Type = w5p->hcsw.Type;
2909
2910                 /* Firmware Workaround */
2911                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2912                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2913                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2914                         Rctl = FC_RCTL_ELS_REQ;
2915                         Type = FC_TYPE_ELS;
2916                         w5p->hcsw.Rctl = Rctl;
2917                         w5p->hcsw.Type = Type;
2918                 }
2919         }
2920
2921         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2922                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2923                                 "0313 Ring %d handler: unexpected Rctl x%x "
2924                                 "Type x%x received\n",
2925                                 pring->ringno, Rctl, Type);
2926
2927         return 1;
2928 }
2929
2930 /**
2931  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2932  * @phba: Pointer to HBA context object.
2933  * @pring: Pointer to driver SLI ring object.
2934  * @prspiocb: Pointer to response iocb object.
2935  *
2936  * This function looks up the iocb_lookup table to get the command iocb
2937  * corresponding to the given response iocb using the iotag of the
2938  * response iocb. This function is called with the hbalock held
2939  * for sli3 devices or the ring_lock for sli4 devices.
2940  * This function returns the command iocb object if it finds the command
2941  * iocb else returns NULL.
2942  **/
2943 static struct lpfc_iocbq *
2944 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2945                       struct lpfc_sli_ring *pring,
2946                       struct lpfc_iocbq *prspiocb)
2947 {
2948         struct lpfc_iocbq *cmd_iocb = NULL;
2949         uint16_t iotag;
2950         lockdep_assert_held(&phba->hbalock);
2951
2952         iotag = prspiocb->iocb.ulpIoTag;
2953
2954         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2955                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2956                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2957                         /* remove from txcmpl queue list */
2958                         list_del_init(&cmd_iocb->list);
2959                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2960                         return cmd_iocb;
2961                 }
2962         }
2963
2964         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2965                         "0317 iotag x%x is out of "
2966                         "range: max iotag x%x wd0 x%x\n",
2967                         iotag, phba->sli.last_iotag,
2968                         *(((uint32_t *) &prspiocb->iocb) + 7));
2969         return NULL;
2970 }
2971
2972 /**
2973  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2974  * @phba: Pointer to HBA context object.
2975  * @pring: Pointer to driver SLI ring object.
2976  * @iotag: IOCB tag.
2977  *
2978  * This function looks up the iocb_lookup table to get the command iocb
2979  * corresponding to the given iotag. This function is called with the
2980  * hbalock held.
2981  * This function returns the command iocb object if it finds the command
2982  * iocb else returns NULL.
2983  **/
2984 static struct lpfc_iocbq *
2985 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2986                              struct lpfc_sli_ring *pring, uint16_t iotag)
2987 {
2988         struct lpfc_iocbq *cmd_iocb = NULL;
2989
2990         lockdep_assert_held(&phba->hbalock);
2991         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2992                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2993                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2994                         /* remove from txcmpl queue list */
2995                         list_del_init(&cmd_iocb->list);
2996                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2997                         return cmd_iocb;
2998                 }
2999         }
3000
3001         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3002                         "0372 iotag x%x lookup error: max iotag (x%x) "
3003                         "iocb_flag x%x\n",
3004                         iotag, phba->sli.last_iotag,
3005                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3006         return NULL;
3007 }
3008
3009 /**
3010  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3011  * @phba: Pointer to HBA context object.
3012  * @pring: Pointer to driver SLI ring object.
3013  * @saveq: Pointer to the response iocb to be processed.
3014  *
3015  * This function is called by the ring event handler for non-fcp
3016  * rings when there is a new response iocb in the response ring.
3017  * The caller is not required to hold any locks. This function
3018  * gets the command iocb associated with the response iocb and
3019  * calls the completion handler for the command iocb. If there
3020  * is no completion handler, the function will free the resources
3021  * associated with command iocb. If the response iocb is for
3022  * an already aborted command iocb, the status of the completion
3023  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3024  * This function always returns 1.
3025  **/
3026 static int
3027 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3028                           struct lpfc_iocbq *saveq)
3029 {
3030         struct lpfc_iocbq *cmdiocbp;
3031         int rc = 1;
3032         unsigned long iflag;
3033
3034         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
3035         if (phba->sli_rev == LPFC_SLI_REV4)
3036                 spin_lock_irqsave(&pring->ring_lock, iflag);
3037         else
3038                 spin_lock_irqsave(&phba->hbalock, iflag);
3039         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3040         if (phba->sli_rev == LPFC_SLI_REV4)
3041                 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3042         else
3043                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3044
3045         if (cmdiocbp) {
3046                 if (cmdiocbp->iocb_cmpl) {
3047                         /*
3048                          * If an ELS command failed send an event to mgmt
3049                          * application.
3050                          */
3051                         if (saveq->iocb.ulpStatus &&
3052                              (pring->ringno == LPFC_ELS_RING) &&
3053                              (cmdiocbp->iocb.ulpCommand ==
3054                                 CMD_ELS_REQUEST64_CR))
3055                                 lpfc_send_els_failure_event(phba,
3056                                         cmdiocbp, saveq);
3057
3058                         /*
3059                          * Post all ELS completions to the worker thread.
3060                          * All other are passed to the completion callback.
3061                          */
3062                         if (pring->ringno == LPFC_ELS_RING) {
3063                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3064                                     (cmdiocbp->iocb_flag &
3065                                                         LPFC_DRIVER_ABORTED)) {
3066                                         spin_lock_irqsave(&phba->hbalock,
3067                                                           iflag);
3068                                         cmdiocbp->iocb_flag &=
3069                                                 ~LPFC_DRIVER_ABORTED;
3070                                         spin_unlock_irqrestore(&phba->hbalock,
3071                                                                iflag);
3072                                         saveq->iocb.ulpStatus =
3073                                                 IOSTAT_LOCAL_REJECT;
3074                                         saveq->iocb.un.ulpWord[4] =
3075                                                 IOERR_SLI_ABORTED;
3076
3077                                         /* Firmware could still be in progress
3078                                          * of DMAing payload, so don't free data
3079                                          * buffer till after a hbeat.
3080                                          */
3081                                         spin_lock_irqsave(&phba->hbalock,
3082                                                           iflag);
3083                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3084                                         spin_unlock_irqrestore(&phba->hbalock,
3085                                                                iflag);
3086                                 }
3087                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3088                                         if (saveq->iocb_flag &
3089                                             LPFC_EXCHANGE_BUSY) {
3090                                                 /* Set cmdiocb flag for the
3091                                                  * exchange busy so sgl (xri)
3092                                                  * will not be released until
3093                                                  * the abort xri is received
3094                                                  * from hba.
3095                                                  */
3096                                                 spin_lock_irqsave(
3097                                                         &phba->hbalock, iflag);
3098                                                 cmdiocbp->iocb_flag |=
3099                                                         LPFC_EXCHANGE_BUSY;
3100                                                 spin_unlock_irqrestore(
3101                                                         &phba->hbalock, iflag);
3102                                         }
3103                                         if (cmdiocbp->iocb_flag &
3104                                             LPFC_DRIVER_ABORTED) {
3105                                                 /*
3106                                                  * Clear LPFC_DRIVER_ABORTED
3107                                                  * bit in case it was driver
3108                                                  * initiated abort.
3109                                                  */
3110                                                 spin_lock_irqsave(
3111                                                         &phba->hbalock, iflag);
3112                                                 cmdiocbp->iocb_flag &=
3113                                                         ~LPFC_DRIVER_ABORTED;
3114                                                 spin_unlock_irqrestore(
3115                                                         &phba->hbalock, iflag);
3116                                                 cmdiocbp->iocb.ulpStatus =
3117                                                         IOSTAT_LOCAL_REJECT;
3118                                                 cmdiocbp->iocb.un.ulpWord[4] =
3119                                                         IOERR_ABORT_REQUESTED;
3120                                                 /*
3121                                                  * For SLI4, irsiocb contains
3122                                                  * NO_XRI in sli_xritag, it
3123                                                  * shall not affect releasing
3124                                                  * sgl (xri) process.
3125                                                  */
3126                                                 saveq->iocb.ulpStatus =
3127                                                         IOSTAT_LOCAL_REJECT;
3128                                                 saveq->iocb.un.ulpWord[4] =
3129                                                         IOERR_SLI_ABORTED;
3130                                                 spin_lock_irqsave(
3131                                                         &phba->hbalock, iflag);
3132                                                 saveq->iocb_flag |=
3133                                                         LPFC_DELAY_MEM_FREE;
3134                                                 spin_unlock_irqrestore(
3135                                                         &phba->hbalock, iflag);
3136                                         }
3137                                 }
3138                         }
3139                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3140                 } else
3141                         lpfc_sli_release_iocbq(phba, cmdiocbp);
3142         } else {
3143                 /*
3144                  * Unknown initiating command based on the response iotag.
3145                  * This could be the case on the ELS ring because of
3146                  * lpfc_els_abort().
3147                  */
3148                 if (pring->ringno != LPFC_ELS_RING) {
3149                         /*
3150                          * Ring <ringno> handler: unexpected completion IoTag
3151                          * <IoTag>
3152                          */
3153                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3154                                          "0322 Ring %d handler: "
3155                                          "unexpected completion IoTag x%x "
3156                                          "Data: x%x x%x x%x x%x\n",
3157                                          pring->ringno,
3158                                          saveq->iocb.ulpIoTag,
3159                                          saveq->iocb.ulpStatus,
3160                                          saveq->iocb.un.ulpWord[4],
3161                                          saveq->iocb.ulpCommand,
3162                                          saveq->iocb.ulpContext);
3163                 }
3164         }
3165
3166         return rc;
3167 }
3168
3169 /**
3170  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3171  * @phba: Pointer to HBA context object.
3172  * @pring: Pointer to driver SLI ring object.
3173  *
3174  * This function is called from the iocb ring event handlers when
3175  * put pointer is ahead of the get pointer for a ring. This function signal
3176  * an error attention condition to the worker thread and the worker
3177  * thread will transition the HBA to offline state.
3178  **/
3179 static void
3180 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3181 {
3182         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3183         /*
3184          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3185          * rsp ring <portRspMax>
3186          */
3187         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3188                         "0312 Ring %d handler: portRspPut %d "
3189                         "is bigger than rsp ring %d\n",
3190                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
3191                         pring->sli.sli3.numRiocb);
3192
3193         phba->link_state = LPFC_HBA_ERROR;
3194
3195         /*
3196          * All error attention handlers are posted to
3197          * worker thread
3198          */
3199         phba->work_ha |= HA_ERATT;
3200         phba->work_hs = HS_FFER3;
3201
3202         lpfc_worker_wake_up(phba);
3203
3204         return;
3205 }
3206
3207 /**
3208  * lpfc_poll_eratt - Error attention polling timer timeout handler
3209  * @ptr: Pointer to address of HBA context object.
3210  *
3211  * This function is invoked by the Error Attention polling timer when the
3212  * timer times out. It will check the SLI Error Attention register for
3213  * possible attention events. If so, it will post an Error Attention event
3214  * and wake up worker thread to process it. Otherwise, it will set up the
3215  * Error Attention polling timer for the next poll.
3216  **/
3217 void lpfc_poll_eratt(struct timer_list *t)
3218 {
3219         struct lpfc_hba *phba;
3220         uint32_t eratt = 0;
3221         uint64_t sli_intr, cnt;
3222
3223         phba = from_timer(phba, t, eratt_poll);
3224
3225         /* Here we will also keep track of interrupts per sec of the hba */
3226         sli_intr = phba->sli.slistat.sli_intr;
3227
3228         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3229                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3230                         sli_intr);
3231         else
3232                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3233
3234         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3235         do_div(cnt, phba->eratt_poll_interval);
3236         phba->sli.slistat.sli_ips = cnt;
3237
3238         phba->sli.slistat.sli_prev_intr = sli_intr;
3239
3240         /* Check chip HA register for error event */
3241         eratt = lpfc_sli_check_eratt(phba);
3242
3243         if (eratt)
3244                 /* Tell the worker thread there is work to do */
3245                 lpfc_worker_wake_up(phba);
3246         else
3247                 /* Restart the timer for next eratt poll */
3248                 mod_timer(&phba->eratt_poll,
3249                           jiffies +
3250                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3251         return;
3252 }
3253
3254
3255 /**
3256  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3257  * @phba: Pointer to HBA context object.
3258  * @pring: Pointer to driver SLI ring object.
3259  * @mask: Host attention register mask for this ring.
3260  *
3261  * This function is called from the interrupt context when there is a ring
3262  * event for the fcp ring. The caller does not hold any lock.
3263  * The function processes each response iocb in the response ring until it
3264  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3265  * LE bit set. The function will call the completion handler of the command iocb
3266  * if the response iocb indicates a completion for a command iocb or it is
3267  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3268  * function if this is an unsolicited iocb.
3269  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3270  * to check it explicitly.
3271  */
3272 int
3273 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3274                                 struct lpfc_sli_ring *pring, uint32_t mask)
3275 {
3276         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3277         IOCB_t *irsp = NULL;
3278         IOCB_t *entry = NULL;
3279         struct lpfc_iocbq *cmdiocbq = NULL;
3280         struct lpfc_iocbq rspiocbq;
3281         uint32_t status;
3282         uint32_t portRspPut, portRspMax;
3283         int rc = 1;
3284         lpfc_iocb_type type;
3285         unsigned long iflag;
3286         uint32_t rsp_cmpl = 0;
3287
3288         spin_lock_irqsave(&phba->hbalock, iflag);
3289         pring->stats.iocb_event++;
3290
3291         /*
3292          * The next available response entry should never exceed the maximum
3293          * entries.  If it does, treat it as an adapter hardware error.
3294          */
3295         portRspMax = pring->sli.sli3.numRiocb;
3296         portRspPut = le32_to_cpu(pgp->rspPutInx);
3297         if (unlikely(portRspPut >= portRspMax)) {
3298                 lpfc_sli_rsp_pointers_error(phba, pring);
3299                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3300                 return 1;
3301         }
3302         if (phba->fcp_ring_in_use) {
3303                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3304                 return 1;
3305         } else
3306                 phba->fcp_ring_in_use = 1;
3307
3308         rmb();
3309         while (pring->sli.sli3.rspidx != portRspPut) {
3310                 /*
3311                  * Fetch an entry off the ring and copy it into a local data
3312                  * structure.  The copy involves a byte-swap since the
3313                  * network byte order and pci byte orders are different.
3314                  */
3315                 entry = lpfc_resp_iocb(phba, pring);
3316                 phba->last_completion_time = jiffies;
3317
3318                 if (++pring->sli.sli3.rspidx >= portRspMax)
3319                         pring->sli.sli3.rspidx = 0;
3320
3321                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3322                                       (uint32_t *) &rspiocbq.iocb,
3323                                       phba->iocb_rsp_size);
3324                 INIT_LIST_HEAD(&(rspiocbq.list));
3325                 irsp = &rspiocbq.iocb;
3326
3327                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3328                 pring->stats.iocb_rsp++;
3329                 rsp_cmpl++;
3330
3331                 if (unlikely(irsp->ulpStatus)) {
3332                         /*
3333                          * If resource errors reported from HBA, reduce
3334                          * queuedepths of the SCSI device.
3335                          */
3336                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3337                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3338                              IOERR_NO_RESOURCES)) {
3339                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3340                                 phba->lpfc_rampdown_queue_depth(phba);
3341                                 spin_lock_irqsave(&phba->hbalock, iflag);
3342                         }
3343
3344                         /* Rsp ring <ringno> error: IOCB */
3345                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3346                                         "0336 Rsp Ring %d error: IOCB Data: "
3347                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3348                                         pring->ringno,
3349                                         irsp->un.ulpWord[0],
3350                                         irsp->un.ulpWord[1],
3351                                         irsp->un.ulpWord[2],
3352                                         irsp->un.ulpWord[3],
3353                                         irsp->un.ulpWord[4],
3354                                         irsp->un.ulpWord[5],
3355                                         *(uint32_t *)&irsp->un1,
3356                                         *((uint32_t *)&irsp->un1 + 1));
3357                 }
3358
3359                 switch (type) {
3360                 case LPFC_ABORT_IOCB:
3361                 case LPFC_SOL_IOCB:
3362                         /*
3363                          * Idle exchange closed via ABTS from port.  No iocb
3364                          * resources need to be recovered.
3365                          */
3366                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3367                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3368                                                 "0333 IOCB cmd 0x%x"
3369                                                 " processed. Skipping"
3370                                                 " completion\n",
3371                                                 irsp->ulpCommand);
3372                                 break;
3373                         }
3374
3375                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3376                                                          &rspiocbq);
3377                         if (unlikely(!cmdiocbq))
3378                                 break;
3379                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3380                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3381                         if (cmdiocbq->iocb_cmpl) {
3382                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3383                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3384                                                       &rspiocbq);
3385                                 spin_lock_irqsave(&phba->hbalock, iflag);
3386                         }
3387                         break;
3388                 case LPFC_UNSOL_IOCB:
3389                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3390                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3391                         spin_lock_irqsave(&phba->hbalock, iflag);
3392                         break;
3393                 default:
3394                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3395                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3396                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3397                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3398                                        MAX_MSG_DATA);
3399                                 dev_warn(&((phba->pcidev)->dev),
3400                                          "lpfc%d: %s\n",
3401                                          phba->brd_no, adaptermsg);
3402                         } else {
3403                                 /* Unknown IOCB command */
3404                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3405                                                 "0334 Unknown IOCB command "
3406                                                 "Data: x%x, x%x x%x x%x x%x\n",
3407                                                 type, irsp->ulpCommand,
3408                                                 irsp->ulpStatus,
3409                                                 irsp->ulpIoTag,
3410                                                 irsp->ulpContext);
3411                         }
3412                         break;
3413                 }
3414
3415                 /*
3416                  * The response IOCB has been processed.  Update the ring
3417                  * pointer in SLIM.  If the port response put pointer has not
3418                  * been updated, sync the pgp->rspPutInx and fetch the new port
3419                  * response put pointer.
3420                  */
3421                 writel(pring->sli.sli3.rspidx,
3422                         &phba->host_gp[pring->ringno].rspGetInx);
3423
3424                 if (pring->sli.sli3.rspidx == portRspPut)
3425                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3426         }
3427
3428         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3429                 pring->stats.iocb_rsp_full++;
3430                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3431                 writel(status, phba->CAregaddr);
3432                 readl(phba->CAregaddr);
3433         }
3434         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3435                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3436                 pring->stats.iocb_cmd_empty++;
3437
3438                 /* Force update of the local copy of cmdGetInx */
3439                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3440                 lpfc_sli_resume_iocb(phba, pring);
3441
3442                 if ((pring->lpfc_sli_cmd_available))
3443                         (pring->lpfc_sli_cmd_available) (phba, pring);
3444
3445         }
3446
3447         phba->fcp_ring_in_use = 0;
3448         spin_unlock_irqrestore(&phba->hbalock, iflag);
3449         return rc;
3450 }
3451
3452 /**
3453  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3454  * @phba: Pointer to HBA context object.
3455  * @pring: Pointer to driver SLI ring object.
3456  * @rspiocbp: Pointer to driver response IOCB object.
3457  *
3458  * This function is called from the worker thread when there is a slow-path
3459  * response IOCB to process. This function chains all the response iocbs until
3460  * seeing the iocb with the LE bit set. The function will call
3461  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3462  * completion of a command iocb. The function will call the
3463  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3464  * The function frees the resources or calls the completion handler if this
3465  * iocb is an abort completion. The function returns NULL when the response
3466  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3467  * this function shall chain the iocb on to the iocb_continueq and return the
3468  * response iocb passed in.
3469  **/
3470 static struct lpfc_iocbq *
3471 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3472                         struct lpfc_iocbq *rspiocbp)
3473 {
3474         struct lpfc_iocbq *saveq;
3475         struct lpfc_iocbq *cmdiocbp;
3476         struct lpfc_iocbq *next_iocb;
3477         IOCB_t *irsp = NULL;
3478         uint32_t free_saveq;
3479         uint8_t iocb_cmd_type;
3480         lpfc_iocb_type type;
3481         unsigned long iflag;
3482         int rc;
3483
3484         spin_lock_irqsave(&phba->hbalock, iflag);
3485         /* First add the response iocb to the countinueq list */
3486         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3487         pring->iocb_continueq_cnt++;
3488
3489         /* Now, determine whether the list is completed for processing */
3490         irsp = &rspiocbp->iocb;
3491         if (irsp->ulpLe) {
3492                 /*
3493                  * By default, the driver expects to free all resources
3494                  * associated with this iocb completion.
3495                  */
3496                 free_saveq = 1;
3497                 saveq = list_get_first(&pring->iocb_continueq,
3498                                        struct lpfc_iocbq, list);
3499                 irsp = &(saveq->iocb);
3500                 list_del_init(&pring->iocb_continueq);
3501                 pring->iocb_continueq_cnt = 0;
3502
3503                 pring->stats.iocb_rsp++;
3504
3505                 /*
3506                  * If resource errors reported from HBA, reduce
3507                  * queuedepths of the SCSI device.
3508                  */
3509                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3510                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3511                      IOERR_NO_RESOURCES)) {
3512                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3513                         phba->lpfc_rampdown_queue_depth(phba);
3514                         spin_lock_irqsave(&phba->hbalock, iflag);
3515                 }
3516
3517                 if (irsp->ulpStatus) {
3518                         /* Rsp ring <ringno> error: IOCB */
3519                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3520                                         "0328 Rsp Ring %d error: "
3521                                         "IOCB Data: "
3522                                         "x%x x%x x%x x%x "
3523                                         "x%x x%x x%x x%x "
3524                                         "x%x x%x x%x x%x "
3525                                         "x%x x%x x%x x%x\n",
3526                                         pring->ringno,
3527                                         irsp->un.ulpWord[0],
3528                                         irsp->un.ulpWord[1],
3529                                         irsp->un.ulpWord[2],
3530                                         irsp->un.ulpWord[3],
3531                                         irsp->un.ulpWord[4],
3532                                         irsp->un.ulpWord[5],
3533                                         *(((uint32_t *) irsp) + 6),
3534                                         *(((uint32_t *) irsp) + 7),
3535                                         *(((uint32_t *) irsp) + 8),
3536                                         *(((uint32_t *) irsp) + 9),
3537                                         *(((uint32_t *) irsp) + 10),
3538                                         *(((uint32_t *) irsp) + 11),
3539                                         *(((uint32_t *) irsp) + 12),
3540                                         *(((uint32_t *) irsp) + 13),
3541                                         *(((uint32_t *) irsp) + 14),
3542                                         *(((uint32_t *) irsp) + 15));
3543                 }
3544
3545                 /*
3546                  * Fetch the IOCB command type and call the correct completion
3547                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3548                  * get freed back to the lpfc_iocb_list by the discovery
3549                  * kernel thread.
3550                  */
3551                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3552                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3553                 switch (type) {
3554                 case LPFC_SOL_IOCB:
3555                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3556                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3557                         spin_lock_irqsave(&phba->hbalock, iflag);
3558                         break;
3559
3560                 case LPFC_UNSOL_IOCB:
3561                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3562                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3563                         spin_lock_irqsave(&phba->hbalock, iflag);
3564                         if (!rc)
3565                                 free_saveq = 0;
3566                         break;
3567
3568                 case LPFC_ABORT_IOCB:
3569                         cmdiocbp = NULL;
3570                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3571                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3572                                                                  saveq);
3573                         if (cmdiocbp) {
3574                                 /* Call the specified completion routine */
3575                                 if (cmdiocbp->iocb_cmpl) {
3576                                         spin_unlock_irqrestore(&phba->hbalock,
3577                                                                iflag);
3578                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3579                                                               saveq);
3580                                         spin_lock_irqsave(&phba->hbalock,
3581                                                           iflag);
3582                                 } else
3583                                         __lpfc_sli_release_iocbq(phba,
3584                                                                  cmdiocbp);
3585                         }
3586                         break;
3587
3588                 case LPFC_UNKNOWN_IOCB:
3589                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3590                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3591                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3592                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3593                                        MAX_MSG_DATA);
3594                                 dev_warn(&((phba->pcidev)->dev),
3595                                          "lpfc%d: %s\n",
3596                                          phba->brd_no, adaptermsg);
3597                         } else {
3598                                 /* Unknown IOCB command */
3599                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3600                                                 "0335 Unknown IOCB "
3601                                                 "command Data: x%x "
3602                                                 "x%x x%x x%x\n",
3603                                                 irsp->ulpCommand,
3604                                                 irsp->ulpStatus,
3605                                                 irsp->ulpIoTag,
3606                                                 irsp->ulpContext);
3607                         }
3608                         break;
3609                 }
3610
3611                 if (free_saveq) {
3612                         list_for_each_entry_safe(rspiocbp, next_iocb,
3613                                                  &saveq->list, list) {
3614                                 list_del_init(&rspiocbp->list);
3615                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3616                         }
3617                         __lpfc_sli_release_iocbq(phba, saveq);
3618                 }
3619                 rspiocbp = NULL;
3620         }
3621         spin_unlock_irqrestore(&phba->hbalock, iflag);
3622         return rspiocbp;
3623 }
3624
3625 /**
3626  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3627  * @phba: Pointer to HBA context object.
3628  * @pring: Pointer to driver SLI ring object.
3629  * @mask: Host attention register mask for this ring.
3630  *
3631  * This routine wraps the actual slow_ring event process routine from the
3632  * API jump table function pointer from the lpfc_hba struct.
3633  **/
3634 void
3635 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3636                                 struct lpfc_sli_ring *pring, uint32_t mask)
3637 {
3638         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3639 }
3640
3641 /**
3642  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3643  * @phba: Pointer to HBA context object.
3644  * @pring: Pointer to driver SLI ring object.
3645  * @mask: Host attention register mask for this ring.
3646  *
3647  * This function is called from the worker thread when there is a ring event
3648  * for non-fcp rings. The caller does not hold any lock. The function will
3649  * remove each response iocb in the response ring and calls the handle
3650  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3651  **/
3652 static void
3653 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3654                                    struct lpfc_sli_ring *pring, uint32_t mask)
3655 {
3656         struct lpfc_pgp *pgp;
3657         IOCB_t *entry;
3658         IOCB_t *irsp = NULL;
3659         struct lpfc_iocbq *rspiocbp = NULL;
3660         uint32_t portRspPut, portRspMax;
3661         unsigned long iflag;
3662         uint32_t status;
3663
3664         pgp = &phba->port_gp[pring->ringno];
3665         spin_lock_irqsave(&phba->hbalock, iflag);
3666         pring->stats.iocb_event++;
3667
3668         /*
3669          * The next available response entry should never exceed the maximum
3670          * entries.  If it does, treat it as an adapter hardware error.
3671          */
3672         portRspMax = pring->sli.sli3.numRiocb;
3673         portRspPut = le32_to_cpu(pgp->rspPutInx);
3674         if (portRspPut >= portRspMax) {
3675                 /*
3676                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3677                  * rsp ring <portRspMax>
3678                  */
3679                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3680                                 "0303 Ring %d handler: portRspPut %d "
3681                                 "is bigger than rsp ring %d\n",
3682                                 pring->ringno, portRspPut, portRspMax);
3683
3684                 phba->link_state = LPFC_HBA_ERROR;
3685                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3686
3687                 phba->work_hs = HS_FFER3;
3688                 lpfc_handle_eratt(phba);
3689
3690                 return;
3691         }
3692
3693         rmb();
3694         while (pring->sli.sli3.rspidx != portRspPut) {
3695                 /*
3696                  * Build a completion list and call the appropriate handler.
3697                  * The process is to get the next available response iocb, get
3698                  * a free iocb from the list, copy the response data into the
3699                  * free iocb, insert to the continuation list, and update the
3700                  * next response index to slim.  This process makes response
3701                  * iocb's in the ring available to DMA as fast as possible but
3702                  * pays a penalty for a copy operation.  Since the iocb is
3703                  * only 32 bytes, this penalty is considered small relative to
3704                  * the PCI reads for register values and a slim write.  When
3705                  * the ulpLe field is set, the entire Command has been
3706                  * received.
3707                  */
3708                 entry = lpfc_resp_iocb(phba, pring);
3709
3710                 phba->last_completion_time = jiffies;
3711                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3712                 if (rspiocbp == NULL) {
3713                         printk(KERN_ERR "%s: out of buffers! Failing "
3714                                "completion.\n", __func__);
3715                         break;
3716                 }
3717
3718                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3719                                       phba->iocb_rsp_size);
3720                 irsp = &rspiocbp->iocb;
3721
3722                 if (++pring->sli.sli3.rspidx >= portRspMax)
3723                         pring->sli.sli3.rspidx = 0;
3724
3725                 if (pring->ringno == LPFC_ELS_RING) {
3726                         lpfc_debugfs_slow_ring_trc(phba,
3727                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3728                                 *(((uint32_t *) irsp) + 4),
3729                                 *(((uint32_t *) irsp) + 6),
3730                                 *(((uint32_t *) irsp) + 7));
3731                 }
3732
3733                 writel(pring->sli.sli3.rspidx,
3734                         &phba->host_gp[pring->ringno].rspGetInx);
3735
3736                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3737                 /* Handle the response IOCB */
3738                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3739                 spin_lock_irqsave(&phba->hbalock, iflag);
3740
3741                 /*
3742                  * If the port response put pointer has not been updated, sync
3743                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3744                  * response put pointer.
3745                  */
3746                 if (pring->sli.sli3.rspidx == portRspPut) {
3747                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3748                 }
3749         } /* while (pring->sli.sli3.rspidx != portRspPut) */
3750
3751         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3752                 /* At least one response entry has been freed */
3753                 pring->stats.iocb_rsp_full++;
3754                 /* SET RxRE_RSP in Chip Att register */
3755                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3756                 writel(status, phba->CAregaddr);
3757                 readl(phba->CAregaddr); /* flush */
3758         }
3759         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3760                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3761                 pring->stats.iocb_cmd_empty++;
3762
3763                 /* Force update of the local copy of cmdGetInx */
3764                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3765                 lpfc_sli_resume_iocb(phba, pring);
3766
3767                 if ((pring->lpfc_sli_cmd_available))
3768                         (pring->lpfc_sli_cmd_available) (phba, pring);
3769
3770         }
3771
3772         spin_unlock_irqrestore(&phba->hbalock, iflag);
3773         return;
3774 }
3775
3776 /**
3777  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3778  * @phba: Pointer to HBA context object.
3779  * @pring: Pointer to driver SLI ring object.
3780  * @mask: Host attention register mask for this ring.
3781  *
3782  * This function is called from the worker thread when there is a pending
3783  * ELS response iocb on the driver internal slow-path response iocb worker
3784  * queue. The caller does not hold any lock. The function will remove each
3785  * response iocb from the response worker queue and calls the handle
3786  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3787  **/
3788 static void
3789 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3790                                    struct lpfc_sli_ring *pring, uint32_t mask)
3791 {
3792         struct lpfc_iocbq *irspiocbq;
3793         struct hbq_dmabuf *dmabuf;
3794         struct lpfc_cq_event *cq_event;
3795         unsigned long iflag;
3796         int count = 0;
3797
3798         spin_lock_irqsave(&phba->hbalock, iflag);
3799         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3800         spin_unlock_irqrestore(&phba->hbalock, iflag);
3801         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3802                 /* Get the response iocb from the head of work queue */
3803                 spin_lock_irqsave(&phba->hbalock, iflag);
3804                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3805                                  cq_event, struct lpfc_cq_event, list);
3806                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3807
3808                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3809                 case CQE_CODE_COMPL_WQE:
3810                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3811                                                  cq_event);
3812                         /* Translate ELS WCQE to response IOCBQ */
3813                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3814                                                                    irspiocbq);
3815                         if (irspiocbq)
3816                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3817                                                            irspiocbq);
3818                         count++;
3819                         break;
3820                 case CQE_CODE_RECEIVE:
3821                 case CQE_CODE_RECEIVE_V1:
3822                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3823                                               cq_event);
3824                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3825                         count++;
3826                         break;
3827                 default:
3828                         break;
3829                 }
3830
3831                 /* Limit the number of events to 64 to avoid soft lockups */
3832                 if (count == 64)
3833                         break;
3834         }
3835 }
3836
3837 /**
3838  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3839  * @phba: Pointer to HBA context object.
3840  * @pring: Pointer to driver SLI ring object.
3841  *
3842  * This function aborts all iocbs in the given ring and frees all the iocb
3843  * objects in txq. This function issues an abort iocb for all the iocb commands
3844  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3845  * the return of this function. The caller is not required to hold any locks.
3846  **/
3847 void
3848 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3849 {
3850         LIST_HEAD(completions);
3851         struct lpfc_iocbq *iocb, *next_iocb;
3852
3853         if (pring->ringno == LPFC_ELS_RING) {
3854                 lpfc_fabric_abort_hba(phba);
3855         }
3856
3857         /* Error everything on txq and txcmplq
3858          * First do the txq.
3859          */
3860         if (phba->sli_rev >= LPFC_SLI_REV4) {
3861                 spin_lock_irq(&pring->ring_lock);
3862                 list_splice_init(&pring->txq, &completions);
3863                 pring->txq_cnt = 0;
3864                 spin_unlock_irq(&pring->ring_lock);
3865
3866                 spin_lock_irq(&phba->hbalock);
3867                 /* Next issue ABTS for everything on the txcmplq */
3868                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3869                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3870                 spin_unlock_irq(&phba->hbalock);
3871         } else {
3872                 spin_lock_irq(&phba->hbalock);
3873                 list_splice_init(&pring->txq, &completions);
3874                 pring->txq_cnt = 0;
3875
3876                 /* Next issue ABTS for everything on the txcmplq */
3877                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3878                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3879                 spin_unlock_irq(&phba->hbalock);
3880         }
3881
3882         /* Cancel all the IOCBs from the completions list */
3883         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3884                               IOERR_SLI_ABORTED);
3885 }
3886
3887 /**
3888  * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3889  * @phba: Pointer to HBA context object.
3890  * @pring: Pointer to driver SLI ring object.
3891  *
3892  * This function aborts all iocbs in the given ring and frees all the iocb
3893  * objects in txq. This function issues an abort iocb for all the iocb commands
3894  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3895  * the return of this function. The caller is not required to hold any locks.
3896  **/
3897 void
3898 lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3899 {
3900         LIST_HEAD(completions);
3901         struct lpfc_iocbq *iocb, *next_iocb;
3902
3903         if (pring->ringno == LPFC_ELS_RING)
3904                 lpfc_fabric_abort_hba(phba);
3905
3906         spin_lock_irq(&phba->hbalock);
3907         /* Next issue ABTS for everything on the txcmplq */
3908         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3909                 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3910         spin_unlock_irq(&phba->hbalock);
3911 }
3912
3913
3914 /**
3915  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3916  * @phba: Pointer to HBA context object.
3917  * @pring: Pointer to driver SLI ring object.
3918  *
3919  * This function aborts all iocbs in FCP rings and frees all the iocb
3920  * objects in txq. This function issues an abort iocb for all the iocb commands
3921  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3922  * the return of this function. The caller is not required to hold any locks.
3923  **/
3924 void
3925 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3926 {
3927         struct lpfc_sli *psli = &phba->sli;
3928         struct lpfc_sli_ring  *pring;
3929         uint32_t i;
3930
3931         /* Look on all the FCP Rings for the iotag */
3932         if (phba->sli_rev >= LPFC_SLI_REV4) {
3933                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3934                         pring = phba->sli4_hba.fcp_wq[i]->pring;
3935                         lpfc_sli_abort_iocb_ring(phba, pring);
3936                 }
3937         } else {
3938                 pring = &psli->sli3_ring[LPFC_FCP_RING];
3939                 lpfc_sli_abort_iocb_ring(phba, pring);
3940         }
3941 }
3942
3943 /**
3944  * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3945  * @phba: Pointer to HBA context object.
3946  *
3947  * This function aborts all wqes in NVME rings. This function issues an
3948  * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3949  * the txcmplq is not guaranteed to complete before the return of this
3950  * function. The caller is not required to hold any locks.
3951  **/
3952 void
3953 lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3954 {
3955         struct lpfc_sli_ring  *pring;
3956         uint32_t i;
3957
3958         if (phba->sli_rev < LPFC_SLI_REV4)
3959                 return;
3960
3961         /* Abort all IO on each NVME ring. */
3962         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3963                 pring = phba->sli4_hba.nvme_wq[i]->pring;
3964                 lpfc_sli_abort_wqe_ring(phba, pring);
3965         }
3966 }
3967
3968
3969 /**
3970  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3971  * @phba: Pointer to HBA context object.
3972  *
3973  * This function flushes all iocbs in the fcp ring and frees all the iocb
3974  * objects in txq and txcmplq. This function will not issue abort iocbs
3975  * for all the iocb commands in txcmplq, they will just be returned with
3976  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3977  * slot has been permanently disabled.
3978  **/
3979 void
3980 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3981 {
3982         LIST_HEAD(txq);
3983         LIST_HEAD(txcmplq);
3984         struct lpfc_sli *psli = &phba->sli;
3985         struct lpfc_sli_ring  *pring;
3986         uint32_t i;
3987         struct lpfc_iocbq *piocb, *next_iocb;
3988
3989         spin_lock_irq(&phba->hbalock);
3990         /* Indicate the I/O queues are flushed */
3991         phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3992         spin_unlock_irq(&phba->hbalock);
3993
3994         /* Look on all the FCP Rings for the iotag */
3995         if (phba->sli_rev >= LPFC_SLI_REV4) {
3996                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3997                         pring = phba->sli4_hba.fcp_wq[i]->pring;
3998
3999                         spin_lock_irq(&pring->ring_lock);
4000                         /* Retrieve everything on txq */
4001                         list_splice_init(&pring->txq, &txq);
4002                         list_for_each_entry_safe(piocb, next_iocb,
4003                                                  &pring->txcmplq, list)
4004                                 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4005                         /* Retrieve everything on the txcmplq */
4006                         list_splice_init(&pring->txcmplq, &txcmplq);
4007                         pring->txq_cnt = 0;
4008                         pring->txcmplq_cnt = 0;
4009                         spin_unlock_irq(&pring->ring_lock);
4010
4011                         /* Flush the txq */
4012                         lpfc_sli_cancel_iocbs(phba, &txq,
4013                                               IOSTAT_LOCAL_REJECT,
4014                                               IOERR_SLI_DOWN);
4015                         /* Flush the txcmpq */
4016                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
4017                                               IOSTAT_LOCAL_REJECT,
4018                                               IOERR_SLI_DOWN);
4019                 }
4020         } else {
4021                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4022
4023                 spin_lock_irq(&phba->hbalock);
4024                 /* Retrieve everything on txq */
4025                 list_splice_init(&pring->txq, &txq);
4026                 list_for_each_entry_safe(piocb, next_iocb,
4027                                          &pring->txcmplq, list)
4028                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4029                 /* Retrieve everything on the txcmplq */
4030                 list_splice_init(&pring->txcmplq, &txcmplq);
4031                 pring->txq_cnt = 0;
4032                 pring->txcmplq_cnt = 0;
4033                 spin_unlock_irq(&phba->hbalock);
4034
4035                 /* Flush the txq */
4036                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4037                                       IOERR_SLI_DOWN);
4038                 /* Flush the txcmpq */
4039                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4040                                       IOERR_SLI_DOWN);
4041         }
4042 }
4043
4044 /**
4045  * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4046  * @phba: Pointer to HBA context object.
4047  *
4048  * This function flushes all wqes in the nvme rings and frees all resources
4049  * in the txcmplq. This function does not issue abort wqes for the IO
4050  * commands in txcmplq, they will just be returned with
4051  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4052  * slot has been permanently disabled.
4053  **/
4054 void
4055 lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4056 {
4057         LIST_HEAD(txcmplq);
4058         struct lpfc_sli_ring  *pring;
4059         uint32_t i;
4060         struct lpfc_iocbq *piocb, *next_iocb;
4061
4062         if (phba->sli_rev < LPFC_SLI_REV4)
4063                 return;
4064
4065         /* Hint to other driver operations that a flush is in progress. */
4066         spin_lock_irq(&phba->hbalock);
4067         phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4068         spin_unlock_irq(&phba->hbalock);
4069
4070         /* Cycle through all NVME rings and complete each IO with
4071          * a local driver reason code.  This is a flush so no
4072          * abort exchange to FW.
4073          */
4074         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4075                 pring = phba->sli4_hba.nvme_wq[i]->pring;
4076
4077                 spin_lock_irq(&pring->ring_lock);
4078                 list_for_each_entry_safe(piocb, next_iocb,
4079                                          &pring->txcmplq, list)
4080                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4081                 /* Retrieve everything on the txcmplq */
4082                 list_splice_init(&pring->txcmplq, &txcmplq);
4083                 pring->txcmplq_cnt = 0;
4084                 spin_unlock_irq(&pring->ring_lock);
4085
4086                 /* Flush the txcmpq &&&PAE */
4087                 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4088                                       IOSTAT_LOCAL_REJECT,
4089                                       IOERR_SLI_DOWN);
4090         }
4091 }
4092
4093 /**
4094  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4095  * @phba: Pointer to HBA context object.
4096  * @mask: Bit mask to be checked.
4097  *
4098  * This function reads the host status register and compares
4099  * with the provided bit mask to check if HBA completed
4100  * the restart. This function will wait in a loop for the
4101  * HBA to complete restart. If the HBA does not restart within
4102  * 15 iterations, the function will reset the HBA again. The
4103  * function returns 1 when HBA fail to restart otherwise returns
4104  * zero.
4105  **/
4106 static int
4107 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4108 {
4109         uint32_t status;
4110         int i = 0;
4111         int retval = 0;
4112
4113         /* Read the HBA Host Status Register */
4114         if (lpfc_readl(phba->HSregaddr, &status))
4115                 return 1;
4116
4117         /*
4118          * Check status register every 100ms for 5 retries, then every
4119          * 500ms for 5, then every 2.5 sec for 5, then reset board and
4120          * every 2.5 sec for 4.
4121          * Break our of the loop if errors occurred during init.
4122          */
4123         while (((status & mask) != mask) &&
4124                !(status & HS_FFERM) &&
4125                i++ < 20) {
4126
4127                 if (i <= 5)
4128                         msleep(10);
4129                 else if (i <= 10)
4130                         msleep(500);
4131                 else
4132                         msleep(2500);
4133
4134                 if (i == 15) {
4135                                 /* Do post */
4136                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4137                         lpfc_sli_brdrestart(phba);
4138                 }
4139                 /* Read the HBA Host Status Register */
4140                 if (lpfc_readl(phba->HSregaddr, &status)) {
4141                         retval = 1;
4142                         break;
4143                 }
4144         }
4145
4146         /* Check to see if any errors occurred during init */
4147         if ((status & HS_FFERM) || (i >= 20)) {
4148                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4149                                 "2751 Adapter failed to restart, "
4150                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4151                                 status,
4152                                 readl(phba->MBslimaddr + 0xa8),
4153                                 readl(phba->MBslimaddr + 0xac));
4154                 phba->link_state = LPFC_HBA_ERROR;
4155                 retval = 1;
4156         }
4157
4158         return retval;
4159 }
4160
4161 /**
4162  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4163  * @phba: Pointer to HBA context object.
4164  * @mask: Bit mask to be checked.
4165  *
4166  * This function checks the host status register to check if HBA is
4167  * ready. This function will wait in a loop for the HBA to be ready
4168  * If the HBA is not ready , the function will will reset the HBA PCI
4169  * function again. The function returns 1 when HBA fail to be ready
4170  * otherwise returns zero.
4171  **/
4172 static int
4173 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4174 {
4175         uint32_t status;
4176         int retval = 0;
4177
4178         /* Read the HBA Host Status Register */
4179         status = lpfc_sli4_post_status_check(phba);
4180
4181         if (status) {
4182                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4183                 lpfc_sli_brdrestart(phba);
4184                 status = lpfc_sli4_post_status_check(phba);
4185         }
4186
4187         /* Check to see if any errors occurred during init */
4188         if (status) {
4189                 phba->link_state = LPFC_HBA_ERROR;
4190                 retval = 1;
4191         } else
4192                 phba->sli4_hba.intr_enable = 0;
4193
4194         return retval;
4195 }
4196
4197 /**
4198  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4199  * @phba: Pointer to HBA context object.
4200  * @mask: Bit mask to be checked.
4201  *
4202  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4203  * from the API jump table function pointer from the lpfc_hba struct.
4204  **/
4205 int
4206 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4207 {
4208         return phba->lpfc_sli_brdready(phba, mask);
4209 }
4210
4211 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4212
4213 /**
4214  * lpfc_reset_barrier - Make HBA ready for HBA reset
4215  * @phba: Pointer to HBA context object.
4216  *
4217  * This function is called before resetting an HBA. This function is called
4218  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4219  **/
4220 void lpfc_reset_barrier(struct lpfc_hba *phba)
4221 {
4222         uint32_t __iomem *resp_buf;
4223         uint32_t __iomem *mbox_buf;
4224         volatile uint32_t mbox;
4225         uint32_t hc_copy, ha_copy, resp_data;
4226         int  i;
4227         uint8_t hdrtype;
4228
4229         lockdep_assert_held(&phba->hbalock);
4230
4231         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4232         if (hdrtype != 0x80 ||
4233             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4234              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4235                 return;
4236
4237         /*
4238          * Tell the other part of the chip to suspend temporarily all
4239          * its DMA activity.
4240          */
4241         resp_buf = phba->MBslimaddr;
4242
4243         /* Disable the error attention */
4244         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4245                 return;
4246         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4247         readl(phba->HCregaddr); /* flush */
4248         phba->link_flag |= LS_IGNORE_ERATT;
4249
4250         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4251                 return;
4252         if (ha_copy & HA_ERATT) {
4253                 /* Clear Chip error bit */
4254                 writel(HA_ERATT, phba->HAregaddr);
4255                 phba->pport->stopped = 1;
4256         }
4257
4258         mbox = 0;
4259         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4260         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4261
4262         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4263         mbox_buf = phba->MBslimaddr;
4264         writel(mbox, mbox_buf);
4265
4266         for (i = 0; i < 50; i++) {
4267                 if (lpfc_readl((resp_buf + 1), &resp_data))
4268                         return;
4269                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4270                         mdelay(1);
4271                 else
4272                         break;
4273         }
4274         resp_data = 0;
4275         if (lpfc_readl((resp_buf + 1), &resp_data))
4276                 return;
4277         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4278                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4279                     phba->pport->stopped)
4280                         goto restore_hc;
4281                 else
4282                         goto clear_errat;
4283         }
4284
4285         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4286         resp_data = 0;
4287         for (i = 0; i < 500; i++) {
4288                 if (lpfc_readl(resp_buf, &resp_data))
4289                         return;
4290                 if (resp_data != mbox)
4291                         mdelay(1);
4292                 else
4293                         break;
4294         }
4295
4296 clear_errat:
4297
4298         while (++i < 500) {
4299                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4300                         return;
4301                 if (!(ha_copy & HA_ERATT))
4302                         mdelay(1);
4303                 else
4304                         break;
4305         }
4306
4307         if (readl(phba->HAregaddr) & HA_ERATT) {
4308                 writel(HA_ERATT, phba->HAregaddr);
4309                 phba->pport->stopped = 1;
4310         }
4311
4312 restore_hc:
4313         phba->link_flag &= ~LS_IGNORE_ERATT;
4314         writel(hc_copy, phba->HCregaddr);
4315         readl(phba->HCregaddr); /* flush */
4316 }
4317
4318 /**
4319  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4320  * @phba: Pointer to HBA context object.
4321  *
4322  * This function issues a kill_board mailbox command and waits for
4323  * the error attention interrupt. This function is called for stopping
4324  * the firmware processing. The caller is not required to hold any
4325  * locks. This function calls lpfc_hba_down_post function to free
4326  * any pending commands after the kill. The function will return 1 when it
4327  * fails to kill the board else will return 0.
4328  **/
4329 int
4330 lpfc_sli_brdkill(struct lpfc_hba *phba)
4331 {
4332         struct lpfc_sli *psli;
4333         LPFC_MBOXQ_t *pmb;
4334         uint32_t status;
4335         uint32_t ha_copy;
4336         int retval;
4337         int i = 0;
4338
4339         psli = &phba->sli;
4340
4341         /* Kill HBA */
4342         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4343                         "0329 Kill HBA Data: x%x x%x\n",
4344                         phba->pport->port_state, psli->sli_flag);
4345
4346         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4347         if (!pmb)
4348                 return 1;
4349
4350         /* Disable the error attention */
4351         spin_lock_irq(&phba->hbalock);
4352         if (lpfc_readl(phba->HCregaddr, &status)) {
4353                 spin_unlock_irq(&phba->hbalock);
4354                 mempool_free(pmb, phba->mbox_mem_pool);
4355                 return 1;
4356         }
4357         status &= ~HC_ERINT_ENA;
4358         writel(status, phba->HCregaddr);
4359         readl(phba->HCregaddr); /* flush */
4360         phba->link_flag |= LS_IGNORE_ERATT;
4361         spin_unlock_irq(&phba->hbalock);
4362
4363         lpfc_kill_board(phba, pmb);
4364         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4365         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4366
4367         if (retval != MBX_SUCCESS) {
4368                 if (retval != MBX_BUSY)
4369                         mempool_free(pmb, phba->mbox_mem_pool);
4370                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4371                                 "2752 KILL_BOARD command failed retval %d\n",
4372                                 retval);
4373                 spin_lock_irq(&phba->hbalock);
4374                 phba->link_flag &= ~LS_IGNORE_ERATT;
4375                 spin_unlock_irq(&phba->hbalock);
4376                 return 1;
4377         }
4378
4379         spin_lock_irq(&phba->hbalock);
4380         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4381         spin_unlock_irq(&phba->hbalock);
4382
4383         mempool_free(pmb, phba->mbox_mem_pool);
4384
4385         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4386          * attention every 100ms for 3 seconds. If we don't get ERATT after
4387          * 3 seconds we still set HBA_ERROR state because the status of the
4388          * board is now undefined.
4389          */
4390         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4391                 return 1;
4392         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4393                 mdelay(100);
4394                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4395                         return 1;
4396         }
4397
4398         del_timer_sync(&psli->mbox_tmo);
4399         if (ha_copy & HA_ERATT) {
4400                 writel(HA_ERATT, phba->HAregaddr);
4401                 phba->pport->stopped = 1;
4402         }
4403         spin_lock_irq(&phba->hbalock);
4404         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4405         psli->mbox_active = NULL;
4406         phba->link_flag &= ~LS_IGNORE_ERATT;
4407         spin_unlock_irq(&phba->hbalock);
4408
4409         lpfc_hba_down_post(phba);
4410         phba->link_state = LPFC_HBA_ERROR;
4411
4412         return ha_copy & HA_ERATT ? 0 : 1;
4413 }
4414
4415 /**
4416  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4417  * @phba: Pointer to HBA context object.
4418  *
4419  * This function resets the HBA by writing HC_INITFF to the control
4420  * register. After the HBA resets, this function resets all the iocb ring
4421  * indices. This function disables PCI layer parity checking during
4422  * the reset.
4423  * This function returns 0 always.
4424  * The caller is not required to hold any locks.
4425  **/
4426 int
4427 lpfc_sli_brdreset(struct lpfc_hba *phba)
4428 {
4429         struct lpfc_sli *psli;
4430         struct lpfc_sli_ring *pring;
4431         uint16_t cfg_value;
4432         int i;
4433
4434         psli = &phba->sli;
4435
4436         /* Reset HBA */
4437         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4438                         "0325 Reset HBA Data: x%x x%x\n",
4439                         (phba->pport) ? phba->pport->port_state : 0,
4440                         psli->sli_flag);
4441
4442         /* perform board reset */
4443         phba->fc_eventTag = 0;
4444         phba->link_events = 0;
4445         if (phba->pport) {
4446                 phba->pport->fc_myDID = 0;
4447                 phba->pport->fc_prevDID = 0;
4448         }
4449
4450         /* Turn off parity checking and serr during the physical reset */
4451         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4452         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4453                               (cfg_value &
4454                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4455
4456         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4457
4458         /* Now toggle INITFF bit in the Host Control Register */
4459         writel(HC_INITFF, phba->HCregaddr);
4460         mdelay(1);
4461         readl(phba->HCregaddr); /* flush */
4462         writel(0, phba->HCregaddr);
4463         readl(phba->HCregaddr); /* flush */
4464
4465         /* Restore PCI cmd register */
4466         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4467
4468         /* Initialize relevant SLI info */
4469         for (i = 0; i < psli->num_rings; i++) {
4470                 pring = &psli->sli3_ring[i];
4471                 pring->flag = 0;
4472                 pring->sli.sli3.rspidx = 0;
4473                 pring->sli.sli3.next_cmdidx  = 0;
4474                 pring->sli.sli3.local_getidx = 0;
4475                 pring->sli.sli3.cmdidx = 0;
4476                 pring->missbufcnt = 0;
4477         }
4478
4479         phba->link_state = LPFC_WARM_START;
4480         return 0;
4481 }
4482
4483 /**
4484  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4485  * @phba: Pointer to HBA context object.
4486  *
4487  * This function resets a SLI4 HBA. This function disables PCI layer parity
4488  * checking during resets the device. The caller is not required to hold
4489  * any locks.
4490  *
4491  * This function returns 0 always.
4492  **/
4493 int
4494 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4495 {
4496         struct lpfc_sli *psli = &phba->sli;
4497         uint16_t cfg_value;
4498         int rc = 0;
4499
4500         /* Reset HBA */
4501         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4502                         "0295 Reset HBA Data: x%x x%x x%x\n",
4503                         phba->pport->port_state, psli->sli_flag,
4504                         phba->hba_flag);
4505
4506         /* perform board reset */
4507         phba->fc_eventTag = 0;
4508         phba->link_events = 0;
4509         phba->pport->fc_myDID = 0;
4510         phba->pport->fc_prevDID = 0;
4511
4512         spin_lock_irq(&phba->hbalock);
4513         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4514         phba->fcf.fcf_flag = 0;
4515         spin_unlock_irq(&phba->hbalock);
4516
4517         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4518         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4519                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4520                 return rc;
4521         }
4522
4523         /* Now physically reset the device */
4524         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4525                         "0389 Performing PCI function reset!\n");
4526
4527         /* Turn off parity checking and serr during the physical reset */
4528         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4529         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4530                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4531
4532         /* Perform FCoE PCI function reset before freeing queue memory */
4533         rc = lpfc_pci_function_reset(phba);
4534
4535         /* Restore PCI cmd register */
4536         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4537
4538         return rc;
4539 }
4540
4541 /**
4542  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4543  * @phba: Pointer to HBA context object.
4544  *
4545  * This function is called in the SLI initialization code path to
4546  * restart the HBA. The caller is not required to hold any lock.
4547  * This function writes MBX_RESTART mailbox command to the SLIM and
4548  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4549  * function to free any pending commands. The function enables
4550  * POST only during the first initialization. The function returns zero.
4551  * The function does not guarantee completion of MBX_RESTART mailbox
4552  * command before the return of this function.
4553  **/
4554 static int
4555 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4556 {
4557         MAILBOX_t *mb;
4558         struct lpfc_sli *psli;
4559         volatile uint32_t word0;
4560         void __iomem *to_slim;
4561         uint32_t hba_aer_enabled;
4562
4563         spin_lock_irq(&phba->hbalock);
4564
4565         /* Take PCIe device Advanced Error Reporting (AER) state */
4566         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4567
4568         psli = &phba->sli;
4569
4570         /* Restart HBA */
4571         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4572                         "0337 Restart HBA Data: x%x x%x\n",
4573                         (phba->pport) ? phba->pport->port_state : 0,
4574                         psli->sli_flag);
4575
4576         word0 = 0;
4577         mb = (MAILBOX_t *) &word0;
4578         mb->mbxCommand = MBX_RESTART;
4579         mb->mbxHc = 1;
4580
4581         lpfc_reset_barrier(phba);
4582
4583         to_slim = phba->MBslimaddr;
4584         writel(*(uint32_t *) mb, to_slim);
4585         readl(to_slim); /* flush */
4586
4587         /* Only skip post after fc_ffinit is completed */
4588         if (phba->pport && phba->pport->port_state)
4589                 word0 = 1;      /* This is really setting up word1 */
4590         else
4591                 word0 = 0;      /* This is really setting up word1 */
4592         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4593         writel(*(uint32_t *) mb, to_slim);
4594         readl(to_slim); /* flush */
4595
4596         lpfc_sli_brdreset(phba);
4597         if (phba->pport)
4598                 phba->pport->stopped = 0;
4599         phba->link_state = LPFC_INIT_START;
4600         phba->hba_flag = 0;
4601         spin_unlock_irq(&phba->hbalock);
4602
4603         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4604         psli->stats_start = ktime_get_seconds();
4605
4606         /* Give the INITFF and Post time to settle. */
4607         mdelay(100);
4608
4609         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4610         if (hba_aer_enabled)
4611                 pci_disable_pcie_error_reporting(phba->pcidev);
4612
4613         lpfc_hba_down_post(phba);
4614
4615         return 0;
4616 }
4617
4618 /**
4619  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4620  * @phba: Pointer to HBA context object.
4621  *
4622  * This function is called in the SLI initialization code path to restart
4623  * a SLI4 HBA. The caller is not required to hold any lock.
4624  * At the end of the function, it calls lpfc_hba_down_post function to
4625  * free any pending commands.
4626  **/
4627 static int
4628 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4629 {
4630         struct lpfc_sli *psli = &phba->sli;
4631         uint32_t hba_aer_enabled;
4632         int rc;
4633
4634         /* Restart HBA */
4635         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4636                         "0296 Restart HBA Data: x%x x%x\n",
4637                         phba->pport->port_state, psli->sli_flag);
4638
4639         /* Take PCIe device Advanced Error Reporting (AER) state */
4640         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4641
4642         rc = lpfc_sli4_brdreset(phba);
4643
4644         spin_lock_irq(&phba->hbalock);
4645         phba->pport->stopped = 0;
4646         phba->link_state = LPFC_INIT_START;
4647         phba->hba_flag = 0;
4648         spin_unlock_irq(&phba->hbalock);
4649
4650         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4651         psli->stats_start = ktime_get_seconds();
4652
4653         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4654         if (hba_aer_enabled)
4655                 pci_disable_pcie_error_reporting(phba->pcidev);
4656
4657         lpfc_hba_down_post(phba);
4658         lpfc_sli4_queue_destroy(phba);
4659
4660         return rc;
4661 }
4662
4663 /**
4664  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4665  * @phba: Pointer to HBA context object.
4666  *
4667  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4668  * API jump table function pointer from the lpfc_hba struct.
4669 **/
4670 int
4671 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4672 {
4673         return phba->lpfc_sli_brdrestart(phba);
4674 }
4675
4676 /**
4677  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4678  * @phba: Pointer to HBA context object.
4679  *
4680  * This function is called after a HBA restart to wait for successful
4681  * restart of the HBA. Successful restart of the HBA is indicated by
4682  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4683  * iteration, the function will restart the HBA again. The function returns
4684  * zero if HBA successfully restarted else returns negative error code.
4685  **/
4686 int
4687 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4688 {
4689         uint32_t status, i = 0;
4690
4691         /* Read the HBA Host Status Register */
4692         if (lpfc_readl(phba->HSregaddr, &status))
4693                 return -EIO;
4694
4695         /* Check status register to see what current state is */
4696         i = 0;
4697         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4698
4699                 /* Check every 10ms for 10 retries, then every 100ms for 90
4700                  * retries, then every 1 sec for 50 retires for a total of
4701                  * ~60 seconds before reset the board again and check every
4702                  * 1 sec for 50 retries. The up to 60 seconds before the
4703                  * board ready is required by the Falcon FIPS zeroization
4704                  * complete, and any reset the board in between shall cause
4705                  * restart of zeroization, further delay the board ready.
4706                  */
4707                 if (i++ >= 200) {
4708                         /* Adapter failed to init, timeout, status reg
4709                            <status> */
4710                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4711                                         "0436 Adapter failed to init, "
4712                                         "timeout, status reg x%x, "
4713                                         "FW Data: A8 x%x AC x%x\n", status,
4714                                         readl(phba->MBslimaddr + 0xa8),
4715                                         readl(phba->MBslimaddr + 0xac));
4716                         phba->link_state = LPFC_HBA_ERROR;
4717                         return -ETIMEDOUT;
4718                 }
4719
4720                 /* Check to see if any errors occurred during init */
4721                 if (status & HS_FFERM) {
4722                         /* ERROR: During chipset initialization */
4723                         /* Adapter failed to init, chipset, status reg
4724                            <status> */
4725                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4726                                         "0437 Adapter failed to init, "
4727                                         "chipset, status reg x%x, "
4728                                         "FW Data: A8 x%x AC x%x\n", status,
4729                                         readl(phba->MBslimaddr + 0xa8),
4730                                         readl(phba->MBslimaddr + 0xac));
4731                         phba->link_state = LPFC_HBA_ERROR;
4732                         return -EIO;
4733                 }
4734
4735                 if (i <= 10)
4736                         msleep(10);
4737                 else if (i <= 100)
4738                         msleep(100);
4739                 else
4740                         msleep(1000);
4741
4742                 if (i == 150) {
4743                         /* Do post */
4744                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4745                         lpfc_sli_brdrestart(phba);
4746                 }
4747                 /* Read the HBA Host Status Register */
4748                 if (lpfc_readl(phba->HSregaddr, &status))
4749                         return -EIO;
4750         }
4751
4752         /* Check to see if any errors occurred during init */
4753         if (status & HS_FFERM) {
4754                 /* ERROR: During chipset initialization */
4755                 /* Adapter failed to init, chipset, status reg <status> */
4756                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4757                                 "0438 Adapter failed to init, chipset, "
4758                                 "status reg x%x, "
4759                                 "FW Data: A8 x%x AC x%x\n", status,
4760                                 readl(phba->MBslimaddr + 0xa8),
4761                                 readl(phba->MBslimaddr + 0xac));
4762                 phba->link_state = LPFC_HBA_ERROR;
4763                 return -EIO;
4764         }
4765
4766         /* Clear all interrupt enable conditions */
4767         writel(0, phba->HCregaddr);
4768         readl(phba->HCregaddr); /* flush */
4769
4770         /* setup host attn register */
4771         writel(0xffffffff, phba->HAregaddr);
4772         readl(phba->HAregaddr); /* flush */
4773         return 0;
4774 }
4775
4776 /**
4777  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4778  *
4779  * This function calculates and returns the number of HBQs required to be
4780  * configured.
4781  **/
4782 int
4783 lpfc_sli_hbq_count(void)
4784 {
4785         return ARRAY_SIZE(lpfc_hbq_defs);
4786 }
4787
4788 /**
4789  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4790  *
4791  * This function adds the number of hbq entries in every HBQ to get
4792  * the total number of hbq entries required for the HBA and returns
4793  * the total count.
4794  **/
4795 static int
4796 lpfc_sli_hbq_entry_count(void)
4797 {
4798         int  hbq_count = lpfc_sli_hbq_count();
4799         int  count = 0;
4800         int  i;
4801
4802         for (i = 0; i < hbq_count; ++i)
4803                 count += lpfc_hbq_defs[i]->entry_count;
4804         return count;
4805 }
4806
4807 /**
4808  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4809  *
4810  * This function calculates amount of memory required for all hbq entries
4811  * to be configured and returns the total memory required.
4812  **/
4813 int
4814 lpfc_sli_hbq_size(void)
4815 {
4816         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4817 }
4818
4819 /**
4820  * lpfc_sli_hbq_setup - configure and initialize HBQs
4821  * @phba: Pointer to HBA context object.
4822  *
4823  * This function is called during the SLI initialization to configure
4824  * all the HBQs and post buffers to the HBQ. The caller is not
4825  * required to hold any locks. This function will return zero if successful
4826  * else it will return negative error code.
4827  **/
4828 static int
4829 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4830 {
4831         int  hbq_count = lpfc_sli_hbq_count();
4832         LPFC_MBOXQ_t *pmb;
4833         MAILBOX_t *pmbox;
4834         uint32_t hbqno;
4835         uint32_t hbq_entry_index;
4836
4837                                 /* Get a Mailbox buffer to setup mailbox
4838                                  * commands for HBA initialization
4839                                  */
4840         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4841
4842         if (!pmb)
4843                 return -ENOMEM;
4844
4845         pmbox = &pmb->u.mb;
4846
4847         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4848         phba->link_state = LPFC_INIT_MBX_CMDS;
4849         phba->hbq_in_use = 1;
4850
4851         hbq_entry_index = 0;
4852         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4853                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4854                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4855                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4856                 phba->hbqs[hbqno].entry_count =
4857                         lpfc_hbq_defs[hbqno]->entry_count;
4858                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4859                         hbq_entry_index, pmb);
4860                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4861
4862                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4863                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4864                            mbxStatus <status>, ring <num> */
4865
4866                         lpfc_printf_log(phba, KERN_ERR,
4867                                         LOG_SLI | LOG_VPORT,
4868                                         "1805 Adapter failed to init. "
4869                                         "Data: x%x x%x x%x\n",
4870                                         pmbox->mbxCommand,
4871                                         pmbox->mbxStatus, hbqno);
4872
4873                         phba->link_state = LPFC_HBA_ERROR;
4874                         mempool_free(pmb, phba->mbox_mem_pool);
4875                         return -ENXIO;
4876                 }
4877         }
4878         phba->hbq_count = hbq_count;
4879
4880         mempool_free(pmb, phba->mbox_mem_pool);
4881
4882         /* Initially populate or replenish the HBQs */
4883         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4884                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4885         return 0;
4886 }
4887
4888 /**
4889  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4890  * @phba: Pointer to HBA context object.
4891  *
4892  * This function is called during the SLI initialization to configure
4893  * all the HBQs and post buffers to the HBQ. The caller is not
4894  * required to hold any locks. This function will return zero if successful
4895  * else it will return negative error code.
4896  **/
4897 static int
4898 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4899 {
4900         phba->hbq_in_use = 1;
4901         phba->hbqs[LPFC_ELS_HBQ].entry_count =
4902                 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4903         phba->hbq_count = 1;
4904         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4905         /* Initially populate or replenish the HBQs */
4906         return 0;
4907 }
4908
4909 /**
4910  * lpfc_sli_config_port - Issue config port mailbox command
4911  * @phba: Pointer to HBA context object.
4912  * @sli_mode: sli mode - 2/3
4913  *
4914  * This function is called by the sli initialization code path
4915  * to issue config_port mailbox command. This function restarts the
4916  * HBA firmware and issues a config_port mailbox command to configure
4917  * the SLI interface in the sli mode specified by sli_mode
4918  * variable. The caller is not required to hold any locks.
4919  * The function returns 0 if successful, else returns negative error
4920  * code.
4921  **/
4922 int
4923 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4924 {
4925         LPFC_MBOXQ_t *pmb;
4926         uint32_t resetcount = 0, rc = 0, done = 0;
4927
4928         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4929         if (!pmb) {
4930                 phba->link_state = LPFC_HBA_ERROR;
4931                 return -ENOMEM;
4932         }
4933
4934         phba->sli_rev = sli_mode;
4935         while (resetcount < 2 && !done) {
4936                 spin_lock_irq(&phba->hbalock);
4937                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4938                 spin_unlock_irq(&phba->hbalock);
4939                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4940                 lpfc_sli_brdrestart(phba);
4941                 rc = lpfc_sli_chipset_init(phba);
4942                 if (rc)
4943                         break;
4944
4945                 spin_lock_irq(&phba->hbalock);
4946                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4947                 spin_unlock_irq(&phba->hbalock);
4948                 resetcount++;
4949
4950                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4951                  * value of 0 means the call was successful.  Any other
4952                  * nonzero value is a failure, but if ERESTART is returned,
4953                  * the driver may reset the HBA and try again.
4954                  */
4955                 rc = lpfc_config_port_prep(phba);
4956                 if (rc == -ERESTART) {
4957                         phba->link_state = LPFC_LINK_UNKNOWN;
4958                         continue;
4959                 } else if (rc)
4960                         break;
4961
4962                 phba->link_state = LPFC_INIT_MBX_CMDS;
4963                 lpfc_config_port(phba, pmb);
4964                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4965                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4966                                         LPFC_SLI3_HBQ_ENABLED |
4967                                         LPFC_SLI3_CRP_ENABLED |
4968                                         LPFC_SLI3_BG_ENABLED |
4969                                         LPFC_SLI3_DSS_ENABLED);
4970                 if (rc != MBX_SUCCESS) {
4971                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4972                                 "0442 Adapter failed to init, mbxCmd x%x "
4973                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4974                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4975                         spin_lock_irq(&phba->hbalock);
4976                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4977                         spin_unlock_irq(&phba->hbalock);
4978                         rc = -ENXIO;
4979                 } else {
4980                         /* Allow asynchronous mailbox command to go through */
4981                         spin_lock_irq(&phba->hbalock);
4982                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4983                         spin_unlock_irq(&phba->hbalock);
4984                         done = 1;
4985
4986                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4987                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
4988                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4989                                         "3110 Port did not grant ASABT\n");
4990                 }
4991         }
4992         if (!done) {
4993                 rc = -EINVAL;
4994                 goto do_prep_failed;
4995         }
4996         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4997                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4998                         rc = -ENXIO;
4999                         goto do_prep_failed;
5000                 }
5001                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5002                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5003                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5004                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5005                                 phba->max_vpi : phba->max_vports;
5006
5007                 } else
5008                         phba->max_vpi = 0;
5009                 phba->fips_level = 0;
5010                 phba->fips_spec_rev = 0;
5011                 if (pmb->u.mb.un.varCfgPort.gdss) {
5012                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
5013                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5014                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5015                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5016                                         "2850 Security Crypto Active. FIPS x%d "
5017                                         "(Spec Rev: x%d)",
5018                                         phba->fips_level, phba->fips_spec_rev);
5019                 }
5020                 if (pmb->u.mb.un.varCfgPort.sec_err) {
5021                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5022                                         "2856 Config Port Security Crypto "
5023                                         "Error: x%x ",
5024                                         pmb->u.mb.un.varCfgPort.sec_err);
5025                 }
5026                 if (pmb->u.mb.un.varCfgPort.gerbm)
5027                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5028                 if (pmb->u.mb.un.varCfgPort.gcrp)
5029                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5030
5031                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5032                 phba->port_gp = phba->mbox->us.s3_pgp.port;
5033
5034                 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5035                         if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5036                                 phba->cfg_enable_bg = 0;
5037                                 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5038                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5039                                                 "0443 Adapter did not grant "
5040                                                 "BlockGuard\n");
5041                         }
5042                 }
5043         } else {
5044                 phba->hbq_get = NULL;
5045                 phba->port_gp = phba->mbox->us.s2.port;
5046                 phba->max_vpi = 0;
5047         }
5048 do_prep_failed:
5049         mempool_free(pmb, phba->mbox_mem_pool);
5050         return rc;
5051 }
5052
5053
5054 /**
5055  * lpfc_sli_hba_setup - SLI initialization function
5056  * @phba: Pointer to HBA context object.
5057  *
5058  * This function is the main SLI initialization function. This function
5059  * is called by the HBA initialization code, HBA reset code and HBA
5060  * error attention handler code. Caller is not required to hold any
5061  * locks. This function issues config_port mailbox command to configure
5062  * the SLI, setup iocb rings and HBQ rings. In the end the function
5063  * calls the config_port_post function to issue init_link mailbox
5064  * command and to start the discovery. The function will return zero
5065  * if successful, else it will return negative error code.
5066  **/
5067 int
5068 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5069 {
5070         uint32_t rc;
5071         int  mode = 3, i;
5072         int longs;
5073
5074         switch (phba->cfg_sli_mode) {
5075         case 2:
5076                 if (phba->cfg_enable_npiv) {
5077                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5078                                 "1824 NPIV enabled: Override sli_mode "
5079                                 "parameter (%d) to auto (0).\n",
5080                                 phba->cfg_sli_mode);
5081                         break;
5082                 }
5083                 mode = 2;
5084                 break;
5085         case 0:
5086         case 3:
5087                 break;
5088         default:
5089                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5090                                 "1819 Unrecognized sli_mode parameter: %d.\n",
5091                                 phba->cfg_sli_mode);
5092
5093                 break;
5094         }
5095         phba->fcp_embed_io = 0; /* SLI4 FC support only */
5096
5097         rc = lpfc_sli_config_port(phba, mode);
5098
5099         if (rc && phba->cfg_sli_mode == 3)
5100                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5101                                 "1820 Unable to select SLI-3.  "
5102                                 "Not supported by adapter.\n");
5103         if (rc && mode != 2)
5104                 rc = lpfc_sli_config_port(phba, 2);
5105         else if (rc && mode == 2)
5106                 rc = lpfc_sli_config_port(phba, 3);
5107         if (rc)
5108                 goto lpfc_sli_hba_setup_error;
5109
5110         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5111         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5112                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5113                 if (!rc) {
5114                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5115                                         "2709 This device supports "
5116                                         "Advanced Error Reporting (AER)\n");
5117                         spin_lock_irq(&phba->hbalock);
5118                         phba->hba_flag |= HBA_AER_ENABLED;
5119                         spin_unlock_irq(&phba->hbalock);
5120                 } else {
5121                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5122                                         "2708 This device does not support "
5123                                         "Advanced Error Reporting (AER): %d\n",
5124                                         rc);
5125                         phba->cfg_aer_support = 0;
5126                 }
5127         }
5128
5129         if (phba->sli_rev == 3) {
5130                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5131                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5132         } else {
5133                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5134                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5135                 phba->sli3_options = 0;
5136         }
5137
5138         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5139                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5140                         phba->sli_rev, phba->max_vpi);
5141         rc = lpfc_sli_ring_map(phba);
5142
5143         if (rc)
5144                 goto lpfc_sli_hba_setup_error;
5145
5146         /* Initialize VPIs. */
5147         if (phba->sli_rev == LPFC_SLI_REV3) {
5148                 /*
5149                  * The VPI bitmask and physical ID array are allocated
5150                  * and initialized once only - at driver load.  A port
5151                  * reset doesn't need to reinitialize this memory.
5152                  */
5153                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5154                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5155                         phba->vpi_bmask = kcalloc(longs,
5156                                                   sizeof(unsigned long),
5157                                                   GFP_KERNEL);
5158                         if (!phba->vpi_bmask) {
5159                                 rc = -ENOMEM;
5160                                 goto lpfc_sli_hba_setup_error;
5161                         }
5162
5163                         phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5164                                                 sizeof(uint16_t),
5165                                                 GFP_KERNEL);
5166                         if (!phba->vpi_ids) {
5167                                 kfree(phba->vpi_bmask);
5168                                 rc = -ENOMEM;
5169                                 goto lpfc_sli_hba_setup_error;
5170                         }
5171                         for (i = 0; i < phba->max_vpi; i++)
5172                                 phba->vpi_ids[i] = i;
5173                 }
5174         }
5175
5176         /* Init HBQs */
5177         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5178                 rc = lpfc_sli_hbq_setup(phba);
5179                 if (rc)
5180                         goto lpfc_sli_hba_setup_error;
5181         }
5182         spin_lock_irq(&phba->hbalock);
5183         phba->sli.sli_flag |= LPFC_PROCESS_LA;
5184         spin_unlock_irq(&phba->hbalock);
5185
5186         rc = lpfc_config_port_post(phba);
5187         if (rc)
5188                 goto lpfc_sli_hba_setup_error;
5189
5190         return rc;
5191
5192 lpfc_sli_hba_setup_error:
5193         phba->link_state = LPFC_HBA_ERROR;
5194         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5195                         "0445 Firmware initialization failed\n");
5196         return rc;
5197 }
5198
5199 /**
5200  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5201  * @phba: Pointer to HBA context object.
5202  * @mboxq: mailbox pointer.
5203  * This function issue a dump mailbox command to read config region
5204  * 23 and parse the records in the region and populate driver
5205  * data structure.
5206  **/
5207 static int
5208 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5209 {
5210         LPFC_MBOXQ_t *mboxq;
5211         struct lpfc_dmabuf *mp;
5212         struct lpfc_mqe *mqe;
5213         uint32_t data_length;
5214         int rc;
5215
5216         /* Program the default value of vlan_id and fc_map */
5217         phba->valid_vlan = 0;
5218         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5219         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5220         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5221
5222         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5223         if (!mboxq)
5224                 return -ENOMEM;
5225
5226         mqe = &mboxq->u.mqe;
5227         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5228                 rc = -ENOMEM;
5229                 goto out_free_mboxq;
5230         }
5231
5232         mp = (struct lpfc_dmabuf *) mboxq->context1;
5233         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5234
5235         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5236                         "(%d):2571 Mailbox cmd x%x Status x%x "
5237                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5238                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5239                         "CQ: x%x x%x x%x x%x\n",
5240                         mboxq->vport ? mboxq->vport->vpi : 0,
5241                         bf_get(lpfc_mqe_command, mqe),
5242                         bf_get(lpfc_mqe_status, mqe),
5243                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5244                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5245                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5246                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5247                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5248                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5249                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5250                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5251                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5252                         mboxq->mcqe.word0,
5253                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5254                         mboxq->mcqe.trailer);
5255
5256         if (rc) {
5257                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5258                 kfree(mp);
5259                 rc = -EIO;
5260                 goto out_free_mboxq;
5261         }
5262         data_length = mqe->un.mb_words[5];
5263         if (data_length > DMP_RGN23_SIZE) {
5264                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5265                 kfree(mp);
5266                 rc = -EIO;
5267                 goto out_free_mboxq;
5268         }
5269
5270         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5271         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5272         kfree(mp);
5273         rc = 0;
5274
5275 out_free_mboxq:
5276         mempool_free(mboxq, phba->mbox_mem_pool);
5277         return rc;
5278 }
5279
5280 /**
5281  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5282  * @phba: pointer to lpfc hba data structure.
5283  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5284  * @vpd: pointer to the memory to hold resulting port vpd data.
5285  * @vpd_size: On input, the number of bytes allocated to @vpd.
5286  *            On output, the number of data bytes in @vpd.
5287  *
5288  * This routine executes a READ_REV SLI4 mailbox command.  In
5289  * addition, this routine gets the port vpd data.
5290  *
5291  * Return codes
5292  *      0 - successful
5293  *      -ENOMEM - could not allocated memory.
5294  **/
5295 static int
5296 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5297                     uint8_t *vpd, uint32_t *vpd_size)
5298 {
5299         int rc = 0;
5300         uint32_t dma_size;
5301         struct lpfc_dmabuf *dmabuf;
5302         struct lpfc_mqe *mqe;
5303
5304         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5305         if (!dmabuf)
5306                 return -ENOMEM;
5307
5308         /*
5309          * Get a DMA buffer for the vpd data resulting from the READ_REV
5310          * mailbox command.
5311          */
5312         dma_size = *vpd_size;
5313         dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5314                                            &dmabuf->phys, GFP_KERNEL);
5315         if (!dmabuf->virt) {
5316                 kfree(dmabuf);
5317                 return -ENOMEM;
5318         }
5319
5320         /*
5321          * The SLI4 implementation of READ_REV conflicts at word1,
5322          * bits 31:16 and SLI4 adds vpd functionality not present
5323          * in SLI3.  This code corrects the conflicts.
5324          */
5325         lpfc_read_rev(phba, mboxq);
5326         mqe = &mboxq->u.mqe;
5327         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5328         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5329         mqe->un.read_rev.word1 &= 0x0000FFFF;
5330         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5331         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5332
5333         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5334         if (rc) {
5335                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5336                                   dmabuf->virt, dmabuf->phys);
5337                 kfree(dmabuf);
5338                 return -EIO;
5339         }
5340
5341         /*
5342          * The available vpd length cannot be bigger than the
5343          * DMA buffer passed to the port.  Catch the less than
5344          * case and update the caller's size.
5345          */
5346         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5347                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5348
5349         memcpy(vpd, dmabuf->virt, *vpd_size);
5350
5351         dma_free_coherent(&phba->pcidev->dev, dma_size,
5352                           dmabuf->virt, dmabuf->phys);
5353         kfree(dmabuf);
5354         return 0;
5355 }
5356
5357 /**
5358  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5359  * @phba: pointer to lpfc hba data structure.
5360  *
5361  * This routine retrieves SLI4 device physical port name this PCI function
5362  * is attached to.
5363  *
5364  * Return codes
5365  *      0 - successful
5366  *      otherwise - failed to retrieve physical port name
5367  **/
5368 static int
5369 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5370 {
5371         LPFC_MBOXQ_t *mboxq;
5372         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5373         struct lpfc_controller_attribute *cntl_attr;
5374         struct lpfc_mbx_get_port_name *get_port_name;
5375         void *virtaddr = NULL;
5376         uint32_t alloclen, reqlen;
5377         uint32_t shdr_status, shdr_add_status;
5378         union lpfc_sli4_cfg_shdr *shdr;
5379         char cport_name = 0;
5380         int rc;
5381
5382         /* We assume nothing at this point */
5383         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5384         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5385
5386         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5387         if (!mboxq)
5388                 return -ENOMEM;
5389         /* obtain link type and link number via READ_CONFIG */
5390         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5391         lpfc_sli4_read_config(phba);
5392         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5393                 goto retrieve_ppname;
5394
5395         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5396         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5397         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5398                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5399                         LPFC_SLI4_MBX_NEMBED);
5400         if (alloclen < reqlen) {
5401                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5402                                 "3084 Allocated DMA memory size (%d) is "
5403                                 "less than the requested DMA memory size "
5404                                 "(%d)\n", alloclen, reqlen);
5405                 rc = -ENOMEM;
5406                 goto out_free_mboxq;
5407         }
5408         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5409         virtaddr = mboxq->sge_array->addr[0];
5410         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5411         shdr = &mbx_cntl_attr->cfg_shdr;
5412         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5413         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5414         if (shdr_status || shdr_add_status || rc) {
5415                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5416                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5417                                 "rc:x%x, status:x%x, add_status:x%x\n",
5418                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5419                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5420                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5421                                 rc, shdr_status, shdr_add_status);
5422                 rc = -ENXIO;
5423                 goto out_free_mboxq;
5424         }
5425         cntl_attr = &mbx_cntl_attr->cntl_attr;
5426         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5427         phba->sli4_hba.lnk_info.lnk_tp =
5428                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5429         phba->sli4_hba.lnk_info.lnk_no =
5430                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5431         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5432                         "3086 lnk_type:%d, lnk_numb:%d\n",
5433                         phba->sli4_hba.lnk_info.lnk_tp,
5434                         phba->sli4_hba.lnk_info.lnk_no);
5435
5436 retrieve_ppname:
5437         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5438                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5439                 sizeof(struct lpfc_mbx_get_port_name) -
5440                 sizeof(struct lpfc_sli4_cfg_mhdr),
5441                 LPFC_SLI4_MBX_EMBED);
5442         get_port_name = &mboxq->u.mqe.un.get_port_name;
5443         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5444         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5445         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5446                 phba->sli4_hba.lnk_info.lnk_tp);
5447         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5448         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5449         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5450         if (shdr_status || shdr_add_status || rc) {
5451                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5452                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5453                                 "rc:x%x, status:x%x, add_status:x%x\n",
5454                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5455                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5456                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5457                                 rc, shdr_status, shdr_add_status);
5458                 rc = -ENXIO;
5459                 goto out_free_mboxq;
5460         }
5461         switch (phba->sli4_hba.lnk_info.lnk_no) {
5462         case LPFC_LINK_NUMBER_0:
5463                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5464                                 &get_port_name->u.response);
5465                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5466                 break;
5467         case LPFC_LINK_NUMBER_1:
5468                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5469                                 &get_port_name->u.response);
5470                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5471                 break;
5472         case LPFC_LINK_NUMBER_2:
5473                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5474                                 &get_port_name->u.response);
5475                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5476                 break;
5477         case LPFC_LINK_NUMBER_3:
5478                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5479                                 &get_port_name->u.response);
5480                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5481                 break;
5482         default:
5483                 break;
5484         }
5485
5486         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5487                 phba->Port[0] = cport_name;
5488                 phba->Port[1] = '\0';
5489                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5490                                 "3091 SLI get port name: %s\n", phba->Port);
5491         }
5492
5493 out_free_mboxq:
5494         if (rc != MBX_TIMEOUT) {
5495                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5496                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5497                 else
5498                         mempool_free(mboxq, phba->mbox_mem_pool);
5499         }
5500         return rc;
5501 }
5502
5503 /**
5504  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5505  * @phba: pointer to lpfc hba data structure.
5506  *
5507  * This routine is called to explicitly arm the SLI4 device's completion and
5508  * event queues
5509  **/
5510 static void
5511 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5512 {
5513         int qidx;
5514         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
5515
5516         sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5517         sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5518         if (sli4_hba->nvmels_cq)
5519                 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
5520                                                 LPFC_QUEUE_REARM);
5521
5522         if (sli4_hba->fcp_cq)
5523                 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5524                         sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
5525                                                 LPFC_QUEUE_REARM);
5526
5527         if (sli4_hba->nvme_cq)
5528                 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5529                         sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
5530                                                 LPFC_QUEUE_REARM);
5531
5532         if (phba->cfg_fof)
5533                 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
5534
5535         if (sli4_hba->hba_eq)
5536                 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5537                         sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5538                                                         LPFC_QUEUE_REARM);
5539
5540         if (phba->nvmet_support) {
5541                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5542                         sli4_hba->sli4_cq_release(
5543                                 sli4_hba->nvmet_cqset[qidx],
5544                                 LPFC_QUEUE_REARM);
5545                 }
5546         }
5547
5548         if (phba->cfg_fof)
5549                 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
5550 }
5551
5552 /**
5553  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5554  * @phba: Pointer to HBA context object.
5555  * @type: The resource extent type.
5556  * @extnt_count: buffer to hold port available extent count.
5557  * @extnt_size: buffer to hold element count per extent.
5558  *
5559  * This function calls the port and retrievs the number of available
5560  * extents and their size for a particular extent type.
5561  *
5562  * Returns: 0 if successful.  Nonzero otherwise.
5563  **/
5564 int
5565 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5566                                uint16_t *extnt_count, uint16_t *extnt_size)
5567 {
5568         int rc = 0;
5569         uint32_t length;
5570         uint32_t mbox_tmo;
5571         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5572         LPFC_MBOXQ_t *mbox;
5573
5574         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5575         if (!mbox)
5576                 return -ENOMEM;
5577
5578         /* Find out how many extents are available for this resource type */
5579         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5580                   sizeof(struct lpfc_sli4_cfg_mhdr));
5581         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5582                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5583                          length, LPFC_SLI4_MBX_EMBED);
5584
5585         /* Send an extents count of 0 - the GET doesn't use it. */
5586         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5587                                         LPFC_SLI4_MBX_EMBED);
5588         if (unlikely(rc)) {
5589                 rc = -EIO;
5590                 goto err_exit;
5591         }
5592
5593         if (!phba->sli4_hba.intr_enable)
5594                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5595         else {
5596                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5597                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5598         }
5599         if (unlikely(rc)) {
5600                 rc = -EIO;
5601                 goto err_exit;
5602         }
5603
5604         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5605         if (bf_get(lpfc_mbox_hdr_status,
5606                    &rsrc_info->header.cfg_shdr.response)) {
5607                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5608                                 "2930 Failed to get resource extents "
5609                                 "Status 0x%x Add'l Status 0x%x\n",
5610                                 bf_get(lpfc_mbox_hdr_status,
5611                                        &rsrc_info->header.cfg_shdr.response),
5612                                 bf_get(lpfc_mbox_hdr_add_status,
5613                                        &rsrc_info->header.cfg_shdr.response));
5614                 rc = -EIO;
5615                 goto err_exit;
5616         }
5617
5618         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5619                               &rsrc_info->u.rsp);
5620         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5621                              &rsrc_info->u.rsp);
5622
5623         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5624                         "3162 Retrieved extents type-%d from port: count:%d, "
5625                         "size:%d\n", type, *extnt_count, *extnt_size);
5626
5627 err_exit:
5628         mempool_free(mbox, phba->mbox_mem_pool);
5629         return rc;
5630 }
5631
5632 /**
5633  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5634  * @phba: Pointer to HBA context object.
5635  * @type: The extent type to check.
5636  *
5637  * This function reads the current available extents from the port and checks
5638  * if the extent count or extent size has changed since the last access.
5639  * Callers use this routine post port reset to understand if there is a
5640  * extent reprovisioning requirement.
5641  *
5642  * Returns:
5643  *   -Error: error indicates problem.
5644  *   1: Extent count or size has changed.
5645  *   0: No changes.
5646  **/
5647 static int
5648 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5649 {
5650         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5651         uint16_t size_diff, rsrc_ext_size;
5652         int rc = 0;
5653         struct lpfc_rsrc_blks *rsrc_entry;
5654         struct list_head *rsrc_blk_list = NULL;
5655
5656         size_diff = 0;
5657         curr_ext_cnt = 0;
5658         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5659                                             &rsrc_ext_cnt,
5660                                             &rsrc_ext_size);
5661         if (unlikely(rc))
5662                 return -EIO;
5663
5664         switch (type) {
5665         case LPFC_RSC_TYPE_FCOE_RPI:
5666                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5667                 break;
5668         case LPFC_RSC_TYPE_FCOE_VPI:
5669                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5670                 break;
5671         case LPFC_RSC_TYPE_FCOE_XRI:
5672                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5673                 break;
5674         case LPFC_RSC_TYPE_FCOE_VFI:
5675                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5676                 break;
5677         default:
5678                 break;
5679         }
5680
5681         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5682                 curr_ext_cnt++;
5683                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5684                         size_diff++;
5685         }
5686
5687         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5688                 rc = 1;
5689
5690         return rc;
5691 }
5692
5693 /**
5694  * lpfc_sli4_cfg_post_extnts -
5695  * @phba: Pointer to HBA context object.
5696  * @extnt_cnt - number of available extents.
5697  * @type - the extent type (rpi, xri, vfi, vpi).
5698  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5699  * @mbox - pointer to the caller's allocated mailbox structure.
5700  *
5701  * This function executes the extents allocation request.  It also
5702  * takes care of the amount of memory needed to allocate or get the
5703  * allocated extents. It is the caller's responsibility to evaluate
5704  * the response.
5705  *
5706  * Returns:
5707  *   -Error:  Error value describes the condition found.
5708  *   0: if successful
5709  **/
5710 static int
5711 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5712                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5713 {
5714         int rc = 0;
5715         uint32_t req_len;
5716         uint32_t emb_len;
5717         uint32_t alloc_len, mbox_tmo;
5718
5719         /* Calculate the total requested length of the dma memory */
5720         req_len = extnt_cnt * sizeof(uint16_t);
5721
5722         /*
5723          * Calculate the size of an embedded mailbox.  The uint32_t
5724          * accounts for extents-specific word.
5725          */
5726         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5727                 sizeof(uint32_t);
5728
5729         /*
5730          * Presume the allocation and response will fit into an embedded
5731          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5732          */
5733         *emb = LPFC_SLI4_MBX_EMBED;
5734         if (req_len > emb_len) {
5735                 req_len = extnt_cnt * sizeof(uint16_t) +
5736                         sizeof(union lpfc_sli4_cfg_shdr) +
5737                         sizeof(uint32_t);
5738                 *emb = LPFC_SLI4_MBX_NEMBED;
5739         }
5740
5741         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5742                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5743                                      req_len, *emb);
5744         if (alloc_len < req_len) {
5745                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5746                         "2982 Allocated DMA memory size (x%x) is "
5747                         "less than the requested DMA memory "
5748                         "size (x%x)\n", alloc_len, req_len);
5749                 return -ENOMEM;
5750         }
5751         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5752         if (unlikely(rc))
5753                 return -EIO;
5754
5755         if (!phba->sli4_hba.intr_enable)
5756                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5757         else {
5758                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5759                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5760         }
5761
5762         if (unlikely(rc))
5763                 rc = -EIO;
5764         return rc;
5765 }
5766
5767 /**
5768  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5769  * @phba: Pointer to HBA context object.
5770  * @type:  The resource extent type to allocate.
5771  *
5772  * This function allocates the number of elements for the specified
5773  * resource type.
5774  **/
5775 static int
5776 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5777 {
5778         bool emb = false;
5779         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5780         uint16_t rsrc_id, rsrc_start, j, k;
5781         uint16_t *ids;
5782         int i, rc;
5783         unsigned long longs;
5784         unsigned long *bmask;
5785         struct lpfc_rsrc_blks *rsrc_blks;
5786         LPFC_MBOXQ_t *mbox;
5787         uint32_t length;
5788         struct lpfc_id_range *id_array = NULL;
5789         void *virtaddr = NULL;
5790         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5791         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5792         struct list_head *ext_blk_list;
5793
5794         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5795                                             &rsrc_cnt,
5796                                             &rsrc_size);
5797         if (unlikely(rc))
5798                 return -EIO;
5799
5800         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5801                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5802                         "3009 No available Resource Extents "
5803                         "for resource type 0x%x: Count: 0x%x, "
5804                         "Size 0x%x\n", type, rsrc_cnt,
5805                         rsrc_size);
5806                 return -ENOMEM;
5807         }
5808
5809         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5810                         "2903 Post resource extents type-0x%x: "
5811                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5812
5813         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5814         if (!mbox)
5815                 return -ENOMEM;
5816
5817         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5818         if (unlikely(rc)) {
5819                 rc = -EIO;
5820                 goto err_exit;
5821         }
5822
5823         /*
5824          * Figure out where the response is located.  Then get local pointers
5825          * to the response data.  The port does not guarantee to respond to
5826          * all extents counts request so update the local variable with the
5827          * allocated count from the port.
5828          */
5829         if (emb == LPFC_SLI4_MBX_EMBED) {
5830                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5831                 id_array = &rsrc_ext->u.rsp.id[0];
5832                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5833         } else {
5834                 virtaddr = mbox->sge_array->addr[0];
5835                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5836                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5837                 id_array = &n_rsrc->id;
5838         }
5839
5840         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5841         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5842
5843         /*
5844          * Based on the resource size and count, correct the base and max
5845          * resource values.
5846          */
5847         length = sizeof(struct lpfc_rsrc_blks);
5848         switch (type) {
5849         case LPFC_RSC_TYPE_FCOE_RPI:
5850                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
5851                                                    sizeof(unsigned long),
5852                                                    GFP_KERNEL);
5853                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5854                         rc = -ENOMEM;
5855                         goto err_exit;
5856                 }
5857                 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
5858                                                  sizeof(uint16_t),
5859                                                  GFP_KERNEL);
5860                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5861                         kfree(phba->sli4_hba.rpi_bmask);
5862                         rc = -ENOMEM;
5863                         goto err_exit;
5864                 }
5865
5866                 /*
5867                  * The next_rpi was initialized with the maximum available
5868                  * count but the port may allocate a smaller number.  Catch
5869                  * that case and update the next_rpi.
5870                  */
5871                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5872
5873                 /* Initialize local ptrs for common extent processing later. */
5874                 bmask = phba->sli4_hba.rpi_bmask;
5875                 ids = phba->sli4_hba.rpi_ids;
5876                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5877                 break;
5878         case LPFC_RSC_TYPE_FCOE_VPI:
5879                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
5880                                           GFP_KERNEL);
5881                 if (unlikely(!phba->vpi_bmask)) {
5882                         rc = -ENOMEM;
5883                         goto err_exit;
5884                 }
5885                 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
5886                                          GFP_KERNEL);
5887                 if (unlikely(!phba->vpi_ids)) {
5888                         kfree(phba->vpi_bmask);
5889                         rc = -ENOMEM;
5890                         goto err_exit;
5891                 }
5892
5893                 /* Initialize local ptrs for common extent processing later. */
5894                 bmask = phba->vpi_bmask;
5895                 ids = phba->vpi_ids;
5896                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5897                 break;
5898         case LPFC_RSC_TYPE_FCOE_XRI:
5899                 phba->sli4_hba.xri_bmask = kcalloc(longs,
5900                                                    sizeof(unsigned long),
5901                                                    GFP_KERNEL);
5902                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5903                         rc = -ENOMEM;
5904                         goto err_exit;
5905                 }
5906                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5907                 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
5908                                                  sizeof(uint16_t),
5909                                                  GFP_KERNEL);
5910                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5911                         kfree(phba->sli4_hba.xri_bmask);
5912                         rc = -ENOMEM;
5913                         goto err_exit;
5914                 }
5915
5916                 /* Initialize local ptrs for common extent processing later. */
5917                 bmask = phba->sli4_hba.xri_bmask;
5918                 ids = phba->sli4_hba.xri_ids;
5919                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5920                 break;
5921         case LPFC_RSC_TYPE_FCOE_VFI:
5922                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
5923                                                    sizeof(unsigned long),
5924                                                    GFP_KERNEL);
5925                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5926                         rc = -ENOMEM;
5927                         goto err_exit;
5928                 }
5929                 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
5930                                                  sizeof(uint16_t),
5931                                                  GFP_KERNEL);
5932                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5933                         kfree(phba->sli4_hba.vfi_bmask);
5934                         rc = -ENOMEM;
5935                         goto err_exit;
5936                 }
5937
5938                 /* Initialize local ptrs for common extent processing later. */
5939                 bmask = phba->sli4_hba.vfi_bmask;
5940                 ids = phba->sli4_hba.vfi_ids;
5941                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5942                 break;
5943         default:
5944                 /* Unsupported Opcode.  Fail call. */
5945                 id_array = NULL;
5946                 bmask = NULL;
5947                 ids = NULL;
5948                 ext_blk_list = NULL;
5949                 goto err_exit;
5950         }
5951
5952         /*
5953          * Complete initializing the extent configuration with the
5954          * allocated ids assigned to this function.  The bitmask serves
5955          * as an index into the array and manages the available ids.  The
5956          * array just stores the ids communicated to the port via the wqes.
5957          */
5958         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5959                 if ((i % 2) == 0)
5960                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5961                                          &id_array[k]);
5962                 else
5963                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5964                                          &id_array[k]);
5965
5966                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5967                 if (unlikely(!rsrc_blks)) {
5968                         rc = -ENOMEM;
5969                         kfree(bmask);
5970                         kfree(ids);
5971                         goto err_exit;
5972                 }
5973                 rsrc_blks->rsrc_start = rsrc_id;
5974                 rsrc_blks->rsrc_size = rsrc_size;
5975                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5976                 rsrc_start = rsrc_id;
5977                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
5978                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5979                                 lpfc_sli4_get_iocb_cnt(phba);
5980                         phba->sli4_hba.nvme_xri_start =
5981                                 phba->sli4_hba.scsi_xri_start +
5982                                 phba->sli4_hba.scsi_xri_max;
5983                 }
5984
5985                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5986                         ids[j] = rsrc_id;
5987                         rsrc_id++;
5988                         j++;
5989                 }
5990                 /* Entire word processed.  Get next word.*/
5991                 if ((i % 2) == 1)
5992                         k++;
5993         }
5994  err_exit:
5995         lpfc_sli4_mbox_cmd_free(phba, mbox);
5996         return rc;
5997 }
5998
5999
6000
6001 /**
6002  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6003  * @phba: Pointer to HBA context object.
6004  * @type: the extent's type.
6005  *
6006  * This function deallocates all extents of a particular resource type.
6007  * SLI4 does not allow for deallocating a particular extent range.  It
6008  * is the caller's responsibility to release all kernel memory resources.
6009  **/
6010 static int
6011 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6012 {
6013         int rc;
6014         uint32_t length, mbox_tmo = 0;
6015         LPFC_MBOXQ_t *mbox;
6016         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6017         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6018
6019         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6020         if (!mbox)
6021                 return -ENOMEM;
6022
6023         /*
6024          * This function sends an embedded mailbox because it only sends the
6025          * the resource type.  All extents of this type are released by the
6026          * port.
6027          */
6028         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6029                   sizeof(struct lpfc_sli4_cfg_mhdr));
6030         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6031                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6032                          length, LPFC_SLI4_MBX_EMBED);
6033
6034         /* Send an extents count of 0 - the dealloc doesn't use it. */
6035         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6036                                         LPFC_SLI4_MBX_EMBED);
6037         if (unlikely(rc)) {
6038                 rc = -EIO;
6039                 goto out_free_mbox;
6040         }
6041         if (!phba->sli4_hba.intr_enable)
6042                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6043         else {
6044                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6045                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6046         }
6047         if (unlikely(rc)) {
6048                 rc = -EIO;
6049                 goto out_free_mbox;
6050         }
6051
6052         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6053         if (bf_get(lpfc_mbox_hdr_status,
6054                    &dealloc_rsrc->header.cfg_shdr.response)) {
6055                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6056                                 "2919 Failed to release resource extents "
6057                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
6058                                 "Resource memory not released.\n",
6059                                 type,
6060                                 bf_get(lpfc_mbox_hdr_status,
6061                                     &dealloc_rsrc->header.cfg_shdr.response),
6062                                 bf_get(lpfc_mbox_hdr_add_status,
6063                                     &dealloc_rsrc->header.cfg_shdr.response));
6064                 rc = -EIO;
6065                 goto out_free_mbox;
6066         }
6067
6068         /* Release kernel memory resources for the specific type. */
6069         switch (type) {
6070         case LPFC_RSC_TYPE_FCOE_VPI:
6071                 kfree(phba->vpi_bmask);
6072                 kfree(phba->vpi_ids);
6073                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6074                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6075                                     &phba->lpfc_vpi_blk_list, list) {
6076                         list_del_init(&rsrc_blk->list);
6077                         kfree(rsrc_blk);
6078                 }
6079                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6080                 break;
6081         case LPFC_RSC_TYPE_FCOE_XRI:
6082                 kfree(phba->sli4_hba.xri_bmask);
6083                 kfree(phba->sli4_hba.xri_ids);
6084                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6085                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
6086                         list_del_init(&rsrc_blk->list);
6087                         kfree(rsrc_blk);
6088                 }
6089                 break;
6090         case LPFC_RSC_TYPE_FCOE_VFI:
6091                 kfree(phba->sli4_hba.vfi_bmask);
6092                 kfree(phba->sli4_hba.vfi_ids);
6093                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6094                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6095                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6096                         list_del_init(&rsrc_blk->list);
6097                         kfree(rsrc_blk);
6098                 }
6099                 break;
6100         case LPFC_RSC_TYPE_FCOE_RPI:
6101                 /* RPI bitmask and physical id array are cleaned up earlier. */
6102                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6103                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6104                         list_del_init(&rsrc_blk->list);
6105                         kfree(rsrc_blk);
6106                 }
6107                 break;
6108         default:
6109                 break;
6110         }
6111
6112         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6113
6114  out_free_mbox:
6115         mempool_free(mbox, phba->mbox_mem_pool);
6116         return rc;
6117 }
6118
6119 static void
6120 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6121                   uint32_t feature)
6122 {
6123         uint32_t len;
6124
6125         len = sizeof(struct lpfc_mbx_set_feature) -
6126                 sizeof(struct lpfc_sli4_cfg_mhdr);
6127         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6128                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
6129                          LPFC_SLI4_MBX_EMBED);
6130
6131         switch (feature) {
6132         case LPFC_SET_UE_RECOVERY:
6133                 bf_set(lpfc_mbx_set_feature_UER,
6134                        &mbox->u.mqe.un.set_feature, 1);
6135                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6136                 mbox->u.mqe.un.set_feature.param_len = 8;
6137                 break;
6138         case LPFC_SET_MDS_DIAGS:
6139                 bf_set(lpfc_mbx_set_feature_mds,
6140                        &mbox->u.mqe.un.set_feature, 1);
6141                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6142                        &mbox->u.mqe.un.set_feature, 1);
6143                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6144                 mbox->u.mqe.un.set_feature.param_len = 8;
6145                 break;
6146         }
6147
6148         return;
6149 }
6150
6151 /**
6152  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6153  * @phba: Pointer to HBA context object.
6154  *
6155  * This function is called to free memory allocated for RAS FW logging
6156  * support in the driver.
6157  **/
6158 void
6159 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6160 {
6161         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6162         struct lpfc_dmabuf *dmabuf, *next;
6163
6164         if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6165                 list_for_each_entry_safe(dmabuf, next,
6166                                     &ras_fwlog->fwlog_buff_list,
6167                                     list) {
6168                         list_del(&dmabuf->list);
6169                         dma_free_coherent(&phba->pcidev->dev,
6170                                           LPFC_RAS_MAX_ENTRY_SIZE,
6171                                           dmabuf->virt, dmabuf->phys);
6172                         kfree(dmabuf);
6173                 }
6174         }
6175
6176         if (ras_fwlog->lwpd.virt) {
6177                 dma_free_coherent(&phba->pcidev->dev,
6178                                   sizeof(uint32_t) * 2,
6179                                   ras_fwlog->lwpd.virt,
6180                                   ras_fwlog->lwpd.phys);
6181                 ras_fwlog->lwpd.virt = NULL;
6182         }
6183
6184         ras_fwlog->ras_active = false;
6185 }
6186
6187 /**
6188  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6189  * @phba: Pointer to HBA context object.
6190  * @fwlog_buff_count: Count of buffers to be created.
6191  *
6192  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6193  * to update FW log is posted to the adapter.
6194  * Buffer count is calculated based on module param ras_fwlog_buffsize
6195  * Size of each buffer posted to FW is 64K.
6196  **/
6197
6198 static int
6199 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6200                         uint32_t fwlog_buff_count)
6201 {
6202         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6203         struct lpfc_dmabuf *dmabuf;
6204         int rc = 0, i = 0;
6205
6206         /* Initialize List */
6207         INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6208
6209         /* Allocate memory for the LWPD */
6210         ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6211                                             sizeof(uint32_t) * 2,
6212                                             &ras_fwlog->lwpd.phys,
6213                                             GFP_KERNEL);
6214         if (!ras_fwlog->lwpd.virt) {
6215                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6216                                 "6185 LWPD Memory Alloc Failed\n");
6217
6218                 return -ENOMEM;
6219         }
6220
6221         ras_fwlog->fw_buffcount = fwlog_buff_count;
6222         for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6223                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6224                                  GFP_KERNEL);
6225                 if (!dmabuf) {
6226                         rc = -ENOMEM;
6227                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6228                                         "6186 Memory Alloc failed FW logging");
6229                         goto free_mem;
6230                 }
6231
6232                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6233                                                   LPFC_RAS_MAX_ENTRY_SIZE,
6234                                                   &dmabuf->phys,
6235                                                   GFP_KERNEL);
6236                 if (!dmabuf->virt) {
6237                         kfree(dmabuf);
6238                         rc = -ENOMEM;
6239                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6240                                         "6187 DMA Alloc Failed FW logging");
6241                         goto free_mem;
6242                 }
6243                 dmabuf->buffer_tag = i;
6244                 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6245         }
6246
6247 free_mem:
6248         if (rc)
6249                 lpfc_sli4_ras_dma_free(phba);
6250
6251         return rc;
6252 }
6253
6254 /**
6255  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6256  * @phba: pointer to lpfc hba data structure.
6257  * @pmboxq: pointer to the driver internal queue element for mailbox command.
6258  *
6259  * Completion handler for driver's RAS MBX command to the device.
6260  **/
6261 static void
6262 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6263 {
6264         MAILBOX_t *mb;
6265         union lpfc_sli4_cfg_shdr *shdr;
6266         uint32_t shdr_status, shdr_add_status;
6267         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6268
6269         mb = &pmb->u.mb;
6270
6271         shdr = (union lpfc_sli4_cfg_shdr *)
6272                 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6273         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6274         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6275
6276         if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6277                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
6278                                 "6188 FW LOG mailbox "
6279                                 "completed with status x%x add_status x%x,"
6280                                 " mbx status x%x\n",
6281                                 shdr_status, shdr_add_status, mb->mbxStatus);
6282
6283                 ras_fwlog->ras_hwsupport = false;
6284                 goto disable_ras;
6285         }
6286
6287         ras_fwlog->ras_active = true;
6288         mempool_free(pmb, phba->mbox_mem_pool);
6289
6290         return;
6291
6292 disable_ras:
6293         /* Free RAS DMA memory */
6294         lpfc_sli4_ras_dma_free(phba);
6295         mempool_free(pmb, phba->mbox_mem_pool);
6296 }
6297
6298 /**
6299  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6300  * @phba: pointer to lpfc hba data structure.
6301  * @fwlog_level: Logging verbosity level.
6302  * @fwlog_enable: Enable/Disable logging.
6303  *
6304  * Initialize memory and post mailbox command to enable FW logging in host
6305  * memory.
6306  **/
6307 int
6308 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6309                          uint32_t fwlog_level,
6310                          uint32_t fwlog_enable)
6311 {
6312         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6313         struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6314         struct lpfc_dmabuf *dmabuf;
6315         LPFC_MBOXQ_t *mbox;
6316         uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6317         int rc = 0;
6318
6319         fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6320                           phba->cfg_ras_fwlog_buffsize);
6321         fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6322
6323         /*
6324          * If re-enabling FW logging support use earlier allocated
6325          * DMA buffers while posting MBX command.
6326          **/
6327         if (!ras_fwlog->lwpd.virt) {
6328                 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6329                 if (rc) {
6330                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6331                                         "6189 FW Log Memory Allocation Failed");
6332                         return rc;
6333                 }
6334         }
6335
6336         /* Setup Mailbox command */
6337         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6338         if (!mbox) {
6339                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6340                                 "6190 RAS MBX Alloc Failed");
6341                 rc = -ENOMEM;
6342                 goto mem_free;
6343         }
6344
6345         ras_fwlog->fw_loglevel = fwlog_level;
6346         len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6347                 sizeof(struct lpfc_sli4_cfg_mhdr));
6348
6349         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6350                          LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6351                          len, LPFC_SLI4_MBX_EMBED);
6352
6353         mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6354         bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6355                fwlog_enable);
6356         bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6357                ras_fwlog->fw_loglevel);
6358         bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6359                ras_fwlog->fw_buffcount);
6360         bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6361                LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6362
6363         /* Update DMA buffer address */
6364         list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6365                 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6366
6367                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6368                         putPaddrLow(dmabuf->phys);
6369
6370                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6371                         putPaddrHigh(dmabuf->phys);
6372         }
6373
6374         /* Update LPWD address */
6375         mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6376         mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6377
6378         mbox->vport = phba->pport;
6379         mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6380
6381         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6382
6383         if (rc == MBX_NOT_FINISHED) {
6384                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6385                                 "6191 FW-Log Mailbox failed. "
6386                                 "status %d mbxStatus : x%x", rc,
6387                                 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6388                 mempool_free(mbox, phba->mbox_mem_pool);
6389                 rc = -EIO;
6390                 goto mem_free;
6391         } else
6392                 rc = 0;
6393 mem_free:
6394         if (rc)
6395                 lpfc_sli4_ras_dma_free(phba);
6396
6397         return rc;
6398 }
6399
6400 /**
6401  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6402  * @phba: Pointer to HBA context object.
6403  *
6404  * Check if RAS is supported on the adapter and initialize it.
6405  **/
6406 void
6407 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6408 {
6409         /* Check RAS FW Log needs to be enabled or not */
6410         if (lpfc_check_fwlog_support(phba))
6411                 return;
6412
6413         lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6414                                  LPFC_RAS_ENABLE_LOGGING);
6415 }
6416
6417 /**
6418  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6419  * @phba: Pointer to HBA context object.
6420  *
6421  * This function allocates all SLI4 resource identifiers.
6422  **/
6423 int
6424 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6425 {
6426         int i, rc, error = 0;
6427         uint16_t count, base;
6428         unsigned long longs;
6429
6430         if (!phba->sli4_hba.rpi_hdrs_in_use)
6431                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6432         if (phba->sli4_hba.extents_in_use) {
6433                 /*
6434                  * The port supports resource extents. The XRI, VPI, VFI, RPI
6435                  * resource extent count must be read and allocated before
6436                  * provisioning the resource id arrays.
6437                  */
6438                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6439                     LPFC_IDX_RSRC_RDY) {
6440                         /*
6441                          * Extent-based resources are set - the driver could
6442                          * be in a port reset. Figure out if any corrective
6443                          * actions need to be taken.
6444                          */
6445                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6446                                                  LPFC_RSC_TYPE_FCOE_VFI);
6447                         if (rc != 0)
6448                                 error++;
6449                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6450                                                  LPFC_RSC_TYPE_FCOE_VPI);
6451                         if (rc != 0)
6452                                 error++;
6453                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6454                                                  LPFC_RSC_TYPE_FCOE_XRI);
6455                         if (rc != 0)
6456                                 error++;
6457                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6458                                                  LPFC_RSC_TYPE_FCOE_RPI);
6459                         if (rc != 0)
6460                                 error++;
6461
6462                         /*
6463                          * It's possible that the number of resources
6464                          * provided to this port instance changed between
6465                          * resets.  Detect this condition and reallocate
6466                          * resources.  Otherwise, there is no action.
6467                          */
6468                         if (error) {
6469                                 lpfc_printf_log(phba, KERN_INFO,
6470                                                 LOG_MBOX | LOG_INIT,
6471                                                 "2931 Detected extent resource "
6472                                                 "change.  Reallocating all "
6473                                                 "extents.\n");
6474                                 rc = lpfc_sli4_dealloc_extent(phba,
6475                                                  LPFC_RSC_TYPE_FCOE_VFI);
6476                                 rc = lpfc_sli4_dealloc_extent(phba,
6477                                                  LPFC_RSC_TYPE_FCOE_VPI);
6478                                 rc = lpfc_sli4_dealloc_extent(phba,
6479                                                  LPFC_RSC_TYPE_FCOE_XRI);
6480                                 rc = lpfc_sli4_dealloc_extent(phba,
6481                                                  LPFC_RSC_TYPE_FCOE_RPI);
6482                         } else
6483                                 return 0;
6484                 }
6485
6486                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6487                 if (unlikely(rc))
6488                         goto err_exit;
6489
6490                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6491                 if (unlikely(rc))
6492                         goto err_exit;
6493
6494                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6495                 if (unlikely(rc))
6496                         goto err_exit;
6497
6498                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6499                 if (unlikely(rc))
6500                         goto err_exit;
6501                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6502                        LPFC_IDX_RSRC_RDY);
6503                 return rc;
6504         } else {
6505                 /*
6506                  * The port does not support resource extents.  The XRI, VPI,
6507                  * VFI, RPI resource ids were determined from READ_CONFIG.
6508                  * Just allocate the bitmasks and provision the resource id
6509                  * arrays.  If a port reset is active, the resources don't
6510                  * need any action - just exit.
6511                  */
6512                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6513                     LPFC_IDX_RSRC_RDY) {
6514                         lpfc_sli4_dealloc_resource_identifiers(phba);
6515                         lpfc_sli4_remove_rpis(phba);
6516                 }
6517                 /* RPIs. */
6518                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6519                 if (count <= 0) {
6520                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6521                                         "3279 Invalid provisioning of "
6522                                         "rpi:%d\n", count);
6523                         rc = -EINVAL;
6524                         goto err_exit;
6525                 }
6526                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6527                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6528                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6529                                                    sizeof(unsigned long),
6530                                                    GFP_KERNEL);
6531                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6532                         rc = -ENOMEM;
6533                         goto err_exit;
6534                 }
6535                 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
6536                                                  GFP_KERNEL);
6537                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6538                         rc = -ENOMEM;
6539                         goto free_rpi_bmask;
6540                 }
6541
6542                 for (i = 0; i < count; i++)
6543                         phba->sli4_hba.rpi_ids[i] = base + i;
6544
6545                 /* VPIs. */
6546                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6547                 if (count <= 0) {
6548                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6549                                         "3280 Invalid provisioning of "
6550                                         "vpi:%d\n", count);
6551                         rc = -EINVAL;
6552                         goto free_rpi_ids;
6553                 }
6554                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6555                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6556                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6557                                           GFP_KERNEL);
6558                 if (unlikely(!phba->vpi_bmask)) {
6559                         rc = -ENOMEM;
6560                         goto free_rpi_ids;
6561                 }
6562                 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
6563                                         GFP_KERNEL);
6564                 if (unlikely(!phba->vpi_ids)) {
6565                         rc = -ENOMEM;
6566                         goto free_vpi_bmask;
6567                 }
6568
6569                 for (i = 0; i < count; i++)
6570                         phba->vpi_ids[i] = base + i;
6571
6572                 /* XRIs. */
6573                 count = phba->sli4_hba.max_cfg_param.max_xri;
6574                 if (count <= 0) {
6575                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6576                                         "3281 Invalid provisioning of "
6577                                         "xri:%d\n", count);
6578                         rc = -EINVAL;
6579                         goto free_vpi_ids;
6580                 }
6581                 base = phba->sli4_hba.max_cfg_param.xri_base;
6582                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6583                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6584                                                    sizeof(unsigned long),
6585                                                    GFP_KERNEL);
6586                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6587                         rc = -ENOMEM;
6588                         goto free_vpi_ids;
6589                 }
6590                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6591                 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
6592                                                  GFP_KERNEL);
6593                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6594                         rc = -ENOMEM;
6595                         goto free_xri_bmask;
6596                 }
6597
6598                 for (i = 0; i < count; i++)
6599                         phba->sli4_hba.xri_ids[i] = base + i;
6600
6601                 /* VFIs. */
6602                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6603                 if (count <= 0) {
6604                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6605                                         "3282 Invalid provisioning of "
6606                                         "vfi:%d\n", count);
6607                         rc = -EINVAL;
6608                         goto free_xri_ids;
6609                 }
6610                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6611                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6612                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6613                                                    sizeof(unsigned long),
6614                                                    GFP_KERNEL);
6615                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6616                         rc = -ENOMEM;
6617                         goto free_xri_ids;
6618                 }
6619                 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
6620                                                  GFP_KERNEL);
6621                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6622                         rc = -ENOMEM;
6623                         goto free_vfi_bmask;
6624                 }
6625
6626                 for (i = 0; i < count; i++)
6627                         phba->sli4_hba.vfi_ids[i] = base + i;
6628
6629                 /*
6630                  * Mark all resources ready.  An HBA reset doesn't need
6631                  * to reset the initialization.
6632                  */
6633                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6634                        LPFC_IDX_RSRC_RDY);
6635                 return 0;
6636         }
6637
6638  free_vfi_bmask:
6639         kfree(phba->sli4_hba.vfi_bmask);
6640         phba->sli4_hba.vfi_bmask = NULL;
6641  free_xri_ids:
6642         kfree(phba->sli4_hba.xri_ids);
6643         phba->sli4_hba.xri_ids = NULL;
6644  free_xri_bmask:
6645         kfree(phba->sli4_hba.xri_bmask);
6646         phba->sli4_hba.xri_bmask = NULL;
6647  free_vpi_ids:
6648         kfree(phba->vpi_ids);
6649         phba->vpi_ids = NULL;
6650  free_vpi_bmask:
6651         kfree(phba->vpi_bmask);
6652         phba->vpi_bmask = NULL;
6653  free_rpi_ids:
6654         kfree(phba->sli4_hba.rpi_ids);
6655         phba->sli4_hba.rpi_ids = NULL;
6656  free_rpi_bmask:
6657         kfree(phba->sli4_hba.rpi_bmask);
6658         phba->sli4_hba.rpi_bmask = NULL;
6659  err_exit:
6660         return rc;
6661 }
6662
6663 /**
6664  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6665  * @phba: Pointer to HBA context object.
6666  *
6667  * This function allocates the number of elements for the specified
6668  * resource type.
6669  **/
6670 int
6671 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6672 {
6673         if (phba->sli4_hba.extents_in_use) {
6674                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6675                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6676                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6677                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6678         } else {
6679                 kfree(phba->vpi_bmask);
6680                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6681                 kfree(phba->vpi_ids);
6682                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6683                 kfree(phba->sli4_hba.xri_bmask);
6684                 kfree(phba->sli4_hba.xri_ids);
6685                 kfree(phba->sli4_hba.vfi_bmask);
6686                 kfree(phba->sli4_hba.vfi_ids);
6687                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6688                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6689         }
6690
6691         return 0;
6692 }
6693
6694 /**
6695  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6696  * @phba: Pointer to HBA context object.
6697  * @type: The resource extent type.
6698  * @extnt_count: buffer to hold port extent count response
6699  * @extnt_size: buffer to hold port extent size response.
6700  *
6701  * This function calls the port to read the host allocated extents
6702  * for a particular type.
6703  **/
6704 int
6705 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6706                                uint16_t *extnt_cnt, uint16_t *extnt_size)
6707 {
6708         bool emb;
6709         int rc = 0;
6710         uint16_t curr_blks = 0;
6711         uint32_t req_len, emb_len;
6712         uint32_t alloc_len, mbox_tmo;
6713         struct list_head *blk_list_head;
6714         struct lpfc_rsrc_blks *rsrc_blk;
6715         LPFC_MBOXQ_t *mbox;
6716         void *virtaddr = NULL;
6717         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6718         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6719         union  lpfc_sli4_cfg_shdr *shdr;
6720
6721         switch (type) {
6722         case LPFC_RSC_TYPE_FCOE_VPI:
6723                 blk_list_head = &phba->lpfc_vpi_blk_list;
6724                 break;
6725         case LPFC_RSC_TYPE_FCOE_XRI:
6726                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6727                 break;
6728         case LPFC_RSC_TYPE_FCOE_VFI:
6729                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6730                 break;
6731         case LPFC_RSC_TYPE_FCOE_RPI:
6732                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6733                 break;
6734         default:
6735                 return -EIO;
6736         }
6737
6738         /* Count the number of extents currently allocatd for this type. */
6739         list_for_each_entry(rsrc_blk, blk_list_head, list) {
6740                 if (curr_blks == 0) {
6741                         /*
6742                          * The GET_ALLOCATED mailbox does not return the size,
6743                          * just the count.  The size should be just the size
6744                          * stored in the current allocated block and all sizes
6745                          * for an extent type are the same so set the return
6746                          * value now.
6747                          */
6748                         *extnt_size = rsrc_blk->rsrc_size;
6749                 }
6750                 curr_blks++;
6751         }
6752
6753         /*
6754          * Calculate the size of an embedded mailbox.  The uint32_t
6755          * accounts for extents-specific word.
6756          */
6757         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6758                 sizeof(uint32_t);
6759
6760         /*
6761          * Presume the allocation and response will fit into an embedded
6762          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6763          */
6764         emb = LPFC_SLI4_MBX_EMBED;
6765         req_len = emb_len;
6766         if (req_len > emb_len) {
6767                 req_len = curr_blks * sizeof(uint16_t) +
6768                         sizeof(union lpfc_sli4_cfg_shdr) +
6769                         sizeof(uint32_t);
6770                 emb = LPFC_SLI4_MBX_NEMBED;
6771         }
6772
6773         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6774         if (!mbox)
6775                 return -ENOMEM;
6776         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6777
6778         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6779                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6780                                      req_len, emb);
6781         if (alloc_len < req_len) {
6782                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6783                         "2983 Allocated DMA memory size (x%x) is "
6784                         "less than the requested DMA memory "
6785                         "size (x%x)\n", alloc_len, req_len);
6786                 rc = -ENOMEM;
6787                 goto err_exit;
6788         }
6789         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6790         if (unlikely(rc)) {
6791                 rc = -EIO;
6792                 goto err_exit;
6793         }
6794
6795         if (!phba->sli4_hba.intr_enable)
6796                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6797         else {
6798                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6799                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6800         }
6801
6802         if (unlikely(rc)) {
6803                 rc = -EIO;
6804                 goto err_exit;
6805         }
6806
6807         /*
6808          * Figure out where the response is located.  Then get local pointers
6809          * to the response data.  The port does not guarantee to respond to
6810          * all extents counts request so update the local variable with the
6811          * allocated count from the port.
6812          */
6813         if (emb == LPFC_SLI4_MBX_EMBED) {
6814                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6815                 shdr = &rsrc_ext->header.cfg_shdr;
6816                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6817         } else {
6818                 virtaddr = mbox->sge_array->addr[0];
6819                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6820                 shdr = &n_rsrc->cfg_shdr;
6821                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6822         }
6823
6824         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6825                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6826                         "2984 Failed to read allocated resources "
6827                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6828                         type,
6829                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
6830                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6831                 rc = -EIO;
6832                 goto err_exit;
6833         }
6834  err_exit:
6835         lpfc_sli4_mbox_cmd_free(phba, mbox);
6836         return rc;
6837 }
6838
6839 /**
6840  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
6841  * @phba: pointer to lpfc hba data structure.
6842  * @pring: Pointer to driver SLI ring object.
6843  * @sgl_list: linked link of sgl buffers to post
6844  * @cnt: number of linked list buffers
6845  *
6846  * This routine walks the list of buffers that have been allocated and
6847  * repost them to the port by using SGL block post. This is needed after a
6848  * pci_function_reset/warm_start or start. It attempts to construct blocks
6849  * of buffer sgls which contains contiguous xris and uses the non-embedded
6850  * SGL block post mailbox commands to post them to the port. For single
6851  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6852  * mailbox command for posting.
6853  *
6854  * Returns: 0 = success, non-zero failure.
6855  **/
6856 static int
6857 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6858                           struct list_head *sgl_list, int cnt)
6859 {
6860         struct lpfc_sglq *sglq_entry = NULL;
6861         struct lpfc_sglq *sglq_entry_next = NULL;
6862         struct lpfc_sglq *sglq_entry_first = NULL;
6863         int status, total_cnt;
6864         int post_cnt = 0, num_posted = 0, block_cnt = 0;
6865         int last_xritag = NO_XRI;
6866         LIST_HEAD(prep_sgl_list);
6867         LIST_HEAD(blck_sgl_list);
6868         LIST_HEAD(allc_sgl_list);
6869         LIST_HEAD(post_sgl_list);
6870         LIST_HEAD(free_sgl_list);
6871
6872         spin_lock_irq(&phba->hbalock);
6873         spin_lock(&phba->sli4_hba.sgl_list_lock);
6874         list_splice_init(sgl_list, &allc_sgl_list);
6875         spin_unlock(&phba->sli4_hba.sgl_list_lock);
6876         spin_unlock_irq(&phba->hbalock);
6877
6878         total_cnt = cnt;
6879         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6880                                  &allc_sgl_list, list) {
6881                 list_del_init(&sglq_entry->list);
6882                 block_cnt++;
6883                 if ((last_xritag != NO_XRI) &&
6884                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
6885                         /* a hole in xri block, form a sgl posting block */
6886                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
6887                         post_cnt = block_cnt - 1;
6888                         /* prepare list for next posting block */
6889                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6890                         block_cnt = 1;
6891                 } else {
6892                         /* prepare list for next posting block */
6893                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6894                         /* enough sgls for non-embed sgl mbox command */
6895                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6896                                 list_splice_init(&prep_sgl_list,
6897                                                  &blck_sgl_list);
6898                                 post_cnt = block_cnt;
6899                                 block_cnt = 0;
6900                         }
6901                 }
6902                 num_posted++;
6903
6904                 /* keep track of last sgl's xritag */
6905                 last_xritag = sglq_entry->sli4_xritag;
6906
6907                 /* end of repost sgl list condition for buffers */
6908                 if (num_posted == total_cnt) {
6909                         if (post_cnt == 0) {
6910                                 list_splice_init(&prep_sgl_list,
6911                                                  &blck_sgl_list);
6912                                 post_cnt = block_cnt;
6913                         } else if (block_cnt == 1) {
6914                                 status = lpfc_sli4_post_sgl(phba,
6915                                                 sglq_entry->phys, 0,
6916                                                 sglq_entry->sli4_xritag);
6917                                 if (!status) {
6918                                         /* successful, put sgl to posted list */
6919                                         list_add_tail(&sglq_entry->list,
6920                                                       &post_sgl_list);
6921                                 } else {
6922                                         /* Failure, put sgl to free list */
6923                                         lpfc_printf_log(phba, KERN_WARNING,
6924                                                 LOG_SLI,
6925                                                 "3159 Failed to post "
6926                                                 "sgl, xritag:x%x\n",
6927                                                 sglq_entry->sli4_xritag);
6928                                         list_add_tail(&sglq_entry->list,
6929                                                       &free_sgl_list);
6930                                         total_cnt--;
6931                                 }
6932                         }
6933                 }
6934
6935                 /* continue until a nembed page worth of sgls */
6936                 if (post_cnt == 0)
6937                         continue;
6938
6939                 /* post the buffer list sgls as a block */
6940                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6941                                                  post_cnt);
6942
6943                 if (!status) {
6944                         /* success, put sgl list to posted sgl list */
6945                         list_splice_init(&blck_sgl_list, &post_sgl_list);
6946                 } else {
6947                         /* Failure, put sgl list to free sgl list */
6948                         sglq_entry_first = list_first_entry(&blck_sgl_list,
6949                                                             struct lpfc_sglq,
6950                                                             list);
6951                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6952                                         "3160 Failed to post sgl-list, "
6953                                         "xritag:x%x-x%x\n",
6954                                         sglq_entry_first->sli4_xritag,
6955                                         (sglq_entry_first->sli4_xritag +
6956                                          post_cnt - 1));
6957                         list_splice_init(&blck_sgl_list, &free_sgl_list);
6958                         total_cnt -= post_cnt;
6959                 }
6960
6961                 /* don't reset xirtag due to hole in xri block */
6962                 if (block_cnt == 0)
6963                         last_xritag = NO_XRI;
6964
6965                 /* reset sgl post count for next round of posting */
6966                 post_cnt = 0;
6967         }
6968
6969         /* free the sgls failed to post */
6970         lpfc_free_sgl_list(phba, &free_sgl_list);
6971
6972         /* push sgls posted to the available list */
6973         if (!list_empty(&post_sgl_list)) {
6974                 spin_lock_irq(&phba->hbalock);
6975                 spin_lock(&phba->sli4_hba.sgl_list_lock);
6976                 list_splice_init(&post_sgl_list, sgl_list);
6977                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6978                 spin_unlock_irq(&phba->hbalock);
6979         } else {
6980                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6981                                 "3161 Failure to post sgl to port.\n");
6982                 return -EIO;
6983         }
6984
6985         /* return the number of XRIs actually posted */
6986         return total_cnt;
6987 }
6988
6989 void
6990 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6991 {
6992         uint32_t len;
6993
6994         len = sizeof(struct lpfc_mbx_set_host_data) -
6995                 sizeof(struct lpfc_sli4_cfg_mhdr);
6996         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6997                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6998                          LPFC_SLI4_MBX_EMBED);
6999
7000         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7001         mbox->u.mqe.un.set_host_data.param_len =
7002                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7003         snprintf(mbox->u.mqe.un.set_host_data.data,
7004                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7005                  "Linux %s v"LPFC_DRIVER_VERSION,
7006                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7007 }
7008
7009 int
7010 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7011                     struct lpfc_queue *drq, int count, int idx)
7012 {
7013         int rc, i;
7014         struct lpfc_rqe hrqe;
7015         struct lpfc_rqe drqe;
7016         struct lpfc_rqb *rqbp;
7017         unsigned long flags;
7018         struct rqb_dmabuf *rqb_buffer;
7019         LIST_HEAD(rqb_buf_list);
7020
7021         spin_lock_irqsave(&phba->hbalock, flags);
7022         rqbp = hrq->rqbp;
7023         for (i = 0; i < count; i++) {
7024                 /* IF RQ is already full, don't bother */
7025                 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7026                         break;
7027                 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7028                 if (!rqb_buffer)
7029                         break;
7030                 rqb_buffer->hrq = hrq;
7031                 rqb_buffer->drq = drq;
7032                 rqb_buffer->idx = idx;
7033                 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7034         }
7035         while (!list_empty(&rqb_buf_list)) {
7036                 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7037                                  hbuf.list);
7038
7039                 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7040                 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7041                 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7042                 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7043                 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7044                 if (rc < 0) {
7045                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7046                                         "6421 Cannot post to HRQ %d: %x %x %x "
7047                                         "DRQ %x %x\n",
7048                                         hrq->queue_id,
7049                                         hrq->host_index,
7050                                         hrq->hba_index,
7051                                         hrq->entry_count,
7052                                         drq->host_index,
7053                                         drq->hba_index);
7054                         rqbp->rqb_free_buffer(phba, rqb_buffer);
7055                 } else {
7056                         list_add_tail(&rqb_buffer->hbuf.list,
7057                                       &rqbp->rqb_buffer_list);
7058                         rqbp->buffer_count++;
7059                 }
7060         }
7061         spin_unlock_irqrestore(&phba->hbalock, flags);
7062         return 1;
7063 }
7064
7065 /**
7066  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
7067  * @phba: Pointer to HBA context object.
7068  *
7069  * This function is the main SLI4 device initialization PCI function. This
7070  * function is called by the HBA initialization code, HBA reset code and
7071  * HBA error attention handler code. Caller is not required to hold any
7072  * locks.
7073  **/
7074 int
7075 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7076 {
7077         int rc, i, cnt;
7078         LPFC_MBOXQ_t *mboxq;
7079         struct lpfc_mqe *mqe;
7080         uint8_t *vpd;
7081         uint32_t vpd_size;
7082         uint32_t ftr_rsp = 0;
7083         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7084         struct lpfc_vport *vport = phba->pport;
7085         struct lpfc_dmabuf *mp;
7086         struct lpfc_rqb *rqbp;
7087
7088         /* Perform a PCI function reset to start from clean */
7089         rc = lpfc_pci_function_reset(phba);
7090         if (unlikely(rc))
7091                 return -ENODEV;
7092
7093         /* Check the HBA Host Status Register for readyness */
7094         rc = lpfc_sli4_post_status_check(phba);
7095         if (unlikely(rc))
7096                 return -ENODEV;
7097         else {
7098                 spin_lock_irq(&phba->hbalock);
7099                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7100                 spin_unlock_irq(&phba->hbalock);
7101         }
7102
7103         /*
7104          * Allocate a single mailbox container for initializing the
7105          * port.
7106          */
7107         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7108         if (!mboxq)
7109                 return -ENOMEM;
7110
7111         /* Issue READ_REV to collect vpd and FW information. */
7112         vpd_size = SLI4_PAGE_SIZE;
7113         vpd = kzalloc(vpd_size, GFP_KERNEL);
7114         if (!vpd) {
7115                 rc = -ENOMEM;
7116                 goto out_free_mbox;
7117         }
7118
7119         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
7120         if (unlikely(rc)) {
7121                 kfree(vpd);
7122                 goto out_free_mbox;
7123         }
7124
7125         mqe = &mboxq->u.mqe;
7126         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
7127         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
7128                 phba->hba_flag |= HBA_FCOE_MODE;
7129                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7130         } else {
7131                 phba->hba_flag &= ~HBA_FCOE_MODE;
7132         }
7133
7134         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7135                 LPFC_DCBX_CEE_MODE)
7136                 phba->hba_flag |= HBA_FIP_SUPPORT;
7137         else
7138                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7139
7140         phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7141
7142         if (phba->sli_rev != LPFC_SLI_REV4) {
7143                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7144                         "0376 READ_REV Error. SLI Level %d "
7145                         "FCoE enabled %d\n",
7146                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
7147                 rc = -EIO;
7148                 kfree(vpd);
7149                 goto out_free_mbox;
7150         }
7151
7152         /*
7153          * Continue initialization with default values even if driver failed
7154          * to read FCoE param config regions, only read parameters if the
7155          * board is FCoE
7156          */
7157         if (phba->hba_flag & HBA_FCOE_MODE &&
7158             lpfc_sli4_read_fcoe_params(phba))
7159                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7160                         "2570 Failed to read FCoE parameters\n");
7161
7162         /*
7163          * Retrieve sli4 device physical port name, failure of doing it
7164          * is considered as non-fatal.
7165          */
7166         rc = lpfc_sli4_retrieve_pport_name(phba);
7167         if (!rc)
7168                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7169                                 "3080 Successful retrieving SLI4 device "
7170                                 "physical port name: %s.\n", phba->Port);
7171
7172         /*
7173          * Evaluate the read rev and vpd data. Populate the driver
7174          * state with the results. If this routine fails, the failure
7175          * is not fatal as the driver will use generic values.
7176          */
7177         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7178         if (unlikely(!rc)) {
7179                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7180                                 "0377 Error %d parsing vpd. "
7181                                 "Using defaults.\n", rc);
7182                 rc = 0;
7183         }
7184         kfree(vpd);
7185
7186         /* Save information as VPD data */
7187         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7188         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
7189
7190         /*
7191          * This is because first G7 ASIC doesn't support the standard
7192          * 0x5a NVME cmd descriptor type/subtype
7193          */
7194         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7195                         LPFC_SLI_INTF_IF_TYPE_6) &&
7196             (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7197             (phba->vpd.rev.smRev == 0) &&
7198             (phba->cfg_nvme_embed_cmd == 1))
7199                 phba->cfg_nvme_embed_cmd = 0;
7200
7201         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7202         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7203                                          &mqe->un.read_rev);
7204         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7205                                        &mqe->un.read_rev);
7206         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7207                                             &mqe->un.read_rev);
7208         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7209                                            &mqe->un.read_rev);
7210         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7211         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7212         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7213         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7214         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7215         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7216         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7217                         "(%d):0380 READ_REV Status x%x "
7218                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7219                         mboxq->vport ? mboxq->vport->vpi : 0,
7220                         bf_get(lpfc_mqe_status, mqe),
7221                         phba->vpd.rev.opFwName,
7222                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7223                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
7224
7225         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
7226         rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7227         if (phba->pport->cfg_lun_queue_depth > rc) {
7228                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7229                                 "3362 LUN queue depth changed from %d to %d\n",
7230                                 phba->pport->cfg_lun_queue_depth, rc);
7231                 phba->pport->cfg_lun_queue_depth = rc;
7232         }
7233
7234         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7235             LPFC_SLI_INTF_IF_TYPE_0) {
7236                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7237                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7238                 if (rc == MBX_SUCCESS) {
7239                         phba->hba_flag |= HBA_RECOVERABLE_UE;
7240                         /* Set 1Sec interval to detect UE */
7241                         phba->eratt_poll_interval = 1;
7242                         phba->sli4_hba.ue_to_sr = bf_get(
7243                                         lpfc_mbx_set_feature_UESR,
7244                                         &mboxq->u.mqe.un.set_feature);
7245                         phba->sli4_hba.ue_to_rp = bf_get(
7246                                         lpfc_mbx_set_feature_UERP,
7247                                         &mboxq->u.mqe.un.set_feature);
7248                 }
7249         }
7250
7251         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7252                 /* Enable MDS Diagnostics only if the SLI Port supports it */
7253                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7254                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7255                 if (rc != MBX_SUCCESS)
7256                         phba->mds_diags_support = 0;
7257         }
7258
7259         /*
7260          * Discover the port's supported feature set and match it against the
7261          * hosts requests.
7262          */
7263         lpfc_request_features(phba, mboxq);
7264         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7265         if (unlikely(rc)) {
7266                 rc = -EIO;
7267                 goto out_free_mbox;
7268         }
7269
7270         /*
7271          * The port must support FCP initiator mode as this is the
7272          * only mode running in the host.
7273          */
7274         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7275                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7276                                 "0378 No support for fcpi mode.\n");
7277                 ftr_rsp++;
7278         }
7279
7280         /* Performance Hints are ONLY for FCoE */
7281         if (phba->hba_flag & HBA_FCOE_MODE) {
7282                 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7283                         phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7284                 else
7285                         phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7286         }
7287
7288         /*
7289          * If the port cannot support the host's requested features
7290          * then turn off the global config parameters to disable the
7291          * feature in the driver.  This is not a fatal error.
7292          */
7293         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7294                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7295                         phba->cfg_enable_bg = 0;
7296                         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
7297                         ftr_rsp++;
7298                 }
7299         }
7300
7301         if (phba->max_vpi && phba->cfg_enable_npiv &&
7302             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7303                 ftr_rsp++;
7304
7305         if (ftr_rsp) {
7306                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7307                                 "0379 Feature Mismatch Data: x%08x %08x "
7308                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7309                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7310                                 phba->cfg_enable_npiv, phba->max_vpi);
7311                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7312                         phba->cfg_enable_bg = 0;
7313                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7314                         phba->cfg_enable_npiv = 0;
7315         }
7316
7317         /* These SLI3 features are assumed in SLI4 */
7318         spin_lock_irq(&phba->hbalock);
7319         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7320         spin_unlock_irq(&phba->hbalock);
7321
7322         /*
7323          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
7324          * calls depends on these resources to complete port setup.
7325          */
7326         rc = lpfc_sli4_alloc_resource_identifiers(phba);
7327         if (rc) {
7328                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7329                                 "2920 Failed to alloc Resource IDs "
7330                                 "rc = x%x\n", rc);
7331                 goto out_free_mbox;
7332         }
7333
7334         lpfc_set_host_data(phba, mboxq);
7335
7336         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7337         if (rc) {
7338                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7339                                 "2134 Failed to set host os driver version %x",
7340                                 rc);
7341         }
7342
7343         /* Read the port's service parameters. */
7344         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7345         if (rc) {
7346                 phba->link_state = LPFC_HBA_ERROR;
7347                 rc = -ENOMEM;
7348                 goto out_free_mbox;
7349         }
7350
7351         mboxq->vport = vport;
7352         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7353         mp = (struct lpfc_dmabuf *) mboxq->context1;
7354         if (rc == MBX_SUCCESS) {
7355                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7356                 rc = 0;
7357         }
7358
7359         /*
7360          * This memory was allocated by the lpfc_read_sparam routine. Release
7361          * it to the mbuf pool.
7362          */
7363         lpfc_mbuf_free(phba, mp->virt, mp->phys);
7364         kfree(mp);
7365         mboxq->context1 = NULL;
7366         if (unlikely(rc)) {
7367                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7368                                 "0382 READ_SPARAM command failed "
7369                                 "status %d, mbxStatus x%x\n",
7370                                 rc, bf_get(lpfc_mqe_status, mqe));
7371                 phba->link_state = LPFC_HBA_ERROR;
7372                 rc = -EIO;
7373                 goto out_free_mbox;
7374         }
7375
7376         lpfc_update_vport_wwn(vport);
7377
7378         /* Update the fc_host data structures with new wwn. */
7379         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7380         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7381
7382         /* Create all the SLI4 queues */
7383         rc = lpfc_sli4_queue_create(phba);
7384         if (rc) {
7385                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7386                                 "3089 Failed to allocate queues\n");
7387                 rc = -ENODEV;
7388                 goto out_free_mbox;
7389         }
7390         /* Set up all the queues to the device */
7391         rc = lpfc_sli4_queue_setup(phba);
7392         if (unlikely(rc)) {
7393                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7394                                 "0381 Error %d during queue setup.\n ", rc);
7395                 goto out_stop_timers;
7396         }
7397         /* Initialize the driver internal SLI layer lists. */
7398         lpfc_sli4_setup(phba);
7399         lpfc_sli4_queue_init(phba);
7400
7401         /* update host els xri-sgl sizes and mappings */
7402         rc = lpfc_sli4_els_sgl_update(phba);
7403         if (unlikely(rc)) {
7404                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7405                                 "1400 Failed to update xri-sgl size and "
7406                                 "mapping: %d\n", rc);
7407                 goto out_destroy_queue;
7408         }
7409
7410         /* register the els sgl pool to the port */
7411         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7412                                        phba->sli4_hba.els_xri_cnt);
7413         if (unlikely(rc < 0)) {
7414                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7415                                 "0582 Error %d during els sgl post "
7416                                 "operation\n", rc);
7417                 rc = -ENODEV;
7418                 goto out_destroy_queue;
7419         }
7420         phba->sli4_hba.els_xri_cnt = rc;
7421
7422         if (phba->nvmet_support) {
7423                 /* update host nvmet xri-sgl sizes and mappings */
7424                 rc = lpfc_sli4_nvmet_sgl_update(phba);
7425                 if (unlikely(rc)) {
7426                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7427                                         "6308 Failed to update nvmet-sgl size "
7428                                         "and mapping: %d\n", rc);
7429                         goto out_destroy_queue;
7430                 }
7431
7432                 /* register the nvmet sgl pool to the port */
7433                 rc = lpfc_sli4_repost_sgl_list(
7434                         phba,
7435                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
7436                         phba->sli4_hba.nvmet_xri_cnt);
7437                 if (unlikely(rc < 0)) {
7438                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7439                                         "3117 Error %d during nvmet "
7440                                         "sgl post\n", rc);
7441                         rc = -ENODEV;
7442                         goto out_destroy_queue;
7443                 }
7444                 phba->sli4_hba.nvmet_xri_cnt = rc;
7445
7446                 cnt = phba->cfg_iocb_cnt * 1024;
7447                 /* We need 1 iocbq for every SGL, for IO processing */
7448                 cnt += phba->sli4_hba.nvmet_xri_cnt;
7449         } else {
7450                 /* update host scsi xri-sgl sizes and mappings */
7451                 rc = lpfc_sli4_scsi_sgl_update(phba);
7452                 if (unlikely(rc)) {
7453                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7454                                         "6309 Failed to update scsi-sgl size "
7455                                         "and mapping: %d\n", rc);
7456                         goto out_destroy_queue;
7457                 }
7458
7459                 /* update host nvme xri-sgl sizes and mappings */
7460                 rc = lpfc_sli4_nvme_sgl_update(phba);
7461                 if (unlikely(rc)) {
7462                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7463                                         "6082 Failed to update nvme-sgl size "
7464                                         "and mapping: %d\n", rc);
7465                         goto out_destroy_queue;
7466                 }
7467
7468                 cnt = phba->cfg_iocb_cnt * 1024;
7469         }
7470
7471         if (!phba->sli.iocbq_lookup) {
7472                 /* Initialize and populate the iocb list per host */
7473                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7474                                 "2821 initialize iocb list %d total %d\n",
7475                                 phba->cfg_iocb_cnt, cnt);
7476                 rc = lpfc_init_iocb_list(phba, cnt);
7477                 if (rc) {
7478                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7479                                         "1413 Failed to init iocb list.\n");
7480                         goto out_destroy_queue;
7481                 }
7482         }
7483
7484         if (phba->nvmet_support)
7485                 lpfc_nvmet_create_targetport(phba);
7486
7487         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
7488                 /* Post initial buffers to all RQs created */
7489                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7490                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7491                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7492                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7493                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
7494                         rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
7495                         rqbp->buffer_count = 0;
7496
7497                         lpfc_post_rq_buffer(
7498                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7499                                 phba->sli4_hba.nvmet_mrq_data[i],
7500                                 phba->cfg_nvmet_mrq_post, i);
7501                 }
7502         }
7503
7504         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7505                 /* register the allocated scsi sgl pool to the port */
7506                 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7507                 if (unlikely(rc)) {
7508                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7509                                         "0383 Error %d during scsi sgl post "
7510                                         "operation\n", rc);
7511                         /* Some Scsi buffers were moved to abort scsi list */
7512                         /* A pci function reset will repost them */
7513                         rc = -ENODEV;
7514                         goto out_destroy_queue;
7515                 }
7516         }
7517
7518         if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7519             (phba->nvmet_support == 0)) {
7520
7521                 /* register the allocated nvme sgl pool to the port */
7522                 rc = lpfc_repost_nvme_sgl_list(phba);
7523                 if (unlikely(rc)) {
7524                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7525                                         "6116 Error %d during nvme sgl post "
7526                                         "operation\n", rc);
7527                         /* Some NVME buffers were moved to abort nvme list */
7528                         /* A pci function reset will repost them */
7529                         rc = -ENODEV;
7530                         goto out_destroy_queue;
7531                 }
7532         }
7533
7534         /* Post the rpi header region to the device. */
7535         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7536         if (unlikely(rc)) {
7537                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7538                                 "0393 Error %d during rpi post operation\n",
7539                                 rc);
7540                 rc = -ENODEV;
7541                 goto out_destroy_queue;
7542         }
7543         lpfc_sli4_node_prep(phba);
7544
7545         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
7546                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
7547                         /*
7548                          * The FC Port needs to register FCFI (index 0)
7549                          */
7550                         lpfc_reg_fcfi(phba, mboxq);
7551                         mboxq->vport = phba->pport;
7552                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7553                         if (rc != MBX_SUCCESS)
7554                                 goto out_unset_queue;
7555                         rc = 0;
7556                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7557                                                 &mboxq->u.mqe.un.reg_fcfi);
7558                 } else {
7559                         /* We are a NVME Target mode with MRQ > 1 */
7560
7561                         /* First register the FCFI */
7562                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7563                         mboxq->vport = phba->pport;
7564                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7565                         if (rc != MBX_SUCCESS)
7566                                 goto out_unset_queue;
7567                         rc = 0;
7568                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7569                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
7570
7571                         /* Next register the MRQs */
7572                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7573                         mboxq->vport = phba->pport;
7574                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7575                         if (rc != MBX_SUCCESS)
7576                                 goto out_unset_queue;
7577                         rc = 0;
7578                 }
7579                 /* Check if the port is configured to be disabled */
7580                 lpfc_sli_read_link_ste(phba);
7581         }
7582
7583         /* Arm the CQs and then EQs on device */
7584         lpfc_sli4_arm_cqeq_intr(phba);
7585
7586         /* Indicate device interrupt mode */
7587         phba->sli4_hba.intr_enable = 1;
7588
7589         /* Allow asynchronous mailbox command to go through */
7590         spin_lock_irq(&phba->hbalock);
7591         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7592         spin_unlock_irq(&phba->hbalock);
7593
7594         /* Post receive buffers to the device */
7595         lpfc_sli4_rb_setup(phba);
7596
7597         /* Reset HBA FCF states after HBA reset */
7598         phba->fcf.fcf_flag = 0;
7599         phba->fcf.current_rec.flag = 0;
7600
7601         /* Start the ELS watchdog timer */
7602         mod_timer(&vport->els_tmofunc,
7603                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7604
7605         /* Start heart beat timer */
7606         mod_timer(&phba->hb_tmofunc,
7607                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7608         phba->hb_outstanding = 0;
7609         phba->last_completion_time = jiffies;
7610
7611         /* Start error attention (ERATT) polling timer */
7612         mod_timer(&phba->eratt_poll,
7613                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7614
7615         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7616         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7617                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7618                 if (!rc) {
7619                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7620                                         "2829 This device supports "
7621                                         "Advanced Error Reporting (AER)\n");
7622                         spin_lock_irq(&phba->hbalock);
7623                         phba->hba_flag |= HBA_AER_ENABLED;
7624                         spin_unlock_irq(&phba->hbalock);
7625                 } else {
7626                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7627                                         "2830 This device does not support "
7628                                         "Advanced Error Reporting (AER)\n");
7629                         phba->cfg_aer_support = 0;
7630                 }
7631                 rc = 0;
7632         }
7633
7634         /*
7635          * The port is ready, set the host's link state to LINK_DOWN
7636          * in preparation for link interrupts.
7637          */
7638         spin_lock_irq(&phba->hbalock);
7639         phba->link_state = LPFC_LINK_DOWN;
7640
7641         /* Check if physical ports are trunked */
7642         if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7643                 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7644         if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7645                 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7646         if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7647                 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7648         if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7649                 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
7650         spin_unlock_irq(&phba->hbalock);
7651
7652         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7653             (phba->hba_flag & LINK_DISABLED)) {
7654                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7655                                 "3103 Adapter Link is disabled.\n");
7656                 lpfc_down_link(phba, mboxq);
7657                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7658                 if (rc != MBX_SUCCESS) {
7659                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7660                                         "3104 Adapter failed to issue "
7661                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7662                         goto out_unset_queue;
7663                 }
7664         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7665                 /* don't perform init_link on SLI4 FC port loopback test */
7666                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7667                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7668                         if (rc)
7669                                 goto out_unset_queue;
7670                 }
7671         }
7672         mempool_free(mboxq, phba->mbox_mem_pool);
7673         return rc;
7674 out_unset_queue:
7675         /* Unset all the queues set up in this routine when error out */
7676         lpfc_sli4_queue_unset(phba);
7677 out_destroy_queue:
7678         lpfc_free_iocb_list(phba);
7679         lpfc_sli4_queue_destroy(phba);
7680 out_stop_timers:
7681         lpfc_stop_hba_timers(phba);
7682 out_free_mbox:
7683         mempool_free(mboxq, phba->mbox_mem_pool);
7684         return rc;
7685 }
7686
7687 /**
7688  * lpfc_mbox_timeout - Timeout call back function for mbox timer
7689  * @ptr: context object - pointer to hba structure.
7690  *
7691  * This is the callback function for mailbox timer. The mailbox
7692  * timer is armed when a new mailbox command is issued and the timer
7693  * is deleted when the mailbox complete. The function is called by
7694  * the kernel timer code when a mailbox does not complete within
7695  * expected time. This function wakes up the worker thread to
7696  * process the mailbox timeout and returns. All the processing is
7697  * done by the worker thread function lpfc_mbox_timeout_handler.
7698  **/
7699 void
7700 lpfc_mbox_timeout(struct timer_list *t)
7701 {
7702         struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
7703         unsigned long iflag;
7704         uint32_t tmo_posted;
7705
7706         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7707         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7708         if (!tmo_posted)
7709                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7710         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7711
7712         if (!tmo_posted)
7713                 lpfc_worker_wake_up(phba);
7714         return;
7715 }
7716
7717 /**
7718  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7719  *                                    are pending
7720  * @phba: Pointer to HBA context object.
7721  *
7722  * This function checks if any mailbox completions are present on the mailbox
7723  * completion queue.
7724  **/
7725 static bool
7726 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7727 {
7728
7729         uint32_t idx;
7730         struct lpfc_queue *mcq;
7731         struct lpfc_mcqe *mcqe;
7732         bool pending_completions = false;
7733         uint8_t qe_valid;
7734
7735         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7736                 return false;
7737
7738         /* Check for completions on mailbox completion queue */
7739
7740         mcq = phba->sli4_hba.mbx_cq;
7741         idx = mcq->hba_index;
7742         qe_valid = mcq->qe_valid;
7743         while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
7744                 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7745                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7746                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7747                         pending_completions = true;
7748                         break;
7749                 }
7750                 idx = (idx + 1) % mcq->entry_count;
7751                 if (mcq->hba_index == idx)
7752                         break;
7753
7754                 /* if the index wrapped around, toggle the valid bit */
7755                 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7756                         qe_valid = (qe_valid) ? 0 : 1;
7757         }
7758         return pending_completions;
7759
7760 }
7761
7762 /**
7763  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7764  *                                            that were missed.
7765  * @phba: Pointer to HBA context object.
7766  *
7767  * For sli4, it is possible to miss an interrupt. As such mbox completions
7768  * maybe missed causing erroneous mailbox timeouts to occur. This function
7769  * checks to see if mbox completions are on the mailbox completion queue
7770  * and will process all the completions associated with the eq for the
7771  * mailbox completion queue.
7772  **/
7773 bool
7774 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7775 {
7776         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
7777         uint32_t eqidx;
7778         struct lpfc_queue *fpeq = NULL;
7779         struct lpfc_eqe *eqe;
7780         bool mbox_pending;
7781
7782         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7783                 return false;
7784
7785         /* Find the eq associated with the mcq */
7786
7787         if (sli4_hba->hba_eq)
7788                 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
7789                         if (sli4_hba->hba_eq[eqidx]->queue_id ==
7790                             sli4_hba->mbx_cq->assoc_qid) {
7791                                 fpeq = sli4_hba->hba_eq[eqidx];
7792                                 break;
7793                         }
7794         if (!fpeq)
7795                 return false;
7796
7797         /* Turn off interrupts from this EQ */
7798
7799         sli4_hba->sli4_eq_clr_intr(fpeq);
7800
7801         /* Check to see if a mbox completion is pending */
7802
7803         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7804
7805         /*
7806          * If a mbox completion is pending, process all the events on EQ
7807          * associated with the mbox completion queue (this could include
7808          * mailbox commands, async events, els commands, receive queue data
7809          * and fcp commands)
7810          */
7811
7812         if (mbox_pending)
7813                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7814                         lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7815                         fpeq->EQ_processed++;
7816                 }
7817
7818         /* Always clear and re-arm the EQ */
7819
7820         sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7821
7822         return mbox_pending;
7823
7824 }
7825
7826 /**
7827  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7828  * @phba: Pointer to HBA context object.
7829  *
7830  * This function is called from worker thread when a mailbox command times out.
7831  * The caller is not required to hold any locks. This function will reset the
7832  * HBA and recover all the pending commands.
7833  **/
7834 void
7835 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7836 {
7837         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7838         MAILBOX_t *mb = NULL;
7839
7840         struct lpfc_sli *psli = &phba->sli;
7841
7842         /* If the mailbox completed, process the completion and return */
7843         if (lpfc_sli4_process_missed_mbox_completions(phba))
7844                 return;
7845
7846         if (pmbox != NULL)
7847                 mb = &pmbox->u.mb;
7848         /* Check the pmbox pointer first.  There is a race condition
7849          * between the mbox timeout handler getting executed in the
7850          * worklist and the mailbox actually completing. When this
7851          * race condition occurs, the mbox_active will be NULL.
7852          */
7853         spin_lock_irq(&phba->hbalock);
7854         if (pmbox == NULL) {
7855                 lpfc_printf_log(phba, KERN_WARNING,
7856                                 LOG_MBOX | LOG_SLI,
7857                                 "0353 Active Mailbox cleared - mailbox timeout "
7858                                 "exiting\n");
7859                 spin_unlock_irq(&phba->hbalock);
7860                 return;
7861         }
7862
7863         /* Mbox cmd <mbxCommand> timeout */
7864         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7865                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7866                         mb->mbxCommand,
7867                         phba->pport->port_state,
7868                         phba->sli.sli_flag,
7869                         phba->sli.mbox_active);
7870         spin_unlock_irq(&phba->hbalock);
7871
7872         /* Setting state unknown so lpfc_sli_abort_iocb_ring
7873          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7874          * it to fail all outstanding SCSI IO.
7875          */
7876         spin_lock_irq(&phba->pport->work_port_lock);
7877         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7878         spin_unlock_irq(&phba->pport->work_port_lock);
7879         spin_lock_irq(&phba->hbalock);
7880         phba->link_state = LPFC_LINK_UNKNOWN;
7881         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7882         spin_unlock_irq(&phba->hbalock);
7883
7884         lpfc_sli_abort_fcp_rings(phba);
7885
7886         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7887                         "0345 Resetting board due to mailbox timeout\n");
7888
7889         /* Reset the HBA device */
7890         lpfc_reset_hba(phba);
7891 }
7892
7893 /**
7894  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7895  * @phba: Pointer to HBA context object.
7896  * @pmbox: Pointer to mailbox object.
7897  * @flag: Flag indicating how the mailbox need to be processed.
7898  *
7899  * This function is called by discovery code and HBA management code
7900  * to submit a mailbox command to firmware with SLI-3 interface spec. This
7901  * function gets the hbalock to protect the data structures.
7902  * The mailbox command can be submitted in polling mode, in which case
7903  * this function will wait in a polling loop for the completion of the
7904  * mailbox.
7905  * If the mailbox is submitted in no_wait mode (not polling) the
7906  * function will submit the command and returns immediately without waiting
7907  * for the mailbox completion. The no_wait is supported only when HBA
7908  * is in SLI2/SLI3 mode - interrupts are enabled.
7909  * The SLI interface allows only one mailbox pending at a time. If the
7910  * mailbox is issued in polling mode and there is already a mailbox
7911  * pending, then the function will return an error. If the mailbox is issued
7912  * in NO_WAIT mode and there is a mailbox pending already, the function
7913  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7914  * The sli layer owns the mailbox object until the completion of mailbox
7915  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7916  * return codes the caller owns the mailbox command after the return of
7917  * the function.
7918  **/
7919 static int
7920 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7921                        uint32_t flag)
7922 {
7923         MAILBOX_t *mbx;
7924         struct lpfc_sli *psli = &phba->sli;
7925         uint32_t status, evtctr;
7926         uint32_t ha_copy, hc_copy;
7927         int i;
7928         unsigned long timeout;
7929         unsigned long drvr_flag = 0;
7930         uint32_t word0, ldata;
7931         void __iomem *to_slim;
7932         int processing_queue = 0;
7933
7934         spin_lock_irqsave(&phba->hbalock, drvr_flag);
7935         if (!pmbox) {
7936                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7937                 /* processing mbox queue from intr_handler */
7938                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7939                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7940                         return MBX_SUCCESS;
7941                 }
7942                 processing_queue = 1;
7943                 pmbox = lpfc_mbox_get(phba);
7944                 if (!pmbox) {
7945                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7946                         return MBX_SUCCESS;
7947                 }
7948         }
7949
7950         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7951                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7952                 if(!pmbox->vport) {
7953                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7954                         lpfc_printf_log(phba, KERN_ERR,
7955                                         LOG_MBOX | LOG_VPORT,
7956                                         "1806 Mbox x%x failed. No vport\n",
7957                                         pmbox->u.mb.mbxCommand);
7958                         dump_stack();
7959                         goto out_not_finished;
7960                 }
7961         }
7962
7963         /* If the PCI channel is in offline state, do not post mbox. */
7964         if (unlikely(pci_channel_offline(phba->pcidev))) {
7965                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7966                 goto out_not_finished;
7967         }
7968
7969         /* If HBA has a deferred error attention, fail the iocb. */
7970         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7971                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7972                 goto out_not_finished;
7973         }
7974
7975         psli = &phba->sli;
7976
7977         mbx = &pmbox->u.mb;
7978         status = MBX_SUCCESS;
7979
7980         if (phba->link_state == LPFC_HBA_ERROR) {
7981                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7982
7983                 /* Mbox command <mbxCommand> cannot issue */
7984                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7985                                 "(%d):0311 Mailbox command x%x cannot "
7986                                 "issue Data: x%x x%x\n",
7987                                 pmbox->vport ? pmbox->vport->vpi : 0,
7988                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7989                 goto out_not_finished;
7990         }
7991
7992         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7993                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7994                         !(hc_copy & HC_MBINT_ENA)) {
7995                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7996                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7997                                 "(%d):2528 Mailbox command x%x cannot "
7998                                 "issue Data: x%x x%x\n",
7999                                 pmbox->vport ? pmbox->vport->vpi : 0,
8000                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8001                         goto out_not_finished;
8002                 }
8003         }
8004
8005         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8006                 /* Polling for a mbox command when another one is already active
8007                  * is not allowed in SLI. Also, the driver must have established
8008                  * SLI2 mode to queue and process multiple mbox commands.
8009                  */
8010
8011                 if (flag & MBX_POLL) {
8012                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8013
8014                         /* Mbox command <mbxCommand> cannot issue */
8015                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8016                                         "(%d):2529 Mailbox command x%x "
8017                                         "cannot issue Data: x%x x%x\n",
8018                                         pmbox->vport ? pmbox->vport->vpi : 0,
8019                                         pmbox->u.mb.mbxCommand,
8020                                         psli->sli_flag, flag);
8021                         goto out_not_finished;
8022                 }
8023
8024                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
8025                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8026                         /* Mbox command <mbxCommand> cannot issue */
8027                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8028                                         "(%d):2530 Mailbox command x%x "
8029                                         "cannot issue Data: x%x x%x\n",
8030                                         pmbox->vport ? pmbox->vport->vpi : 0,
8031                                         pmbox->u.mb.mbxCommand,
8032                                         psli->sli_flag, flag);
8033                         goto out_not_finished;
8034                 }
8035
8036                 /* Another mailbox command is still being processed, queue this
8037                  * command to be processed later.
8038                  */
8039                 lpfc_mbox_put(phba, pmbox);
8040
8041                 /* Mbox cmd issue - BUSY */
8042                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8043                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
8044                                 "x%x x%x x%x x%x\n",
8045                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
8046                                 mbx->mbxCommand,
8047                                 phba->pport ? phba->pport->port_state : 0xff,
8048                                 psli->sli_flag, flag);
8049
8050                 psli->slistat.mbox_busy++;
8051                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8052
8053                 if (pmbox->vport) {
8054                         lpfc_debugfs_disc_trc(pmbox->vport,
8055                                 LPFC_DISC_TRC_MBOX_VPORT,
8056                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
8057                                 (uint32_t)mbx->mbxCommand,
8058                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8059                 }
8060                 else {
8061                         lpfc_debugfs_disc_trc(phba->pport,
8062                                 LPFC_DISC_TRC_MBOX,
8063                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
8064                                 (uint32_t)mbx->mbxCommand,
8065                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8066                 }
8067
8068                 return MBX_BUSY;
8069         }
8070
8071         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8072
8073         /* If we are not polling, we MUST be in SLI2 mode */
8074         if (flag != MBX_POLL) {
8075                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
8076                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
8077                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8078                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8079                         /* Mbox command <mbxCommand> cannot issue */
8080                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8081                                         "(%d):2531 Mailbox command x%x "
8082                                         "cannot issue Data: x%x x%x\n",
8083                                         pmbox->vport ? pmbox->vport->vpi : 0,
8084                                         pmbox->u.mb.mbxCommand,
8085                                         psli->sli_flag, flag);
8086                         goto out_not_finished;
8087                 }
8088                 /* timeout active mbox command */
8089                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8090                                            1000);
8091                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
8092         }
8093
8094         /* Mailbox cmd <cmd> issue */
8095         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8096                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
8097                         "x%x\n",
8098                         pmbox->vport ? pmbox->vport->vpi : 0,
8099                         mbx->mbxCommand,
8100                         phba->pport ? phba->pport->port_state : 0xff,
8101                         psli->sli_flag, flag);
8102
8103         if (mbx->mbxCommand != MBX_HEARTBEAT) {
8104                 if (pmbox->vport) {
8105                         lpfc_debugfs_disc_trc(pmbox->vport,
8106                                 LPFC_DISC_TRC_MBOX_VPORT,
8107                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8108                                 (uint32_t)mbx->mbxCommand,
8109                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8110                 }
8111                 else {
8112                         lpfc_debugfs_disc_trc(phba->pport,
8113                                 LPFC_DISC_TRC_MBOX,
8114                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
8115                                 (uint32_t)mbx->mbxCommand,
8116                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8117                 }
8118         }
8119
8120         psli->slistat.mbox_cmd++;
8121         evtctr = psli->slistat.mbox_event;
8122
8123         /* next set own bit for the adapter and copy over command word */
8124         mbx->mbxOwner = OWN_CHIP;
8125
8126         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8127                 /* Populate mbox extension offset word. */
8128                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
8129                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8130                                 = (uint8_t *)phba->mbox_ext
8131                                   - (uint8_t *)phba->mbox;
8132                 }
8133
8134                 /* Copy the mailbox extension data */
8135                 if (pmbox->in_ext_byte_len && pmbox->context2) {
8136                         lpfc_sli_pcimem_bcopy(pmbox->context2,
8137                                 (uint8_t *)phba->mbox_ext,
8138                                 pmbox->in_ext_byte_len);
8139                 }
8140                 /* Copy command data to host SLIM area */
8141                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
8142         } else {
8143                 /* Populate mbox extension offset word. */
8144                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
8145                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8146                                 = MAILBOX_HBA_EXT_OFFSET;
8147
8148                 /* Copy the mailbox extension data */
8149                 if (pmbox->in_ext_byte_len && pmbox->context2)
8150                         lpfc_memcpy_to_slim(phba->MBslimaddr +
8151                                 MAILBOX_HBA_EXT_OFFSET,
8152                                 pmbox->context2, pmbox->in_ext_byte_len);
8153
8154                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8155                         /* copy command data into host mbox for cmpl */
8156                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8157                                               MAILBOX_CMD_SIZE);
8158
8159                 /* First copy mbox command data to HBA SLIM, skip past first
8160                    word */
8161                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
8162                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
8163                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
8164
8165                 /* Next copy over first word, with mbxOwner set */
8166                 ldata = *((uint32_t *)mbx);
8167                 to_slim = phba->MBslimaddr;
8168                 writel(ldata, to_slim);
8169                 readl(to_slim); /* flush */
8170
8171                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8172                         /* switch over to host mailbox */
8173                         psli->sli_flag |= LPFC_SLI_ACTIVE;
8174         }
8175
8176         wmb();
8177
8178         switch (flag) {
8179         case MBX_NOWAIT:
8180                 /* Set up reference to mailbox command */
8181                 psli->mbox_active = pmbox;
8182                 /* Interrupt board to do it */
8183                 writel(CA_MBATT, phba->CAregaddr);
8184                 readl(phba->CAregaddr); /* flush */
8185                 /* Don't wait for it to finish, just return */
8186                 break;
8187
8188         case MBX_POLL:
8189                 /* Set up null reference to mailbox command */
8190                 psli->mbox_active = NULL;
8191                 /* Interrupt board to do it */
8192                 writel(CA_MBATT, phba->CAregaddr);
8193                 readl(phba->CAregaddr); /* flush */
8194
8195                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8196                         /* First read mbox status word */
8197                         word0 = *((uint32_t *)phba->mbox);
8198                         word0 = le32_to_cpu(word0);
8199                 } else {
8200                         /* First read mbox status word */
8201                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
8202                                 spin_unlock_irqrestore(&phba->hbalock,
8203                                                        drvr_flag);
8204                                 goto out_not_finished;
8205                         }
8206                 }
8207
8208                 /* Read the HBA Host Attention Register */
8209                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8210                         spin_unlock_irqrestore(&phba->hbalock,
8211                                                        drvr_flag);
8212                         goto out_not_finished;
8213                 }
8214                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8215                                                         1000) + jiffies;
8216                 i = 0;
8217                 /* Wait for command to complete */
8218                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8219                        (!(ha_copy & HA_MBATT) &&
8220                         (phba->link_state > LPFC_WARM_START))) {
8221                         if (time_after(jiffies, timeout)) {
8222                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8223                                 spin_unlock_irqrestore(&phba->hbalock,
8224                                                        drvr_flag);
8225                                 goto out_not_finished;
8226                         }
8227
8228                         /* Check if we took a mbox interrupt while we were
8229                            polling */
8230                         if (((word0 & OWN_CHIP) != OWN_CHIP)
8231                             && (evtctr != psli->slistat.mbox_event))
8232                                 break;
8233
8234                         if (i++ > 10) {
8235                                 spin_unlock_irqrestore(&phba->hbalock,
8236                                                        drvr_flag);
8237                                 msleep(1);
8238                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8239                         }
8240
8241                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8242                                 /* First copy command data */
8243                                 word0 = *((uint32_t *)phba->mbox);
8244                                 word0 = le32_to_cpu(word0);
8245                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
8246                                         MAILBOX_t *slimmb;
8247                                         uint32_t slimword0;
8248                                         /* Check real SLIM for any errors */
8249                                         slimword0 = readl(phba->MBslimaddr);
8250                                         slimmb = (MAILBOX_t *) & slimword0;
8251                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8252                                             && slimmb->mbxStatus) {
8253                                                 psli->sli_flag &=
8254                                                     ~LPFC_SLI_ACTIVE;
8255                                                 word0 = slimword0;
8256                                         }
8257                                 }
8258                         } else {
8259                                 /* First copy command data */
8260                                 word0 = readl(phba->MBslimaddr);
8261                         }
8262                         /* Read the HBA Host Attention Register */
8263                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8264                                 spin_unlock_irqrestore(&phba->hbalock,
8265                                                        drvr_flag);
8266                                 goto out_not_finished;
8267                         }
8268                 }
8269
8270                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8271                         /* copy results back to user */
8272                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8273                                                 MAILBOX_CMD_SIZE);
8274                         /* Copy the mailbox extension data */
8275                         if (pmbox->out_ext_byte_len && pmbox->context2) {
8276                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
8277                                                       pmbox->context2,
8278                                                       pmbox->out_ext_byte_len);
8279                         }
8280                 } else {
8281                         /* First copy command data */
8282                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
8283                                                 MAILBOX_CMD_SIZE);
8284                         /* Copy the mailbox extension data */
8285                         if (pmbox->out_ext_byte_len && pmbox->context2) {
8286                                 lpfc_memcpy_from_slim(pmbox->context2,
8287                                         phba->MBslimaddr +
8288                                         MAILBOX_HBA_EXT_OFFSET,
8289                                         pmbox->out_ext_byte_len);
8290                         }
8291                 }
8292
8293                 writel(HA_MBATT, phba->HAregaddr);
8294                 readl(phba->HAregaddr); /* flush */
8295
8296                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8297                 status = mbx->mbxStatus;
8298         }
8299
8300         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8301         return status;
8302
8303 out_not_finished:
8304         if (processing_queue) {
8305                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
8306                 lpfc_mbox_cmpl_put(phba, pmbox);
8307         }
8308         return MBX_NOT_FINISHED;
8309 }
8310
8311 /**
8312  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8313  * @phba: Pointer to HBA context object.
8314  *
8315  * The function blocks the posting of SLI4 asynchronous mailbox commands from
8316  * the driver internal pending mailbox queue. It will then try to wait out the
8317  * possible outstanding mailbox command before return.
8318  *
8319  * Returns:
8320  *      0 - the outstanding mailbox command completed; otherwise, the wait for
8321  *      the outstanding mailbox command timed out.
8322  **/
8323 static int
8324 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8325 {
8326         struct lpfc_sli *psli = &phba->sli;
8327         int rc = 0;
8328         unsigned long timeout = 0;
8329
8330         /* Mark the asynchronous mailbox command posting as blocked */
8331         spin_lock_irq(&phba->hbalock);
8332         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8333         /* Determine how long we might wait for the active mailbox
8334          * command to be gracefully completed by firmware.
8335          */
8336         if (phba->sli.mbox_active)
8337                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8338                                                 phba->sli.mbox_active) *
8339                                                 1000) + jiffies;
8340         spin_unlock_irq(&phba->hbalock);
8341
8342         /* Make sure the mailbox is really active */
8343         if (timeout)
8344                 lpfc_sli4_process_missed_mbox_completions(phba);
8345
8346         /* Wait for the outstnading mailbox command to complete */
8347         while (phba->sli.mbox_active) {
8348                 /* Check active mailbox complete status every 2ms */
8349                 msleep(2);
8350                 if (time_after(jiffies, timeout)) {
8351                         /* Timeout, marked the outstanding cmd not complete */
8352                         rc = 1;
8353                         break;
8354                 }
8355         }
8356
8357         /* Can not cleanly block async mailbox command, fails it */
8358         if (rc) {
8359                 spin_lock_irq(&phba->hbalock);
8360                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8361                 spin_unlock_irq(&phba->hbalock);
8362         }
8363         return rc;
8364 }
8365
8366 /**
8367  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8368  * @phba: Pointer to HBA context object.
8369  *
8370  * The function unblocks and resume posting of SLI4 asynchronous mailbox
8371  * commands from the driver internal pending mailbox queue. It makes sure
8372  * that there is no outstanding mailbox command before resuming posting
8373  * asynchronous mailbox commands. If, for any reason, there is outstanding
8374  * mailbox command, it will try to wait it out before resuming asynchronous
8375  * mailbox command posting.
8376  **/
8377 static void
8378 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8379 {
8380         struct lpfc_sli *psli = &phba->sli;
8381
8382         spin_lock_irq(&phba->hbalock);
8383         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8384                 /* Asynchronous mailbox posting is not blocked, do nothing */
8385                 spin_unlock_irq(&phba->hbalock);
8386                 return;
8387         }
8388
8389         /* Outstanding synchronous mailbox command is guaranteed to be done,
8390          * successful or timeout, after timing-out the outstanding mailbox
8391          * command shall always be removed, so just unblock posting async
8392          * mailbox command and resume
8393          */
8394         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8395         spin_unlock_irq(&phba->hbalock);
8396
8397         /* wake up worker thread to post asynchronlous mailbox command */
8398         lpfc_worker_wake_up(phba);
8399 }
8400
8401 /**
8402  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8403  * @phba: Pointer to HBA context object.
8404  * @mboxq: Pointer to mailbox object.
8405  *
8406  * The function waits for the bootstrap mailbox register ready bit from
8407  * port for twice the regular mailbox command timeout value.
8408  *
8409  *      0 - no timeout on waiting for bootstrap mailbox register ready.
8410  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8411  **/
8412 static int
8413 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8414 {
8415         uint32_t db_ready;
8416         unsigned long timeout;
8417         struct lpfc_register bmbx_reg;
8418
8419         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8420                                    * 1000) + jiffies;
8421
8422         do {
8423                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8424                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8425                 if (!db_ready)
8426                         msleep(2);
8427
8428                 if (time_after(jiffies, timeout))
8429                         return MBXERR_ERROR;
8430         } while (!db_ready);
8431
8432         return 0;
8433 }
8434
8435 /**
8436  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8437  * @phba: Pointer to HBA context object.
8438  * @mboxq: Pointer to mailbox object.
8439  *
8440  * The function posts a mailbox to the port.  The mailbox is expected
8441  * to be comletely filled in and ready for the port to operate on it.
8442  * This routine executes a synchronous completion operation on the
8443  * mailbox by polling for its completion.
8444  *
8445  * The caller must not be holding any locks when calling this routine.
8446  *
8447  * Returns:
8448  *      MBX_SUCCESS - mailbox posted successfully
8449  *      Any of the MBX error values.
8450  **/
8451 static int
8452 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8453 {
8454         int rc = MBX_SUCCESS;
8455         unsigned long iflag;
8456         uint32_t mcqe_status;
8457         uint32_t mbx_cmnd;
8458         struct lpfc_sli *psli = &phba->sli;
8459         struct lpfc_mqe *mb = &mboxq->u.mqe;
8460         struct lpfc_bmbx_create *mbox_rgn;
8461         struct dma_address *dma_address;
8462
8463         /*
8464          * Only one mailbox can be active to the bootstrap mailbox region
8465          * at a time and there is no queueing provided.
8466          */
8467         spin_lock_irqsave(&phba->hbalock, iflag);
8468         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8469                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8470                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8471                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
8472                                 "cannot issue Data: x%x x%x\n",
8473                                 mboxq->vport ? mboxq->vport->vpi : 0,
8474                                 mboxq->u.mb.mbxCommand,
8475                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8476                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8477                                 psli->sli_flag, MBX_POLL);
8478                 return MBXERR_ERROR;
8479         }
8480         /* The server grabs the token and owns it until release */
8481         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8482         phba->sli.mbox_active = mboxq;
8483         spin_unlock_irqrestore(&phba->hbalock, iflag);
8484
8485         /* wait for bootstrap mbox register for readyness */
8486         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8487         if (rc)
8488                 goto exit;
8489
8490         /*
8491          * Initialize the bootstrap memory region to avoid stale data areas
8492          * in the mailbox post.  Then copy the caller's mailbox contents to
8493          * the bmbx mailbox region.
8494          */
8495         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8496         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8497         lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8498                                sizeof(struct lpfc_mqe));
8499
8500         /* Post the high mailbox dma address to the port and wait for ready. */
8501         dma_address = &phba->sli4_hba.bmbx.dma_address;
8502         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8503
8504         /* wait for bootstrap mbox register for hi-address write done */
8505         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8506         if (rc)
8507                 goto exit;
8508
8509         /* Post the low mailbox dma address to the port. */
8510         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
8511
8512         /* wait for bootstrap mbox register for low address write done */
8513         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8514         if (rc)
8515                 goto exit;
8516
8517         /*
8518          * Read the CQ to ensure the mailbox has completed.
8519          * If so, update the mailbox status so that the upper layers
8520          * can complete the request normally.
8521          */
8522         lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8523                                sizeof(struct lpfc_mqe));
8524         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8525         lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8526                                sizeof(struct lpfc_mcqe));
8527         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
8528         /*
8529          * When the CQE status indicates a failure and the mailbox status
8530          * indicates success then copy the CQE status into the mailbox status
8531          * (and prefix it with x4000).
8532          */
8533         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
8534                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8535                         bf_set(lpfc_mqe_status, mb,
8536                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
8537                 rc = MBXERR_ERROR;
8538         } else
8539                 lpfc_sli4_swap_str(phba, mboxq);
8540
8541         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8542                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
8543                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8544                         " x%x x%x CQ: x%x x%x x%x x%x\n",
8545                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8546                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8547                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8548                         bf_get(lpfc_mqe_status, mb),
8549                         mb->un.mb_words[0], mb->un.mb_words[1],
8550                         mb->un.mb_words[2], mb->un.mb_words[3],
8551                         mb->un.mb_words[4], mb->un.mb_words[5],
8552                         mb->un.mb_words[6], mb->un.mb_words[7],
8553                         mb->un.mb_words[8], mb->un.mb_words[9],
8554                         mb->un.mb_words[10], mb->un.mb_words[11],
8555                         mb->un.mb_words[12], mboxq->mcqe.word0,
8556                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
8557                         mboxq->mcqe.trailer);
8558 exit:
8559         /* We are holding the token, no needed for lock when release */
8560         spin_lock_irqsave(&phba->hbalock, iflag);
8561         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8562         phba->sli.mbox_active = NULL;
8563         spin_unlock_irqrestore(&phba->hbalock, iflag);
8564         return rc;
8565 }
8566
8567 /**
8568  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8569  * @phba: Pointer to HBA context object.
8570  * @pmbox: Pointer to mailbox object.
8571  * @flag: Flag indicating how the mailbox need to be processed.
8572  *
8573  * This function is called by discovery code and HBA management code to submit
8574  * a mailbox command to firmware with SLI-4 interface spec.
8575  *
8576  * Return codes the caller owns the mailbox command after the return of the
8577  * function.
8578  **/
8579 static int
8580 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8581                        uint32_t flag)
8582 {
8583         struct lpfc_sli *psli = &phba->sli;
8584         unsigned long iflags;
8585         int rc;
8586
8587         /* dump from issue mailbox command if setup */
8588         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8589
8590         rc = lpfc_mbox_dev_check(phba);
8591         if (unlikely(rc)) {
8592                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8593                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8594                                 "cannot issue Data: x%x x%x\n",
8595                                 mboxq->vport ? mboxq->vport->vpi : 0,
8596                                 mboxq->u.mb.mbxCommand,
8597                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8598                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8599                                 psli->sli_flag, flag);
8600                 goto out_not_finished;
8601         }
8602
8603         /* Detect polling mode and jump to a handler */
8604         if (!phba->sli4_hba.intr_enable) {
8605                 if (flag == MBX_POLL)
8606                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8607                 else
8608                         rc = -EIO;
8609                 if (rc != MBX_SUCCESS)
8610                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8611                                         "(%d):2541 Mailbox command x%x "
8612                                         "(x%x/x%x) failure: "
8613                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8614                                         "Data: x%x x%x\n,",
8615                                         mboxq->vport ? mboxq->vport->vpi : 0,
8616                                         mboxq->u.mb.mbxCommand,
8617                                         lpfc_sli_config_mbox_subsys_get(phba,
8618                                                                         mboxq),
8619                                         lpfc_sli_config_mbox_opcode_get(phba,
8620                                                                         mboxq),
8621                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8622                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8623                                         bf_get(lpfc_mcqe_ext_status,
8624                                                &mboxq->mcqe),
8625                                         psli->sli_flag, flag);
8626                 return rc;
8627         } else if (flag == MBX_POLL) {
8628                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8629                                 "(%d):2542 Try to issue mailbox command "
8630                                 "x%x (x%x/x%x) synchronously ahead of async "
8631                                 "mailbox command queue: x%x x%x\n",
8632                                 mboxq->vport ? mboxq->vport->vpi : 0,
8633                                 mboxq->u.mb.mbxCommand,
8634                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8635                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8636                                 psli->sli_flag, flag);
8637                 /* Try to block the asynchronous mailbox posting */
8638                 rc = lpfc_sli4_async_mbox_block(phba);
8639                 if (!rc) {
8640                         /* Successfully blocked, now issue sync mbox cmd */
8641                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8642                         if (rc != MBX_SUCCESS)
8643                                 lpfc_printf_log(phba, KERN_WARNING,
8644                                         LOG_MBOX | LOG_SLI,
8645                                         "(%d):2597 Sync Mailbox command "
8646                                         "x%x (x%x/x%x) failure: "
8647                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8648                                         "Data: x%x x%x\n,",
8649                                         mboxq->vport ? mboxq->vport->vpi : 0,
8650                                         mboxq->u.mb.mbxCommand,
8651                                         lpfc_sli_config_mbox_subsys_get(phba,
8652                                                                         mboxq),
8653                                         lpfc_sli_config_mbox_opcode_get(phba,
8654                                                                         mboxq),
8655                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8656                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8657                                         bf_get(lpfc_mcqe_ext_status,
8658                                                &mboxq->mcqe),
8659                                         psli->sli_flag, flag);
8660                         /* Unblock the async mailbox posting afterward */
8661                         lpfc_sli4_async_mbox_unblock(phba);
8662                 }
8663                 return rc;
8664         }
8665
8666         /* Now, interrupt mode asynchrous mailbox command */
8667         rc = lpfc_mbox_cmd_check(phba, mboxq);
8668         if (rc) {
8669                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8670                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
8671                                 "cannot issue Data: x%x x%x\n",
8672                                 mboxq->vport ? mboxq->vport->vpi : 0,
8673                                 mboxq->u.mb.mbxCommand,
8674                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8675                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8676                                 psli->sli_flag, flag);
8677                 goto out_not_finished;
8678         }
8679
8680         /* Put the mailbox command to the driver internal FIFO */
8681         psli->slistat.mbox_busy++;
8682         spin_lock_irqsave(&phba->hbalock, iflags);
8683         lpfc_mbox_put(phba, mboxq);
8684         spin_unlock_irqrestore(&phba->hbalock, iflags);
8685         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8686                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
8687                         "x%x (x%x/x%x) x%x x%x x%x\n",
8688                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8689                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8690                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8691                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8692                         phba->pport->port_state,
8693                         psli->sli_flag, MBX_NOWAIT);
8694         /* Wake up worker thread to transport mailbox command from head */
8695         lpfc_worker_wake_up(phba);
8696
8697         return MBX_BUSY;
8698
8699 out_not_finished:
8700         return MBX_NOT_FINISHED;
8701 }
8702
8703 /**
8704  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8705  * @phba: Pointer to HBA context object.
8706  *
8707  * This function is called by worker thread to send a mailbox command to
8708  * SLI4 HBA firmware.
8709  *
8710  **/
8711 int
8712 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8713 {
8714         struct lpfc_sli *psli = &phba->sli;
8715         LPFC_MBOXQ_t *mboxq;
8716         int rc = MBX_SUCCESS;
8717         unsigned long iflags;
8718         struct lpfc_mqe *mqe;
8719         uint32_t mbx_cmnd;
8720
8721         /* Check interrupt mode before post async mailbox command */
8722         if (unlikely(!phba->sli4_hba.intr_enable))
8723                 return MBX_NOT_FINISHED;
8724
8725         /* Check for mailbox command service token */
8726         spin_lock_irqsave(&phba->hbalock, iflags);
8727         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8728                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8729                 return MBX_NOT_FINISHED;
8730         }
8731         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8732                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8733                 return MBX_NOT_FINISHED;
8734         }
8735         if (unlikely(phba->sli.mbox_active)) {
8736                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8737                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8738                                 "0384 There is pending active mailbox cmd\n");
8739                 return MBX_NOT_FINISHED;
8740         }
8741         /* Take the mailbox command service token */
8742         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8743
8744         /* Get the next mailbox command from head of queue */
8745         mboxq = lpfc_mbox_get(phba);
8746
8747         /* If no more mailbox command waiting for post, we're done */
8748         if (!mboxq) {
8749                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8750                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8751                 return MBX_SUCCESS;
8752         }
8753         phba->sli.mbox_active = mboxq;
8754         spin_unlock_irqrestore(&phba->hbalock, iflags);
8755
8756         /* Check device readiness for posting mailbox command */
8757         rc = lpfc_mbox_dev_check(phba);
8758         if (unlikely(rc))
8759                 /* Driver clean routine will clean up pending mailbox */
8760                 goto out_not_finished;
8761
8762         /* Prepare the mbox command to be posted */
8763         mqe = &mboxq->u.mqe;
8764         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8765
8766         /* Start timer for the mbox_tmo and log some mailbox post messages */
8767         mod_timer(&psli->mbox_tmo, (jiffies +
8768                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8769
8770         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8771                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8772                         "x%x x%x\n",
8773                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8774                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8775                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8776                         phba->pport->port_state, psli->sli_flag);
8777
8778         if (mbx_cmnd != MBX_HEARTBEAT) {
8779                 if (mboxq->vport) {
8780                         lpfc_debugfs_disc_trc(mboxq->vport,
8781                                 LPFC_DISC_TRC_MBOX_VPORT,
8782                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8783                                 mbx_cmnd, mqe->un.mb_words[0],
8784                                 mqe->un.mb_words[1]);
8785                 } else {
8786                         lpfc_debugfs_disc_trc(phba->pport,
8787                                 LPFC_DISC_TRC_MBOX,
8788                                 "MBOX Send: cmd:x%x mb:x%x x%x",
8789                                 mbx_cmnd, mqe->un.mb_words[0],
8790                                 mqe->un.mb_words[1]);
8791                 }
8792         }
8793         psli->slistat.mbox_cmd++;
8794
8795         /* Post the mailbox command to the port */
8796         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8797         if (rc != MBX_SUCCESS) {
8798                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8799                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
8800                                 "cannot issue Data: x%x x%x\n",
8801                                 mboxq->vport ? mboxq->vport->vpi : 0,
8802                                 mboxq->u.mb.mbxCommand,
8803                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8804                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8805                                 psli->sli_flag, MBX_NOWAIT);
8806                 goto out_not_finished;
8807         }
8808
8809         return rc;
8810
8811 out_not_finished:
8812         spin_lock_irqsave(&phba->hbalock, iflags);
8813         if (phba->sli.mbox_active) {
8814                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8815                 __lpfc_mbox_cmpl_put(phba, mboxq);
8816                 /* Release the token */
8817                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8818                 phba->sli.mbox_active = NULL;
8819         }
8820         spin_unlock_irqrestore(&phba->hbalock, iflags);
8821
8822         return MBX_NOT_FINISHED;
8823 }
8824
8825 /**
8826  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8827  * @phba: Pointer to HBA context object.
8828  * @pmbox: Pointer to mailbox object.
8829  * @flag: Flag indicating how the mailbox need to be processed.
8830  *
8831  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8832  * the API jump table function pointer from the lpfc_hba struct.
8833  *
8834  * Return codes the caller owns the mailbox command after the return of the
8835  * function.
8836  **/
8837 int
8838 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8839 {
8840         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8841 }
8842
8843 /**
8844  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8845  * @phba: The hba struct for which this call is being executed.
8846  * @dev_grp: The HBA PCI-Device group number.
8847  *
8848  * This routine sets up the mbox interface API function jump table in @phba
8849  * struct.
8850  * Returns: 0 - success, -ENODEV - failure.
8851  **/
8852 int
8853 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8854 {
8855
8856         switch (dev_grp) {
8857         case LPFC_PCI_DEV_LP:
8858                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8859                 phba->lpfc_sli_handle_slow_ring_event =
8860                                 lpfc_sli_handle_slow_ring_event_s3;
8861                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8862                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8863                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8864                 break;
8865         case LPFC_PCI_DEV_OC:
8866                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8867                 phba->lpfc_sli_handle_slow_ring_event =
8868                                 lpfc_sli_handle_slow_ring_event_s4;
8869                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8870                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8871                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8872                 break;
8873         default:
8874                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8875                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
8876                                 dev_grp);
8877                 return -ENODEV;
8878                 break;
8879         }
8880         return 0;
8881 }
8882
8883 /**
8884  * __lpfc_sli_ringtx_put - Add an iocb to the txq
8885  * @phba: Pointer to HBA context object.
8886  * @pring: Pointer to driver SLI ring object.
8887  * @piocb: Pointer to address of newly added command iocb.
8888  *
8889  * This function is called with hbalock held to add a command
8890  * iocb to the txq when SLI layer cannot submit the command iocb
8891  * to the ring.
8892  **/
8893 void
8894 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8895                     struct lpfc_iocbq *piocb)
8896 {
8897         lockdep_assert_held(&phba->hbalock);
8898         /* Insert the caller's iocb in the txq tail for later processing. */
8899         list_add_tail(&piocb->list, &pring->txq);
8900 }
8901
8902 /**
8903  * lpfc_sli_next_iocb - Get the next iocb in the txq
8904  * @phba: Pointer to HBA context object.
8905  * @pring: Pointer to driver SLI ring object.
8906  * @piocb: Pointer to address of newly added command iocb.
8907  *
8908  * This function is called with hbalock held before a new
8909  * iocb is submitted to the firmware. This function checks
8910  * txq to flush the iocbs in txq to Firmware before
8911  * submitting new iocbs to the Firmware.
8912  * If there are iocbs in the txq which need to be submitted
8913  * to firmware, lpfc_sli_next_iocb returns the first element
8914  * of the txq after dequeuing it from txq.
8915  * If there is no iocb in the txq then the function will return
8916  * *piocb and *piocb is set to NULL. Caller needs to check
8917  * *piocb to find if there are more commands in the txq.
8918  **/
8919 static struct lpfc_iocbq *
8920 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8921                    struct lpfc_iocbq **piocb)
8922 {
8923         struct lpfc_iocbq * nextiocb;
8924
8925         lockdep_assert_held(&phba->hbalock);
8926
8927         nextiocb = lpfc_sli_ringtx_get(phba, pring);
8928         if (!nextiocb) {
8929                 nextiocb = *piocb;
8930                 *piocb = NULL;
8931         }
8932
8933         return nextiocb;
8934 }
8935
8936 /**
8937  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8938  * @phba: Pointer to HBA context object.
8939  * @ring_number: SLI ring number to issue iocb on.
8940  * @piocb: Pointer to command iocb.
8941  * @flag: Flag indicating if this command can be put into txq.
8942  *
8943  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8944  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8945  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8946  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8947  * this function allows only iocbs for posting buffers. This function finds
8948  * next available slot in the command ring and posts the command to the
8949  * available slot and writes the port attention register to request HBA start
8950  * processing new iocb. If there is no slot available in the ring and
8951  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8952  * the function returns IOCB_BUSY.
8953  *
8954  * This function is called with hbalock held. The function will return success
8955  * after it successfully submit the iocb to firmware or after adding to the
8956  * txq.
8957  **/
8958 static int
8959 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8960                     struct lpfc_iocbq *piocb, uint32_t flag)
8961 {
8962         struct lpfc_iocbq *nextiocb;
8963         IOCB_t *iocb;
8964         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
8965
8966         lockdep_assert_held(&phba->hbalock);
8967
8968         if (piocb->iocb_cmpl && (!piocb->vport) &&
8969            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8970            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8971                 lpfc_printf_log(phba, KERN_ERR,
8972                                 LOG_SLI | LOG_VPORT,
8973                                 "1807 IOCB x%x failed. No vport\n",
8974                                 piocb->iocb.ulpCommand);
8975                 dump_stack();
8976                 return IOCB_ERROR;
8977         }
8978
8979
8980         /* If the PCI channel is in offline state, do not post iocbs. */
8981         if (unlikely(pci_channel_offline(phba->pcidev)))
8982                 return IOCB_ERROR;
8983
8984         /* If HBA has a deferred error attention, fail the iocb. */
8985         if (unlikely(phba->hba_flag & DEFER_ERATT))
8986                 return IOCB_ERROR;
8987
8988         /*
8989          * We should never get an IOCB if we are in a < LINK_DOWN state
8990          */
8991         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8992                 return IOCB_ERROR;
8993
8994         /*
8995          * Check to see if we are blocking IOCB processing because of a
8996          * outstanding event.
8997          */
8998         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8999                 goto iocb_busy;
9000
9001         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
9002                 /*
9003                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
9004                  * can be issued if the link is not up.
9005                  */
9006                 switch (piocb->iocb.ulpCommand) {
9007                 case CMD_GEN_REQUEST64_CR:
9008                 case CMD_GEN_REQUEST64_CX:
9009                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9010                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
9011                                         FC_RCTL_DD_UNSOL_CMD) ||
9012                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9013                                         MENLO_TRANSPORT_TYPE))
9014
9015                                 goto iocb_busy;
9016                         break;
9017                 case CMD_QUE_RING_BUF_CN:
9018                 case CMD_QUE_RING_BUF64_CN:
9019                         /*
9020                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9021                          * completion, iocb_cmpl MUST be 0.
9022                          */
9023                         if (piocb->iocb_cmpl)
9024                                 piocb->iocb_cmpl = NULL;
9025                         /*FALLTHROUGH*/
9026                 case CMD_CREATE_XRI_CR:
9027                 case CMD_CLOSE_XRI_CN:
9028                 case CMD_CLOSE_XRI_CX:
9029                         break;
9030                 default:
9031                         goto iocb_busy;
9032                 }
9033
9034         /*
9035          * For FCP commands, we must be in a state where we can process link
9036          * attention events.
9037          */
9038         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
9039                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
9040                 goto iocb_busy;
9041         }
9042
9043         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9044                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9045                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9046
9047         if (iocb)
9048                 lpfc_sli_update_ring(phba, pring);
9049         else
9050                 lpfc_sli_update_full_ring(phba, pring);
9051
9052         if (!piocb)
9053                 return IOCB_SUCCESS;
9054
9055         goto out_busy;
9056
9057  iocb_busy:
9058         pring->stats.iocb_cmd_delay++;
9059
9060  out_busy:
9061
9062         if (!(flag & SLI_IOCB_RET_IOCB)) {
9063                 __lpfc_sli_ringtx_put(phba, pring, piocb);
9064                 return IOCB_SUCCESS;
9065         }
9066
9067         return IOCB_BUSY;
9068 }
9069
9070 /**
9071  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9072  * @phba: Pointer to HBA context object.
9073  * @piocb: Pointer to command iocb.
9074  * @sglq: Pointer to the scatter gather queue object.
9075  *
9076  * This routine converts the bpl or bde that is in the IOCB
9077  * to a sgl list for the sli4 hardware. The physical address
9078  * of the bpl/bde is converted back to a virtual address.
9079  * If the IOCB contains a BPL then the list of BDE's is
9080  * converted to sli4_sge's. If the IOCB contains a single
9081  * BDE then it is converted to a single sli_sge.
9082  * The IOCB is still in cpu endianess so the contents of
9083  * the bpl can be used without byte swapping.
9084  *
9085  * Returns valid XRI = Success, NO_XRI = Failure.
9086 **/
9087 static uint16_t
9088 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9089                 struct lpfc_sglq *sglq)
9090 {
9091         uint16_t xritag = NO_XRI;
9092         struct ulp_bde64 *bpl = NULL;
9093         struct ulp_bde64 bde;
9094         struct sli4_sge *sgl  = NULL;
9095         struct lpfc_dmabuf *dmabuf;
9096         IOCB_t *icmd;
9097         int numBdes = 0;
9098         int i = 0;
9099         uint32_t offset = 0; /* accumulated offset in the sg request list */
9100         int inbound = 0; /* number of sg reply entries inbound from firmware */
9101
9102         if (!piocbq || !sglq)
9103                 return xritag;
9104
9105         sgl  = (struct sli4_sge *)sglq->sgl;
9106         icmd = &piocbq->iocb;
9107         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9108                 return sglq->sli4_xritag;
9109         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9110                 numBdes = icmd->un.genreq64.bdl.bdeSize /
9111                                 sizeof(struct ulp_bde64);
9112                 /* The addrHigh and addrLow fields within the IOCB
9113                  * have not been byteswapped yet so there is no
9114                  * need to swap them back.
9115                  */
9116                 if (piocbq->context3)
9117                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9118                 else
9119                         return xritag;
9120
9121                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
9122                 if (!bpl)
9123                         return xritag;
9124
9125                 for (i = 0; i < numBdes; i++) {
9126                         /* Should already be byte swapped. */
9127                         sgl->addr_hi = bpl->addrHigh;
9128                         sgl->addr_lo = bpl->addrLow;
9129
9130                         sgl->word2 = le32_to_cpu(sgl->word2);
9131                         if ((i+1) == numBdes)
9132                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
9133                         else
9134                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
9135                         /* swap the size field back to the cpu so we
9136                          * can assign it to the sgl.
9137                          */
9138                         bde.tus.w = le32_to_cpu(bpl->tus.w);
9139                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
9140                         /* The offsets in the sgl need to be accumulated
9141                          * separately for the request and reply lists.
9142                          * The request is always first, the reply follows.
9143                          */
9144                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9145                                 /* add up the reply sg entries */
9146                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9147                                         inbound++;
9148                                 /* first inbound? reset the offset */
9149                                 if (inbound == 1)
9150                                         offset = 0;
9151                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
9152                                 bf_set(lpfc_sli4_sge_type, sgl,
9153                                         LPFC_SGE_TYPE_DATA);
9154                                 offset += bde.tus.f.bdeSize;
9155                         }
9156                         sgl->word2 = cpu_to_le32(sgl->word2);
9157                         bpl++;
9158                         sgl++;
9159                 }
9160         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9161                         /* The addrHigh and addrLow fields of the BDE have not
9162                          * been byteswapped yet so they need to be swapped
9163                          * before putting them in the sgl.
9164                          */
9165                         sgl->addr_hi =
9166                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9167                         sgl->addr_lo =
9168                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
9169                         sgl->word2 = le32_to_cpu(sgl->word2);
9170                         bf_set(lpfc_sli4_sge_last, sgl, 1);
9171                         sgl->word2 = cpu_to_le32(sgl->word2);
9172                         sgl->sge_len =
9173                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
9174         }
9175         return sglq->sli4_xritag;
9176 }
9177
9178 /**
9179  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9180  * @phba: Pointer to HBA context object.
9181  * @piocb: Pointer to command iocb.
9182  * @wqe: Pointer to the work queue entry.
9183  *
9184  * This routine converts the iocb command to its Work Queue Entry
9185  * equivalent. The wqe pointer should not have any fields set when
9186  * this routine is called because it will memcpy over them.
9187  * This routine does not set the CQ_ID or the WQEC bits in the
9188  * wqe.
9189  *
9190  * Returns: 0 = Success, IOCB_ERROR = Failure.
9191  **/
9192 static int
9193 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
9194                 union lpfc_wqe128 *wqe)
9195 {
9196         uint32_t xmit_len = 0, total_len = 0;
9197         uint8_t ct = 0;
9198         uint32_t fip;
9199         uint32_t abort_tag;
9200         uint8_t command_type = ELS_COMMAND_NON_FIP;
9201         uint8_t cmnd;
9202         uint16_t xritag;
9203         uint16_t abrt_iotag;
9204         struct lpfc_iocbq *abrtiocbq;
9205         struct ulp_bde64 *bpl = NULL;
9206         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
9207         int numBdes, i;
9208         struct ulp_bde64 bde;
9209         struct lpfc_nodelist *ndlp;
9210         uint32_t *pcmd;
9211         uint32_t if_type;
9212
9213         fip = phba->hba_flag & HBA_FIP_SUPPORT;
9214         /* The fcp commands will set command type */
9215         if (iocbq->iocb_flag &  LPFC_IO_FCP)
9216                 command_type = FCP_COMMAND;
9217         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
9218                 command_type = ELS_COMMAND_FIP;
9219         else
9220                 command_type = ELS_COMMAND_NON_FIP;
9221
9222         if (phba->fcp_embed_io)
9223                 memset(wqe, 0, sizeof(union lpfc_wqe128));
9224         /* Some of the fields are in the right position already */
9225         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
9226         if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9227                 /* The ct field has moved so reset */
9228                 wqe->generic.wqe_com.word7 = 0;
9229                 wqe->generic.wqe_com.word10 = 0;
9230         }
9231
9232         abort_tag = (uint32_t) iocbq->iotag;
9233         xritag = iocbq->sli4_xritag;
9234         /* words0-2 bpl convert bde */
9235         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9236                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9237                                 sizeof(struct ulp_bde64);
9238                 bpl  = (struct ulp_bde64 *)
9239                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9240                 if (!bpl)
9241                         return IOCB_ERROR;
9242
9243                 /* Should already be byte swapped. */
9244                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
9245                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
9246                 /* swap the size field back to the cpu so we
9247                  * can assign it to the sgl.
9248                  */
9249                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
9250                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9251                 total_len = 0;
9252                 for (i = 0; i < numBdes; i++) {
9253                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
9254                         total_len += bde.tus.f.bdeSize;
9255                 }
9256         } else
9257                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
9258
9259         iocbq->iocb.ulpIoTag = iocbq->iotag;
9260         cmnd = iocbq->iocb.ulpCommand;
9261
9262         switch (iocbq->iocb.ulpCommand) {
9263         case CMD_ELS_REQUEST64_CR:
9264                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9265                         ndlp = iocbq->context_un.ndlp;
9266                 else
9267                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
9268                 if (!iocbq->iocb.ulpLe) {
9269                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9270                                 "2007 Only Limited Edition cmd Format"
9271                                 " supported 0x%x\n",
9272                                 iocbq->iocb.ulpCommand);
9273                         return IOCB_ERROR;
9274                 }
9275
9276                 wqe->els_req.payload_len = xmit_len;
9277                 /* Els_reguest64 has a TMO */
9278                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9279                         iocbq->iocb.ulpTimeout);
9280                 /* Need a VF for word 4 set the vf bit*/
9281                 bf_set(els_req64_vf, &wqe->els_req, 0);
9282                 /* And a VFID for word 12 */
9283                 bf_set(els_req64_vfid, &wqe->els_req, 0);
9284                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9285                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9286                        iocbq->iocb.ulpContext);
9287                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9288                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
9289                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
9290                 if (command_type == ELS_COMMAND_FIP)
9291                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9292                                         >> LPFC_FIP_ELS_ID_SHIFT);
9293                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9294                                         iocbq->context2)->virt);
9295                 if_type = bf_get(lpfc_sli_intf_if_type,
9296                                         &phba->sli4_hba.sli_intf);
9297                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9298                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
9299                                 *pcmd == ELS_CMD_SCR ||
9300                                 *pcmd == ELS_CMD_FDISC ||
9301                                 *pcmd == ELS_CMD_LOGO ||
9302                                 *pcmd == ELS_CMD_PLOGI)) {
9303                                 bf_set(els_req64_sp, &wqe->els_req, 1);
9304                                 bf_set(els_req64_sid, &wqe->els_req,
9305                                         iocbq->vport->fc_myDID);
9306                                 if ((*pcmd == ELS_CMD_FLOGI) &&
9307                                         !(phba->fc_topology ==
9308                                                 LPFC_TOPOLOGY_LOOP))
9309                                         bf_set(els_req64_sid, &wqe->els_req, 0);
9310                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9311                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9312                                         phba->vpi_ids[iocbq->vport->vpi]);
9313                         } else if (pcmd && iocbq->context1) {
9314                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9315                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9316                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9317                         }
9318                 }
9319                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9320                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9321                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9322                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9323                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9324                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9325                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9326                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
9327                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
9328                 break;
9329         case CMD_XMIT_SEQUENCE64_CX:
9330                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9331                        iocbq->iocb.un.ulpWord[3]);
9332                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
9333                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9334                 /* The entire sequence is transmitted for this IOCB */
9335                 xmit_len = total_len;
9336                 cmnd = CMD_XMIT_SEQUENCE64_CR;
9337                 if (phba->link_flag & LS_LOOPBACK_MODE)
9338                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
9339         case CMD_XMIT_SEQUENCE64_CR:
9340                 /* word3 iocb=io_tag32 wqe=reserved */
9341                 wqe->xmit_sequence.rsvd3 = 0;
9342                 /* word4 relative_offset memcpy */
9343                 /* word5 r_ctl/df_ctl memcpy */
9344                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9345                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9346                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9347                        LPFC_WQE_IOD_WRITE);
9348                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9349                        LPFC_WQE_LENLOC_WORD12);
9350                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
9351                 wqe->xmit_sequence.xmit_len = xmit_len;
9352                 command_type = OTHER_COMMAND;
9353                 break;
9354         case CMD_XMIT_BCAST64_CN:
9355                 /* word3 iocb=iotag32 wqe=seq_payload_len */
9356                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
9357                 /* word4 iocb=rsvd wqe=rsvd */
9358                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9359                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
9360                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
9361                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9362                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9363                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9364                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9365                        LPFC_WQE_LENLOC_WORD3);
9366                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
9367                 break;
9368         case CMD_FCP_IWRITE64_CR:
9369                 command_type = FCP_COMMAND_DATA_OUT;
9370                 /* word3 iocb=iotag wqe=payload_offset_len */
9371                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9372                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9373                        xmit_len + sizeof(struct fcp_rsp));
9374                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9375                        0);
9376                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9377                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9378                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9379                        iocbq->iocb.ulpFCP2Rcvy);
9380                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9381                 /* Always open the exchange */
9382                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9383                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9384                        LPFC_WQE_LENLOC_WORD4);
9385                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
9386                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
9387                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9388                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
9389                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9390                         if (iocbq->priority) {
9391                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9392                                        (iocbq->priority << 1));
9393                         } else {
9394                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9395                                        (phba->cfg_XLanePriority << 1));
9396                         }
9397                 }
9398                 /* Note, word 10 is already initialized to 0 */
9399
9400                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9401                 if (phba->cfg_enable_pbde)
9402                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9403                 else
9404                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9405
9406                 if (phba->fcp_embed_io) {
9407                         struct lpfc_scsi_buf *lpfc_cmd;
9408                         struct sli4_sge *sgl;
9409                         struct fcp_cmnd *fcp_cmnd;
9410                         uint32_t *ptr;
9411
9412                         /* 128 byte wqe support here */
9413
9414                         lpfc_cmd = iocbq->context1;
9415                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9416                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9417
9418                         /* Word 0-2 - FCP_CMND */
9419                         wqe->generic.bde.tus.f.bdeFlags =
9420                                 BUFF_TYPE_BDE_IMMED;
9421                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9422                         wqe->generic.bde.addrHigh = 0;
9423                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9424
9425                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9426                         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
9427
9428                         /* Word 22-29  FCP CMND Payload */
9429                         ptr = &wqe->words[22];
9430                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9431                 }
9432                 break;
9433         case CMD_FCP_IREAD64_CR:
9434                 /* word3 iocb=iotag wqe=payload_offset_len */
9435                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9436                 bf_set(payload_offset_len, &wqe->fcp_iread,
9437                        xmit_len + sizeof(struct fcp_rsp));
9438                 bf_set(cmd_buff_len, &wqe->fcp_iread,
9439                        0);
9440                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9441                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9442                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9443                        iocbq->iocb.ulpFCP2Rcvy);
9444                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
9445                 /* Always open the exchange */
9446                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9447                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9448                        LPFC_WQE_LENLOC_WORD4);
9449                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
9450                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
9451                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9452                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
9453                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9454                         if (iocbq->priority) {
9455                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9456                                        (iocbq->priority << 1));
9457                         } else {
9458                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9459                                        (phba->cfg_XLanePriority << 1));
9460                         }
9461                 }
9462                 /* Note, word 10 is already initialized to 0 */
9463
9464                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9465                 if (phba->cfg_enable_pbde)
9466                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9467                 else
9468                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9469
9470                 if (phba->fcp_embed_io) {
9471                         struct lpfc_scsi_buf *lpfc_cmd;
9472                         struct sli4_sge *sgl;
9473                         struct fcp_cmnd *fcp_cmnd;
9474                         uint32_t *ptr;
9475
9476                         /* 128 byte wqe support here */
9477
9478                         lpfc_cmd = iocbq->context1;
9479                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9480                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9481
9482                         /* Word 0-2 - FCP_CMND */
9483                         wqe->generic.bde.tus.f.bdeFlags =
9484                                 BUFF_TYPE_BDE_IMMED;
9485                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9486                         wqe->generic.bde.addrHigh = 0;
9487                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9488
9489                         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9490                         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
9491
9492                         /* Word 22-29  FCP CMND Payload */
9493                         ptr = &wqe->words[22];
9494                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9495                 }
9496                 break;
9497         case CMD_FCP_ICMND64_CR:
9498                 /* word3 iocb=iotag wqe=payload_offset_len */
9499                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9500                 bf_set(payload_offset_len, &wqe->fcp_icmd,
9501                        xmit_len + sizeof(struct fcp_rsp));
9502                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9503                        0);
9504                 /* word3 iocb=IO_TAG wqe=reserved */
9505                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
9506                 /* Always open the exchange */
9507                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9508                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9509                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9510                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9511                        LPFC_WQE_LENLOC_NONE);
9512                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9513                        iocbq->iocb.ulpFCP2Rcvy);
9514                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9515                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
9516                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9517                         if (iocbq->priority) {
9518                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9519                                        (iocbq->priority << 1));
9520                         } else {
9521                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9522                                        (phba->cfg_XLanePriority << 1));
9523                         }
9524                 }
9525                 /* Note, word 10 is already initialized to 0 */
9526
9527                 if (phba->fcp_embed_io) {
9528                         struct lpfc_scsi_buf *lpfc_cmd;
9529                         struct sli4_sge *sgl;
9530                         struct fcp_cmnd *fcp_cmnd;
9531                         uint32_t *ptr;
9532
9533                         /* 128 byte wqe support here */
9534
9535                         lpfc_cmd = iocbq->context1;
9536                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9537                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9538
9539                         /* Word 0-2 - FCP_CMND */
9540                         wqe->generic.bde.tus.f.bdeFlags =
9541                                 BUFF_TYPE_BDE_IMMED;
9542                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9543                         wqe->generic.bde.addrHigh = 0;
9544                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9545
9546                         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9547                         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
9548
9549                         /* Word 22-29  FCP CMND Payload */
9550                         ptr = &wqe->words[22];
9551                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9552                 }
9553                 break;
9554         case CMD_GEN_REQUEST64_CR:
9555                 /* For this command calculate the xmit length of the
9556                  * request bde.
9557                  */
9558                 xmit_len = 0;
9559                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9560                         sizeof(struct ulp_bde64);
9561                 for (i = 0; i < numBdes; i++) {
9562                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9563                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9564                                 break;
9565                         xmit_len += bde.tus.f.bdeSize;
9566                 }
9567                 /* word3 iocb=IO_TAG wqe=request_payload_len */
9568                 wqe->gen_req.request_payload_len = xmit_len;
9569                 /* word4 iocb=parameter wqe=relative_offset memcpy */
9570                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
9571                 /* word6 context tag copied in memcpy */
9572                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
9573                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9574                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9575                                 "2015 Invalid CT %x command 0x%x\n",
9576                                 ct, iocbq->iocb.ulpCommand);
9577                         return IOCB_ERROR;
9578                 }
9579                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9580                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9581                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9582                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9583                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9584                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9585                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9586                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
9587                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
9588                 command_type = OTHER_COMMAND;
9589                 break;
9590         case CMD_XMIT_ELS_RSP64_CX:
9591                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9592                 /* words0-2 BDE memcpy */
9593                 /* word3 iocb=iotag32 wqe=response_payload_len */
9594                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
9595                 /* word4 */
9596                 wqe->xmit_els_rsp.word4 = 0;
9597                 /* word5 iocb=rsvd wge=did */
9598                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
9599                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
9600
9601                 if_type = bf_get(lpfc_sli_intf_if_type,
9602                                         &phba->sli4_hba.sli_intf);
9603                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9604                         if (iocbq->vport->fc_flag & FC_PT2PT) {
9605                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9606                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9607                                         iocbq->vport->fc_myDID);
9608                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
9609                                         bf_set(wqe_els_did,
9610                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
9611                                 }
9612                         }
9613                 }
9614                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9615                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9616                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9617                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
9618                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9619                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
9620                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9621                                phba->vpi_ids[iocbq->vport->vpi]);
9622                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9623                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9624                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9625                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9626                        LPFC_WQE_LENLOC_WORD3);
9627                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9628                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9629                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9630                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9631                                         iocbq->context2)->virt);
9632                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9633                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9634                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9635                                         iocbq->vport->fc_myDID);
9636                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9637                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9638                                         phba->vpi_ids[phba->pport->vpi]);
9639                 }
9640                 command_type = OTHER_COMMAND;
9641                 break;
9642         case CMD_CLOSE_XRI_CN:
9643         case CMD_ABORT_XRI_CN:
9644         case CMD_ABORT_XRI_CX:
9645                 /* words 0-2 memcpy should be 0 rserved */
9646                 /* port will send abts */
9647                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9648                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9649                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9650                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9651                 } else
9652                         fip = 0;
9653
9654                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9655                         /*
9656                          * The link is down, or the command was ELS_FIP
9657                          * so the fw does not need to send abts
9658                          * on the wire.
9659                          */
9660                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9661                 else
9662                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9663                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9664                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9665                 wqe->abort_cmd.rsrvd5 = 0;
9666                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9667                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9668                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9669                 /*
9670                  * The abort handler will send us CMD_ABORT_XRI_CN or
9671                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9672                  */
9673                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9674                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9675                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9676                        LPFC_WQE_LENLOC_NONE);
9677                 cmnd = CMD_ABORT_XRI_CX;
9678                 command_type = OTHER_COMMAND;
9679                 xritag = 0;
9680                 break;
9681         case CMD_XMIT_BLS_RSP64_CX:
9682                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9683                 /* As BLS ABTS RSP WQE is very different from other WQEs,
9684                  * we re-construct this WQE here based on information in
9685                  * iocbq from scratch.
9686                  */
9687                 memset(wqe, 0, sizeof(union lpfc_wqe));
9688                 /* OX_ID is invariable to who sent ABTS to CT exchange */
9689                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9690                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9691                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9692                     LPFC_ABTS_UNSOL_INT) {
9693                         /* ABTS sent by initiator to CT exchange, the
9694                          * RX_ID field will be filled with the newly
9695                          * allocated responder XRI.
9696                          */
9697                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9698                                iocbq->sli4_xritag);
9699                 } else {
9700                         /* ABTS sent by responder to CT exchange, the
9701                          * RX_ID field will be filled with the responder
9702                          * RX_ID from ABTS.
9703                          */
9704                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9705                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9706                 }
9707                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9708                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9709
9710                 /* Use CT=VPI */
9711                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9712                         ndlp->nlp_DID);
9713                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9714                         iocbq->iocb.ulpContext);
9715                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9716                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9717                         phba->vpi_ids[phba->pport->vpi]);
9718                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9719                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9720                        LPFC_WQE_LENLOC_NONE);
9721                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9722                 command_type = OTHER_COMMAND;
9723                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9724                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9725                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9726                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9727                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9728                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9729                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9730                 }
9731
9732                 break;
9733         case CMD_SEND_FRAME:
9734                 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9735                 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9736                 return 0;
9737         case CMD_XRI_ABORTED_CX:
9738         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9739         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9740         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9741         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9742         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9743         default:
9744                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9745                                 "2014 Invalid command 0x%x\n",
9746                                 iocbq->iocb.ulpCommand);
9747                 return IOCB_ERROR;
9748                 break;
9749         }
9750
9751         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9752                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9753         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9754                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9755         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9756                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9757         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9758                               LPFC_IO_DIF_INSERT);
9759         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9760         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9761         wqe->generic.wqe_com.abort_tag = abort_tag;
9762         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9763         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9764         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9765         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9766         return 0;
9767 }
9768
9769 /**
9770  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9771  * @phba: Pointer to HBA context object.
9772  * @ring_number: SLI ring number to issue iocb on.
9773  * @piocb: Pointer to command iocb.
9774  * @flag: Flag indicating if this command can be put into txq.
9775  *
9776  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9777  * an iocb command to an HBA with SLI-4 interface spec.
9778  *
9779  * This function is called with hbalock held. The function will return success
9780  * after it successfully submit the iocb to firmware or after adding to the
9781  * txq.
9782  **/
9783 static int
9784 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9785                          struct lpfc_iocbq *piocb, uint32_t flag)
9786 {
9787         struct lpfc_sglq *sglq;
9788         union lpfc_wqe128 wqe;
9789         struct lpfc_queue *wq;
9790         struct lpfc_sli_ring *pring;
9791
9792         /* Get the WQ */
9793         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9794             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9795                 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9796                         wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9797                 else
9798                         wq = phba->sli4_hba.oas_wq;
9799         } else {
9800                 wq = phba->sli4_hba.els_wq;
9801         }
9802
9803         /* Get corresponding ring */
9804         pring = wq->pring;
9805
9806         /*
9807          * The WQE can be either 64 or 128 bytes,
9808          */
9809
9810         lockdep_assert_held(&phba->hbalock);
9811
9812         if (piocb->sli4_xritag == NO_XRI) {
9813                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9814                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9815                         sglq = NULL;
9816                 else {
9817                         if (!list_empty(&pring->txq)) {
9818                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
9819                                         __lpfc_sli_ringtx_put(phba,
9820                                                 pring, piocb);
9821                                         return IOCB_SUCCESS;
9822                                 } else {
9823                                         return IOCB_BUSY;
9824                                 }
9825                         } else {
9826                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9827                                 if (!sglq) {
9828                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
9829                                                 __lpfc_sli_ringtx_put(phba,
9830                                                                 pring,
9831                                                                 piocb);
9832                                                 return IOCB_SUCCESS;
9833                                         } else
9834                                                 return IOCB_BUSY;
9835                                 }
9836                         }
9837                 }
9838         } else if (piocb->iocb_flag &  LPFC_IO_FCP)
9839                 /* These IO's already have an XRI and a mapped sgl. */
9840                 sglq = NULL;
9841         else {
9842                 /*
9843                  * This is a continuation of a commandi,(CX) so this
9844                  * sglq is on the active list
9845                  */
9846                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9847                 if (!sglq)
9848                         return IOCB_ERROR;
9849         }
9850
9851         if (sglq) {
9852                 piocb->sli4_lxritag = sglq->sli4_lxritag;
9853                 piocb->sli4_xritag = sglq->sli4_xritag;
9854                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9855                         return IOCB_ERROR;
9856         }
9857
9858         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
9859                 return IOCB_ERROR;
9860
9861         if (lpfc_sli4_wq_put(wq, &wqe))
9862                 return IOCB_ERROR;
9863         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9864
9865         return 0;
9866 }
9867
9868 /**
9869  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9870  *
9871  * This routine wraps the actual lockless version for issusing IOCB function
9872  * pointer from the lpfc_hba struct.
9873  *
9874  * Return codes:
9875  * IOCB_ERROR - Error
9876  * IOCB_SUCCESS - Success
9877  * IOCB_BUSY - Busy
9878  **/
9879 int
9880 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9881                 struct lpfc_iocbq *piocb, uint32_t flag)
9882 {
9883         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9884 }
9885
9886 /**
9887  * lpfc_sli_api_table_setup - Set up sli api function jump table
9888  * @phba: The hba struct for which this call is being executed.
9889  * @dev_grp: The HBA PCI-Device group number.
9890  *
9891  * This routine sets up the SLI interface API function jump table in @phba
9892  * struct.
9893  * Returns: 0 - success, -ENODEV - failure.
9894  **/
9895 int
9896 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9897 {
9898
9899         switch (dev_grp) {
9900         case LPFC_PCI_DEV_LP:
9901                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9902                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9903                 break;
9904         case LPFC_PCI_DEV_OC:
9905                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9906                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9907                 break;
9908         default:
9909                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9910                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
9911                                 dev_grp);
9912                 return -ENODEV;
9913                 break;
9914         }
9915         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9916         return 0;
9917 }
9918
9919 /**
9920  * lpfc_sli4_calc_ring - Calculates which ring to use
9921  * @phba: Pointer to HBA context object.
9922  * @piocb: Pointer to command iocb.
9923  *
9924  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9925  * hba_wqidx, thus we need to calculate the corresponding ring.
9926  * Since ABORTS must go on the same WQ of the command they are
9927  * aborting, we use command's hba_wqidx.
9928  */
9929 struct lpfc_sli_ring *
9930 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9931 {
9932         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9933                 if (!(phba->cfg_fof) ||
9934                     (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9935                         if (unlikely(!phba->sli4_hba.fcp_wq))
9936                                 return NULL;
9937                         /*
9938                          * for abort iocb hba_wqidx should already
9939                          * be setup based on what work queue we used.
9940                          */
9941                         if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9942                                 piocb->hba_wqidx =
9943                                         lpfc_sli4_scmd_to_wqidx_distr(phba,
9944                                                               piocb->context1);
9945                                 piocb->hba_wqidx = piocb->hba_wqidx %
9946                                         phba->cfg_fcp_io_channel;
9947                         }
9948                         return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
9949                 } else {
9950                         if (unlikely(!phba->sli4_hba.oas_wq))
9951                                 return NULL;
9952                         piocb->hba_wqidx = 0;
9953                         return phba->sli4_hba.oas_wq->pring;
9954                 }
9955         } else {
9956                 if (unlikely(!phba->sli4_hba.els_wq))
9957                         return NULL;
9958                 piocb->hba_wqidx = 0;
9959                 return phba->sli4_hba.els_wq->pring;
9960         }
9961 }
9962
9963 /**
9964  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9965  * @phba: Pointer to HBA context object.
9966  * @pring: Pointer to driver SLI ring object.
9967  * @piocb: Pointer to command iocb.
9968  * @flag: Flag indicating if this command can be put into txq.
9969  *
9970  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9971  * function. This function gets the hbalock and calls
9972  * __lpfc_sli_issue_iocb function and will return the error returned
9973  * by __lpfc_sli_issue_iocb function. This wrapper is used by
9974  * functions which do not hold hbalock.
9975  **/
9976 int
9977 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9978                     struct lpfc_iocbq *piocb, uint32_t flag)
9979 {
9980         struct lpfc_hba_eq_hdl *hba_eq_hdl;
9981         struct lpfc_sli_ring *pring;
9982         struct lpfc_queue *fpeq;
9983         struct lpfc_eqe *eqe;
9984         unsigned long iflags;
9985         int rc, idx;
9986
9987         if (phba->sli_rev == LPFC_SLI_REV4) {
9988                 pring = lpfc_sli4_calc_ring(phba, piocb);
9989                 if (unlikely(pring == NULL))
9990                         return IOCB_ERROR;
9991
9992                 spin_lock_irqsave(&pring->ring_lock, iflags);
9993                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9994                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9995
9996                 if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
9997                         idx = piocb->hba_wqidx;
9998                         hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
9999
10000                         if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
10001
10002                                 /* Get associated EQ with this index */
10003                                 fpeq = phba->sli4_hba.hba_eq[idx];
10004
10005                                 /* Turn off interrupts from this EQ */
10006                                 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
10007
10008                                 /*
10009                                  * Process all the events on FCP EQ
10010                                  */
10011                                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10012                                         lpfc_sli4_hba_handle_eqe(phba,
10013                                                 eqe, idx);
10014                                         fpeq->EQ_processed++;
10015                                 }
10016
10017                                 /* Always clear and re-arm the EQ */
10018                                 phba->sli4_hba.sli4_eq_release(fpeq,
10019                                         LPFC_QUEUE_REARM);
10020                         }
10021                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
10022                 }
10023         } else {
10024                 /* For now, SLI2/3 will still use hbalock */
10025                 spin_lock_irqsave(&phba->hbalock, iflags);
10026                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10027                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10028         }
10029         return rc;
10030 }
10031
10032 /**
10033  * lpfc_extra_ring_setup - Extra ring setup function
10034  * @phba: Pointer to HBA context object.
10035  *
10036  * This function is called while driver attaches with the
10037  * HBA to setup the extra ring. The extra ring is used
10038  * only when driver needs to support target mode functionality
10039  * or IP over FC functionalities.
10040  *
10041  * This function is called with no lock held. SLI3 only.
10042  **/
10043 static int
10044 lpfc_extra_ring_setup( struct lpfc_hba *phba)
10045 {
10046         struct lpfc_sli *psli;
10047         struct lpfc_sli_ring *pring;
10048
10049         psli = &phba->sli;
10050
10051         /* Adjust cmd/rsp ring iocb entries more evenly */
10052
10053         /* Take some away from the FCP ring */
10054         pring = &psli->sli3_ring[LPFC_FCP_RING];
10055         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10056         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10057         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10058         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10059
10060         /* and give them to the extra ring */
10061         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
10062
10063         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10064         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10065         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10066         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10067
10068         /* Setup default profile for this ring */
10069         pring->iotag_max = 4096;
10070         pring->num_mask = 1;
10071         pring->prt[0].profile = 0;      /* Mask 0 */
10072         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10073         pring->prt[0].type = phba->cfg_multi_ring_type;
10074         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10075         return 0;
10076 }
10077
10078 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10079  * @phba: Pointer to HBA context object.
10080  * @iocbq: Pointer to iocb object.
10081  *
10082  * The async_event handler calls this routine when it receives
10083  * an ASYNC_STATUS_CN event from the port.  The port generates
10084  * this event when an Abort Sequence request to an rport fails
10085  * twice in succession.  The abort could be originated by the
10086  * driver or by the port.  The ABTS could have been for an ELS
10087  * or FCP IO.  The port only generates this event when an ABTS
10088  * fails to complete after one retry.
10089  */
10090 static void
10091 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10092                           struct lpfc_iocbq *iocbq)
10093 {
10094         struct lpfc_nodelist *ndlp = NULL;
10095         uint16_t rpi = 0, vpi = 0;
10096         struct lpfc_vport *vport = NULL;
10097
10098         /* The rpi in the ulpContext is vport-sensitive. */
10099         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10100         rpi = iocbq->iocb.ulpContext;
10101
10102         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10103                         "3092 Port generated ABTS async event "
10104                         "on vpi %d rpi %d status 0x%x\n",
10105                         vpi, rpi, iocbq->iocb.ulpStatus);
10106
10107         vport = lpfc_find_vport_by_vpid(phba, vpi);
10108         if (!vport)
10109                 goto err_exit;
10110         ndlp = lpfc_findnode_rpi(vport, rpi);
10111         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10112                 goto err_exit;
10113
10114         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10115                 lpfc_sli_abts_recover_port(vport, ndlp);
10116         return;
10117
10118  err_exit:
10119         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10120                         "3095 Event Context not found, no "
10121                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10122                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10123                         vpi, rpi);
10124 }
10125
10126 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10127  * @phba: pointer to HBA context object.
10128  * @ndlp: nodelist pointer for the impacted rport.
10129  * @axri: pointer to the wcqe containing the failed exchange.
10130  *
10131  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10132  * port.  The port generates this event when an abort exchange request to an
10133  * rport fails twice in succession with no reply.  The abort could be originated
10134  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
10135  */
10136 void
10137 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10138                            struct lpfc_nodelist *ndlp,
10139                            struct sli4_wcqe_xri_aborted *axri)
10140 {
10141         struct lpfc_vport *vport;
10142         uint32_t ext_status = 0;
10143
10144         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
10145                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10146                                 "3115 Node Context not found, driver "
10147                                 "ignoring abts err event\n");
10148                 return;
10149         }
10150
10151         vport = ndlp->vport;
10152         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10153                         "3116 Port generated FCP XRI ABORT event on "
10154                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
10155                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
10156                         bf_get(lpfc_wcqe_xa_xri, axri),
10157                         bf_get(lpfc_wcqe_xa_status, axri),
10158                         axri->parameter);
10159
10160         /*
10161          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
10162          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10163          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10164          */
10165         ext_status = axri->parameter & IOERR_PARAM_MASK;
10166         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10167             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
10168                 lpfc_sli_abts_recover_port(vport, ndlp);
10169 }
10170
10171 /**
10172  * lpfc_sli_async_event_handler - ASYNC iocb handler function
10173  * @phba: Pointer to HBA context object.
10174  * @pring: Pointer to driver SLI ring object.
10175  * @iocbq: Pointer to iocb object.
10176  *
10177  * This function is called by the slow ring event handler
10178  * function when there is an ASYNC event iocb in the ring.
10179  * This function is called with no lock held.
10180  * Currently this function handles only temperature related
10181  * ASYNC events. The function decodes the temperature sensor
10182  * event message and posts events for the management applications.
10183  **/
10184 static void
10185 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10186         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10187 {
10188         IOCB_t *icmd;
10189         uint16_t evt_code;
10190         struct temp_event temp_event_data;
10191         struct Scsi_Host *shost;
10192         uint32_t *iocb_w;
10193
10194         icmd = &iocbq->iocb;
10195         evt_code = icmd->un.asyncstat.evt_code;
10196
10197         switch (evt_code) {
10198         case ASYNC_TEMP_WARN:
10199         case ASYNC_TEMP_SAFE:
10200                 temp_event_data.data = (uint32_t) icmd->ulpContext;
10201                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10202                 if (evt_code == ASYNC_TEMP_WARN) {
10203                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10204                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10205                                 "0347 Adapter is very hot, please take "
10206                                 "corrective action. temperature : %d Celsius\n",
10207                                 (uint32_t) icmd->ulpContext);
10208                 } else {
10209                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
10210                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10211                                 "0340 Adapter temperature is OK now. "
10212                                 "temperature : %d Celsius\n",
10213                                 (uint32_t) icmd->ulpContext);
10214                 }
10215
10216                 /* Send temperature change event to applications */
10217                 shost = lpfc_shost_from_vport(phba->pport);
10218                 fc_host_post_vendor_event(shost, fc_get_event_number(),
10219                         sizeof(temp_event_data), (char *) &temp_event_data,
10220                         LPFC_NL_VENDOR_ID);
10221                 break;
10222         case ASYNC_STATUS_CN:
10223                 lpfc_sli_abts_err_handler(phba, iocbq);
10224                 break;
10225         default:
10226                 iocb_w = (uint32_t *) icmd;
10227                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10228                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
10229                         " evt_code 0x%x\n"
10230                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
10231                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
10232                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
10233                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
10234                         pring->ringno, icmd->un.asyncstat.evt_code,
10235                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10236                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10237                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10238                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10239
10240                 break;
10241         }
10242 }
10243
10244
10245 /**
10246  * lpfc_sli4_setup - SLI ring setup function
10247  * @phba: Pointer to HBA context object.
10248  *
10249  * lpfc_sli_setup sets up rings of the SLI interface with
10250  * number of iocbs per ring and iotags. This function is
10251  * called while driver attach to the HBA and before the
10252  * interrupts are enabled. So there is no need for locking.
10253  *
10254  * This function always returns 0.
10255  **/
10256 int
10257 lpfc_sli4_setup(struct lpfc_hba *phba)
10258 {
10259         struct lpfc_sli_ring *pring;
10260
10261         pring = phba->sli4_hba.els_wq->pring;
10262         pring->num_mask = LPFC_MAX_RING_MASK;
10263         pring->prt[0].profile = 0;      /* Mask 0 */
10264         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10265         pring->prt[0].type = FC_TYPE_ELS;
10266         pring->prt[0].lpfc_sli_rcv_unsol_event =
10267             lpfc_els_unsol_event;
10268         pring->prt[1].profile = 0;      /* Mask 1 */
10269         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10270         pring->prt[1].type = FC_TYPE_ELS;
10271         pring->prt[1].lpfc_sli_rcv_unsol_event =
10272             lpfc_els_unsol_event;
10273         pring->prt[2].profile = 0;      /* Mask 2 */
10274         /* NameServer Inquiry */
10275         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10276         /* NameServer */
10277         pring->prt[2].type = FC_TYPE_CT;
10278         pring->prt[2].lpfc_sli_rcv_unsol_event =
10279             lpfc_ct_unsol_event;
10280         pring->prt[3].profile = 0;      /* Mask 3 */
10281         /* NameServer response */
10282         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10283         /* NameServer */
10284         pring->prt[3].type = FC_TYPE_CT;
10285         pring->prt[3].lpfc_sli_rcv_unsol_event =
10286             lpfc_ct_unsol_event;
10287         return 0;
10288 }
10289
10290 /**
10291  * lpfc_sli_setup - SLI ring setup function
10292  * @phba: Pointer to HBA context object.
10293  *
10294  * lpfc_sli_setup sets up rings of the SLI interface with
10295  * number of iocbs per ring and iotags. This function is
10296  * called while driver attach to the HBA and before the
10297  * interrupts are enabled. So there is no need for locking.
10298  *
10299  * This function always returns 0. SLI3 only.
10300  **/
10301 int
10302 lpfc_sli_setup(struct lpfc_hba *phba)
10303 {
10304         int i, totiocbsize = 0;
10305         struct lpfc_sli *psli = &phba->sli;
10306         struct lpfc_sli_ring *pring;
10307
10308         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
10309         psli->sli_flag = 0;
10310
10311         psli->iocbq_lookup = NULL;
10312         psli->iocbq_lookup_len = 0;
10313         psli->last_iotag = 0;
10314
10315         for (i = 0; i < psli->num_rings; i++) {
10316                 pring = &psli->sli3_ring[i];
10317                 switch (i) {
10318                 case LPFC_FCP_RING:     /* ring 0 - FCP */
10319                         /* numCiocb and numRiocb are used in config_port */
10320                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10321                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10322                         pring->sli.sli3.numCiocb +=
10323                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10324                         pring->sli.sli3.numRiocb +=
10325                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10326                         pring->sli.sli3.numCiocb +=
10327                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10328                         pring->sli.sli3.numRiocb +=
10329                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10330                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10331                                                         SLI3_IOCB_CMD_SIZE :
10332                                                         SLI2_IOCB_CMD_SIZE;
10333                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10334                                                         SLI3_IOCB_RSP_SIZE :
10335                                                         SLI2_IOCB_RSP_SIZE;
10336                         pring->iotag_ctr = 0;
10337                         pring->iotag_max =
10338                             (phba->cfg_hba_queue_depth * 2);
10339                         pring->fast_iotag = pring->iotag_max;
10340                         pring->num_mask = 0;
10341                         break;
10342                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
10343                         /* numCiocb and numRiocb are used in config_port */
10344                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10345                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10346                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10347                                                         SLI3_IOCB_CMD_SIZE :
10348                                                         SLI2_IOCB_CMD_SIZE;
10349                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10350                                                         SLI3_IOCB_RSP_SIZE :
10351                                                         SLI2_IOCB_RSP_SIZE;
10352                         pring->iotag_max = phba->cfg_hba_queue_depth;
10353                         pring->num_mask = 0;
10354                         break;
10355                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
10356                         /* numCiocb and numRiocb are used in config_port */
10357                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10358                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10359                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10360                                                         SLI3_IOCB_CMD_SIZE :
10361                                                         SLI2_IOCB_CMD_SIZE;
10362                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10363                                                         SLI3_IOCB_RSP_SIZE :
10364                                                         SLI2_IOCB_RSP_SIZE;
10365                         pring->fast_iotag = 0;
10366                         pring->iotag_ctr = 0;
10367                         pring->iotag_max = 4096;
10368                         pring->lpfc_sli_rcv_async_status =
10369                                 lpfc_sli_async_event_handler;
10370                         pring->num_mask = LPFC_MAX_RING_MASK;
10371                         pring->prt[0].profile = 0;      /* Mask 0 */
10372                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10373                         pring->prt[0].type = FC_TYPE_ELS;
10374                         pring->prt[0].lpfc_sli_rcv_unsol_event =
10375                             lpfc_els_unsol_event;
10376                         pring->prt[1].profile = 0;      /* Mask 1 */
10377                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10378                         pring->prt[1].type = FC_TYPE_ELS;
10379                         pring->prt[1].lpfc_sli_rcv_unsol_event =
10380                             lpfc_els_unsol_event;
10381                         pring->prt[2].profile = 0;      /* Mask 2 */
10382                         /* NameServer Inquiry */
10383                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10384                         /* NameServer */
10385                         pring->prt[2].type = FC_TYPE_CT;
10386                         pring->prt[2].lpfc_sli_rcv_unsol_event =
10387                             lpfc_ct_unsol_event;
10388                         pring->prt[3].profile = 0;      /* Mask 3 */
10389                         /* NameServer response */
10390                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10391                         /* NameServer */
10392                         pring->prt[3].type = FC_TYPE_CT;
10393                         pring->prt[3].lpfc_sli_rcv_unsol_event =
10394                             lpfc_ct_unsol_event;
10395                         break;
10396                 }
10397                 totiocbsize += (pring->sli.sli3.numCiocb *
10398                         pring->sli.sli3.sizeCiocb) +
10399                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
10400         }
10401         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
10402                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
10403                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10404                        "SLI2 SLIM Data: x%x x%lx\n",
10405                        phba->brd_no, totiocbsize,
10406                        (unsigned long) MAX_SLIM_IOCB_SIZE);
10407         }
10408         if (phba->cfg_multi_ring_support == 2)
10409                 lpfc_extra_ring_setup(phba);
10410
10411         return 0;
10412 }
10413
10414 /**
10415  * lpfc_sli4_queue_init - Queue initialization function
10416  * @phba: Pointer to HBA context object.
10417  *
10418  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
10419  * ring. This function also initializes ring indices of each ring.
10420  * This function is called during the initialization of the SLI
10421  * interface of an HBA.
10422  * This function is called with no lock held and always returns
10423  * 1.
10424  **/
10425 void
10426 lpfc_sli4_queue_init(struct lpfc_hba *phba)
10427 {
10428         struct lpfc_sli *psli;
10429         struct lpfc_sli_ring *pring;
10430         int i;
10431
10432         psli = &phba->sli;
10433         spin_lock_irq(&phba->hbalock);
10434         INIT_LIST_HEAD(&psli->mboxq);
10435         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10436         /* Initialize list headers for txq and txcmplq as double linked lists */
10437         for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10438                 pring = phba->sli4_hba.fcp_wq[i]->pring;
10439                 pring->flag = 0;
10440                 pring->ringno = LPFC_FCP_RING;
10441                 INIT_LIST_HEAD(&pring->txq);
10442                 INIT_LIST_HEAD(&pring->txcmplq);
10443                 INIT_LIST_HEAD(&pring->iocb_continueq);
10444                 spin_lock_init(&pring->ring_lock);
10445         }
10446         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10447                 pring = phba->sli4_hba.nvme_wq[i]->pring;
10448                 pring->flag = 0;
10449                 pring->ringno = LPFC_FCP_RING;
10450                 INIT_LIST_HEAD(&pring->txq);
10451                 INIT_LIST_HEAD(&pring->txcmplq);
10452                 INIT_LIST_HEAD(&pring->iocb_continueq);
10453                 spin_lock_init(&pring->ring_lock);
10454         }
10455         pring = phba->sli4_hba.els_wq->pring;
10456         pring->flag = 0;
10457         pring->ringno = LPFC_ELS_RING;
10458         INIT_LIST_HEAD(&pring->txq);
10459         INIT_LIST_HEAD(&pring->txcmplq);
10460         INIT_LIST_HEAD(&pring->iocb_continueq);
10461         spin_lock_init(&pring->ring_lock);
10462
10463         if (phba->cfg_nvme_io_channel) {
10464                 pring = phba->sli4_hba.nvmels_wq->pring;
10465                 pring->flag = 0;
10466                 pring->ringno = LPFC_ELS_RING;
10467                 INIT_LIST_HEAD(&pring->txq);
10468                 INIT_LIST_HEAD(&pring->txcmplq);
10469                 INIT_LIST_HEAD(&pring->iocb_continueq);
10470                 spin_lock_init(&pring->ring_lock);
10471         }
10472
10473         if (phba->cfg_fof) {
10474                 pring = phba->sli4_hba.oas_wq->pring;
10475                 pring->flag = 0;
10476                 pring->ringno = LPFC_FCP_RING;
10477                 INIT_LIST_HEAD(&pring->txq);
10478                 INIT_LIST_HEAD(&pring->txcmplq);
10479                 INIT_LIST_HEAD(&pring->iocb_continueq);
10480                 spin_lock_init(&pring->ring_lock);
10481         }
10482
10483         spin_unlock_irq(&phba->hbalock);
10484 }
10485
10486 /**
10487  * lpfc_sli_queue_init - Queue initialization function
10488  * @phba: Pointer to HBA context object.
10489  *
10490  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10491  * ring. This function also initializes ring indices of each ring.
10492  * This function is called during the initialization of the SLI
10493  * interface of an HBA.
10494  * This function is called with no lock held and always returns
10495  * 1.
10496  **/
10497 void
10498 lpfc_sli_queue_init(struct lpfc_hba *phba)
10499 {
10500         struct lpfc_sli *psli;
10501         struct lpfc_sli_ring *pring;
10502         int i;
10503
10504         psli = &phba->sli;
10505         spin_lock_irq(&phba->hbalock);
10506         INIT_LIST_HEAD(&psli->mboxq);
10507         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10508         /* Initialize list headers for txq and txcmplq as double linked lists */
10509         for (i = 0; i < psli->num_rings; i++) {
10510                 pring = &psli->sli3_ring[i];
10511                 pring->ringno = i;
10512                 pring->sli.sli3.next_cmdidx  = 0;
10513                 pring->sli.sli3.local_getidx = 0;
10514                 pring->sli.sli3.cmdidx = 0;
10515                 INIT_LIST_HEAD(&pring->iocb_continueq);
10516                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
10517                 INIT_LIST_HEAD(&pring->postbufq);
10518                 pring->flag = 0;
10519                 INIT_LIST_HEAD(&pring->txq);
10520                 INIT_LIST_HEAD(&pring->txcmplq);
10521                 spin_lock_init(&pring->ring_lock);
10522         }
10523         spin_unlock_irq(&phba->hbalock);
10524 }
10525
10526 /**
10527  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10528  * @phba: Pointer to HBA context object.
10529  *
10530  * This routine flushes the mailbox command subsystem. It will unconditionally
10531  * flush all the mailbox commands in the three possible stages in the mailbox
10532  * command sub-system: pending mailbox command queue; the outstanding mailbox
10533  * command; and completed mailbox command queue. It is caller's responsibility
10534  * to make sure that the driver is in the proper state to flush the mailbox
10535  * command sub-system. Namely, the posting of mailbox commands into the
10536  * pending mailbox command queue from the various clients must be stopped;
10537  * either the HBA is in a state that it will never works on the outstanding
10538  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10539  * mailbox command has been completed.
10540  **/
10541 static void
10542 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10543 {
10544         LIST_HEAD(completions);
10545         struct lpfc_sli *psli = &phba->sli;
10546         LPFC_MBOXQ_t *pmb;
10547         unsigned long iflag;
10548
10549         /* Disable softirqs, including timers from obtaining phba->hbalock */
10550         local_bh_disable();
10551
10552         /* Flush all the mailbox commands in the mbox system */
10553         spin_lock_irqsave(&phba->hbalock, iflag);
10554
10555         /* The pending mailbox command queue */
10556         list_splice_init(&phba->sli.mboxq, &completions);
10557         /* The outstanding active mailbox command */
10558         if (psli->mbox_active) {
10559                 list_add_tail(&psli->mbox_active->list, &completions);
10560                 psli->mbox_active = NULL;
10561                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10562         }
10563         /* The completed mailbox command queue */
10564         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10565         spin_unlock_irqrestore(&phba->hbalock, iflag);
10566
10567         /* Enable softirqs again, done with phba->hbalock */
10568         local_bh_enable();
10569
10570         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10571         while (!list_empty(&completions)) {
10572                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10573                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10574                 if (pmb->mbox_cmpl)
10575                         pmb->mbox_cmpl(phba, pmb);
10576         }
10577 }
10578
10579 /**
10580  * lpfc_sli_host_down - Vport cleanup function
10581  * @vport: Pointer to virtual port object.
10582  *
10583  * lpfc_sli_host_down is called to clean up the resources
10584  * associated with a vport before destroying virtual
10585  * port data structures.
10586  * This function does following operations:
10587  * - Free discovery resources associated with this virtual
10588  *   port.
10589  * - Free iocbs associated with this virtual port in
10590  *   the txq.
10591  * - Send abort for all iocb commands associated with this
10592  *   vport in txcmplq.
10593  *
10594  * This function is called with no lock held and always returns 1.
10595  **/
10596 int
10597 lpfc_sli_host_down(struct lpfc_vport *vport)
10598 {
10599         LIST_HEAD(completions);
10600         struct lpfc_hba *phba = vport->phba;
10601         struct lpfc_sli *psli = &phba->sli;
10602         struct lpfc_queue *qp = NULL;
10603         struct lpfc_sli_ring *pring;
10604         struct lpfc_iocbq *iocb, *next_iocb;
10605         int i;
10606         unsigned long flags = 0;
10607         uint16_t prev_pring_flag;
10608
10609         lpfc_cleanup_discovery_resources(vport);
10610
10611         spin_lock_irqsave(&phba->hbalock, flags);
10612
10613         /*
10614          * Error everything on the txq since these iocbs
10615          * have not been given to the FW yet.
10616          * Also issue ABTS for everything on the txcmplq
10617          */
10618         if (phba->sli_rev != LPFC_SLI_REV4) {
10619                 for (i = 0; i < psli->num_rings; i++) {
10620                         pring = &psli->sli3_ring[i];
10621                         prev_pring_flag = pring->flag;
10622                         /* Only slow rings */
10623                         if (pring->ringno == LPFC_ELS_RING) {
10624                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10625                                 /* Set the lpfc data pending flag */
10626                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10627                         }
10628                         list_for_each_entry_safe(iocb, next_iocb,
10629                                                  &pring->txq, list) {
10630                                 if (iocb->vport != vport)
10631                                         continue;
10632                                 list_move_tail(&iocb->list, &completions);
10633                         }
10634                         list_for_each_entry_safe(iocb, next_iocb,
10635                                                  &pring->txcmplq, list) {
10636                                 if (iocb->vport != vport)
10637                                         continue;
10638                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10639                         }
10640                         pring->flag = prev_pring_flag;
10641                 }
10642         } else {
10643                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10644                         pring = qp->pring;
10645                         if (!pring)
10646                                 continue;
10647                         if (pring == phba->sli4_hba.els_wq->pring) {
10648                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10649                                 /* Set the lpfc data pending flag */
10650                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10651                         }
10652                         prev_pring_flag = pring->flag;
10653                         spin_lock_irq(&pring->ring_lock);
10654                         list_for_each_entry_safe(iocb, next_iocb,
10655                                                  &pring->txq, list) {
10656                                 if (iocb->vport != vport)
10657                                         continue;
10658                                 list_move_tail(&iocb->list, &completions);
10659                         }
10660                         spin_unlock_irq(&pring->ring_lock);
10661                         list_for_each_entry_safe(iocb, next_iocb,
10662                                                  &pring->txcmplq, list) {
10663                                 if (iocb->vport != vport)
10664                                         continue;
10665                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10666                         }
10667                         pring->flag = prev_pring_flag;
10668                 }
10669         }
10670         spin_unlock_irqrestore(&phba->hbalock, flags);
10671
10672         /* Cancel all the IOCBs from the completions list */
10673         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10674                               IOERR_SLI_DOWN);
10675         return 1;
10676 }
10677
10678 /**
10679  * lpfc_sli_hba_down - Resource cleanup function for the HBA
10680  * @phba: Pointer to HBA context object.
10681  *
10682  * This function cleans up all iocb, buffers, mailbox commands
10683  * while shutting down the HBA. This function is called with no
10684  * lock held and always returns 1.
10685  * This function does the following to cleanup driver resources:
10686  * - Free discovery resources for each virtual port
10687  * - Cleanup any pending fabric iocbs
10688  * - Iterate through the iocb txq and free each entry
10689  *   in the list.
10690  * - Free up any buffer posted to the HBA
10691  * - Free mailbox commands in the mailbox queue.
10692  **/
10693 int
10694 lpfc_sli_hba_down(struct lpfc_hba *phba)
10695 {
10696         LIST_HEAD(completions);
10697         struct lpfc_sli *psli = &phba->sli;
10698         struct lpfc_queue *qp = NULL;
10699         struct lpfc_sli_ring *pring;
10700         struct lpfc_dmabuf *buf_ptr;
10701         unsigned long flags = 0;
10702         int i;
10703
10704         /* Shutdown the mailbox command sub-system */
10705         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10706
10707         lpfc_hba_down_prep(phba);
10708
10709         /* Disable softirqs, including timers from obtaining phba->hbalock */
10710         local_bh_disable();
10711
10712         lpfc_fabric_abort_hba(phba);
10713
10714         spin_lock_irqsave(&phba->hbalock, flags);
10715
10716         /*
10717          * Error everything on the txq since these iocbs
10718          * have not been given to the FW yet.
10719          */
10720         if (phba->sli_rev != LPFC_SLI_REV4) {
10721                 for (i = 0; i < psli->num_rings; i++) {
10722                         pring = &psli->sli3_ring[i];
10723                         /* Only slow rings */
10724                         if (pring->ringno == LPFC_ELS_RING) {
10725                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10726                                 /* Set the lpfc data pending flag */
10727                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10728                         }
10729                         list_splice_init(&pring->txq, &completions);
10730                 }
10731         } else {
10732                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10733                         pring = qp->pring;
10734                         if (!pring)
10735                                 continue;
10736                         spin_lock_irq(&pring->ring_lock);
10737                         list_splice_init(&pring->txq, &completions);
10738                         spin_unlock_irq(&pring->ring_lock);
10739                         if (pring == phba->sli4_hba.els_wq->pring) {
10740                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10741                                 /* Set the lpfc data pending flag */
10742                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10743                         }
10744                 }
10745         }
10746         spin_unlock_irqrestore(&phba->hbalock, flags);
10747
10748         /* Cancel all the IOCBs from the completions list */
10749         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10750                               IOERR_SLI_DOWN);
10751
10752         spin_lock_irqsave(&phba->hbalock, flags);
10753         list_splice_init(&phba->elsbuf, &completions);
10754         phba->elsbuf_cnt = 0;
10755         phba->elsbuf_prev_cnt = 0;
10756         spin_unlock_irqrestore(&phba->hbalock, flags);
10757
10758         while (!list_empty(&completions)) {
10759                 list_remove_head(&completions, buf_ptr,
10760                         struct lpfc_dmabuf, list);
10761                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10762                 kfree(buf_ptr);
10763         }
10764
10765         /* Enable softirqs again, done with phba->hbalock */
10766         local_bh_enable();
10767
10768         /* Return any active mbox cmds */
10769         del_timer_sync(&psli->mbox_tmo);
10770
10771         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10772         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10773         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10774
10775         return 1;
10776 }
10777
10778 /**
10779  * lpfc_sli_pcimem_bcopy - SLI memory copy function
10780  * @srcp: Source memory pointer.
10781  * @destp: Destination memory pointer.
10782  * @cnt: Number of words required to be copied.
10783  *
10784  * This function is used for copying data between driver memory
10785  * and the SLI memory. This function also changes the endianness
10786  * of each word if native endianness is different from SLI
10787  * endianness. This function can be called with or without
10788  * lock.
10789  **/
10790 void
10791 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10792 {
10793         uint32_t *src = srcp;
10794         uint32_t *dest = destp;
10795         uint32_t ldata;
10796         int i;
10797
10798         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10799                 ldata = *src;
10800                 ldata = le32_to_cpu(ldata);
10801                 *dest = ldata;
10802                 src++;
10803                 dest++;
10804         }
10805 }
10806
10807
10808 /**
10809  * lpfc_sli_bemem_bcopy - SLI memory copy function
10810  * @srcp: Source memory pointer.
10811  * @destp: Destination memory pointer.
10812  * @cnt: Number of words required to be copied.
10813  *
10814  * This function is used for copying data between a data structure
10815  * with big endian representation to local endianness.
10816  * This function can be called with or without lock.
10817  **/
10818 void
10819 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10820 {
10821         uint32_t *src = srcp;
10822         uint32_t *dest = destp;
10823         uint32_t ldata;
10824         int i;
10825
10826         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10827                 ldata = *src;
10828                 ldata = be32_to_cpu(ldata);
10829                 *dest = ldata;
10830                 src++;
10831                 dest++;
10832         }
10833 }
10834
10835 /**
10836  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10837  * @phba: Pointer to HBA context object.
10838  * @pring: Pointer to driver SLI ring object.
10839  * @mp: Pointer to driver buffer object.
10840  *
10841  * This function is called with no lock held.
10842  * It always return zero after adding the buffer to the postbufq
10843  * buffer list.
10844  **/
10845 int
10846 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10847                          struct lpfc_dmabuf *mp)
10848 {
10849         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10850            later */
10851         spin_lock_irq(&phba->hbalock);
10852         list_add_tail(&mp->list, &pring->postbufq);
10853         pring->postbufq_cnt++;
10854         spin_unlock_irq(&phba->hbalock);
10855         return 0;
10856 }
10857
10858 /**
10859  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10860  * @phba: Pointer to HBA context object.
10861  *
10862  * When HBQ is enabled, buffers are searched based on tags. This function
10863  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10864  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10865  * does not conflict with tags of buffer posted for unsolicited events.
10866  * The function returns the allocated tag. The function is called with
10867  * no locks held.
10868  **/
10869 uint32_t
10870 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10871 {
10872         spin_lock_irq(&phba->hbalock);
10873         phba->buffer_tag_count++;
10874         /*
10875          * Always set the QUE_BUFTAG_BIT to distiguish between
10876          * a tag assigned by HBQ.
10877          */
10878         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10879         spin_unlock_irq(&phba->hbalock);
10880         return phba->buffer_tag_count;
10881 }
10882
10883 /**
10884  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10885  * @phba: Pointer to HBA context object.
10886  * @pring: Pointer to driver SLI ring object.
10887  * @tag: Buffer tag.
10888  *
10889  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10890  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10891  * iocb is posted to the response ring with the tag of the buffer.
10892  * This function searches the pring->postbufq list using the tag
10893  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10894  * iocb. If the buffer is found then lpfc_dmabuf object of the
10895  * buffer is returned to the caller else NULL is returned.
10896  * This function is called with no lock held.
10897  **/
10898 struct lpfc_dmabuf *
10899 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10900                         uint32_t tag)
10901 {
10902         struct lpfc_dmabuf *mp, *next_mp;
10903         struct list_head *slp = &pring->postbufq;
10904
10905         /* Search postbufq, from the beginning, looking for a match on tag */
10906         spin_lock_irq(&phba->hbalock);
10907         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10908                 if (mp->buffer_tag == tag) {
10909                         list_del_init(&mp->list);
10910                         pring->postbufq_cnt--;
10911                         spin_unlock_irq(&phba->hbalock);
10912                         return mp;
10913                 }
10914         }
10915
10916         spin_unlock_irq(&phba->hbalock);
10917         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10918                         "0402 Cannot find virtual addr for buffer tag on "
10919                         "ring %d Data x%lx x%p x%p x%x\n",
10920                         pring->ringno, (unsigned long) tag,
10921                         slp->next, slp->prev, pring->postbufq_cnt);
10922
10923         return NULL;
10924 }
10925
10926 /**
10927  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10928  * @phba: Pointer to HBA context object.
10929  * @pring: Pointer to driver SLI ring object.
10930  * @phys: DMA address of the buffer.
10931  *
10932  * This function searches the buffer list using the dma_address
10933  * of unsolicited event to find the driver's lpfc_dmabuf object
10934  * corresponding to the dma_address. The function returns the
10935  * lpfc_dmabuf object if a buffer is found else it returns NULL.
10936  * This function is called by the ct and els unsolicited event
10937  * handlers to get the buffer associated with the unsolicited
10938  * event.
10939  *
10940  * This function is called with no lock held.
10941  **/
10942 struct lpfc_dmabuf *
10943 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10944                          dma_addr_t phys)
10945 {
10946         struct lpfc_dmabuf *mp, *next_mp;
10947         struct list_head *slp = &pring->postbufq;
10948
10949         /* Search postbufq, from the beginning, looking for a match on phys */
10950         spin_lock_irq(&phba->hbalock);
10951         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10952                 if (mp->phys == phys) {
10953                         list_del_init(&mp->list);
10954                         pring->postbufq_cnt--;
10955                         spin_unlock_irq(&phba->hbalock);
10956                         return mp;
10957                 }
10958         }
10959
10960         spin_unlock_irq(&phba->hbalock);
10961         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10962                         "0410 Cannot find virtual addr for mapped buf on "
10963                         "ring %d Data x%llx x%p x%p x%x\n",
10964                         pring->ringno, (unsigned long long)phys,
10965                         slp->next, slp->prev, pring->postbufq_cnt);
10966         return NULL;
10967 }
10968
10969 /**
10970  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
10971  * @phba: Pointer to HBA context object.
10972  * @cmdiocb: Pointer to driver command iocb object.
10973  * @rspiocb: Pointer to driver response iocb object.
10974  *
10975  * This function is the completion handler for the abort iocbs for
10976  * ELS commands. This function is called from the ELS ring event
10977  * handler with no lock held. This function frees memory resources
10978  * associated with the abort iocb.
10979  **/
10980 static void
10981 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10982                         struct lpfc_iocbq *rspiocb)
10983 {
10984         IOCB_t *irsp = &rspiocb->iocb;
10985         uint16_t abort_iotag, abort_context;
10986         struct lpfc_iocbq *abort_iocb = NULL;
10987
10988         if (irsp->ulpStatus) {
10989
10990                 /*
10991                  * Assume that the port already completed and returned, or
10992                  * will return the iocb. Just Log the message.
10993                  */
10994                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10995                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10996
10997                 spin_lock_irq(&phba->hbalock);
10998                 if (phba->sli_rev < LPFC_SLI_REV4) {
10999                         if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11000                             irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11001                             irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11002                                 spin_unlock_irq(&phba->hbalock);
11003                                 goto release_iocb;
11004                         }
11005                         if (abort_iotag != 0 &&
11006                                 abort_iotag <= phba->sli.last_iotag)
11007                                 abort_iocb =
11008                                         phba->sli.iocbq_lookup[abort_iotag];
11009                 } else
11010                         /* For sli4 the abort_tag is the XRI,
11011                          * so the abort routine puts the iotag  of the iocb
11012                          * being aborted in the context field of the abort
11013                          * IOCB.
11014                          */
11015                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
11016
11017                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11018                                 "0327 Cannot abort els iocb %p "
11019                                 "with tag %x context %x, abort status %x, "
11020                                 "abort code %x\n",
11021                                 abort_iocb, abort_iotag, abort_context,
11022                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
11023
11024                 spin_unlock_irq(&phba->hbalock);
11025         }
11026 release_iocb:
11027         lpfc_sli_release_iocbq(phba, cmdiocb);
11028         return;
11029 }
11030
11031 /**
11032  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
11033  * @phba: Pointer to HBA context object.
11034  * @cmdiocb: Pointer to driver command iocb object.
11035  * @rspiocb: Pointer to driver response iocb object.
11036  *
11037  * The function is called from SLI ring event handler with no
11038  * lock held. This function is the completion handler for ELS commands
11039  * which are aborted. The function frees memory resources used for
11040  * the aborted ELS commands.
11041  **/
11042 static void
11043 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11044                      struct lpfc_iocbq *rspiocb)
11045 {
11046         IOCB_t *irsp = &rspiocb->iocb;
11047
11048         /* ELS cmd tag <ulpIoTag> completes */
11049         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11050                         "0139 Ignoring ELS cmd tag x%x completion Data: "
11051                         "x%x x%x x%x\n",
11052                         irsp->ulpIoTag, irsp->ulpStatus,
11053                         irsp->un.ulpWord[4], irsp->ulpTimeout);
11054         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11055                 lpfc_ct_free_iocb(phba, cmdiocb);
11056         else
11057                 lpfc_els_free_iocb(phba, cmdiocb);
11058         return;
11059 }
11060
11061 /**
11062  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
11063  * @phba: Pointer to HBA context object.
11064  * @pring: Pointer to driver SLI ring object.
11065  * @cmdiocb: Pointer to driver command iocb object.
11066  *
11067  * This function issues an abort iocb for the provided command iocb down to
11068  * the port. Other than the case the outstanding command iocb is an abort
11069  * request, this function issues abort out unconditionally. This function is
11070  * called with hbalock held. The function returns 0 when it fails due to
11071  * memory allocation failure or when the command iocb is an abort request.
11072  **/
11073 static int
11074 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11075                            struct lpfc_iocbq *cmdiocb)
11076 {
11077         struct lpfc_vport *vport = cmdiocb->vport;
11078         struct lpfc_iocbq *abtsiocbp;
11079         IOCB_t *icmd = NULL;
11080         IOCB_t *iabt = NULL;
11081         int retval;
11082         unsigned long iflags;
11083         struct lpfc_nodelist *ndlp;
11084
11085         lockdep_assert_held(&phba->hbalock);
11086
11087         /*
11088          * There are certain command types we don't want to abort.  And we
11089          * don't want to abort commands that are already in the process of
11090          * being aborted.
11091          */
11092         icmd = &cmdiocb->iocb;
11093         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11094             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11095             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11096                 return 0;
11097
11098         /* issue ABTS for this IOCB based on iotag */
11099         abtsiocbp = __lpfc_sli_get_iocbq(phba);
11100         if (abtsiocbp == NULL)
11101                 return 0;
11102
11103         /* This signals the response to set the correct status
11104          * before calling the completion handler
11105          */
11106         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11107
11108         iabt = &abtsiocbp->iocb;
11109         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11110         iabt->un.acxri.abortContextTag = icmd->ulpContext;
11111         if (phba->sli_rev == LPFC_SLI_REV4) {
11112                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
11113                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
11114         } else {
11115                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
11116                 if (pring->ringno == LPFC_ELS_RING) {
11117                         ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11118                         iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11119                 }
11120         }
11121         iabt->ulpLe = 1;
11122         iabt->ulpClass = icmd->ulpClass;
11123
11124         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11125         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
11126         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11127                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
11128         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11129                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
11130
11131         if (phba->link_state >= LPFC_LINK_UP)
11132                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11133         else
11134                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11135
11136         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
11137         abtsiocbp->vport = vport;
11138
11139         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11140                          "0339 Abort xri x%x, original iotag x%x, "
11141                          "abort cmd iotag x%x\n",
11142                          iabt->un.acxri.abortIoTag,
11143                          iabt->un.acxri.abortContextTag,
11144                          abtsiocbp->iotag);
11145
11146         if (phba->sli_rev == LPFC_SLI_REV4) {
11147                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11148                 if (unlikely(pring == NULL))
11149                         return 0;
11150                 /* Note: both hbalock and ring_lock need to be set here */
11151                 spin_lock_irqsave(&pring->ring_lock, iflags);
11152                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11153                         abtsiocbp, 0);
11154                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11155         } else {
11156                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11157                         abtsiocbp, 0);
11158         }
11159
11160         if (retval)
11161                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
11162
11163         /*
11164          * Caller to this routine should check for IOCB_ERROR
11165          * and handle it properly.  This routine no longer removes
11166          * iocb off txcmplq and call compl in case of IOCB_ERROR.
11167          */
11168         return retval;
11169 }
11170
11171 /**
11172  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11173  * @phba: Pointer to HBA context object.
11174  * @pring: Pointer to driver SLI ring object.
11175  * @cmdiocb: Pointer to driver command iocb object.
11176  *
11177  * This function issues an abort iocb for the provided command iocb. In case
11178  * of unloading, the abort iocb will not be issued to commands on the ELS
11179  * ring. Instead, the callback function shall be changed to those commands
11180  * so that nothing happens when them finishes. This function is called with
11181  * hbalock held. The function returns 0 when the command iocb is an abort
11182  * request.
11183  **/
11184 int
11185 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11186                            struct lpfc_iocbq *cmdiocb)
11187 {
11188         struct lpfc_vport *vport = cmdiocb->vport;
11189         int retval = IOCB_ERROR;
11190         IOCB_t *icmd = NULL;
11191
11192         lockdep_assert_held(&phba->hbalock);
11193
11194         /*
11195          * There are certain command types we don't want to abort.  And we
11196          * don't want to abort commands that are already in the process of
11197          * being aborted.
11198          */
11199         icmd = &cmdiocb->iocb;
11200         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11201             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11202             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11203                 return 0;
11204
11205         if (!pring) {
11206                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11207                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11208                 else
11209                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11210                 goto abort_iotag_exit;
11211         }
11212
11213         /*
11214          * If we're unloading, don't abort iocb on the ELS ring, but change
11215          * the callback so that nothing happens when it finishes.
11216          */
11217         if ((vport->load_flag & FC_UNLOADING) &&
11218             (pring->ringno == LPFC_ELS_RING)) {
11219                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11220                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11221                 else
11222                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11223                 goto abort_iotag_exit;
11224         }
11225
11226         /* Now, we try to issue the abort to the cmdiocb out */
11227         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11228
11229 abort_iotag_exit:
11230         /*
11231          * Caller to this routine should check for IOCB_ERROR
11232          * and handle it properly.  This routine no longer removes
11233          * iocb off txcmplq and call compl in case of IOCB_ERROR.
11234          */
11235         return retval;
11236 }
11237
11238 /**
11239  * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11240  * @phba: Pointer to HBA context object.
11241  * @pring: Pointer to driver SLI ring object.
11242  * @cmdiocb: Pointer to driver command iocb object.
11243  *
11244  * This function issues an abort iocb for the provided command iocb down to
11245  * the port. Other than the case the outstanding command iocb is an abort
11246  * request, this function issues abort out unconditionally. This function is
11247  * called with hbalock held. The function returns 0 when it fails due to
11248  * memory allocation failure or when the command iocb is an abort request.
11249  **/
11250 static int
11251 lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11252                         struct lpfc_iocbq *cmdiocb)
11253 {
11254         struct lpfc_vport *vport = cmdiocb->vport;
11255         struct lpfc_iocbq *abtsiocbp;
11256         union lpfc_wqe128 *abts_wqe;
11257         int retval;
11258
11259         /*
11260          * There are certain command types we don't want to abort.  And we
11261          * don't want to abort commands that are already in the process of
11262          * being aborted.
11263          */
11264         if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11265             cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11266             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11267                 return 0;
11268
11269         /* issue ABTS for this io based on iotag */
11270         abtsiocbp = __lpfc_sli_get_iocbq(phba);
11271         if (abtsiocbp == NULL)
11272                 return 0;
11273
11274         /* This signals the response to set the correct status
11275          * before calling the completion handler
11276          */
11277         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11278
11279         /* Complete prepping the abort wqe and issue to the FW. */
11280         abts_wqe = &abtsiocbp->wqe;
11281         bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
11282         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11283
11284         /* Explicitly set reserved fields to zero.*/
11285         abts_wqe->abort_cmd.rsrvd4 = 0;
11286         abts_wqe->abort_cmd.rsrvd5 = 0;
11287
11288         /* WQE Common - word 6.  Context is XRI tag.  Set 0. */
11289         bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
11290         bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
11291
11292         /* word 7 */
11293         bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
11294         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11295         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11296                cmdiocb->iocb.ulpClass);
11297
11298         /* word 8 - tell the FW to abort the IO associated with this
11299          * outstanding exchange ID.
11300          */
11301         abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11302
11303         /* word 9 - this is the iotag for the abts_wqe completion. */
11304         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11305                abtsiocbp->iotag);
11306
11307         /* word 10 */
11308         bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11309         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11310         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11311
11312         /* word 11 */
11313         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11314         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11315         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11316
11317         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11318         abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11319         abtsiocbp->vport = vport;
11320         abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
11321         retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
11322         if (retval) {
11323                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11324                                  "6147 Failed abts issue_wqe with status x%x "
11325                                  "for oxid x%x\n",
11326                                  retval, cmdiocb->sli4_xritag);
11327                 lpfc_sli_release_iocbq(phba, abtsiocbp);
11328                 return retval;
11329         }
11330
11331         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11332                          "6148 Drv Abort NVME Request Issued for "
11333                          "ox_id x%x on reqtag x%x\n",
11334                          cmdiocb->sli4_xritag,
11335                          abtsiocbp->iotag);
11336
11337         return retval;
11338 }
11339
11340 /**
11341  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11342  * @phba: pointer to lpfc HBA data structure.
11343  *
11344  * This routine will abort all pending and outstanding iocbs to an HBA.
11345  **/
11346 void
11347 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11348 {
11349         struct lpfc_sli *psli = &phba->sli;
11350         struct lpfc_sli_ring *pring;
11351         struct lpfc_queue *qp = NULL;
11352         int i;
11353
11354         if (phba->sli_rev != LPFC_SLI_REV4) {
11355                 for (i = 0; i < psli->num_rings; i++) {
11356                         pring = &psli->sli3_ring[i];
11357                         lpfc_sli_abort_iocb_ring(phba, pring);
11358                 }
11359                 return;
11360         }
11361         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11362                 pring = qp->pring;
11363                 if (!pring)
11364                         continue;
11365                 lpfc_sli_abort_iocb_ring(phba, pring);
11366         }
11367 }
11368
11369 /**
11370  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
11371  * @iocbq: Pointer to driver iocb object.
11372  * @vport: Pointer to driver virtual port object.
11373  * @tgt_id: SCSI ID of the target.
11374  * @lun_id: LUN ID of the scsi device.
11375  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11376  *
11377  * This function acts as an iocb filter for functions which abort or count
11378  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11379  * 0 if the filtering criteria is met for the given iocb and will return
11380  * 1 if the filtering criteria is not met.
11381  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11382  * given iocb is for the SCSI device specified by vport, tgt_id and
11383  * lun_id parameter.
11384  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
11385  * given iocb is for the SCSI target specified by vport and tgt_id
11386  * parameters.
11387  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11388  * given iocb is for the SCSI host associated with the given vport.
11389  * This function is called with no locks held.
11390  **/
11391 static int
11392 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11393                            uint16_t tgt_id, uint64_t lun_id,
11394                            lpfc_ctx_cmd ctx_cmd)
11395 {
11396         struct lpfc_scsi_buf *lpfc_cmd;
11397         int rc = 1;
11398
11399         if (iocbq->vport != vport)
11400                 return rc;
11401
11402         if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
11403             !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
11404                 return rc;
11405
11406         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11407
11408         if (lpfc_cmd->pCmd == NULL)
11409                 return rc;
11410
11411         switch (ctx_cmd) {
11412         case LPFC_CTX_LUN:
11413                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11414                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11415                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
11416                         rc = 0;
11417                 break;
11418         case LPFC_CTX_TGT:
11419                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11420                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
11421                         rc = 0;
11422                 break;
11423         case LPFC_CTX_HOST:
11424                 rc = 0;
11425                 break;
11426         default:
11427                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
11428                         __func__, ctx_cmd);
11429                 break;
11430         }
11431
11432         return rc;
11433 }
11434
11435 /**
11436  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
11437  * @vport: Pointer to virtual port.
11438  * @tgt_id: SCSI ID of the target.
11439  * @lun_id: LUN ID of the scsi device.
11440  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11441  *
11442  * This function returns number of FCP commands pending for the vport.
11443  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11444  * commands pending on the vport associated with SCSI device specified
11445  * by tgt_id and lun_id parameters.
11446  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11447  * commands pending on the vport associated with SCSI target specified
11448  * by tgt_id parameter.
11449  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11450  * commands pending on the vport.
11451  * This function returns the number of iocbs which satisfy the filter.
11452  * This function is called without any lock held.
11453  **/
11454 int
11455 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11456                   lpfc_ctx_cmd ctx_cmd)
11457 {
11458         struct lpfc_hba *phba = vport->phba;
11459         struct lpfc_iocbq *iocbq;
11460         int sum, i;
11461
11462         spin_lock_irq(&phba->hbalock);
11463         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11464                 iocbq = phba->sli.iocbq_lookup[i];
11465
11466                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11467                                                 ctx_cmd) == 0)
11468                         sum++;
11469         }
11470         spin_unlock_irq(&phba->hbalock);
11471
11472         return sum;
11473 }
11474
11475 /**
11476  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11477  * @phba: Pointer to HBA context object
11478  * @cmdiocb: Pointer to command iocb object.
11479  * @rspiocb: Pointer to response iocb object.
11480  *
11481  * This function is called when an aborted FCP iocb completes. This
11482  * function is called by the ring event handler with no lock held.
11483  * This function frees the iocb.
11484  **/
11485 void
11486 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11487                         struct lpfc_iocbq *rspiocb)
11488 {
11489         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11490                         "3096 ABORT_XRI_CN completing on rpi x%x "
11491                         "original iotag x%x, abort cmd iotag x%x "
11492                         "status 0x%x, reason 0x%x\n",
11493                         cmdiocb->iocb.un.acxri.abortContextTag,
11494                         cmdiocb->iocb.un.acxri.abortIoTag,
11495                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11496                         rspiocb->iocb.un.ulpWord[4]);
11497         lpfc_sli_release_iocbq(phba, cmdiocb);
11498         return;
11499 }
11500
11501 /**
11502  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
11503  * @vport: Pointer to virtual port.
11504  * @pring: Pointer to driver SLI ring object.
11505  * @tgt_id: SCSI ID of the target.
11506  * @lun_id: LUN ID of the scsi device.
11507  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11508  *
11509  * This function sends an abort command for every SCSI command
11510  * associated with the given virtual port pending on the ring
11511  * filtered by lpfc_sli_validate_fcp_iocb function.
11512  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11513  * FCP iocbs associated with lun specified by tgt_id and lun_id
11514  * parameters
11515  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11516  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11517  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11518  * FCP iocbs associated with virtual port.
11519  * This function returns number of iocbs it failed to abort.
11520  * This function is called with no locks held.
11521  **/
11522 int
11523 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11524                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
11525 {
11526         struct lpfc_hba *phba = vport->phba;
11527         struct lpfc_iocbq *iocbq;
11528         struct lpfc_iocbq *abtsiocb;
11529         struct lpfc_sli_ring *pring_s4;
11530         IOCB_t *cmd = NULL;
11531         int errcnt = 0, ret_val = 0;
11532         int i;
11533
11534         /* all I/Os are in process of being flushed */
11535         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11536                 return errcnt;
11537
11538         for (i = 1; i <= phba->sli.last_iotag; i++) {
11539                 iocbq = phba->sli.iocbq_lookup[i];
11540
11541                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11542                                                abort_cmd) != 0)
11543                         continue;
11544
11545                 /*
11546                  * If the iocbq is already being aborted, don't take a second
11547                  * action, but do count it.
11548                  */
11549                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11550                         continue;
11551
11552                 /* issue ABTS for this IOCB based on iotag */
11553                 abtsiocb = lpfc_sli_get_iocbq(phba);
11554                 if (abtsiocb == NULL) {
11555                         errcnt++;
11556                         continue;
11557                 }
11558
11559                 /* indicate the IO is being aborted by the driver. */
11560                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11561
11562                 cmd = &iocbq->iocb;
11563                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11564                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
11565                 if (phba->sli_rev == LPFC_SLI_REV4)
11566                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11567                 else
11568                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
11569                 abtsiocb->iocb.ulpLe = 1;
11570                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
11571                 abtsiocb->vport = vport;
11572
11573                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11574                 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
11575                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11576                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
11577                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11578                         abtsiocb->iocb_flag |= LPFC_IO_FOF;
11579
11580                 if (lpfc_is_link_up(phba))
11581                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11582                 else
11583                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11584
11585                 /* Setup callback routine and issue the command. */
11586                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11587                 if (phba->sli_rev == LPFC_SLI_REV4) {
11588                         pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11589                         if (!pring_s4)
11590                                 continue;
11591                         ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11592                                                       abtsiocb, 0);
11593                 } else
11594                         ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11595                                                       abtsiocb, 0);
11596                 if (ret_val == IOCB_ERROR) {
11597                         lpfc_sli_release_iocbq(phba, abtsiocb);
11598                         errcnt++;
11599                         continue;
11600                 }
11601         }
11602
11603         return errcnt;
11604 }
11605
11606 /**
11607  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11608  * @vport: Pointer to virtual port.
11609  * @pring: Pointer to driver SLI ring object.
11610  * @tgt_id: SCSI ID of the target.
11611  * @lun_id: LUN ID of the scsi device.
11612  * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11613  *
11614  * This function sends an abort command for every SCSI command
11615  * associated with the given virtual port pending on the ring
11616  * filtered by lpfc_sli_validate_fcp_iocb function.
11617  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11618  * FCP iocbs associated with lun specified by tgt_id and lun_id
11619  * parameters
11620  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11621  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11622  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11623  * FCP iocbs associated with virtual port.
11624  * This function returns number of iocbs it aborted .
11625  * This function is called with no locks held right after a taskmgmt
11626  * command is sent.
11627  **/
11628 int
11629 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11630                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11631 {
11632         struct lpfc_hba *phba = vport->phba;
11633         struct lpfc_scsi_buf *lpfc_cmd;
11634         struct lpfc_iocbq *abtsiocbq;
11635         struct lpfc_nodelist *ndlp;
11636         struct lpfc_iocbq *iocbq;
11637         IOCB_t *icmd;
11638         int sum, i, ret_val;
11639         unsigned long iflags;
11640         struct lpfc_sli_ring *pring_s4;
11641
11642         spin_lock_irqsave(&phba->hbalock, iflags);
11643
11644         /* all I/Os are in process of being flushed */
11645         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11646                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11647                 return 0;
11648         }
11649         sum = 0;
11650
11651         for (i = 1; i <= phba->sli.last_iotag; i++) {
11652                 iocbq = phba->sli.iocbq_lookup[i];
11653
11654                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11655                                                cmd) != 0)
11656                         continue;
11657
11658                 /*
11659                  * If the iocbq is already being aborted, don't take a second
11660                  * action, but do count it.
11661                  */
11662                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11663                         continue;
11664
11665                 /* issue ABTS for this IOCB based on iotag */
11666                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11667                 if (abtsiocbq == NULL)
11668                         continue;
11669
11670                 icmd = &iocbq->iocb;
11671                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11672                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11673                 if (phba->sli_rev == LPFC_SLI_REV4)
11674                         abtsiocbq->iocb.un.acxri.abortIoTag =
11675                                                          iocbq->sli4_xritag;
11676                 else
11677                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11678                 abtsiocbq->iocb.ulpLe = 1;
11679                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11680                 abtsiocbq->vport = vport;
11681
11682                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11683                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11684                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11685                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11686                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11687                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11688
11689                 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11690                 ndlp = lpfc_cmd->rdata->pnode;
11691
11692                 if (lpfc_is_link_up(phba) &&
11693                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11694                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11695                 else
11696                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11697
11698                 /* Setup callback routine and issue the command. */
11699                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11700
11701                 /*
11702                  * Indicate the IO is being aborted by the driver and set
11703                  * the caller's flag into the aborted IO.
11704                  */
11705                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11706
11707                 if (phba->sli_rev == LPFC_SLI_REV4) {
11708                         pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11709                         if (!pring_s4)
11710                                 continue;
11711                         /* Note: both hbalock and ring_lock must be set here */
11712                         spin_lock(&pring_s4->ring_lock);
11713                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11714                                                         abtsiocbq, 0);
11715                         spin_unlock(&pring_s4->ring_lock);
11716                 } else {
11717                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11718                                                         abtsiocbq, 0);
11719                 }
11720
11721
11722                 if (ret_val == IOCB_ERROR)
11723                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
11724                 else
11725                         sum++;
11726         }
11727         spin_unlock_irqrestore(&phba->hbalock, iflags);
11728         return sum;
11729 }
11730
11731 /**
11732  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11733  * @phba: Pointer to HBA context object.
11734  * @cmdiocbq: Pointer to command iocb.
11735  * @rspiocbq: Pointer to response iocb.
11736  *
11737  * This function is the completion handler for iocbs issued using
11738  * lpfc_sli_issue_iocb_wait function. This function is called by the
11739  * ring event handler function without any lock held. This function
11740  * can be called from both worker thread context and interrupt
11741  * context. This function also can be called from other thread which
11742  * cleans up the SLI layer objects.
11743  * This function copy the contents of the response iocb to the
11744  * response iocb memory object provided by the caller of
11745  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11746  * sleeps for the iocb completion.
11747  **/
11748 static void
11749 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11750                         struct lpfc_iocbq *cmdiocbq,
11751                         struct lpfc_iocbq *rspiocbq)
11752 {
11753         wait_queue_head_t *pdone_q;
11754         unsigned long iflags;
11755         struct lpfc_scsi_buf *lpfc_cmd;
11756
11757         spin_lock_irqsave(&phba->hbalock, iflags);
11758         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11759
11760                 /*
11761                  * A time out has occurred for the iocb.  If a time out
11762                  * completion handler has been supplied, call it.  Otherwise,
11763                  * just free the iocbq.
11764                  */
11765
11766                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11767                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11768                 cmdiocbq->wait_iocb_cmpl = NULL;
11769                 if (cmdiocbq->iocb_cmpl)
11770                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11771                 else
11772                         lpfc_sli_release_iocbq(phba, cmdiocbq);
11773                 return;
11774         }
11775
11776         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11777         if (cmdiocbq->context2 && rspiocbq)
11778                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11779                        &rspiocbq->iocb, sizeof(IOCB_t));
11780
11781         /* Set the exchange busy flag for task management commands */
11782         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11783                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11784                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11785                         cur_iocbq);
11786                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11787         }
11788
11789         pdone_q = cmdiocbq->context_un.wait_queue;
11790         if (pdone_q)
11791                 wake_up(pdone_q);
11792         spin_unlock_irqrestore(&phba->hbalock, iflags);
11793         return;
11794 }
11795
11796 /**
11797  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11798  * @phba: Pointer to HBA context object..
11799  * @piocbq: Pointer to command iocb.
11800  * @flag: Flag to test.
11801  *
11802  * This routine grabs the hbalock and then test the iocb_flag to
11803  * see if the passed in flag is set.
11804  * Returns:
11805  * 1 if flag is set.
11806  * 0 if flag is not set.
11807  **/
11808 static int
11809 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11810                  struct lpfc_iocbq *piocbq, uint32_t flag)
11811 {
11812         unsigned long iflags;
11813         int ret;
11814
11815         spin_lock_irqsave(&phba->hbalock, iflags);
11816         ret = piocbq->iocb_flag & flag;
11817         spin_unlock_irqrestore(&phba->hbalock, iflags);
11818         return ret;
11819
11820 }
11821
11822 /**
11823  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11824  * @phba: Pointer to HBA context object..
11825  * @pring: Pointer to sli ring.
11826  * @piocb: Pointer to command iocb.
11827  * @prspiocbq: Pointer to response iocb.
11828  * @timeout: Timeout in number of seconds.
11829  *
11830  * This function issues the iocb to firmware and waits for the
11831  * iocb to complete. The iocb_cmpl field of the shall be used
11832  * to handle iocbs which time out. If the field is NULL, the
11833  * function shall free the iocbq structure.  If more clean up is
11834  * needed, the caller is expected to provide a completion function
11835  * that will provide the needed clean up.  If the iocb command is
11836  * not completed within timeout seconds, the function will either
11837  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11838  * completion function set in the iocb_cmpl field and then return
11839  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
11840  * resources if this function returns IOCB_TIMEDOUT.
11841  * The function waits for the iocb completion using an
11842  * non-interruptible wait.
11843  * This function will sleep while waiting for iocb completion.
11844  * So, this function should not be called from any context which
11845  * does not allow sleeping. Due to the same reason, this function
11846  * cannot be called with interrupt disabled.
11847  * This function assumes that the iocb completions occur while
11848  * this function sleep. So, this function cannot be called from
11849  * the thread which process iocb completion for this ring.
11850  * This function clears the iocb_flag of the iocb object before
11851  * issuing the iocb and the iocb completion handler sets this
11852  * flag and wakes this thread when the iocb completes.
11853  * The contents of the response iocb will be copied to prspiocbq
11854  * by the completion handler when the command completes.
11855  * This function returns IOCB_SUCCESS when success.
11856  * This function is called with no lock held.
11857  **/
11858 int
11859 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11860                          uint32_t ring_number,
11861                          struct lpfc_iocbq *piocb,
11862                          struct lpfc_iocbq *prspiocbq,
11863                          uint32_t timeout)
11864 {
11865         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11866         long timeleft, timeout_req = 0;
11867         int retval = IOCB_SUCCESS;
11868         uint32_t creg_val;
11869         struct lpfc_iocbq *iocb;
11870         int txq_cnt = 0;
11871         int txcmplq_cnt = 0;
11872         struct lpfc_sli_ring *pring;
11873         unsigned long iflags;
11874         bool iocb_completed = true;
11875
11876         if (phba->sli_rev >= LPFC_SLI_REV4)
11877                 pring = lpfc_sli4_calc_ring(phba, piocb);
11878         else
11879                 pring = &phba->sli.sli3_ring[ring_number];
11880         /*
11881          * If the caller has provided a response iocbq buffer, then context2
11882          * is NULL or its an error.
11883          */
11884         if (prspiocbq) {
11885                 if (piocb->context2)
11886                         return IOCB_ERROR;
11887                 piocb->context2 = prspiocbq;
11888         }
11889
11890         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11891         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11892         piocb->context_un.wait_queue = &done_q;
11893         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11894
11895         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11896                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11897                         return IOCB_ERROR;
11898                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11899                 writel(creg_val, phba->HCregaddr);
11900                 readl(phba->HCregaddr); /* flush */
11901         }
11902
11903         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11904                                      SLI_IOCB_RET_IOCB);
11905         if (retval == IOCB_SUCCESS) {
11906                 timeout_req = msecs_to_jiffies(timeout * 1000);
11907                 timeleft = wait_event_timeout(done_q,
11908                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11909                                 timeout_req);
11910                 spin_lock_irqsave(&phba->hbalock, iflags);
11911                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11912
11913                         /*
11914                          * IOCB timed out.  Inform the wake iocb wait
11915                          * completion function and set local status
11916                          */
11917
11918                         iocb_completed = false;
11919                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11920                 }
11921                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11922                 if (iocb_completed) {
11923                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11924                                         "0331 IOCB wake signaled\n");
11925                         /* Note: we are not indicating if the IOCB has a success
11926                          * status or not - that's for the caller to check.
11927                          * IOCB_SUCCESS means just that the command was sent and
11928                          * completed. Not that it completed successfully.
11929                          * */
11930                 } else if (timeleft == 0) {
11931                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11932                                         "0338 IOCB wait timeout error - no "
11933                                         "wake response Data x%x\n", timeout);
11934                         retval = IOCB_TIMEDOUT;
11935                 } else {
11936                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11937                                         "0330 IOCB wake NOT set, "
11938                                         "Data x%x x%lx\n",
11939                                         timeout, (timeleft / jiffies));
11940                         retval = IOCB_TIMEDOUT;
11941                 }
11942         } else if (retval == IOCB_BUSY) {
11943                 if (phba->cfg_log_verbose & LOG_SLI) {
11944                         list_for_each_entry(iocb, &pring->txq, list) {
11945                                 txq_cnt++;
11946                         }
11947                         list_for_each_entry(iocb, &pring->txcmplq, list) {
11948                                 txcmplq_cnt++;
11949                         }
11950                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11951                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11952                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11953                 }
11954                 return retval;
11955         } else {
11956                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11957                                 "0332 IOCB wait issue failed, Data x%x\n",
11958                                 retval);
11959                 retval = IOCB_ERROR;
11960         }
11961
11962         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11963                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11964                         return IOCB_ERROR;
11965                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11966                 writel(creg_val, phba->HCregaddr);
11967                 readl(phba->HCregaddr); /* flush */
11968         }
11969
11970         if (prspiocbq)
11971                 piocb->context2 = NULL;
11972
11973         piocb->context_un.wait_queue = NULL;
11974         piocb->iocb_cmpl = NULL;
11975         return retval;
11976 }
11977
11978 /**
11979  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
11980  * @phba: Pointer to HBA context object.
11981  * @pmboxq: Pointer to driver mailbox object.
11982  * @timeout: Timeout in number of seconds.
11983  *
11984  * This function issues the mailbox to firmware and waits for the
11985  * mailbox command to complete. If the mailbox command is not
11986  * completed within timeout seconds, it returns MBX_TIMEOUT.
11987  * The function waits for the mailbox completion using an
11988  * interruptible wait. If the thread is woken up due to a
11989  * signal, MBX_TIMEOUT error is returned to the caller. Caller
11990  * should not free the mailbox resources, if this function returns
11991  * MBX_TIMEOUT.
11992  * This function will sleep while waiting for mailbox completion.
11993  * So, this function should not be called from any context which
11994  * does not allow sleeping. Due to the same reason, this function
11995  * cannot be called with interrupt disabled.
11996  * This function assumes that the mailbox completion occurs while
11997  * this function sleep. So, this function cannot be called from
11998  * the worker thread which processes mailbox completion.
11999  * This function is called in the context of HBA management
12000  * applications.
12001  * This function returns MBX_SUCCESS when successful.
12002  * This function is called with no lock held.
12003  **/
12004 int
12005 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
12006                          uint32_t timeout)
12007 {
12008         struct completion mbox_done;
12009         int retval;
12010         unsigned long flag;
12011
12012         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
12013         /* setup wake call as IOCB callback */
12014         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
12015
12016         /* setup context3 field to pass wait_queue pointer to wake function  */
12017         init_completion(&mbox_done);
12018         pmboxq->context3 = &mbox_done;
12019         /* now issue the command */
12020         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
12021         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
12022                 wait_for_completion_timeout(&mbox_done,
12023                                             msecs_to_jiffies(timeout * 1000));
12024
12025                 spin_lock_irqsave(&phba->hbalock, flag);
12026                 pmboxq->context3 = NULL;
12027                 /*
12028                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
12029                  * else do not free the resources.
12030                  */
12031                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
12032                         retval = MBX_SUCCESS;
12033                 } else {
12034                         retval = MBX_TIMEOUT;
12035                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12036                 }
12037                 spin_unlock_irqrestore(&phba->hbalock, flag);
12038         }
12039         return retval;
12040 }
12041
12042 /**
12043  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
12044  * @phba: Pointer to HBA context.
12045  *
12046  * This function is called to shutdown the driver's mailbox sub-system.
12047  * It first marks the mailbox sub-system is in a block state to prevent
12048  * the asynchronous mailbox command from issued off the pending mailbox
12049  * command queue. If the mailbox command sub-system shutdown is due to
12050  * HBA error conditions such as EEH or ERATT, this routine shall invoke
12051  * the mailbox sub-system flush routine to forcefully bring down the
12052  * mailbox sub-system. Otherwise, if it is due to normal condition (such
12053  * as with offline or HBA function reset), this routine will wait for the
12054  * outstanding mailbox command to complete before invoking the mailbox
12055  * sub-system flush routine to gracefully bring down mailbox sub-system.
12056  **/
12057 void
12058 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
12059 {
12060         struct lpfc_sli *psli = &phba->sli;
12061         unsigned long timeout;
12062
12063         if (mbx_action == LPFC_MBX_NO_WAIT) {
12064                 /* delay 100ms for port state */
12065                 msleep(100);
12066                 lpfc_sli_mbox_sys_flush(phba);
12067                 return;
12068         }
12069         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
12070
12071         /* Disable softirqs, including timers from obtaining phba->hbalock */
12072         local_bh_disable();
12073
12074         spin_lock_irq(&phba->hbalock);
12075         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
12076
12077         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
12078                 /* Determine how long we might wait for the active mailbox
12079                  * command to be gracefully completed by firmware.
12080                  */
12081                 if (phba->sli.mbox_active)
12082                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12083                                                 phba->sli.mbox_active) *
12084                                                 1000) + jiffies;
12085                 spin_unlock_irq(&phba->hbalock);
12086
12087                 /* Enable softirqs again, done with phba->hbalock */
12088                 local_bh_enable();
12089
12090                 while (phba->sli.mbox_active) {
12091                         /* Check active mailbox complete status every 2ms */
12092                         msleep(2);
12093                         if (time_after(jiffies, timeout))
12094                                 /* Timeout, let the mailbox flush routine to
12095                                  * forcefully release active mailbox command
12096                                  */
12097                                 break;
12098                 }
12099         } else {
12100                 spin_unlock_irq(&phba->hbalock);
12101
12102                 /* Enable softirqs again, done with phba->hbalock */
12103                 local_bh_enable();
12104         }
12105
12106         lpfc_sli_mbox_sys_flush(phba);
12107 }
12108
12109 /**
12110  * lpfc_sli_eratt_read - read sli-3 error attention events
12111  * @phba: Pointer to HBA context.
12112  *
12113  * This function is called to read the SLI3 device error attention registers
12114  * for possible error attention events. The caller must hold the hostlock
12115  * with spin_lock_irq().
12116  *
12117  * This function returns 1 when there is Error Attention in the Host Attention
12118  * Register and returns 0 otherwise.
12119  **/
12120 static int
12121 lpfc_sli_eratt_read(struct lpfc_hba *phba)
12122 {
12123         uint32_t ha_copy;
12124
12125         /* Read chip Host Attention (HA) register */
12126         if (lpfc_readl(phba->HAregaddr, &ha_copy))
12127                 goto unplug_err;
12128
12129         if (ha_copy & HA_ERATT) {
12130                 /* Read host status register to retrieve error event */
12131                 if (lpfc_sli_read_hs(phba))
12132                         goto unplug_err;
12133
12134                 /* Check if there is a deferred error condition is active */
12135                 if ((HS_FFER1 & phba->work_hs) &&
12136                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12137                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
12138                         phba->hba_flag |= DEFER_ERATT;
12139                         /* Clear all interrupt enable conditions */
12140                         writel(0, phba->HCregaddr);
12141                         readl(phba->HCregaddr);
12142                 }
12143
12144                 /* Set the driver HA work bitmap */
12145                 phba->work_ha |= HA_ERATT;
12146                 /* Indicate polling handles this ERATT */
12147                 phba->hba_flag |= HBA_ERATT_HANDLED;
12148                 return 1;
12149         }
12150         return 0;
12151
12152 unplug_err:
12153         /* Set the driver HS work bitmap */
12154         phba->work_hs |= UNPLUG_ERR;
12155         /* Set the driver HA work bitmap */
12156         phba->work_ha |= HA_ERATT;
12157         /* Indicate polling handles this ERATT */
12158         phba->hba_flag |= HBA_ERATT_HANDLED;
12159         return 1;
12160 }
12161
12162 /**
12163  * lpfc_sli4_eratt_read - read sli-4 error attention events
12164  * @phba: Pointer to HBA context.
12165  *
12166  * This function is called to read the SLI4 device error attention registers
12167  * for possible error attention events. The caller must hold the hostlock
12168  * with spin_lock_irq().
12169  *
12170  * This function returns 1 when there is Error Attention in the Host Attention
12171  * Register and returns 0 otherwise.
12172  **/
12173 static int
12174 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12175 {
12176         uint32_t uerr_sta_hi, uerr_sta_lo;
12177         uint32_t if_type, portsmphr;
12178         struct lpfc_register portstat_reg;
12179
12180         /*
12181          * For now, use the SLI4 device internal unrecoverable error
12182          * registers for error attention. This can be changed later.
12183          */
12184         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12185         switch (if_type) {
12186         case LPFC_SLI_INTF_IF_TYPE_0:
12187                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12188                         &uerr_sta_lo) ||
12189                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12190                         &uerr_sta_hi)) {
12191                         phba->work_hs |= UNPLUG_ERR;
12192                         phba->work_ha |= HA_ERATT;
12193                         phba->hba_flag |= HBA_ERATT_HANDLED;
12194                         return 1;
12195                 }
12196                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12197                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12198                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12199                                         "1423 HBA Unrecoverable error: "
12200                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12201                                         "ue_mask_lo_reg=0x%x, "
12202                                         "ue_mask_hi_reg=0x%x\n",
12203                                         uerr_sta_lo, uerr_sta_hi,
12204                                         phba->sli4_hba.ue_mask_lo,
12205                                         phba->sli4_hba.ue_mask_hi);
12206                         phba->work_status[0] = uerr_sta_lo;
12207                         phba->work_status[1] = uerr_sta_hi;
12208                         phba->work_ha |= HA_ERATT;
12209                         phba->hba_flag |= HBA_ERATT_HANDLED;
12210                         return 1;
12211                 }
12212                 break;
12213         case LPFC_SLI_INTF_IF_TYPE_2:
12214         case LPFC_SLI_INTF_IF_TYPE_6:
12215                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12216                         &portstat_reg.word0) ||
12217                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12218                         &portsmphr)){
12219                         phba->work_hs |= UNPLUG_ERR;
12220                         phba->work_ha |= HA_ERATT;
12221                         phba->hba_flag |= HBA_ERATT_HANDLED;
12222                         return 1;
12223                 }
12224                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12225                         phba->work_status[0] =
12226                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12227                         phba->work_status[1] =
12228                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12229                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12230                                         "2885 Port Status Event: "
12231                                         "port status reg 0x%x, "
12232                                         "port smphr reg 0x%x, "
12233                                         "error 1=0x%x, error 2=0x%x\n",
12234                                         portstat_reg.word0,
12235                                         portsmphr,
12236                                         phba->work_status[0],
12237                                         phba->work_status[1]);
12238                         phba->work_ha |= HA_ERATT;
12239                         phba->hba_flag |= HBA_ERATT_HANDLED;
12240                         return 1;
12241                 }
12242                 break;
12243         case LPFC_SLI_INTF_IF_TYPE_1:
12244         default:
12245                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12246                                 "2886 HBA Error Attention on unsupported "
12247                                 "if type %d.", if_type);
12248                 return 1;
12249         }
12250
12251         return 0;
12252 }
12253
12254 /**
12255  * lpfc_sli_check_eratt - check error attention events
12256  * @phba: Pointer to HBA context.
12257  *
12258  * This function is called from timer soft interrupt context to check HBA's
12259  * error attention register bit for error attention events.
12260  *
12261  * This function returns 1 when there is Error Attention in the Host Attention
12262  * Register and returns 0 otherwise.
12263  **/
12264 int
12265 lpfc_sli_check_eratt(struct lpfc_hba *phba)
12266 {
12267         uint32_t ha_copy;
12268
12269         /* If somebody is waiting to handle an eratt, don't process it
12270          * here. The brdkill function will do this.
12271          */
12272         if (phba->link_flag & LS_IGNORE_ERATT)
12273                 return 0;
12274
12275         /* Check if interrupt handler handles this ERATT */
12276         spin_lock_irq(&phba->hbalock);
12277         if (phba->hba_flag & HBA_ERATT_HANDLED) {
12278                 /* Interrupt handler has handled ERATT */
12279                 spin_unlock_irq(&phba->hbalock);
12280                 return 0;
12281         }
12282
12283         /*
12284          * If there is deferred error attention, do not check for error
12285          * attention
12286          */
12287         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12288                 spin_unlock_irq(&phba->hbalock);
12289                 return 0;
12290         }
12291
12292         /* If PCI channel is offline, don't process it */
12293         if (unlikely(pci_channel_offline(phba->pcidev))) {
12294                 spin_unlock_irq(&phba->hbalock);
12295                 return 0;
12296         }
12297
12298         switch (phba->sli_rev) {
12299         case LPFC_SLI_REV2:
12300         case LPFC_SLI_REV3:
12301                 /* Read chip Host Attention (HA) register */
12302                 ha_copy = lpfc_sli_eratt_read(phba);
12303                 break;
12304         case LPFC_SLI_REV4:
12305                 /* Read device Uncoverable Error (UERR) registers */
12306                 ha_copy = lpfc_sli4_eratt_read(phba);
12307                 break;
12308         default:
12309                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12310                                 "0299 Invalid SLI revision (%d)\n",
12311                                 phba->sli_rev);
12312                 ha_copy = 0;
12313                 break;
12314         }
12315         spin_unlock_irq(&phba->hbalock);
12316
12317         return ha_copy;
12318 }
12319
12320 /**
12321  * lpfc_intr_state_check - Check device state for interrupt handling
12322  * @phba: Pointer to HBA context.
12323  *
12324  * This inline routine checks whether a device or its PCI slot is in a state
12325  * that the interrupt should be handled.
12326  *
12327  * This function returns 0 if the device or the PCI slot is in a state that
12328  * interrupt should be handled, otherwise -EIO.
12329  */
12330 static inline int
12331 lpfc_intr_state_check(struct lpfc_hba *phba)
12332 {
12333         /* If the pci channel is offline, ignore all the interrupts */
12334         if (unlikely(pci_channel_offline(phba->pcidev)))
12335                 return -EIO;
12336
12337         /* Update device level interrupt statistics */
12338         phba->sli.slistat.sli_intr++;
12339
12340         /* Ignore all interrupts during initialization. */
12341         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12342                 return -EIO;
12343
12344         return 0;
12345 }
12346
12347 /**
12348  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
12349  * @irq: Interrupt number.
12350  * @dev_id: The device context pointer.
12351  *
12352  * This function is directly called from the PCI layer as an interrupt
12353  * service routine when device with SLI-3 interface spec is enabled with
12354  * MSI-X multi-message interrupt mode and there are slow-path events in
12355  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12356  * interrupt mode, this function is called as part of the device-level
12357  * interrupt handler. When the PCI slot is in error recovery or the HBA
12358  * is undergoing initialization, the interrupt handler will not process
12359  * the interrupt. The link attention and ELS ring attention events are
12360  * handled by the worker thread. The interrupt handler signals the worker
12361  * thread and returns for these events. This function is called without
12362  * any lock held. It gets the hbalock to access and update SLI data
12363  * structures.
12364  *
12365  * This function returns IRQ_HANDLED when interrupt is handled else it
12366  * returns IRQ_NONE.
12367  **/
12368 irqreturn_t
12369 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
12370 {
12371         struct lpfc_hba  *phba;
12372         uint32_t ha_copy, hc_copy;
12373         uint32_t work_ha_copy;
12374         unsigned long status;
12375         unsigned long iflag;
12376         uint32_t control;
12377
12378         MAILBOX_t *mbox, *pmbox;
12379         struct lpfc_vport *vport;
12380         struct lpfc_nodelist *ndlp;
12381         struct lpfc_dmabuf *mp;
12382         LPFC_MBOXQ_t *pmb;
12383         int rc;
12384
12385         /*
12386          * Get the driver's phba structure from the dev_id and
12387          * assume the HBA is not interrupting.
12388          */
12389         phba = (struct lpfc_hba *)dev_id;
12390
12391         if (unlikely(!phba))
12392                 return IRQ_NONE;
12393
12394         /*
12395          * Stuff needs to be attented to when this function is invoked as an
12396          * individual interrupt handler in MSI-X multi-message interrupt mode
12397          */
12398         if (phba->intr_type == MSIX) {
12399                 /* Check device state for handling interrupt */
12400                 if (lpfc_intr_state_check(phba))
12401                         return IRQ_NONE;
12402                 /* Need to read HA REG for slow-path events */
12403                 spin_lock_irqsave(&phba->hbalock, iflag);
12404                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12405                         goto unplug_error;
12406                 /* If somebody is waiting to handle an eratt don't process it
12407                  * here. The brdkill function will do this.
12408                  */
12409                 if (phba->link_flag & LS_IGNORE_ERATT)
12410                         ha_copy &= ~HA_ERATT;
12411                 /* Check the need for handling ERATT in interrupt handler */
12412                 if (ha_copy & HA_ERATT) {
12413                         if (phba->hba_flag & HBA_ERATT_HANDLED)
12414                                 /* ERATT polling has handled ERATT */
12415                                 ha_copy &= ~HA_ERATT;
12416                         else
12417                                 /* Indicate interrupt handler handles ERATT */
12418                                 phba->hba_flag |= HBA_ERATT_HANDLED;
12419                 }
12420
12421                 /*
12422                  * If there is deferred error attention, do not check for any
12423                  * interrupt.
12424                  */
12425                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12426                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12427                         return IRQ_NONE;
12428                 }
12429
12430                 /* Clear up only attention source related to slow-path */
12431                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12432                         goto unplug_error;
12433
12434                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12435                         HC_LAINT_ENA | HC_ERINT_ENA),
12436                         phba->HCregaddr);
12437                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12438                         phba->HAregaddr);
12439                 writel(hc_copy, phba->HCregaddr);
12440                 readl(phba->HAregaddr); /* flush */
12441                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12442         } else
12443                 ha_copy = phba->ha_copy;
12444
12445         work_ha_copy = ha_copy & phba->work_ha_mask;
12446
12447         if (work_ha_copy) {
12448                 if (work_ha_copy & HA_LATT) {
12449                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12450                                 /*
12451                                  * Turn off Link Attention interrupts
12452                                  * until CLEAR_LA done
12453                                  */
12454                                 spin_lock_irqsave(&phba->hbalock, iflag);
12455                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
12456                                 if (lpfc_readl(phba->HCregaddr, &control))
12457                                         goto unplug_error;
12458                                 control &= ~HC_LAINT_ENA;
12459                                 writel(control, phba->HCregaddr);
12460                                 readl(phba->HCregaddr); /* flush */
12461                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12462                         }
12463                         else
12464                                 work_ha_copy &= ~HA_LATT;
12465                 }
12466
12467                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
12468                         /*
12469                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12470                          * the only slow ring.
12471                          */
12472                         status = (work_ha_copy &
12473                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
12474                         status >>= (4*LPFC_ELS_RING);
12475                         if (status & HA_RXMASK) {
12476                                 spin_lock_irqsave(&phba->hbalock, iflag);
12477                                 if (lpfc_readl(phba->HCregaddr, &control))
12478                                         goto unplug_error;
12479
12480                                 lpfc_debugfs_slow_ring_trc(phba,
12481                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
12482                                 control, status,
12483                                 (uint32_t)phba->sli.slistat.sli_intr);
12484
12485                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
12486                                         lpfc_debugfs_slow_ring_trc(phba,
12487                                                 "ISR Disable ring:"
12488                                                 "pwork:x%x hawork:x%x wait:x%x",
12489                                                 phba->work_ha, work_ha_copy,
12490                                                 (uint32_t)((unsigned long)
12491                                                 &phba->work_waitq));
12492
12493                                         control &=
12494                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
12495                                         writel(control, phba->HCregaddr);
12496                                         readl(phba->HCregaddr); /* flush */
12497                                 }
12498                                 else {
12499                                         lpfc_debugfs_slow_ring_trc(phba,
12500                                                 "ISR slow ring:   pwork:"
12501                                                 "x%x hawork:x%x wait:x%x",
12502                                                 phba->work_ha, work_ha_copy,
12503                                                 (uint32_t)((unsigned long)
12504                                                 &phba->work_waitq));
12505                                 }
12506                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12507                         }
12508                 }
12509                 spin_lock_irqsave(&phba->hbalock, iflag);
12510                 if (work_ha_copy & HA_ERATT) {
12511                         if (lpfc_sli_read_hs(phba))
12512                                 goto unplug_error;
12513                         /*
12514                          * Check if there is a deferred error condition
12515                          * is active
12516                          */
12517                         if ((HS_FFER1 & phba->work_hs) &&
12518                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12519                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
12520                                   phba->work_hs)) {
12521                                 phba->hba_flag |= DEFER_ERATT;
12522                                 /* Clear all interrupt enable conditions */
12523                                 writel(0, phba->HCregaddr);
12524                                 readl(phba->HCregaddr);
12525                         }
12526                 }
12527
12528                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
12529                         pmb = phba->sli.mbox_active;
12530                         pmbox = &pmb->u.mb;
12531                         mbox = phba->mbox;
12532                         vport = pmb->vport;
12533
12534                         /* First check out the status word */
12535                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12536                         if (pmbox->mbxOwner != OWN_HOST) {
12537                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12538                                 /*
12539                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
12540                                  * mbxStatus <status>
12541                                  */
12542                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12543                                                 LOG_SLI,
12544                                                 "(%d):0304 Stray Mailbox "
12545                                                 "Interrupt mbxCommand x%x "
12546                                                 "mbxStatus x%x\n",
12547                                                 (vport ? vport->vpi : 0),
12548                                                 pmbox->mbxCommand,
12549                                                 pmbox->mbxStatus);
12550                                 /* clear mailbox attention bit */
12551                                 work_ha_copy &= ~HA_MBATT;
12552                         } else {
12553                                 phba->sli.mbox_active = NULL;
12554                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12555                                 phba->last_completion_time = jiffies;
12556                                 del_timer(&phba->sli.mbox_tmo);
12557                                 if (pmb->mbox_cmpl) {
12558                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
12559                                                         MAILBOX_CMD_SIZE);
12560                                         if (pmb->out_ext_byte_len &&
12561                                                 pmb->context2)
12562                                                 lpfc_sli_pcimem_bcopy(
12563                                                 phba->mbox_ext,
12564                                                 pmb->context2,
12565                                                 pmb->out_ext_byte_len);
12566                                 }
12567                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12568                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12569
12570                                         lpfc_debugfs_disc_trc(vport,
12571                                                 LPFC_DISC_TRC_MBOX_VPORT,
12572                                                 "MBOX dflt rpi: : "
12573                                                 "status:x%x rpi:x%x",
12574                                                 (uint32_t)pmbox->mbxStatus,
12575                                                 pmbox->un.varWords[0], 0);
12576
12577                                         if (!pmbox->mbxStatus) {
12578                                                 mp = (struct lpfc_dmabuf *)
12579                                                         (pmb->context1);
12580                                                 ndlp = (struct lpfc_nodelist *)
12581                                                         pmb->context2;
12582
12583                                                 /* Reg_LOGIN of dflt RPI was
12584                                                  * successful. new lets get
12585                                                  * rid of the RPI using the
12586                                                  * same mbox buffer.
12587                                                  */
12588                                                 lpfc_unreg_login(phba,
12589                                                         vport->vpi,
12590                                                         pmbox->un.varWords[0],
12591                                                         pmb);
12592                                                 pmb->mbox_cmpl =
12593                                                         lpfc_mbx_cmpl_dflt_rpi;
12594                                                 pmb->context1 = mp;
12595                                                 pmb->context2 = ndlp;
12596                                                 pmb->vport = vport;
12597                                                 rc = lpfc_sli_issue_mbox(phba,
12598                                                                 pmb,
12599                                                                 MBX_NOWAIT);
12600                                                 if (rc != MBX_BUSY)
12601                                                         lpfc_printf_log(phba,
12602                                                         KERN_ERR,
12603                                                         LOG_MBOX | LOG_SLI,
12604                                                         "0350 rc should have"
12605                                                         "been MBX_BUSY\n");
12606                                                 if (rc != MBX_NOT_FINISHED)
12607                                                         goto send_current_mbox;
12608                                         }
12609                                 }
12610                                 spin_lock_irqsave(
12611                                                 &phba->pport->work_port_lock,
12612                                                 iflag);
12613                                 phba->pport->work_port_events &=
12614                                         ~WORKER_MBOX_TMO;
12615                                 spin_unlock_irqrestore(
12616                                                 &phba->pport->work_port_lock,
12617                                                 iflag);
12618                                 lpfc_mbox_cmpl_put(phba, pmb);
12619                         }
12620                 } else
12621                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12622
12623                 if ((work_ha_copy & HA_MBATT) &&
12624                     (phba->sli.mbox_active == NULL)) {
12625 send_current_mbox:
12626                         /* Process next mailbox command if there is one */
12627                         do {
12628                                 rc = lpfc_sli_issue_mbox(phba, NULL,
12629                                                          MBX_NOWAIT);
12630                         } while (rc == MBX_NOT_FINISHED);
12631                         if (rc != MBX_SUCCESS)
12632                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12633                                                 LOG_SLI, "0349 rc should be "
12634                                                 "MBX_SUCCESS\n");
12635                 }
12636
12637                 spin_lock_irqsave(&phba->hbalock, iflag);
12638                 phba->work_ha |= work_ha_copy;
12639                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12640                 lpfc_worker_wake_up(phba);
12641         }
12642         return IRQ_HANDLED;
12643 unplug_error:
12644         spin_unlock_irqrestore(&phba->hbalock, iflag);
12645         return IRQ_HANDLED;
12646
12647 } /* lpfc_sli_sp_intr_handler */
12648
12649 /**
12650  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
12651  * @irq: Interrupt number.
12652  * @dev_id: The device context pointer.
12653  *
12654  * This function is directly called from the PCI layer as an interrupt
12655  * service routine when device with SLI-3 interface spec is enabled with
12656  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12657  * ring event in the HBA. However, when the device is enabled with either
12658  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12659  * device-level interrupt handler. When the PCI slot is in error recovery
12660  * or the HBA is undergoing initialization, the interrupt handler will not
12661  * process the interrupt. The SCSI FCP fast-path ring event are handled in
12662  * the intrrupt context. This function is called without any lock held.
12663  * It gets the hbalock to access and update SLI data structures.
12664  *
12665  * This function returns IRQ_HANDLED when interrupt is handled else it
12666  * returns IRQ_NONE.
12667  **/
12668 irqreturn_t
12669 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
12670 {
12671         struct lpfc_hba  *phba;
12672         uint32_t ha_copy;
12673         unsigned long status;
12674         unsigned long iflag;
12675         struct lpfc_sli_ring *pring;
12676
12677         /* Get the driver's phba structure from the dev_id and
12678          * assume the HBA is not interrupting.
12679          */
12680         phba = (struct lpfc_hba *) dev_id;
12681
12682         if (unlikely(!phba))
12683                 return IRQ_NONE;
12684
12685         /*
12686          * Stuff needs to be attented to when this function is invoked as an
12687          * individual interrupt handler in MSI-X multi-message interrupt mode
12688          */
12689         if (phba->intr_type == MSIX) {
12690                 /* Check device state for handling interrupt */
12691                 if (lpfc_intr_state_check(phba))
12692                         return IRQ_NONE;
12693                 /* Need to read HA REG for FCP ring and other ring events */
12694                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12695                         return IRQ_HANDLED;
12696                 /* Clear up only attention source related to fast-path */
12697                 spin_lock_irqsave(&phba->hbalock, iflag);
12698                 /*
12699                  * If there is deferred error attention, do not check for
12700                  * any interrupt.
12701                  */
12702                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12703                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12704                         return IRQ_NONE;
12705                 }
12706                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12707                         phba->HAregaddr);
12708                 readl(phba->HAregaddr); /* flush */
12709                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12710         } else
12711                 ha_copy = phba->ha_copy;
12712
12713         /*
12714          * Process all events on FCP ring. Take the optimized path for FCP IO.
12715          */
12716         ha_copy &= ~(phba->work_ha_mask);
12717
12718         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12719         status >>= (4*LPFC_FCP_RING);
12720         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12721         if (status & HA_RXMASK)
12722                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
12723
12724         if (phba->cfg_multi_ring_support == 2) {
12725                 /*
12726                  * Process all events on extra ring. Take the optimized path
12727                  * for extra ring IO.
12728                  */
12729                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12730                 status >>= (4*LPFC_EXTRA_RING);
12731                 if (status & HA_RXMASK) {
12732                         lpfc_sli_handle_fast_ring_event(phba,
12733                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12734                                         status);
12735                 }
12736         }
12737         return IRQ_HANDLED;
12738 }  /* lpfc_sli_fp_intr_handler */
12739
12740 /**
12741  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12742  * @irq: Interrupt number.
12743  * @dev_id: The device context pointer.
12744  *
12745  * This function is the HBA device-level interrupt handler to device with
12746  * SLI-3 interface spec, called from the PCI layer when either MSI or
12747  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12748  * requires driver attention. This function invokes the slow-path interrupt
12749  * attention handling function and fast-path interrupt attention handling
12750  * function in turn to process the relevant HBA attention events. This
12751  * function is called without any lock held. It gets the hbalock to access
12752  * and update SLI data structures.
12753  *
12754  * This function returns IRQ_HANDLED when interrupt is handled, else it
12755  * returns IRQ_NONE.
12756  **/
12757 irqreturn_t
12758 lpfc_sli_intr_handler(int irq, void *dev_id)
12759 {
12760         struct lpfc_hba  *phba;
12761         irqreturn_t sp_irq_rc, fp_irq_rc;
12762         unsigned long status1, status2;
12763         uint32_t hc_copy;
12764
12765         /*
12766          * Get the driver's phba structure from the dev_id and
12767          * assume the HBA is not interrupting.
12768          */
12769         phba = (struct lpfc_hba *) dev_id;
12770
12771         if (unlikely(!phba))
12772                 return IRQ_NONE;
12773
12774         /* Check device state for handling interrupt */
12775         if (lpfc_intr_state_check(phba))
12776                 return IRQ_NONE;
12777
12778         spin_lock(&phba->hbalock);
12779         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12780                 spin_unlock(&phba->hbalock);
12781                 return IRQ_HANDLED;
12782         }
12783
12784         if (unlikely(!phba->ha_copy)) {
12785                 spin_unlock(&phba->hbalock);
12786                 return IRQ_NONE;
12787         } else if (phba->ha_copy & HA_ERATT) {
12788                 if (phba->hba_flag & HBA_ERATT_HANDLED)
12789                         /* ERATT polling has handled ERATT */
12790                         phba->ha_copy &= ~HA_ERATT;
12791                 else
12792                         /* Indicate interrupt handler handles ERATT */
12793                         phba->hba_flag |= HBA_ERATT_HANDLED;
12794         }
12795
12796         /*
12797          * If there is deferred error attention, do not check for any interrupt.
12798          */
12799         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12800                 spin_unlock(&phba->hbalock);
12801                 return IRQ_NONE;
12802         }
12803
12804         /* Clear attention sources except link and error attentions */
12805         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12806                 spin_unlock(&phba->hbalock);
12807                 return IRQ_HANDLED;
12808         }
12809         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12810                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12811                 phba->HCregaddr);
12812         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12813         writel(hc_copy, phba->HCregaddr);
12814         readl(phba->HAregaddr); /* flush */
12815         spin_unlock(&phba->hbalock);
12816
12817         /*
12818          * Invokes slow-path host attention interrupt handling as appropriate.
12819          */
12820
12821         /* status of events with mailbox and link attention */
12822         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12823
12824         /* status of events with ELS ring */
12825         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
12826         status2 >>= (4*LPFC_ELS_RING);
12827
12828         if (status1 || (status2 & HA_RXMASK))
12829                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12830         else
12831                 sp_irq_rc = IRQ_NONE;
12832
12833         /*
12834          * Invoke fast-path host attention interrupt handling as appropriate.
12835          */
12836
12837         /* status of events with FCP ring */
12838         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12839         status1 >>= (4*LPFC_FCP_RING);
12840
12841         /* status of events with extra ring */
12842         if (phba->cfg_multi_ring_support == 2) {
12843                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12844                 status2 >>= (4*LPFC_EXTRA_RING);
12845         } else
12846                 status2 = 0;
12847
12848         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12849                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12850         else
12851                 fp_irq_rc = IRQ_NONE;
12852
12853         /* Return device-level interrupt handling status */
12854         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12855 }  /* lpfc_sli_intr_handler */
12856
12857 /**
12858  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12859  * @phba: pointer to lpfc hba data structure.
12860  *
12861  * This routine is invoked by the worker thread to process all the pending
12862  * SLI4 FCP abort XRI events.
12863  **/
12864 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12865 {
12866         struct lpfc_cq_event *cq_event;
12867
12868         /* First, declare the fcp xri abort event has been handled */
12869         spin_lock_irq(&phba->hbalock);
12870         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12871         spin_unlock_irq(&phba->hbalock);
12872         /* Now, handle all the fcp xri abort events */
12873         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12874                 /* Get the first event from the head of the event queue */
12875                 spin_lock_irq(&phba->hbalock);
12876                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12877                                  cq_event, struct lpfc_cq_event, list);
12878                 spin_unlock_irq(&phba->hbalock);
12879                 /* Notify aborted XRI for FCP work queue */
12880                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12881                 /* Free the event processed back to the free pool */
12882                 lpfc_sli4_cq_event_release(phba, cq_event);
12883         }
12884 }
12885
12886 /**
12887  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12888  * @phba: pointer to lpfc hba data structure.
12889  *
12890  * This routine is invoked by the worker thread to process all the pending
12891  * SLI4 els abort xri events.
12892  **/
12893 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12894 {
12895         struct lpfc_cq_event *cq_event;
12896
12897         /* First, declare the els xri abort event has been handled */
12898         spin_lock_irq(&phba->hbalock);
12899         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12900         spin_unlock_irq(&phba->hbalock);
12901         /* Now, handle all the els xri abort events */
12902         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12903                 /* Get the first event from the head of the event queue */
12904                 spin_lock_irq(&phba->hbalock);
12905                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12906                                  cq_event, struct lpfc_cq_event, list);
12907                 spin_unlock_irq(&phba->hbalock);
12908                 /* Notify aborted XRI for ELS work queue */
12909                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12910                 /* Free the event processed back to the free pool */
12911                 lpfc_sli4_cq_event_release(phba, cq_event);
12912         }
12913 }
12914
12915 /**
12916  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12917  * @phba: pointer to lpfc hba data structure
12918  * @pIocbIn: pointer to the rspiocbq
12919  * @pIocbOut: pointer to the cmdiocbq
12920  * @wcqe: pointer to the complete wcqe
12921  *
12922  * This routine transfers the fields of a command iocbq to a response iocbq
12923  * by copying all the IOCB fields from command iocbq and transferring the
12924  * completion status information from the complete wcqe.
12925  **/
12926 static void
12927 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12928                               struct lpfc_iocbq *pIocbIn,
12929                               struct lpfc_iocbq *pIocbOut,
12930                               struct lpfc_wcqe_complete *wcqe)
12931 {
12932         int numBdes, i;
12933         unsigned long iflags;
12934         uint32_t status, max_response;
12935         struct lpfc_dmabuf *dmabuf;
12936         struct ulp_bde64 *bpl, bde;
12937         size_t offset = offsetof(struct lpfc_iocbq, iocb);
12938
12939         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12940                sizeof(struct lpfc_iocbq) - offset);
12941         /* Map WCQE parameters into irspiocb parameters */
12942         status = bf_get(lpfc_wcqe_c_status, wcqe);
12943         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12944         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12945                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12946                         pIocbIn->iocb.un.fcpi.fcpi_parm =
12947                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
12948                                         wcqe->total_data_placed;
12949                 else
12950                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12951         else {
12952                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12953                 switch (pIocbOut->iocb.ulpCommand) {
12954                 case CMD_ELS_REQUEST64_CR:
12955                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12956                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
12957                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12958                         max_response = bde.tus.f.bdeSize;
12959                         break;
12960                 case CMD_GEN_REQUEST64_CR:
12961                         max_response = 0;
12962                         if (!pIocbOut->context3)
12963                                 break;
12964                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12965                                         sizeof(struct ulp_bde64);
12966                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12967                         bpl = (struct ulp_bde64 *)dmabuf->virt;
12968                         for (i = 0; i < numBdes; i++) {
12969                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12970                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12971                                         max_response += bde.tus.f.bdeSize;
12972                         }
12973                         break;
12974                 default:
12975                         max_response = wcqe->total_data_placed;
12976                         break;
12977                 }
12978                 if (max_response < wcqe->total_data_placed)
12979                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12980                 else
12981                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12982                                 wcqe->total_data_placed;
12983         }
12984
12985         /* Convert BG errors for completion status */
12986         if (status == CQE_STATUS_DI_ERROR) {
12987                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12988
12989                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12990                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12991                 else
12992                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12993
12994                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12995                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12996                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12997                                 BGS_GUARD_ERR_MASK;
12998                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12999                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13000                                 BGS_APPTAG_ERR_MASK;
13001                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13002                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13003                                 BGS_REFTAG_ERR_MASK;
13004
13005                 /* Check to see if there was any good data before the error */
13006                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13007                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13008                                 BGS_HI_WATER_MARK_PRESENT_MASK;
13009                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
13010                                 wcqe->total_data_placed;
13011                 }
13012
13013                 /*
13014                 * Set ALL the error bits to indicate we don't know what
13015                 * type of error it is.
13016                 */
13017                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13018                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13019                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13020                                 BGS_GUARD_ERR_MASK);
13021         }
13022
13023         /* Pick up HBA exchange busy condition */
13024         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13025                 spin_lock_irqsave(&phba->hbalock, iflags);
13026                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13027                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13028         }
13029 }
13030
13031 /**
13032  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13033  * @phba: Pointer to HBA context object.
13034  * @wcqe: Pointer to work-queue completion queue entry.
13035  *
13036  * This routine handles an ELS work-queue completion event and construct
13037  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13038  * discovery engine to handle.
13039  *
13040  * Return: Pointer to the receive IOCBQ, NULL otherwise.
13041  **/
13042 static struct lpfc_iocbq *
13043 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13044                                struct lpfc_iocbq *irspiocbq)
13045 {
13046         struct lpfc_sli_ring *pring;
13047         struct lpfc_iocbq *cmdiocbq;
13048         struct lpfc_wcqe_complete *wcqe;
13049         unsigned long iflags;
13050
13051         pring = lpfc_phba_elsring(phba);
13052         if (unlikely(!pring))
13053                 return NULL;
13054
13055         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
13056         spin_lock_irqsave(&pring->ring_lock, iflags);
13057         pring->stats.iocb_event++;
13058         /* Look up the ELS command IOCB and create pseudo response IOCB */
13059         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13060                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13061         if (unlikely(!cmdiocbq)) {
13062                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13063                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13064                                 "0386 ELS complete with no corresponding "
13065                                 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13066                                 wcqe->word0, wcqe->total_data_placed,
13067                                 wcqe->parameter, wcqe->word3);
13068                 lpfc_sli_release_iocbq(phba, irspiocbq);
13069                 return NULL;
13070         }
13071
13072         /* Put the iocb back on the txcmplq */
13073         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13074         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13075
13076         /* Fake the irspiocbq and copy necessary response information */
13077         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
13078
13079         return irspiocbq;
13080 }
13081
13082 inline struct lpfc_cq_event *
13083 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13084 {
13085         struct lpfc_cq_event *cq_event;
13086
13087         /* Allocate a new internal CQ_EVENT entry */
13088         cq_event = lpfc_sli4_cq_event_alloc(phba);
13089         if (!cq_event) {
13090                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13091                                 "0602 Failed to alloc CQ_EVENT entry\n");
13092                 return NULL;
13093         }
13094
13095         /* Move the CQE into the event */
13096         memcpy(&cq_event->cqe, entry, size);
13097         return cq_event;
13098 }
13099
13100 /**
13101  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13102  * @phba: Pointer to HBA context object.
13103  * @cqe: Pointer to mailbox completion queue entry.
13104  *
13105  * This routine process a mailbox completion queue entry with asynchrous
13106  * event.
13107  *
13108  * Return: true if work posted to worker thread, otherwise false.
13109  **/
13110 static bool
13111 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13112 {
13113         struct lpfc_cq_event *cq_event;
13114         unsigned long iflags;
13115
13116         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13117                         "0392 Async Event: word0:x%x, word1:x%x, "
13118                         "word2:x%x, word3:x%x\n", mcqe->word0,
13119                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13120
13121         cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13122         if (!cq_event)
13123                 return false;
13124         spin_lock_irqsave(&phba->hbalock, iflags);
13125         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13126         /* Set the async event flag */
13127         phba->hba_flag |= ASYNC_EVENT;
13128         spin_unlock_irqrestore(&phba->hbalock, iflags);
13129
13130         return true;
13131 }
13132
13133 /**
13134  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13135  * @phba: Pointer to HBA context object.
13136  * @cqe: Pointer to mailbox completion queue entry.
13137  *
13138  * This routine process a mailbox completion queue entry with mailbox
13139  * completion event.
13140  *
13141  * Return: true if work posted to worker thread, otherwise false.
13142  **/
13143 static bool
13144 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13145 {
13146         uint32_t mcqe_status;
13147         MAILBOX_t *mbox, *pmbox;
13148         struct lpfc_mqe *mqe;
13149         struct lpfc_vport *vport;
13150         struct lpfc_nodelist *ndlp;
13151         struct lpfc_dmabuf *mp;
13152         unsigned long iflags;
13153         LPFC_MBOXQ_t *pmb;
13154         bool workposted = false;
13155         int rc;
13156
13157         /* If not a mailbox complete MCQE, out by checking mailbox consume */
13158         if (!bf_get(lpfc_trailer_completed, mcqe))
13159                 goto out_no_mqe_complete;
13160
13161         /* Get the reference to the active mbox command */
13162         spin_lock_irqsave(&phba->hbalock, iflags);
13163         pmb = phba->sli.mbox_active;
13164         if (unlikely(!pmb)) {
13165                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13166                                 "1832 No pending MBOX command to handle\n");
13167                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13168                 goto out_no_mqe_complete;
13169         }
13170         spin_unlock_irqrestore(&phba->hbalock, iflags);
13171         mqe = &pmb->u.mqe;
13172         pmbox = (MAILBOX_t *)&pmb->u.mqe;
13173         mbox = phba->mbox;
13174         vport = pmb->vport;
13175
13176         /* Reset heartbeat timer */
13177         phba->last_completion_time = jiffies;
13178         del_timer(&phba->sli.mbox_tmo);
13179
13180         /* Move mbox data to caller's mailbox region, do endian swapping */
13181         if (pmb->mbox_cmpl && mbox)
13182                 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
13183
13184         /*
13185          * For mcqe errors, conditionally move a modified error code to
13186          * the mbox so that the error will not be missed.
13187          */
13188         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13189         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13190                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13191                         bf_set(lpfc_mqe_status, mqe,
13192                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
13193         }
13194         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13195                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13196                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13197                                       "MBOX dflt rpi: status:x%x rpi:x%x",
13198                                       mcqe_status,
13199                                       pmbox->un.varWords[0], 0);
13200                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
13201                         mp = (struct lpfc_dmabuf *)(pmb->context1);
13202                         ndlp = (struct lpfc_nodelist *)pmb->context2;
13203                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
13204                          * RID of the PPI using the same mbox buffer.
13205                          */
13206                         lpfc_unreg_login(phba, vport->vpi,
13207                                          pmbox->un.varWords[0], pmb);
13208                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
13209                         pmb->context1 = mp;
13210                         pmb->context2 = ndlp;
13211                         pmb->vport = vport;
13212                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13213                         if (rc != MBX_BUSY)
13214                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13215                                                 LOG_SLI, "0385 rc should "
13216                                                 "have been MBX_BUSY\n");
13217                         if (rc != MBX_NOT_FINISHED)
13218                                 goto send_current_mbox;
13219                 }
13220         }
13221         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13222         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13223         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13224
13225         /* There is mailbox completion work to do */
13226         spin_lock_irqsave(&phba->hbalock, iflags);
13227         __lpfc_mbox_cmpl_put(phba, pmb);
13228         phba->work_ha |= HA_MBATT;
13229         spin_unlock_irqrestore(&phba->hbalock, iflags);
13230         workposted = true;
13231
13232 send_current_mbox:
13233         spin_lock_irqsave(&phba->hbalock, iflags);
13234         /* Release the mailbox command posting token */
13235         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13236         /* Setting active mailbox pointer need to be in sync to flag clear */
13237         phba->sli.mbox_active = NULL;
13238         spin_unlock_irqrestore(&phba->hbalock, iflags);
13239         /* Wake up worker thread to post the next pending mailbox command */
13240         lpfc_worker_wake_up(phba);
13241 out_no_mqe_complete:
13242         if (bf_get(lpfc_trailer_consumed, mcqe))
13243                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13244         return workposted;
13245 }
13246
13247 /**
13248  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13249  * @phba: Pointer to HBA context object.
13250  * @cqe: Pointer to mailbox completion queue entry.
13251  *
13252  * This routine process a mailbox completion queue entry, it invokes the
13253  * proper mailbox complete handling or asynchrous event handling routine
13254  * according to the MCQE's async bit.
13255  *
13256  * Return: true if work posted to worker thread, otherwise false.
13257  **/
13258 static bool
13259 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
13260 {
13261         struct lpfc_mcqe mcqe;
13262         bool workposted;
13263
13264         /* Copy the mailbox MCQE and convert endian order as needed */
13265         lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
13266
13267         /* Invoke the proper event handling routine */
13268         if (!bf_get(lpfc_trailer_async, &mcqe))
13269                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13270         else
13271                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13272         return workposted;
13273 }
13274
13275 /**
13276  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13277  * @phba: Pointer to HBA context object.
13278  * @cq: Pointer to associated CQ
13279  * @wcqe: Pointer to work-queue completion queue entry.
13280  *
13281  * This routine handles an ELS work-queue completion event.
13282  *
13283  * Return: true if work posted to worker thread, otherwise false.
13284  **/
13285 static bool
13286 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13287                              struct lpfc_wcqe_complete *wcqe)
13288 {
13289         struct lpfc_iocbq *irspiocbq;
13290         unsigned long iflags;
13291         struct lpfc_sli_ring *pring = cq->pring;
13292         int txq_cnt = 0;
13293         int txcmplq_cnt = 0;
13294         int fcp_txcmplq_cnt = 0;
13295
13296         /* Check for response status */
13297         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13298                 /* Log the error status */
13299                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13300                                 "0357 ELS CQE error: status=x%x: "
13301                                 "CQE: %08x %08x %08x %08x\n",
13302                                 bf_get(lpfc_wcqe_c_status, wcqe),
13303                                 wcqe->word0, wcqe->total_data_placed,
13304                                 wcqe->parameter, wcqe->word3);
13305         }
13306
13307         /* Get an irspiocbq for later ELS response processing use */
13308         irspiocbq = lpfc_sli_get_iocbq(phba);
13309         if (!irspiocbq) {
13310                 if (!list_empty(&pring->txq))
13311                         txq_cnt++;
13312                 if (!list_empty(&pring->txcmplq))
13313                         txcmplq_cnt++;
13314                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13315                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13316                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
13317                         txq_cnt, phba->iocb_cnt,
13318                         fcp_txcmplq_cnt,
13319                         txcmplq_cnt);
13320                 return false;
13321         }
13322
13323         /* Save off the slow-path queue event for work thread to process */
13324         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
13325         spin_lock_irqsave(&phba->hbalock, iflags);
13326         list_add_tail(&irspiocbq->cq_event.list,
13327                       &phba->sli4_hba.sp_queue_event);
13328         phba->hba_flag |= HBA_SP_QUEUE_EVT;
13329         spin_unlock_irqrestore(&phba->hbalock, iflags);
13330
13331         return true;
13332 }
13333
13334 /**
13335  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13336  * @phba: Pointer to HBA context object.
13337  * @wcqe: Pointer to work-queue completion queue entry.
13338  *
13339  * This routine handles slow-path WQ entry consumed event by invoking the
13340  * proper WQ release routine to the slow-path WQ.
13341  **/
13342 static void
13343 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13344                              struct lpfc_wcqe_release *wcqe)
13345 {
13346         /* sanity check on queue memory */
13347         if (unlikely(!phba->sli4_hba.els_wq))
13348                 return;
13349         /* Check for the slow-path ELS work queue */
13350         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13351                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13352                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13353         else
13354                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13355                                 "2579 Slow-path wqe consume event carries "
13356                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13357                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13358                                 phba->sli4_hba.els_wq->queue_id);
13359 }
13360
13361 /**
13362  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13363  * @phba: Pointer to HBA context object.
13364  * @cq: Pointer to a WQ completion queue.
13365  * @wcqe: Pointer to work-queue completion queue entry.
13366  *
13367  * This routine handles an XRI abort event.
13368  *
13369  * Return: true if work posted to worker thread, otherwise false.
13370  **/
13371 static bool
13372 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13373                                    struct lpfc_queue *cq,
13374                                    struct sli4_wcqe_xri_aborted *wcqe)
13375 {
13376         bool workposted = false;
13377         struct lpfc_cq_event *cq_event;
13378         unsigned long iflags;
13379
13380         switch (cq->subtype) {
13381         case LPFC_FCP:
13382                 cq_event = lpfc_cq_event_setup(
13383                         phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13384                 if (!cq_event)
13385                         return false;
13386                 spin_lock_irqsave(&phba->hbalock, iflags);
13387                 list_add_tail(&cq_event->list,
13388                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13389                 /* Set the fcp xri abort event flag */
13390                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13391                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13392                 workposted = true;
13393                 break;
13394         case LPFC_NVME_LS: /* NVME LS uses ELS resources */
13395         case LPFC_ELS:
13396                 cq_event = lpfc_cq_event_setup(
13397                         phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13398                 if (!cq_event)
13399                         return false;
13400                 spin_lock_irqsave(&phba->hbalock, iflags);
13401                 list_add_tail(&cq_event->list,
13402                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13403                 /* Set the els xri abort event flag */
13404                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13405                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13406                 workposted = true;
13407                 break;
13408         case LPFC_NVME:
13409                 /* Notify aborted XRI for NVME work queue */
13410                 if (phba->nvmet_support)
13411                         lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13412                 else
13413                         lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13414
13415                 workposted = false;
13416                 break;
13417         default:
13418                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13419                                 "0603 Invalid CQ subtype %d: "
13420                                 "%08x %08x %08x %08x\n",
13421                                 cq->subtype, wcqe->word0, wcqe->parameter,
13422                                 wcqe->word2, wcqe->word3);
13423                 workposted = false;
13424                 break;
13425         }
13426         return workposted;
13427 }
13428
13429 /**
13430  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13431  * @phba: Pointer to HBA context object.
13432  * @rcqe: Pointer to receive-queue completion queue entry.
13433  *
13434  * This routine process a receive-queue completion queue entry.
13435  *
13436  * Return: true if work posted to worker thread, otherwise false.
13437  **/
13438 static bool
13439 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13440 {
13441         bool workposted = false;
13442         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13443         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
13444         struct lpfc_nvmet_tgtport *tgtp;
13445         struct hbq_dmabuf *dma_buf;
13446         uint32_t status, rq_id;
13447         unsigned long iflags;
13448
13449         /* sanity check on queue memory */
13450         if (unlikely(!hrq) || unlikely(!drq))
13451                 return workposted;
13452
13453         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13454                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13455         else
13456                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13457         if (rq_id != hrq->queue_id)
13458                 goto out;
13459
13460         status = bf_get(lpfc_rcqe_status, rcqe);
13461         switch (status) {
13462         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13463                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13464                                 "2537 Receive Frame Truncated!!\n");
13465         case FC_STATUS_RQ_SUCCESS:
13466                 spin_lock_irqsave(&phba->hbalock, iflags);
13467                 lpfc_sli4_rq_release(hrq, drq);
13468                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13469                 if (!dma_buf) {
13470                         hrq->RQ_no_buf_found++;
13471                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13472                         goto out;
13473                 }
13474                 hrq->RQ_rcv_buf++;
13475                 hrq->RQ_buf_posted--;
13476                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
13477
13478                 /* save off the frame for the word thread to process */
13479                 list_add_tail(&dma_buf->cq_event.list,
13480                               &phba->sli4_hba.sp_queue_event);
13481                 /* Frame received */
13482                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
13483                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13484                 workposted = true;
13485                 break;
13486         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13487                 if (phba->nvmet_support) {
13488                         tgtp = phba->targetport->private;
13489                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13490                                         "6402 RQE Error x%x, posted %d err_cnt "
13491                                         "%d: %x %x %x\n",
13492                                         status, hrq->RQ_buf_posted,
13493                                         hrq->RQ_no_posted_buf,
13494                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13495                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13496                                         atomic_read(&tgtp->xmt_fcp_release));
13497                 }
13498                 /* fallthrough */
13499
13500         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13501                 hrq->RQ_no_posted_buf++;
13502                 /* Post more buffers if possible */
13503                 spin_lock_irqsave(&phba->hbalock, iflags);
13504                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13505                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13506                 workposted = true;
13507                 break;
13508         }
13509 out:
13510         return workposted;
13511 }
13512
13513 /**
13514  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13515  * @phba: Pointer to HBA context object.
13516  * @cq: Pointer to the completion queue.
13517  * @wcqe: Pointer to a completion queue entry.
13518  *
13519  * This routine process a slow-path work-queue or receive queue completion queue
13520  * entry.
13521  *
13522  * Return: true if work posted to worker thread, otherwise false.
13523  **/
13524 static bool
13525 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13526                          struct lpfc_cqe *cqe)
13527 {
13528         struct lpfc_cqe cqevt;
13529         bool workposted = false;
13530
13531         /* Copy the work queue CQE and convert endian order if needed */
13532         lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
13533
13534         /* Check and process for different type of WCQE and dispatch */
13535         switch (bf_get(lpfc_cqe_code, &cqevt)) {
13536         case CQE_CODE_COMPL_WQE:
13537                 /* Process the WQ/RQ complete event */
13538                 phba->last_completion_time = jiffies;
13539                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
13540                                 (struct lpfc_wcqe_complete *)&cqevt);
13541                 break;
13542         case CQE_CODE_RELEASE_WQE:
13543                 /* Process the WQ release event */
13544                 lpfc_sli4_sp_handle_rel_wcqe(phba,
13545                                 (struct lpfc_wcqe_release *)&cqevt);
13546                 break;
13547         case CQE_CODE_XRI_ABORTED:
13548                 /* Process the WQ XRI abort event */
13549                 phba->last_completion_time = jiffies;
13550                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13551                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
13552                 break;
13553         case CQE_CODE_RECEIVE:
13554         case CQE_CODE_RECEIVE_V1:
13555                 /* Process the RQ event */
13556                 phba->last_completion_time = jiffies;
13557                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
13558                                 (struct lpfc_rcqe *)&cqevt);
13559                 break;
13560         default:
13561                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13562                                 "0388 Not a valid WCQE code: x%x\n",
13563                                 bf_get(lpfc_cqe_code, &cqevt));
13564                 break;
13565         }
13566         return workposted;
13567 }
13568
13569 /**
13570  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13571  * @phba: Pointer to HBA context object.
13572  * @eqe: Pointer to fast-path event queue entry.
13573  *
13574  * This routine process a event queue entry from the slow-path event queue.
13575  * It will check the MajorCode and MinorCode to determine this is for a
13576  * completion event on a completion queue, if not, an error shall be logged
13577  * and just return. Otherwise, it will get to the corresponding completion
13578  * queue and process all the entries on that completion queue, rearm the
13579  * completion queue, and then return.
13580  *
13581  **/
13582 static void
13583 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13584         struct lpfc_queue *speq)
13585 {
13586         struct lpfc_queue *cq = NULL, *childq;
13587         uint16_t cqid;
13588
13589         /* Get the reference to the corresponding CQ */
13590         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13591
13592         list_for_each_entry(childq, &speq->child_list, list) {
13593                 if (childq->queue_id == cqid) {
13594                         cq = childq;
13595                         break;
13596                 }
13597         }
13598         if (unlikely(!cq)) {
13599                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13600                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13601                                         "0365 Slow-path CQ identifier "
13602                                         "(%d) does not exist\n", cqid);
13603                 return;
13604         }
13605
13606         /* Save EQ associated with this CQ */
13607         cq->assoc_qp = speq;
13608
13609         if (!queue_work(phba->wq, &cq->spwork))
13610                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13611                                 "0390 Cannot schedule soft IRQ "
13612                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13613                                 cqid, cq->queue_id, smp_processor_id());
13614 }
13615
13616 /**
13617  * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13618  * @phba: Pointer to HBA context object.
13619  *
13620  * This routine process a event queue entry from the slow-path event queue.
13621  * It will check the MajorCode and MinorCode to determine this is for a
13622  * completion event on a completion queue, if not, an error shall be logged
13623  * and just return. Otherwise, it will get to the corresponding completion
13624  * queue and process all the entries on that completion queue, rearm the
13625  * completion queue, and then return.
13626  *
13627  **/
13628 static void
13629 lpfc_sli4_sp_process_cq(struct work_struct *work)
13630 {
13631         struct lpfc_queue *cq =
13632                 container_of(work, struct lpfc_queue, spwork);
13633         struct lpfc_hba *phba = cq->phba;
13634         struct lpfc_cqe *cqe;
13635         bool workposted = false;
13636         int ccount = 0;
13637
13638         /* Process all the entries to the CQ */
13639         switch (cq->type) {
13640         case LPFC_MCQ:
13641                 while ((cqe = lpfc_sli4_cq_get(cq))) {
13642                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
13643                         if (!(++ccount % cq->entry_repost))
13644                                 break;
13645                         cq->CQ_mbox++;
13646                 }
13647                 break;
13648         case LPFC_WCQ:
13649                 while ((cqe = lpfc_sli4_cq_get(cq))) {
13650                         if (cq->subtype == LPFC_FCP ||
13651                             cq->subtype == LPFC_NVME) {
13652 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13653                                 if (phba->ktime_on)
13654                                         cq->isr_timestamp = ktime_get_ns();
13655                                 else
13656                                         cq->isr_timestamp = 0;
13657 #endif
13658                                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
13659                                                                        cqe);
13660                         } else {
13661                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13662                                                                       cqe);
13663                         }
13664                         if (!(++ccount % cq->entry_repost))
13665                                 break;
13666                 }
13667
13668                 /* Track the max number of CQEs processed in 1 EQ */
13669                 if (ccount > cq->CQ_max_cqe)
13670                         cq->CQ_max_cqe = ccount;
13671                 break;
13672         default:
13673                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13674                                 "0370 Invalid completion queue type (%d)\n",
13675                                 cq->type);
13676                 return;
13677         }
13678
13679         /* Catch the no cq entry condition, log an error */
13680         if (unlikely(ccount == 0))
13681                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13682                                 "0371 No entry from the CQ: identifier "
13683                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13684
13685         /* In any case, flash and re-arm the RCQ */
13686         phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
13687
13688         /* wake up worker thread if there are works to be done */
13689         if (workposted)
13690                 lpfc_worker_wake_up(phba);
13691 }
13692
13693 /**
13694  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
13695  * @phba: Pointer to HBA context object.
13696  * @cq: Pointer to associated CQ
13697  * @wcqe: Pointer to work-queue completion queue entry.
13698  *
13699  * This routine process a fast-path work queue completion entry from fast-path
13700  * event queue for FCP command response completion.
13701  **/
13702 static void
13703 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13704                              struct lpfc_wcqe_complete *wcqe)
13705 {
13706         struct lpfc_sli_ring *pring = cq->pring;
13707         struct lpfc_iocbq *cmdiocbq;
13708         struct lpfc_iocbq irspiocbq;
13709         unsigned long iflags;
13710
13711         /* Check for response status */
13712         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13713                 /* If resource errors reported from HBA, reduce queue
13714                  * depth of the SCSI device.
13715                  */
13716                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13717                      IOSTAT_LOCAL_REJECT)) &&
13718                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
13719                      IOERR_NO_RESOURCES))
13720                         phba->lpfc_rampdown_queue_depth(phba);
13721
13722                 /* Log the error status */
13723                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13724                                 "0373 FCP CQE error: status=x%x: "
13725                                 "CQE: %08x %08x %08x %08x\n",
13726                                 bf_get(lpfc_wcqe_c_status, wcqe),
13727                                 wcqe->word0, wcqe->total_data_placed,
13728                                 wcqe->parameter, wcqe->word3);
13729         }
13730
13731         /* Look up the FCP command IOCB and create pseudo response IOCB */
13732         spin_lock_irqsave(&pring->ring_lock, iflags);
13733         pring->stats.iocb_event++;
13734         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13735                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13736         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13737         if (unlikely(!cmdiocbq)) {
13738                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13739                                 "0374 FCP complete with no corresponding "
13740                                 "cmdiocb: iotag (%d)\n",
13741                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13742                 return;
13743         }
13744 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13745         cmdiocbq->isr_timestamp = cq->isr_timestamp;
13746 #endif
13747         if (cmdiocbq->iocb_cmpl == NULL) {
13748                 if (cmdiocbq->wqe_cmpl) {
13749                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13750                                 spin_lock_irqsave(&phba->hbalock, iflags);
13751                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13752                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13753                         }
13754
13755                         /* Pass the cmd_iocb and the wcqe to the upper layer */
13756                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13757                         return;
13758                 }
13759                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13760                                 "0375 FCP cmdiocb not callback function "
13761                                 "iotag: (%d)\n",
13762                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13763                 return;
13764         }
13765
13766         /* Fake the irspiocb and copy necessary response information */
13767         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13768
13769         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13770                 spin_lock_irqsave(&phba->hbalock, iflags);
13771                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13772                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13773         }
13774
13775         /* Pass the cmd_iocb and the rsp state to the upper layer */
13776         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13777 }
13778
13779 /**
13780  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13781  * @phba: Pointer to HBA context object.
13782  * @cq: Pointer to completion queue.
13783  * @wcqe: Pointer to work-queue completion queue entry.
13784  *
13785  * This routine handles an fast-path WQ entry consumed event by invoking the
13786  * proper WQ release routine to the slow-path WQ.
13787  **/
13788 static void
13789 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13790                              struct lpfc_wcqe_release *wcqe)
13791 {
13792         struct lpfc_queue *childwq;
13793         bool wqid_matched = false;
13794         uint16_t hba_wqid;
13795
13796         /* Check for fast-path FCP work queue release */
13797         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13798         list_for_each_entry(childwq, &cq->child_list, list) {
13799                 if (childwq->queue_id == hba_wqid) {
13800                         lpfc_sli4_wq_release(childwq,
13801                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13802                         if (childwq->q_flag & HBA_NVMET_WQFULL)
13803                                 lpfc_nvmet_wqfull_process(phba, childwq);
13804                         wqid_matched = true;
13805                         break;
13806                 }
13807         }
13808         /* Report warning log message if no match found */
13809         if (wqid_matched != true)
13810                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13811                                 "2580 Fast-path wqe consume event carries "
13812                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13813 }
13814
13815 /**
13816  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13817  * @phba: Pointer to HBA context object.
13818  * @rcqe: Pointer to receive-queue completion queue entry.
13819  *
13820  * This routine process a receive-queue completion queue entry.
13821  *
13822  * Return: true if work posted to worker thread, otherwise false.
13823  **/
13824 static bool
13825 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13826                             struct lpfc_rcqe *rcqe)
13827 {
13828         bool workposted = false;
13829         struct lpfc_queue *hrq;
13830         struct lpfc_queue *drq;
13831         struct rqb_dmabuf *dma_buf;
13832         struct fc_frame_header *fc_hdr;
13833         struct lpfc_nvmet_tgtport *tgtp;
13834         uint32_t status, rq_id;
13835         unsigned long iflags;
13836         uint32_t fctl, idx;
13837
13838         if ((phba->nvmet_support == 0) ||
13839             (phba->sli4_hba.nvmet_cqset == NULL))
13840                 return workposted;
13841
13842         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13843         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13844         drq = phba->sli4_hba.nvmet_mrq_data[idx];
13845
13846         /* sanity check on queue memory */
13847         if (unlikely(!hrq) || unlikely(!drq))
13848                 return workposted;
13849
13850         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13851                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13852         else
13853                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13854
13855         if ((phba->nvmet_support == 0) ||
13856             (rq_id != hrq->queue_id))
13857                 return workposted;
13858
13859         status = bf_get(lpfc_rcqe_status, rcqe);
13860         switch (status) {
13861         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13862                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13863                                 "6126 Receive Frame Truncated!!\n");
13864                 /* Drop thru */
13865         case FC_STATUS_RQ_SUCCESS:
13866                 spin_lock_irqsave(&phba->hbalock, iflags);
13867                 lpfc_sli4_rq_release(hrq, drq);
13868                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13869                 if (!dma_buf) {
13870                         hrq->RQ_no_buf_found++;
13871                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13872                         goto out;
13873                 }
13874                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13875                 hrq->RQ_rcv_buf++;
13876                 hrq->RQ_buf_posted--;
13877                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13878
13879                 /* Just some basic sanity checks on FCP Command frame */
13880                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13881                 fc_hdr->fh_f_ctl[1] << 8 |
13882                 fc_hdr->fh_f_ctl[2]);
13883                 if (((fctl &
13884                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13885                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13886                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13887                         goto drop;
13888
13889                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13890                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length,  rcqe);
13891                         lpfc_nvmet_unsol_fcp_event(
13892                                 phba, idx, dma_buf,
13893                                 cq->isr_timestamp);
13894                         return false;
13895                 }
13896 drop:
13897                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13898                 break;
13899         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13900                 if (phba->nvmet_support) {
13901                         tgtp = phba->targetport->private;
13902                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13903                                         "6401 RQE Error x%x, posted %d err_cnt "
13904                                         "%d: %x %x %x\n",
13905                                         status, hrq->RQ_buf_posted,
13906                                         hrq->RQ_no_posted_buf,
13907                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13908                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13909                                         atomic_read(&tgtp->xmt_fcp_release));
13910                 }
13911                 /* fallthrough */
13912
13913         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13914                 hrq->RQ_no_posted_buf++;
13915                 /* Post more buffers if possible */
13916                 break;
13917         }
13918 out:
13919         return workposted;
13920 }
13921
13922 /**
13923  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13924  * @cq: Pointer to the completion queue.
13925  * @eqe: Pointer to fast-path completion queue entry.
13926  *
13927  * This routine process a fast-path work queue completion entry from fast-path
13928  * event queue for FCP command response completion.
13929  **/
13930 static int
13931 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13932                          struct lpfc_cqe *cqe)
13933 {
13934         struct lpfc_wcqe_release wcqe;
13935         bool workposted = false;
13936
13937         /* Copy the work queue CQE and convert endian order if needed */
13938         lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13939
13940         /* Check and process for different type of WCQE and dispatch */
13941         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13942         case CQE_CODE_COMPL_WQE:
13943         case CQE_CODE_NVME_ERSP:
13944                 cq->CQ_wq++;
13945                 /* Process the WQ complete event */
13946                 phba->last_completion_time = jiffies;
13947                 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13948                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13949                                 (struct lpfc_wcqe_complete *)&wcqe);
13950                 if (cq->subtype == LPFC_NVME_LS)
13951                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13952                                 (struct lpfc_wcqe_complete *)&wcqe);
13953                 break;
13954         case CQE_CODE_RELEASE_WQE:
13955                 cq->CQ_release_wqe++;
13956                 /* Process the WQ release event */
13957                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13958                                 (struct lpfc_wcqe_release *)&wcqe);
13959                 break;
13960         case CQE_CODE_XRI_ABORTED:
13961                 cq->CQ_xri_aborted++;
13962                 /* Process the WQ XRI abort event */
13963                 phba->last_completion_time = jiffies;
13964                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13965                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
13966                 break;
13967         case CQE_CODE_RECEIVE_V1:
13968         case CQE_CODE_RECEIVE:
13969                 phba->last_completion_time = jiffies;
13970                 if (cq->subtype == LPFC_NVMET) {
13971                         workposted = lpfc_sli4_nvmet_handle_rcqe(
13972                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
13973                 }
13974                 break;
13975         default:
13976                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13977                                 "0144 Not a valid CQE code: x%x\n",
13978                                 bf_get(lpfc_wcqe_c_code, &wcqe));
13979                 break;
13980         }
13981         return workposted;
13982 }
13983
13984 /**
13985  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
13986  * @phba: Pointer to HBA context object.
13987  * @eqe: Pointer to fast-path event queue entry.
13988  *
13989  * This routine process a event queue entry from the fast-path event queue.
13990  * It will check the MajorCode and MinorCode to determine this is for a
13991  * completion event on a completion queue, if not, an error shall be logged
13992  * and just return. Otherwise, it will get to the corresponding completion
13993  * queue and process all the entries on the completion queue, rearm the
13994  * completion queue, and then return.
13995  **/
13996 static void
13997 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13998                         uint32_t qidx)
13999 {
14000         struct lpfc_queue *cq = NULL;
14001         uint16_t cqid, id;
14002
14003         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14004                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14005                                 "0366 Not a valid completion "
14006                                 "event: majorcode=x%x, minorcode=x%x\n",
14007                                 bf_get_le32(lpfc_eqe_major_code, eqe),
14008                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
14009                 return;
14010         }
14011
14012         /* Get the reference to the corresponding CQ */
14013         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14014
14015         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14016                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14017                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14018                         /* Process NVMET unsol rcv */
14019                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14020                         goto  process_cq;
14021                 }
14022         }
14023
14024         if (phba->sli4_hba.nvme_cq_map &&
14025             (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
14026                 /* Process NVME / NVMET command completion */
14027                 cq = phba->sli4_hba.nvme_cq[qidx];
14028                 goto  process_cq;
14029         }
14030
14031         if (phba->sli4_hba.fcp_cq_map &&
14032             (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
14033                 /* Process FCP command completion */
14034                 cq = phba->sli4_hba.fcp_cq[qidx];
14035                 goto  process_cq;
14036         }
14037
14038         if (phba->sli4_hba.nvmels_cq &&
14039             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14040                 /* Process NVME unsol rcv */
14041                 cq = phba->sli4_hba.nvmels_cq;
14042         }
14043
14044         /* Otherwise this is a Slow path event */
14045         if (cq == NULL) {
14046                 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
14047                 return;
14048         }
14049
14050 process_cq:
14051         if (unlikely(cqid != cq->queue_id)) {
14052                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14053                                 "0368 Miss-matched fast-path completion "
14054                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14055                                 cqid, cq->queue_id);
14056                 return;
14057         }
14058
14059         /* Save EQ associated with this CQ */
14060         cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
14061
14062         if (!queue_work(phba->wq, &cq->irqwork))
14063                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14064                                 "0363 Cannot schedule soft IRQ "
14065                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14066                                 cqid, cq->queue_id, smp_processor_id());
14067 }
14068
14069 /**
14070  * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14071  * @phba: Pointer to HBA context object.
14072  * @eqe: Pointer to fast-path event queue entry.
14073  *
14074  * This routine process a event queue entry from the fast-path event queue.
14075  * It will check the MajorCode and MinorCode to determine this is for a
14076  * completion event on a completion queue, if not, an error shall be logged
14077  * and just return. Otherwise, it will get to the corresponding completion
14078  * queue and process all the entries on the completion queue, rearm the
14079  * completion queue, and then return.
14080  **/
14081 static void
14082 lpfc_sli4_hba_process_cq(struct work_struct *work)
14083 {
14084         struct lpfc_queue *cq =
14085                 container_of(work, struct lpfc_queue, irqwork);
14086         struct lpfc_hba *phba = cq->phba;
14087         struct lpfc_cqe *cqe;
14088         bool workposted = false;
14089         int ccount = 0;
14090
14091         /* Process all the entries to the CQ */
14092         while ((cqe = lpfc_sli4_cq_get(cq))) {
14093 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14094                 if (phba->ktime_on)
14095                         cq->isr_timestamp = ktime_get_ns();
14096                 else
14097                         cq->isr_timestamp = 0;
14098 #endif
14099                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
14100                 if (!(++ccount % cq->entry_repost))
14101                         break;
14102         }
14103
14104         /* Track the max number of CQEs processed in 1 EQ */
14105         if (ccount > cq->CQ_max_cqe)
14106                 cq->CQ_max_cqe = ccount;
14107         cq->assoc_qp->EQ_cqe_cnt += ccount;
14108
14109         /* Catch the no cq entry condition */
14110         if (unlikely(ccount == 0))
14111                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14112                                 "0369 No entry from fast-path completion "
14113                                 "queue fcpcqid=%d\n", cq->queue_id);
14114
14115         /* In any case, flash and re-arm the CQ */
14116         phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
14117
14118         /* wake up worker thread if there are works to be done */
14119         if (workposted)
14120                 lpfc_worker_wake_up(phba);
14121 }
14122
14123 static void
14124 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
14125 {
14126         struct lpfc_eqe *eqe;
14127
14128         /* walk all the EQ entries and drop on the floor */
14129         while ((eqe = lpfc_sli4_eq_get(eq)))
14130                 ;
14131
14132         /* Clear and re-arm the EQ */
14133         phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
14134 }
14135
14136
14137 /**
14138  * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
14139  *                           entry
14140  * @phba: Pointer to HBA context object.
14141  * @eqe: Pointer to fast-path event queue entry.
14142  *
14143  * This routine process a event queue entry from the Flash Optimized Fabric
14144  * event queue.  It will check the MajorCode and MinorCode to determine this
14145  * is for a completion event on a completion queue, if not, an error shall be
14146  * logged and just return. Otherwise, it will get to the corresponding
14147  * completion queue and process all the entries on the completion queue, rearm
14148  * the completion queue, and then return.
14149  **/
14150 static void
14151 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
14152 {
14153         struct lpfc_queue *cq;
14154         uint16_t cqid;
14155
14156         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14157                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14158                                 "9147 Not a valid completion "
14159                                 "event: majorcode=x%x, minorcode=x%x\n",
14160                                 bf_get_le32(lpfc_eqe_major_code, eqe),
14161                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
14162                 return;
14163         }
14164
14165         /* Get the reference to the corresponding CQ */
14166         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14167
14168         /* Next check for OAS */
14169         cq = phba->sli4_hba.oas_cq;
14170         if (unlikely(!cq)) {
14171                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14172                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14173                                         "9148 OAS completion queue "
14174                                         "does not exist\n");
14175                 return;
14176         }
14177
14178         if (unlikely(cqid != cq->queue_id)) {
14179                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14180                                 "9149 Miss-matched fast-path compl "
14181                                 "queue id: eqcqid=%d, fcpcqid=%d\n",
14182                                 cqid, cq->queue_id);
14183                 return;
14184         }
14185
14186         /* Save EQ associated with this CQ */
14187         cq->assoc_qp = phba->sli4_hba.fof_eq;
14188
14189         /* CQ work will be processed on CPU affinitized to this IRQ */
14190         if (!queue_work(phba->wq, &cq->irqwork))
14191                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14192                                 "0367 Cannot schedule soft IRQ "
14193                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14194                                 cqid, cq->queue_id, smp_processor_id());
14195 }
14196
14197 /**
14198  * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
14199  * @irq: Interrupt number.
14200  * @dev_id: The device context pointer.
14201  *
14202  * This function is directly called from the PCI layer as an interrupt
14203  * service routine when device with SLI-4 interface spec is enabled with
14204  * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
14205  * IOCB ring event in the HBA. However, when the device is enabled with either
14206  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14207  * device-level interrupt handler. When the PCI slot is in error recovery
14208  * or the HBA is undergoing initialization, the interrupt handler will not
14209  * process the interrupt. The Flash Optimized Fabric ring event are handled in
14210  * the intrrupt context. This function is called without any lock held.
14211  * It gets the hbalock to access and update SLI data structures. Note that,
14212  * the EQ to CQ are one-to-one map such that the EQ index is
14213  * equal to that of CQ index.
14214  *
14215  * This function returns IRQ_HANDLED when interrupt is handled else it
14216  * returns IRQ_NONE.
14217  **/
14218 irqreturn_t
14219 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
14220 {
14221         struct lpfc_hba *phba;
14222         struct lpfc_hba_eq_hdl *hba_eq_hdl;
14223         struct lpfc_queue *eq;
14224         struct lpfc_eqe *eqe;
14225         unsigned long iflag;
14226         int ecount = 0;
14227
14228         /* Get the driver's phba structure from the dev_id */
14229         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14230         phba = hba_eq_hdl->phba;
14231
14232         if (unlikely(!phba))
14233                 return IRQ_NONE;
14234
14235         /* Get to the EQ struct associated with this vector */
14236         eq = phba->sli4_hba.fof_eq;
14237         if (unlikely(!eq))
14238                 return IRQ_NONE;
14239
14240         /* Check device state for handling interrupt */
14241         if (unlikely(lpfc_intr_state_check(phba))) {
14242                 /* Check again for link_state with lock held */
14243                 spin_lock_irqsave(&phba->hbalock, iflag);
14244                 if (phba->link_state < LPFC_LINK_DOWN)
14245                         /* Flush, clear interrupt, and rearm the EQ */
14246                         lpfc_sli4_eq_flush(phba, eq);
14247                 spin_unlock_irqrestore(&phba->hbalock, iflag);
14248                 return IRQ_NONE;
14249         }
14250
14251         /*
14252          * Process all the event on FCP fast-path EQ
14253          */
14254         while ((eqe = lpfc_sli4_eq_get(eq))) {
14255                 lpfc_sli4_fof_handle_eqe(phba, eqe);
14256                 if (!(++ecount % eq->entry_repost))
14257                         break;
14258                 eq->EQ_processed++;
14259         }
14260
14261         /* Track the max number of EQEs processed in 1 intr */
14262         if (ecount > eq->EQ_max_eqe)
14263                 eq->EQ_max_eqe = ecount;
14264
14265
14266         if (unlikely(ecount == 0)) {
14267                 eq->EQ_no_entry++;
14268
14269                 if (phba->intr_type == MSIX)
14270                         /* MSI-X treated interrupt served as no EQ share INT */
14271                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14272                                         "9145 MSI-X interrupt with no EQE\n");
14273                 else {
14274                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14275                                         "9146 ISR interrupt with no EQE\n");
14276                         /* Non MSI-X treated on interrupt as EQ share INT */
14277                         return IRQ_NONE;
14278                 }
14279         }
14280         /* Always clear and re-arm the fast-path EQ */
14281         phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
14282         return IRQ_HANDLED;
14283 }
14284
14285 /**
14286  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
14287  * @irq: Interrupt number.
14288  * @dev_id: The device context pointer.
14289  *
14290  * This function is directly called from the PCI layer as an interrupt
14291  * service routine when device with SLI-4 interface spec is enabled with
14292  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14293  * ring event in the HBA. However, when the device is enabled with either
14294  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14295  * device-level interrupt handler. When the PCI slot is in error recovery
14296  * or the HBA is undergoing initialization, the interrupt handler will not
14297  * process the interrupt. The SCSI FCP fast-path ring event are handled in
14298  * the intrrupt context. This function is called without any lock held.
14299  * It gets the hbalock to access and update SLI data structures. Note that,
14300  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14301  * equal to that of FCP CQ index.
14302  *
14303  * The link attention and ELS ring attention events are handled
14304  * by the worker thread. The interrupt handler signals the worker thread
14305  * and returns for these events. This function is called without any lock
14306  * held. It gets the hbalock to access and update SLI data structures.
14307  *
14308  * This function returns IRQ_HANDLED when interrupt is handled else it
14309  * returns IRQ_NONE.
14310  **/
14311 irqreturn_t
14312 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
14313 {
14314         struct lpfc_hba *phba;
14315         struct lpfc_hba_eq_hdl *hba_eq_hdl;
14316         struct lpfc_queue *fpeq;
14317         struct lpfc_eqe *eqe;
14318         unsigned long iflag;
14319         int ecount = 0;
14320         int hba_eqidx;
14321
14322         /* Get the driver's phba structure from the dev_id */
14323         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14324         phba = hba_eq_hdl->phba;
14325         hba_eqidx = hba_eq_hdl->idx;
14326
14327         if (unlikely(!phba))
14328                 return IRQ_NONE;
14329         if (unlikely(!phba->sli4_hba.hba_eq))
14330                 return IRQ_NONE;
14331
14332         /* Get to the EQ struct associated with this vector */
14333         fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
14334         if (unlikely(!fpeq))
14335                 return IRQ_NONE;
14336
14337         if (lpfc_fcp_look_ahead) {
14338                 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
14339                         phba->sli4_hba.sli4_eq_clr_intr(fpeq);
14340                 else {
14341                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14342                         return IRQ_NONE;
14343                 }
14344         }
14345
14346         /* Check device state for handling interrupt */
14347         if (unlikely(lpfc_intr_state_check(phba))) {
14348                 /* Check again for link_state with lock held */
14349                 spin_lock_irqsave(&phba->hbalock, iflag);
14350                 if (phba->link_state < LPFC_LINK_DOWN)
14351                         /* Flush, clear interrupt, and rearm the EQ */
14352                         lpfc_sli4_eq_flush(phba, fpeq);
14353                 spin_unlock_irqrestore(&phba->hbalock, iflag);
14354                 if (lpfc_fcp_look_ahead)
14355                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14356                 return IRQ_NONE;
14357         }
14358
14359         /*
14360          * Process all the event on FCP fast-path EQ
14361          */
14362         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
14363                 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14364                 if (!(++ecount % fpeq->entry_repost))
14365                         break;
14366                 fpeq->EQ_processed++;
14367         }
14368
14369         /* Track the max number of EQEs processed in 1 intr */
14370         if (ecount > fpeq->EQ_max_eqe)
14371                 fpeq->EQ_max_eqe = ecount;
14372
14373         /* Always clear and re-arm the fast-path EQ */
14374         phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
14375
14376         if (unlikely(ecount == 0)) {
14377                 fpeq->EQ_no_entry++;
14378
14379                 if (lpfc_fcp_look_ahead) {
14380                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14381                         return IRQ_NONE;
14382                 }
14383
14384                 if (phba->intr_type == MSIX)
14385                         /* MSI-X treated interrupt served as no EQ share INT */
14386                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14387                                         "0358 MSI-X interrupt with no EQE\n");
14388                 else
14389                         /* Non MSI-X treated on interrupt as EQ share INT */
14390                         return IRQ_NONE;
14391         }
14392
14393         if (lpfc_fcp_look_ahead)
14394                 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14395
14396         return IRQ_HANDLED;
14397 } /* lpfc_sli4_fp_intr_handler */
14398
14399 /**
14400  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14401  * @irq: Interrupt number.
14402  * @dev_id: The device context pointer.
14403  *
14404  * This function is the device-level interrupt handler to device with SLI-4
14405  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14406  * interrupt mode is enabled and there is an event in the HBA which requires
14407  * driver attention. This function invokes the slow-path interrupt attention
14408  * handling function and fast-path interrupt attention handling function in
14409  * turn to process the relevant HBA attention events. This function is called
14410  * without any lock held. It gets the hbalock to access and update SLI data
14411  * structures.
14412  *
14413  * This function returns IRQ_HANDLED when interrupt is handled, else it
14414  * returns IRQ_NONE.
14415  **/
14416 irqreturn_t
14417 lpfc_sli4_intr_handler(int irq, void *dev_id)
14418 {
14419         struct lpfc_hba  *phba;
14420         irqreturn_t hba_irq_rc;
14421         bool hba_handled = false;
14422         int qidx;
14423
14424         /* Get the driver's phba structure from the dev_id */
14425         phba = (struct lpfc_hba *)dev_id;
14426
14427         if (unlikely(!phba))
14428                 return IRQ_NONE;
14429
14430         /*
14431          * Invoke fast-path host attention interrupt handling as appropriate.
14432          */
14433         for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
14434                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
14435                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14436                 if (hba_irq_rc == IRQ_HANDLED)
14437                         hba_handled |= true;
14438         }
14439
14440         if (phba->cfg_fof) {
14441                 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
14442                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14443                 if (hba_irq_rc == IRQ_HANDLED)
14444                         hba_handled |= true;
14445         }
14446
14447         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
14448 } /* lpfc_sli4_intr_handler */
14449
14450 /**
14451  * lpfc_sli4_queue_free - free a queue structure and associated memory
14452  * @queue: The queue structure to free.
14453  *
14454  * This function frees a queue structure and the DMAable memory used for
14455  * the host resident queue. This function must be called after destroying the
14456  * queue on the HBA.
14457  **/
14458 void
14459 lpfc_sli4_queue_free(struct lpfc_queue *queue)
14460 {
14461         struct lpfc_dmabuf *dmabuf;
14462
14463         if (!queue)
14464                 return;
14465
14466         while (!list_empty(&queue->page_list)) {
14467                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14468                                  list);
14469                 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
14470                                   dmabuf->virt, dmabuf->phys);
14471                 kfree(dmabuf);
14472         }
14473         if (queue->rqbp) {
14474                 lpfc_free_rq_buffer(queue->phba, queue);
14475                 kfree(queue->rqbp);
14476         }
14477
14478         if (!list_empty(&queue->wq_list))
14479                 list_del(&queue->wq_list);
14480
14481         kfree(queue);
14482         return;
14483 }
14484
14485 /**
14486  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14487  * @phba: The HBA that this queue is being created on.
14488  * @page_size: The size of a queue page
14489  * @entry_size: The size of each queue entry for this queue.
14490  * @entry count: The number of entries that this queue will handle.
14491  *
14492  * This function allocates a queue structure and the DMAable memory used for
14493  * the host resident queue. This function must be called before creating the
14494  * queue on the HBA.
14495  **/
14496 struct lpfc_queue *
14497 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14498                       uint32_t entry_size, uint32_t entry_count)
14499 {
14500         struct lpfc_queue *queue;
14501         struct lpfc_dmabuf *dmabuf;
14502         int x, total_qe_count;
14503         void *dma_pointer;
14504         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14505
14506         if (!phba->sli4_hba.pc_sli4_params.supported)
14507                 hw_page_size = page_size;
14508
14509         queue = kzalloc(sizeof(struct lpfc_queue) +
14510                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14511         if (!queue)
14512                 return NULL;
14513         queue->page_count = (ALIGN(entry_size * entry_count,
14514                         hw_page_size))/hw_page_size;
14515
14516         /* If needed, Adjust page count to match the max the adapter supports */
14517         if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14518                 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14519
14520         INIT_LIST_HEAD(&queue->list);
14521         INIT_LIST_HEAD(&queue->wq_list);
14522         INIT_LIST_HEAD(&queue->wqfull_list);
14523         INIT_LIST_HEAD(&queue->page_list);
14524         INIT_LIST_HEAD(&queue->child_list);
14525
14526         /* Set queue parameters now.  If the system cannot provide memory
14527          * resources, the free routine needs to know what was allocated.
14528          */
14529         queue->entry_size = entry_size;
14530         queue->entry_count = entry_count;
14531         queue->page_size = hw_page_size;
14532         queue->phba = phba;
14533
14534         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14535                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14536                 if (!dmabuf)
14537                         goto out_fail;
14538                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14539                                                    hw_page_size, &dmabuf->phys,
14540                                                    GFP_KERNEL);
14541                 if (!dmabuf->virt) {
14542                         kfree(dmabuf);
14543                         goto out_fail;
14544                 }
14545                 dmabuf->buffer_tag = x;
14546                 list_add_tail(&dmabuf->list, &queue->page_list);
14547                 /* initialize queue's entry array */
14548                 dma_pointer = dmabuf->virt;
14549                 for (; total_qe_count < entry_count &&
14550                      dma_pointer < (hw_page_size + dmabuf->virt);
14551                      total_qe_count++, dma_pointer += entry_size) {
14552                         queue->qe[total_qe_count].address = dma_pointer;
14553                 }
14554         }
14555         INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14556         INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
14557
14558         /* entry_repost will be set during q creation */
14559
14560         return queue;
14561 out_fail:
14562         lpfc_sli4_queue_free(queue);
14563         return NULL;
14564 }
14565
14566 /**
14567  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14568  * @phba: HBA structure that indicates port to create a queue on.
14569  * @pci_barset: PCI BAR set flag.
14570  *
14571  * This function shall perform iomap of the specified PCI BAR address to host
14572  * memory address if not already done so and return it. The returned host
14573  * memory address can be NULL.
14574  */
14575 static void __iomem *
14576 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14577 {
14578         if (!phba->pcidev)
14579                 return NULL;
14580
14581         switch (pci_barset) {
14582         case WQ_PCI_BAR_0_AND_1:
14583                 return phba->pci_bar0_memmap_p;
14584         case WQ_PCI_BAR_2_AND_3:
14585                 return phba->pci_bar2_memmap_p;
14586         case WQ_PCI_BAR_4_AND_5:
14587                 return phba->pci_bar4_memmap_p;
14588         default:
14589                 break;
14590         }
14591         return NULL;
14592 }
14593
14594 /**
14595  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
14596  * @phba: HBA structure that indicates port to create a queue on.
14597  * @startq: The starting FCP EQ to modify
14598  *
14599  * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
14600  * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14601  * updated in one mailbox command.
14602  *
14603  * The @phba struct is used to send mailbox command to HBA. The @startq
14604  * is used to get the starting FCP EQ to change.
14605  * This function is asynchronous and will wait for the mailbox
14606  * command to finish before continuing.
14607  *
14608  * On success this function will return a zero. If unable to allocate enough
14609  * memory this function will return -ENOMEM. If the queue create mailbox command
14610  * fails this function will return -ENXIO.
14611  **/
14612 int
14613 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14614                          uint32_t numq, uint32_t imax)
14615 {
14616         struct lpfc_mbx_modify_eq_delay *eq_delay;
14617         LPFC_MBOXQ_t *mbox;
14618         struct lpfc_queue *eq;
14619         int cnt, rc, length, status = 0;
14620         uint32_t shdr_status, shdr_add_status;
14621         uint32_t result, val;
14622         int qidx;
14623         union lpfc_sli4_cfg_shdr *shdr;
14624         uint16_t dmult;
14625
14626         if (startq >= phba->io_channel_irqs)
14627                 return 0;
14628
14629         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14630         if (!mbox)
14631                 return -ENOMEM;
14632         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14633                   sizeof(struct lpfc_sli4_cfg_mhdr));
14634         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14635                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14636                          length, LPFC_SLI4_MBX_EMBED);
14637         eq_delay = &mbox->u.mqe.un.eq_delay;
14638
14639         /* Calculate delay multiper from maximum interrupt per second */
14640         result = imax / phba->io_channel_irqs;
14641         if (result > LPFC_DMULT_CONST || result == 0)
14642                 dmult = 0;
14643         else
14644                 dmult = LPFC_DMULT_CONST/result - 1;
14645         if (dmult > LPFC_DMULT_MAX)
14646                 dmult = LPFC_DMULT_MAX;
14647
14648         cnt = 0;
14649         for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14650                 eq = phba->sli4_hba.hba_eq[qidx];
14651                 if (!eq)
14652                         continue;
14653                 eq->q_mode = imax;
14654                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14655                 eq_delay->u.request.eq[cnt].phase = 0;
14656                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14657                 cnt++;
14658
14659                 /* q_mode is only used for auto_imax */
14660                 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14661                         /* Use EQ Delay Register method for q_mode */
14662
14663                         /* Convert for EQ Delay register */
14664                         val =  phba->cfg_fcp_imax;
14665                         if (val) {
14666                                 /* First, interrupts per sec per EQ */
14667                                 val = phba->cfg_fcp_imax /
14668                                         phba->io_channel_irqs;
14669
14670                                 /* us delay between each interrupt */
14671                                 val = LPFC_SEC_TO_USEC / val;
14672                         }
14673                         eq->q_mode = val;
14674                 } else {
14675                         eq->q_mode = imax;
14676                 }
14677
14678                 if (cnt >= numq)
14679                         break;
14680         }
14681         eq_delay->u.request.num_eq = cnt;
14682
14683         mbox->vport = phba->pport;
14684         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14685         mbox->context1 = NULL;
14686         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14687         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14688         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14689         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14690         if (shdr_status || shdr_add_status || rc) {
14691                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14692                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
14693                                 "status x%x add_status x%x, mbx status x%x\n",
14694                                 shdr_status, shdr_add_status, rc);
14695                 status = -ENXIO;
14696         }
14697         mempool_free(mbox, phba->mbox_mem_pool);
14698         return status;
14699 }
14700
14701 /**
14702  * lpfc_eq_create - Create an Event Queue on the HBA
14703  * @phba: HBA structure that indicates port to create a queue on.
14704  * @eq: The queue structure to use to create the event queue.
14705  * @imax: The maximum interrupt per second limit.
14706  *
14707  * This function creates an event queue, as detailed in @eq, on a port,
14708  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14709  *
14710  * The @phba struct is used to send mailbox command to HBA. The @eq struct
14711  * is used to get the entry count and entry size that are necessary to
14712  * determine the number of pages to allocate and use for this queue. This
14713  * function will send the EQ_CREATE mailbox command to the HBA to setup the
14714  * event queue. This function is asynchronous and will wait for the mailbox
14715  * command to finish before continuing.
14716  *
14717  * On success this function will return a zero. If unable to allocate enough
14718  * memory this function will return -ENOMEM. If the queue create mailbox command
14719  * fails this function will return -ENXIO.
14720  **/
14721 int
14722 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
14723 {
14724         struct lpfc_mbx_eq_create *eq_create;
14725         LPFC_MBOXQ_t *mbox;
14726         int rc, length, status = 0;
14727         struct lpfc_dmabuf *dmabuf;
14728         uint32_t shdr_status, shdr_add_status;
14729         union lpfc_sli4_cfg_shdr *shdr;
14730         uint16_t dmult;
14731         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14732
14733         /* sanity check on queue memory */
14734         if (!eq)
14735                 return -ENODEV;
14736         if (!phba->sli4_hba.pc_sli4_params.supported)
14737                 hw_page_size = SLI4_PAGE_SIZE;
14738
14739         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14740         if (!mbox)
14741                 return -ENOMEM;
14742         length = (sizeof(struct lpfc_mbx_eq_create) -
14743                   sizeof(struct lpfc_sli4_cfg_mhdr));
14744         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14745                          LPFC_MBOX_OPCODE_EQ_CREATE,
14746                          length, LPFC_SLI4_MBX_EMBED);
14747         eq_create = &mbox->u.mqe.un.eq_create;
14748         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14749         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14750                eq->page_count);
14751         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14752                LPFC_EQE_SIZE);
14753         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
14754
14755         /* Use version 2 of CREATE_EQ if eqav is set */
14756         if (phba->sli4_hba.pc_sli4_params.eqav) {
14757                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14758                        LPFC_Q_CREATE_VERSION_2);
14759                 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14760                        phba->sli4_hba.pc_sli4_params.eqav);
14761         }
14762
14763         /* don't setup delay multiplier using EQ_CREATE */
14764         dmult = 0;
14765         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14766                dmult);
14767         switch (eq->entry_count) {
14768         default:
14769                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14770                                 "0360 Unsupported EQ count. (%d)\n",
14771                                 eq->entry_count);
14772                 if (eq->entry_count < 256)
14773                         return -EINVAL;
14774                 /* otherwise default to smallest count (drop through) */
14775         case 256:
14776                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14777                        LPFC_EQ_CNT_256);
14778                 break;
14779         case 512:
14780                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14781                        LPFC_EQ_CNT_512);
14782                 break;
14783         case 1024:
14784                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14785                        LPFC_EQ_CNT_1024);
14786                 break;
14787         case 2048:
14788                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14789                        LPFC_EQ_CNT_2048);
14790                 break;
14791         case 4096:
14792                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14793                        LPFC_EQ_CNT_4096);
14794                 break;
14795         }
14796         list_for_each_entry(dmabuf, &eq->page_list, list) {
14797                 memset(dmabuf->virt, 0, hw_page_size);
14798                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14799                                         putPaddrLow(dmabuf->phys);
14800                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14801                                         putPaddrHigh(dmabuf->phys);
14802         }
14803         mbox->vport = phba->pport;
14804         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14805         mbox->context1 = NULL;
14806         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14807         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14808         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14809         if (shdr_status || shdr_add_status || rc) {
14810                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14811                                 "2500 EQ_CREATE mailbox failed with "
14812                                 "status x%x add_status x%x, mbx status x%x\n",
14813                                 shdr_status, shdr_add_status, rc);
14814                 status = -ENXIO;
14815         }
14816         eq->type = LPFC_EQ;
14817         eq->subtype = LPFC_NONE;
14818         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14819         if (eq->queue_id == 0xFFFF)
14820                 status = -ENXIO;
14821         eq->host_index = 0;
14822         eq->hba_index = 0;
14823         eq->entry_repost = LPFC_EQ_REPOST;
14824
14825         mempool_free(mbox, phba->mbox_mem_pool);
14826         return status;
14827 }
14828
14829 /**
14830  * lpfc_cq_create - Create a Completion Queue on the HBA
14831  * @phba: HBA structure that indicates port to create a queue on.
14832  * @cq: The queue structure to use to create the completion queue.
14833  * @eq: The event queue to bind this completion queue to.
14834  *
14835  * This function creates a completion queue, as detailed in @wq, on a port,
14836  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14837  *
14838  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14839  * is used to get the entry count and entry size that are necessary to
14840  * determine the number of pages to allocate and use for this queue. The @eq
14841  * is used to indicate which event queue to bind this completion queue to. This
14842  * function will send the CQ_CREATE mailbox command to the HBA to setup the
14843  * completion queue. This function is asynchronous and will wait for the mailbox
14844  * command to finish before continuing.
14845  *
14846  * On success this function will return a zero. If unable to allocate enough
14847  * memory this function will return -ENOMEM. If the queue create mailbox command
14848  * fails this function will return -ENXIO.
14849  **/
14850 int
14851 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14852                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14853 {
14854         struct lpfc_mbx_cq_create *cq_create;
14855         struct lpfc_dmabuf *dmabuf;
14856         LPFC_MBOXQ_t *mbox;
14857         int rc, length, status = 0;
14858         uint32_t shdr_status, shdr_add_status;
14859         union lpfc_sli4_cfg_shdr *shdr;
14860
14861         /* sanity check on queue memory */
14862         if (!cq || !eq)
14863                 return -ENODEV;
14864
14865         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14866         if (!mbox)
14867                 return -ENOMEM;
14868         length = (sizeof(struct lpfc_mbx_cq_create) -
14869                   sizeof(struct lpfc_sli4_cfg_mhdr));
14870         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14871                          LPFC_MBOX_OPCODE_CQ_CREATE,
14872                          length, LPFC_SLI4_MBX_EMBED);
14873         cq_create = &mbox->u.mqe.un.cq_create;
14874         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14875         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14876                     cq->page_count);
14877         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14878         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14879         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14880                phba->sli4_hba.pc_sli4_params.cqv);
14881         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14882                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14883                        (cq->page_size / SLI4_PAGE_SIZE));
14884                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14885                        eq->queue_id);
14886                 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14887                        phba->sli4_hba.pc_sli4_params.cqav);
14888         } else {
14889                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14890                        eq->queue_id);
14891         }
14892         switch (cq->entry_count) {
14893         case 2048:
14894         case 4096:
14895                 if (phba->sli4_hba.pc_sli4_params.cqv ==
14896                     LPFC_Q_CREATE_VERSION_2) {
14897                         cq_create->u.request.context.lpfc_cq_context_count =
14898                                 cq->entry_count;
14899                         bf_set(lpfc_cq_context_count,
14900                                &cq_create->u.request.context,
14901                                LPFC_CQ_CNT_WORD7);
14902                         break;
14903                 }
14904                 /* Fall Thru */
14905         default:
14906                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14907                                 "0361 Unsupported CQ count: "
14908                                 "entry cnt %d sz %d pg cnt %d\n",
14909                                 cq->entry_count, cq->entry_size,
14910                                 cq->page_count);
14911                 if (cq->entry_count < 256) {
14912                         status = -EINVAL;
14913                         goto out;
14914                 }
14915                 /* otherwise default to smallest count (drop through) */
14916         case 256:
14917                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14918                        LPFC_CQ_CNT_256);
14919                 break;
14920         case 512:
14921                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14922                        LPFC_CQ_CNT_512);
14923                 break;
14924         case 1024:
14925                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14926                        LPFC_CQ_CNT_1024);
14927                 break;
14928         }
14929         list_for_each_entry(dmabuf, &cq->page_list, list) {
14930                 memset(dmabuf->virt, 0, cq->page_size);
14931                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14932                                         putPaddrLow(dmabuf->phys);
14933                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14934                                         putPaddrHigh(dmabuf->phys);
14935         }
14936         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14937
14938         /* The IOCTL status is embedded in the mailbox subheader. */
14939         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14940         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14941         if (shdr_status || shdr_add_status || rc) {
14942                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14943                                 "2501 CQ_CREATE mailbox failed with "
14944                                 "status x%x add_status x%x, mbx status x%x\n",
14945                                 shdr_status, shdr_add_status, rc);
14946                 status = -ENXIO;
14947                 goto out;
14948         }
14949         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14950         if (cq->queue_id == 0xFFFF) {
14951                 status = -ENXIO;
14952                 goto out;
14953         }
14954         /* link the cq onto the parent eq child list */
14955         list_add_tail(&cq->list, &eq->child_list);
14956         /* Set up completion queue's type and subtype */
14957         cq->type = type;
14958         cq->subtype = subtype;
14959         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14960         cq->assoc_qid = eq->queue_id;
14961         cq->host_index = 0;
14962         cq->hba_index = 0;
14963         cq->entry_repost = LPFC_CQ_REPOST;
14964
14965 out:
14966         mempool_free(mbox, phba->mbox_mem_pool);
14967         return status;
14968 }
14969
14970 /**
14971  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14972  * @phba: HBA structure that indicates port to create a queue on.
14973  * @cqp: The queue structure array to use to create the completion queues.
14974  * @eqp: The event queue array to bind these completion queues to.
14975  *
14976  * This function creates a set of  completion queue, s to support MRQ
14977  * as detailed in @cqp, on a port,
14978  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14979  *
14980  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14981  * is used to get the entry count and entry size that are necessary to
14982  * determine the number of pages to allocate and use for this queue. The @eq
14983  * is used to indicate which event queue to bind this completion queue to. This
14984  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14985  * completion queue. This function is asynchronous and will wait for the mailbox
14986  * command to finish before continuing.
14987  *
14988  * On success this function will return a zero. If unable to allocate enough
14989  * memory this function will return -ENOMEM. If the queue create mailbox command
14990  * fails this function will return -ENXIO.
14991  **/
14992 int
14993 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14994                    struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14995 {
14996         struct lpfc_queue *cq;
14997         struct lpfc_queue *eq;
14998         struct lpfc_mbx_cq_create_set *cq_set;
14999         struct lpfc_dmabuf *dmabuf;
15000         LPFC_MBOXQ_t *mbox;
15001         int rc, length, alloclen, status = 0;
15002         int cnt, idx, numcq, page_idx = 0;
15003         uint32_t shdr_status, shdr_add_status;
15004         union lpfc_sli4_cfg_shdr *shdr;
15005         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15006
15007         /* sanity check on queue memory */
15008         numcq = phba->cfg_nvmet_mrq;
15009         if (!cqp || !eqp || !numcq)
15010                 return -ENODEV;
15011
15012         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15013         if (!mbox)
15014                 return -ENOMEM;
15015
15016         length = sizeof(struct lpfc_mbx_cq_create_set);
15017         length += ((numcq * cqp[0]->page_count) *
15018                    sizeof(struct dma_address));
15019         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15020                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15021                         LPFC_SLI4_MBX_NEMBED);
15022         if (alloclen < length) {
15023                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15024                                 "3098 Allocated DMA memory size (%d) is "
15025                                 "less than the requested DMA memory size "
15026                                 "(%d)\n", alloclen, length);
15027                 status = -ENOMEM;
15028                 goto out;
15029         }
15030         cq_set = mbox->sge_array->addr[0];
15031         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15032         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15033
15034         for (idx = 0; idx < numcq; idx++) {
15035                 cq = cqp[idx];
15036                 eq = eqp[idx];
15037                 if (!cq || !eq) {
15038                         status = -ENOMEM;
15039                         goto out;
15040                 }
15041                 if (!phba->sli4_hba.pc_sli4_params.supported)
15042                         hw_page_size = cq->page_size;
15043
15044                 switch (idx) {
15045                 case 0:
15046                         bf_set(lpfc_mbx_cq_create_set_page_size,
15047                                &cq_set->u.request,
15048                                (hw_page_size / SLI4_PAGE_SIZE));
15049                         bf_set(lpfc_mbx_cq_create_set_num_pages,
15050                                &cq_set->u.request, cq->page_count);
15051                         bf_set(lpfc_mbx_cq_create_set_evt,
15052                                &cq_set->u.request, 1);
15053                         bf_set(lpfc_mbx_cq_create_set_valid,
15054                                &cq_set->u.request, 1);
15055                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
15056                                &cq_set->u.request, 0);
15057                         bf_set(lpfc_mbx_cq_create_set_num_cq,
15058                                &cq_set->u.request, numcq);
15059                         bf_set(lpfc_mbx_cq_create_set_autovalid,
15060                                &cq_set->u.request,
15061                                phba->sli4_hba.pc_sli4_params.cqav);
15062                         switch (cq->entry_count) {
15063                         case 2048:
15064                         case 4096:
15065                                 if (phba->sli4_hba.pc_sli4_params.cqv ==
15066                                     LPFC_Q_CREATE_VERSION_2) {
15067                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15068                                                &cq_set->u.request,
15069                                                 cq->entry_count);
15070                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15071                                                &cq_set->u.request,
15072                                                LPFC_CQ_CNT_WORD7);
15073                                         break;
15074                                 }
15075                                 /* Fall Thru */
15076                         default:
15077                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15078                                                 "3118 Bad CQ count. (%d)\n",
15079                                                 cq->entry_count);
15080                                 if (cq->entry_count < 256) {
15081                                         status = -EINVAL;
15082                                         goto out;
15083                                 }
15084                                 /* otherwise default to smallest (drop thru) */
15085                         case 256:
15086                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15087                                        &cq_set->u.request, LPFC_CQ_CNT_256);
15088                                 break;
15089                         case 512:
15090                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15091                                        &cq_set->u.request, LPFC_CQ_CNT_512);
15092                                 break;
15093                         case 1024:
15094                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15095                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
15096                                 break;
15097                         }
15098                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
15099                                &cq_set->u.request, eq->queue_id);
15100                         break;
15101                 case 1:
15102                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
15103                                &cq_set->u.request, eq->queue_id);
15104                         break;
15105                 case 2:
15106                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
15107                                &cq_set->u.request, eq->queue_id);
15108                         break;
15109                 case 3:
15110                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
15111                                &cq_set->u.request, eq->queue_id);
15112                         break;
15113                 case 4:
15114                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
15115                                &cq_set->u.request, eq->queue_id);
15116                         break;
15117                 case 5:
15118                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
15119                                &cq_set->u.request, eq->queue_id);
15120                         break;
15121                 case 6:
15122                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
15123                                &cq_set->u.request, eq->queue_id);
15124                         break;
15125                 case 7:
15126                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
15127                                &cq_set->u.request, eq->queue_id);
15128                         break;
15129                 case 8:
15130                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
15131                                &cq_set->u.request, eq->queue_id);
15132                         break;
15133                 case 9:
15134                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
15135                                &cq_set->u.request, eq->queue_id);
15136                         break;
15137                 case 10:
15138                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
15139                                &cq_set->u.request, eq->queue_id);
15140                         break;
15141                 case 11:
15142                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
15143                                &cq_set->u.request, eq->queue_id);
15144                         break;
15145                 case 12:
15146                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
15147                                &cq_set->u.request, eq->queue_id);
15148                         break;
15149                 case 13:
15150                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
15151                                &cq_set->u.request, eq->queue_id);
15152                         break;
15153                 case 14:
15154                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
15155                                &cq_set->u.request, eq->queue_id);
15156                         break;
15157                 case 15:
15158                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
15159                                &cq_set->u.request, eq->queue_id);
15160                         break;
15161                 }
15162
15163                 /* link the cq onto the parent eq child list */
15164                 list_add_tail(&cq->list, &eq->child_list);
15165                 /* Set up completion queue's type and subtype */
15166                 cq->type = type;
15167                 cq->subtype = subtype;
15168                 cq->assoc_qid = eq->queue_id;
15169                 cq->host_index = 0;
15170                 cq->hba_index = 0;
15171                 cq->entry_repost = LPFC_CQ_REPOST;
15172                 cq->chann = idx;
15173
15174                 rc = 0;
15175                 list_for_each_entry(dmabuf, &cq->page_list, list) {
15176                         memset(dmabuf->virt, 0, hw_page_size);
15177                         cnt = page_idx + dmabuf->buffer_tag;
15178                         cq_set->u.request.page[cnt].addr_lo =
15179                                         putPaddrLow(dmabuf->phys);
15180                         cq_set->u.request.page[cnt].addr_hi =
15181                                         putPaddrHigh(dmabuf->phys);
15182                         rc++;
15183                 }
15184                 page_idx += rc;
15185         }
15186
15187         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15188
15189         /* The IOCTL status is embedded in the mailbox subheader. */
15190         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15191         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15192         if (shdr_status || shdr_add_status || rc) {
15193                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15194                                 "3119 CQ_CREATE_SET mailbox failed with "
15195                                 "status x%x add_status x%x, mbx status x%x\n",
15196                                 shdr_status, shdr_add_status, rc);
15197                 status = -ENXIO;
15198                 goto out;
15199         }
15200         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15201         if (rc == 0xFFFF) {
15202                 status = -ENXIO;
15203                 goto out;
15204         }
15205
15206         for (idx = 0; idx < numcq; idx++) {
15207                 cq = cqp[idx];
15208                 cq->queue_id = rc + idx;
15209         }
15210
15211 out:
15212         lpfc_sli4_mbox_cmd_free(phba, mbox);
15213         return status;
15214 }
15215
15216 /**
15217  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
15218  * @phba: HBA structure that indicates port to create a queue on.
15219  * @mq: The queue structure to use to create the mailbox queue.
15220  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15221  * @cq: The completion queue to associate with this cq.
15222  *
15223  * This function provides failback (fb) functionality when the
15224  * mq_create_ext fails on older FW generations.  It's purpose is identical
15225  * to mq_create_ext otherwise.
15226  *
15227  * This routine cannot fail as all attributes were previously accessed and
15228  * initialized in mq_create_ext.
15229  **/
15230 static void
15231 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15232                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
15233 {
15234         struct lpfc_mbx_mq_create *mq_create;
15235         struct lpfc_dmabuf *dmabuf;
15236         int length;
15237
15238         length = (sizeof(struct lpfc_mbx_mq_create) -
15239                   sizeof(struct lpfc_sli4_cfg_mhdr));
15240         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15241                          LPFC_MBOX_OPCODE_MQ_CREATE,
15242                          length, LPFC_SLI4_MBX_EMBED);
15243         mq_create = &mbox->u.mqe.un.mq_create;
15244         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
15245                mq->page_count);
15246         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
15247                cq->queue_id);
15248         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15249         switch (mq->entry_count) {
15250         case 16:
15251                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15252                        LPFC_MQ_RING_SIZE_16);
15253                 break;
15254         case 32:
15255                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15256                        LPFC_MQ_RING_SIZE_32);
15257                 break;
15258         case 64:
15259                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15260                        LPFC_MQ_RING_SIZE_64);
15261                 break;
15262         case 128:
15263                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15264                        LPFC_MQ_RING_SIZE_128);
15265                 break;
15266         }
15267         list_for_each_entry(dmabuf, &mq->page_list, list) {
15268                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15269                         putPaddrLow(dmabuf->phys);
15270                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15271                         putPaddrHigh(dmabuf->phys);
15272         }
15273 }
15274
15275 /**
15276  * lpfc_mq_create - Create a mailbox Queue on the HBA
15277  * @phba: HBA structure that indicates port to create a queue on.
15278  * @mq: The queue structure to use to create the mailbox queue.
15279  * @cq: The completion queue to associate with this cq.
15280  * @subtype: The queue's subtype.
15281  *
15282  * This function creates a mailbox queue, as detailed in @mq, on a port,
15283  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15284  *
15285  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15286  * is used to get the entry count and entry size that are necessary to
15287  * determine the number of pages to allocate and use for this queue. This
15288  * function will send the MQ_CREATE mailbox command to the HBA to setup the
15289  * mailbox queue. This function is asynchronous and will wait for the mailbox
15290  * command to finish before continuing.
15291  *
15292  * On success this function will return a zero. If unable to allocate enough
15293  * memory this function will return -ENOMEM. If the queue create mailbox command
15294  * fails this function will return -ENXIO.
15295  **/
15296 int32_t
15297 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15298                struct lpfc_queue *cq, uint32_t subtype)
15299 {
15300         struct lpfc_mbx_mq_create *mq_create;
15301         struct lpfc_mbx_mq_create_ext *mq_create_ext;
15302         struct lpfc_dmabuf *dmabuf;
15303         LPFC_MBOXQ_t *mbox;
15304         int rc, length, status = 0;
15305         uint32_t shdr_status, shdr_add_status;
15306         union lpfc_sli4_cfg_shdr *shdr;
15307         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15308
15309         /* sanity check on queue memory */
15310         if (!mq || !cq)
15311                 return -ENODEV;
15312         if (!phba->sli4_hba.pc_sli4_params.supported)
15313                 hw_page_size = SLI4_PAGE_SIZE;
15314
15315         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15316         if (!mbox)
15317                 return -ENOMEM;
15318         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15319                   sizeof(struct lpfc_sli4_cfg_mhdr));
15320         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15321                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15322                          length, LPFC_SLI4_MBX_EMBED);
15323
15324         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
15325         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
15326         bf_set(lpfc_mbx_mq_create_ext_num_pages,
15327                &mq_create_ext->u.request, mq->page_count);
15328         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15329                &mq_create_ext->u.request, 1);
15330         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
15331                &mq_create_ext->u.request, 1);
15332         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15333                &mq_create_ext->u.request, 1);
15334         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15335                &mq_create_ext->u.request, 1);
15336         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15337                &mq_create_ext->u.request, 1);
15338         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
15339         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15340                phba->sli4_hba.pc_sli4_params.mqv);
15341         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15342                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15343                        cq->queue_id);
15344         else
15345                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15346                        cq->queue_id);
15347         switch (mq->entry_count) {
15348         default:
15349                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15350                                 "0362 Unsupported MQ count. (%d)\n",
15351                                 mq->entry_count);
15352                 if (mq->entry_count < 16) {
15353                         status = -EINVAL;
15354                         goto out;
15355                 }
15356                 /* otherwise default to smallest count (drop through) */
15357         case 16:
15358                 bf_set(lpfc_mq_context_ring_size,
15359                        &mq_create_ext->u.request.context,
15360                        LPFC_MQ_RING_SIZE_16);
15361                 break;
15362         case 32:
15363                 bf_set(lpfc_mq_context_ring_size,
15364                        &mq_create_ext->u.request.context,
15365                        LPFC_MQ_RING_SIZE_32);
15366                 break;
15367         case 64:
15368                 bf_set(lpfc_mq_context_ring_size,
15369                        &mq_create_ext->u.request.context,
15370                        LPFC_MQ_RING_SIZE_64);
15371                 break;
15372         case 128:
15373                 bf_set(lpfc_mq_context_ring_size,
15374                        &mq_create_ext->u.request.context,
15375                        LPFC_MQ_RING_SIZE_128);
15376                 break;
15377         }
15378         list_for_each_entry(dmabuf, &mq->page_list, list) {
15379                 memset(dmabuf->virt, 0, hw_page_size);
15380                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15381                                         putPaddrLow(dmabuf->phys);
15382                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
15383                                         putPaddrHigh(dmabuf->phys);
15384         }
15385         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15386         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15387                               &mq_create_ext->u.response);
15388         if (rc != MBX_SUCCESS) {
15389                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15390                                 "2795 MQ_CREATE_EXT failed with "
15391                                 "status x%x. Failback to MQ_CREATE.\n",
15392                                 rc);
15393                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15394                 mq_create = &mbox->u.mqe.un.mq_create;
15395                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15396                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15397                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15398                                       &mq_create->u.response);
15399         }
15400
15401         /* The IOCTL status is embedded in the mailbox subheader. */
15402         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15403         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15404         if (shdr_status || shdr_add_status || rc) {
15405                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15406                                 "2502 MQ_CREATE mailbox failed with "
15407                                 "status x%x add_status x%x, mbx status x%x\n",
15408                                 shdr_status, shdr_add_status, rc);
15409                 status = -ENXIO;
15410                 goto out;
15411         }
15412         if (mq->queue_id == 0xFFFF) {
15413                 status = -ENXIO;
15414                 goto out;
15415         }
15416         mq->type = LPFC_MQ;
15417         mq->assoc_qid = cq->queue_id;
15418         mq->subtype = subtype;
15419         mq->host_index = 0;
15420         mq->hba_index = 0;
15421         mq->entry_repost = LPFC_MQ_REPOST;
15422
15423         /* link the mq onto the parent cq child list */
15424         list_add_tail(&mq->list, &cq->child_list);
15425 out:
15426         mempool_free(mbox, phba->mbox_mem_pool);
15427         return status;
15428 }
15429
15430 /**
15431  * lpfc_wq_create - Create a Work Queue on the HBA
15432  * @phba: HBA structure that indicates port to create a queue on.
15433  * @wq: The queue structure to use to create the work queue.
15434  * @cq: The completion queue to bind this work queue to.
15435  * @subtype: The subtype of the work queue indicating its functionality.
15436  *
15437  * This function creates a work queue, as detailed in @wq, on a port, described
15438  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15439  *
15440  * The @phba struct is used to send mailbox command to HBA. The @wq struct
15441  * is used to get the entry count and entry size that are necessary to
15442  * determine the number of pages to allocate and use for this queue. The @cq
15443  * is used to indicate which completion queue to bind this work queue to. This
15444  * function will send the WQ_CREATE mailbox command to the HBA to setup the
15445  * work queue. This function is asynchronous and will wait for the mailbox
15446  * command to finish before continuing.
15447  *
15448  * On success this function will return a zero. If unable to allocate enough
15449  * memory this function will return -ENOMEM. If the queue create mailbox command
15450  * fails this function will return -ENXIO.
15451  **/
15452 int
15453 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15454                struct lpfc_queue *cq, uint32_t subtype)
15455 {
15456         struct lpfc_mbx_wq_create *wq_create;
15457         struct lpfc_dmabuf *dmabuf;
15458         LPFC_MBOXQ_t *mbox;
15459         int rc, length, status = 0;
15460         uint32_t shdr_status, shdr_add_status;
15461         union lpfc_sli4_cfg_shdr *shdr;
15462         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15463         struct dma_address *page;
15464         void __iomem *bar_memmap_p;
15465         uint32_t db_offset;
15466         uint16_t pci_barset;
15467         uint8_t dpp_barset;
15468         uint32_t dpp_offset;
15469         unsigned long pg_addr;
15470         uint8_t wq_create_version;
15471
15472         /* sanity check on queue memory */
15473         if (!wq || !cq)
15474                 return -ENODEV;
15475         if (!phba->sli4_hba.pc_sli4_params.supported)
15476                 hw_page_size = wq->page_size;
15477
15478         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15479         if (!mbox)
15480                 return -ENOMEM;
15481         length = (sizeof(struct lpfc_mbx_wq_create) -
15482                   sizeof(struct lpfc_sli4_cfg_mhdr));
15483         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15484                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15485                          length, LPFC_SLI4_MBX_EMBED);
15486         wq_create = &mbox->u.mqe.un.wq_create;
15487         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
15488         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15489                     wq->page_count);
15490         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15491                     cq->queue_id);
15492
15493         /* wqv is the earliest version supported, NOT the latest */
15494         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15495                phba->sli4_hba.pc_sli4_params.wqv);
15496
15497         if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15498             (wq->page_size > SLI4_PAGE_SIZE))
15499                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15500         else
15501                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15502
15503
15504         if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15505                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15506         else
15507                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15508
15509         switch (wq_create_version) {
15510         case LPFC_Q_CREATE_VERSION_1:
15511                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15512                        wq->entry_count);
15513                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15514                        LPFC_Q_CREATE_VERSION_1);
15515
15516                 switch (wq->entry_size) {
15517                 default:
15518                 case 64:
15519                         bf_set(lpfc_mbx_wq_create_wqe_size,
15520                                &wq_create->u.request_1,
15521                                LPFC_WQ_WQE_SIZE_64);
15522                         break;
15523                 case 128:
15524                         bf_set(lpfc_mbx_wq_create_wqe_size,
15525                                &wq_create->u.request_1,
15526                                LPFC_WQ_WQE_SIZE_128);
15527                         break;
15528                 }
15529                 /* Request DPP by default */
15530                 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
15531                 bf_set(lpfc_mbx_wq_create_page_size,
15532                        &wq_create->u.request_1,
15533                        (wq->page_size / SLI4_PAGE_SIZE));
15534                 page = wq_create->u.request_1.page;
15535                 break;
15536         default:
15537                 page = wq_create->u.request.page;
15538                 break;
15539         }
15540
15541         list_for_each_entry(dmabuf, &wq->page_list, list) {
15542                 memset(dmabuf->virt, 0, hw_page_size);
15543                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15544                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
15545         }
15546
15547         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15548                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15549
15550         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15551         /* The IOCTL status is embedded in the mailbox subheader. */
15552         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15553         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15554         if (shdr_status || shdr_add_status || rc) {
15555                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15556                                 "2503 WQ_CREATE mailbox failed with "
15557                                 "status x%x add_status x%x, mbx status x%x\n",
15558                                 shdr_status, shdr_add_status, rc);
15559                 status = -ENXIO;
15560                 goto out;
15561         }
15562
15563         if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15564                 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15565                                         &wq_create->u.response);
15566         else
15567                 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15568                                         &wq_create->u.response_1);
15569
15570         if (wq->queue_id == 0xFFFF) {
15571                 status = -ENXIO;
15572                 goto out;
15573         }
15574
15575         wq->db_format = LPFC_DB_LIST_FORMAT;
15576         if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15577                 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15578                         wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15579                                                &wq_create->u.response);
15580                         if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15581                             (wq->db_format != LPFC_DB_RING_FORMAT)) {
15582                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15583                                                 "3265 WQ[%d] doorbell format "
15584                                                 "not supported: x%x\n",
15585                                                 wq->queue_id, wq->db_format);
15586                                 status = -EINVAL;
15587                                 goto out;
15588                         }
15589                         pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15590                                             &wq_create->u.response);
15591                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15592                                                                    pci_barset);
15593                         if (!bar_memmap_p) {
15594                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15595                                                 "3263 WQ[%d] failed to memmap "
15596                                                 "pci barset:x%x\n",
15597                                                 wq->queue_id, pci_barset);
15598                                 status = -ENOMEM;
15599                                 goto out;
15600                         }
15601                         db_offset = wq_create->u.response.doorbell_offset;
15602                         if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15603                             (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15604                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15605                                                 "3252 WQ[%d] doorbell offset "
15606                                                 "not supported: x%x\n",
15607                                                 wq->queue_id, db_offset);
15608                                 status = -EINVAL;
15609                                 goto out;
15610                         }
15611                         wq->db_regaddr = bar_memmap_p + db_offset;
15612                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15613                                         "3264 WQ[%d]: barset:x%x, offset:x%x, "
15614                                         "format:x%x\n", wq->queue_id,
15615                                         pci_barset, db_offset, wq->db_format);
15616                 } else
15617                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15618         } else {
15619                 /* Check if DPP was honored by the firmware */
15620                 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15621                                     &wq_create->u.response_1);
15622                 if (wq->dpp_enable) {
15623                         pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15624                                             &wq_create->u.response_1);
15625                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15626                                                                    pci_barset);
15627                         if (!bar_memmap_p) {
15628                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15629                                                 "3267 WQ[%d] failed to memmap "
15630                                                 "pci barset:x%x\n",
15631                                                 wq->queue_id, pci_barset);
15632                                 status = -ENOMEM;
15633                                 goto out;
15634                         }
15635                         db_offset = wq_create->u.response_1.doorbell_offset;
15636                         wq->db_regaddr = bar_memmap_p + db_offset;
15637                         wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15638                                             &wq_create->u.response_1);
15639                         dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15640                                             &wq_create->u.response_1);
15641                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15642                                                                    dpp_barset);
15643                         if (!bar_memmap_p) {
15644                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15645                                                 "3268 WQ[%d] failed to memmap "
15646                                                 "pci barset:x%x\n",
15647                                                 wq->queue_id, dpp_barset);
15648                                 status = -ENOMEM;
15649                                 goto out;
15650                         }
15651                         dpp_offset = wq_create->u.response_1.dpp_offset;
15652                         wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15653                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15654                                         "3271 WQ[%d]: barset:x%x, offset:x%x, "
15655                                         "dpp_id:x%x dpp_barset:x%x "
15656                                         "dpp_offset:x%x\n",
15657                                         wq->queue_id, pci_barset, db_offset,
15658                                         wq->dpp_id, dpp_barset, dpp_offset);
15659
15660                         /* Enable combined writes for DPP aperture */
15661                         pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15662 #ifdef CONFIG_X86
15663                         rc = set_memory_wc(pg_addr, 1);
15664                         if (rc) {
15665                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15666                                         "3272 Cannot setup Combined "
15667                                         "Write on WQ[%d] - disable DPP\n",
15668                                         wq->queue_id);
15669                                 phba->cfg_enable_dpp = 0;
15670                         }
15671 #else
15672                         phba->cfg_enable_dpp = 0;
15673 #endif
15674                 } else
15675                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15676         }
15677         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15678         if (wq->pring == NULL) {
15679                 status = -ENOMEM;
15680                 goto out;
15681         }
15682         wq->type = LPFC_WQ;
15683         wq->assoc_qid = cq->queue_id;
15684         wq->subtype = subtype;
15685         wq->host_index = 0;
15686         wq->hba_index = 0;
15687         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
15688
15689         /* link the wq onto the parent cq child list */
15690         list_add_tail(&wq->list, &cq->child_list);
15691 out:
15692         mempool_free(mbox, phba->mbox_mem_pool);
15693         return status;
15694 }
15695
15696 /**
15697  * lpfc_rq_create - Create a Receive Queue on the HBA
15698  * @phba: HBA structure that indicates port to create a queue on.
15699  * @hrq: The queue structure to use to create the header receive queue.
15700  * @drq: The queue structure to use to create the data receive queue.
15701  * @cq: The completion queue to bind this work queue to.
15702  *
15703  * This function creates a receive buffer queue pair , as detailed in @hrq and
15704  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15705  * to the HBA.
15706  *
15707  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15708  * struct is used to get the entry count that is necessary to determine the
15709  * number of pages to use for this queue. The @cq is used to indicate which
15710  * completion queue to bind received buffers that are posted to these queues to.
15711  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15712  * receive queue pair. This function is asynchronous and will wait for the
15713  * mailbox command to finish before continuing.
15714  *
15715  * On success this function will return a zero. If unable to allocate enough
15716  * memory this function will return -ENOMEM. If the queue create mailbox command
15717  * fails this function will return -ENXIO.
15718  **/
15719 int
15720 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15721                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15722 {
15723         struct lpfc_mbx_rq_create *rq_create;
15724         struct lpfc_dmabuf *dmabuf;
15725         LPFC_MBOXQ_t *mbox;
15726         int rc, length, status = 0;
15727         uint32_t shdr_status, shdr_add_status;
15728         union lpfc_sli4_cfg_shdr *shdr;
15729         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15730         void __iomem *bar_memmap_p;
15731         uint32_t db_offset;
15732         uint16_t pci_barset;
15733
15734         /* sanity check on queue memory */
15735         if (!hrq || !drq || !cq)
15736                 return -ENODEV;
15737         if (!phba->sli4_hba.pc_sli4_params.supported)
15738                 hw_page_size = SLI4_PAGE_SIZE;
15739
15740         if (hrq->entry_count != drq->entry_count)
15741                 return -EINVAL;
15742         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15743         if (!mbox)
15744                 return -ENOMEM;
15745         length = (sizeof(struct lpfc_mbx_rq_create) -
15746                   sizeof(struct lpfc_sli4_cfg_mhdr));
15747         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15748                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15749                          length, LPFC_SLI4_MBX_EMBED);
15750         rq_create = &mbox->u.mqe.un.rq_create;
15751         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15752         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15753                phba->sli4_hba.pc_sli4_params.rqv);
15754         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15755                 bf_set(lpfc_rq_context_rqe_count_1,
15756                        &rq_create->u.request.context,
15757                        hrq->entry_count);
15758                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
15759                 bf_set(lpfc_rq_context_rqe_size,
15760                        &rq_create->u.request.context,
15761                        LPFC_RQE_SIZE_8);
15762                 bf_set(lpfc_rq_context_page_size,
15763                        &rq_create->u.request.context,
15764                        LPFC_RQ_PAGE_SIZE_4096);
15765         } else {
15766                 switch (hrq->entry_count) {
15767                 default:
15768                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15769                                         "2535 Unsupported RQ count. (%d)\n",
15770                                         hrq->entry_count);
15771                         if (hrq->entry_count < 512) {
15772                                 status = -EINVAL;
15773                                 goto out;
15774                         }
15775                         /* otherwise default to smallest count (drop through) */
15776                 case 512:
15777                         bf_set(lpfc_rq_context_rqe_count,
15778                                &rq_create->u.request.context,
15779                                LPFC_RQ_RING_SIZE_512);
15780                         break;
15781                 case 1024:
15782                         bf_set(lpfc_rq_context_rqe_count,
15783                                &rq_create->u.request.context,
15784                                LPFC_RQ_RING_SIZE_1024);
15785                         break;
15786                 case 2048:
15787                         bf_set(lpfc_rq_context_rqe_count,
15788                                &rq_create->u.request.context,
15789                                LPFC_RQ_RING_SIZE_2048);
15790                         break;
15791                 case 4096:
15792                         bf_set(lpfc_rq_context_rqe_count,
15793                                &rq_create->u.request.context,
15794                                LPFC_RQ_RING_SIZE_4096);
15795                         break;
15796                 }
15797                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15798                        LPFC_HDR_BUF_SIZE);
15799         }
15800         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15801                cq->queue_id);
15802         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15803                hrq->page_count);
15804         list_for_each_entry(dmabuf, &hrq->page_list, list) {
15805                 memset(dmabuf->virt, 0, hw_page_size);
15806                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15807                                         putPaddrLow(dmabuf->phys);
15808                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15809                                         putPaddrHigh(dmabuf->phys);
15810         }
15811         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15812                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15813
15814         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15815         /* The IOCTL status is embedded in the mailbox subheader. */
15816         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15817         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15818         if (shdr_status || shdr_add_status || rc) {
15819                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15820                                 "2504 RQ_CREATE mailbox failed with "
15821                                 "status x%x add_status x%x, mbx status x%x\n",
15822                                 shdr_status, shdr_add_status, rc);
15823                 status = -ENXIO;
15824                 goto out;
15825         }
15826         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15827         if (hrq->queue_id == 0xFFFF) {
15828                 status = -ENXIO;
15829                 goto out;
15830         }
15831
15832         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15833                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15834                                         &rq_create->u.response);
15835                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15836                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15837                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15838                                         "3262 RQ [%d] doorbell format not "
15839                                         "supported: x%x\n", hrq->queue_id,
15840                                         hrq->db_format);
15841                         status = -EINVAL;
15842                         goto out;
15843                 }
15844
15845                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15846                                     &rq_create->u.response);
15847                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15848                 if (!bar_memmap_p) {
15849                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15850                                         "3269 RQ[%d] failed to memmap pci "
15851                                         "barset:x%x\n", hrq->queue_id,
15852                                         pci_barset);
15853                         status = -ENOMEM;
15854                         goto out;
15855                 }
15856
15857                 db_offset = rq_create->u.response.doorbell_offset;
15858                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15859                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15860                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15861                                         "3270 RQ[%d] doorbell offset not "
15862                                         "supported: x%x\n", hrq->queue_id,
15863                                         db_offset);
15864                         status = -EINVAL;
15865                         goto out;
15866                 }
15867                 hrq->db_regaddr = bar_memmap_p + db_offset;
15868                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15869                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15870                                 "format:x%x\n", hrq->queue_id, pci_barset,
15871                                 db_offset, hrq->db_format);
15872         } else {
15873                 hrq->db_format = LPFC_DB_RING_FORMAT;
15874                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15875         }
15876         hrq->type = LPFC_HRQ;
15877         hrq->assoc_qid = cq->queue_id;
15878         hrq->subtype = subtype;
15879         hrq->host_index = 0;
15880         hrq->hba_index = 0;
15881         hrq->entry_repost = LPFC_RQ_REPOST;
15882
15883         /* now create the data queue */
15884         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15885                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15886                          length, LPFC_SLI4_MBX_EMBED);
15887         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15888                phba->sli4_hba.pc_sli4_params.rqv);
15889         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15890                 bf_set(lpfc_rq_context_rqe_count_1,
15891                        &rq_create->u.request.context, hrq->entry_count);
15892                 if (subtype == LPFC_NVMET)
15893                         rq_create->u.request.context.buffer_size =
15894                                 LPFC_NVMET_DATA_BUF_SIZE;
15895                 else
15896                         rq_create->u.request.context.buffer_size =
15897                                 LPFC_DATA_BUF_SIZE;
15898                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15899                        LPFC_RQE_SIZE_8);
15900                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15901                        (PAGE_SIZE/SLI4_PAGE_SIZE));
15902         } else {
15903                 switch (drq->entry_count) {
15904                 default:
15905                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15906                                         "2536 Unsupported RQ count. (%d)\n",
15907                                         drq->entry_count);
15908                         if (drq->entry_count < 512) {
15909                                 status = -EINVAL;
15910                                 goto out;
15911                         }
15912                         /* otherwise default to smallest count (drop through) */
15913                 case 512:
15914                         bf_set(lpfc_rq_context_rqe_count,
15915                                &rq_create->u.request.context,
15916                                LPFC_RQ_RING_SIZE_512);
15917                         break;
15918                 case 1024:
15919                         bf_set(lpfc_rq_context_rqe_count,
15920                                &rq_create->u.request.context,
15921                                LPFC_RQ_RING_SIZE_1024);
15922                         break;
15923                 case 2048:
15924                         bf_set(lpfc_rq_context_rqe_count,
15925                                &rq_create->u.request.context,
15926                                LPFC_RQ_RING_SIZE_2048);
15927                         break;
15928                 case 4096:
15929                         bf_set(lpfc_rq_context_rqe_count,
15930                                &rq_create->u.request.context,
15931                                LPFC_RQ_RING_SIZE_4096);
15932                         break;
15933                 }
15934                 if (subtype == LPFC_NVMET)
15935                         bf_set(lpfc_rq_context_buf_size,
15936                                &rq_create->u.request.context,
15937                                LPFC_NVMET_DATA_BUF_SIZE);
15938                 else
15939                         bf_set(lpfc_rq_context_buf_size,
15940                                &rq_create->u.request.context,
15941                                LPFC_DATA_BUF_SIZE);
15942         }
15943         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15944                cq->queue_id);
15945         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15946                drq->page_count);
15947         list_for_each_entry(dmabuf, &drq->page_list, list) {
15948                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15949                                         putPaddrLow(dmabuf->phys);
15950                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15951                                         putPaddrHigh(dmabuf->phys);
15952         }
15953         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15954                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15955         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15956         /* The IOCTL status is embedded in the mailbox subheader. */
15957         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15958         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15959         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15960         if (shdr_status || shdr_add_status || rc) {
15961                 status = -ENXIO;
15962                 goto out;
15963         }
15964         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15965         if (drq->queue_id == 0xFFFF) {
15966                 status = -ENXIO;
15967                 goto out;
15968         }
15969         drq->type = LPFC_DRQ;
15970         drq->assoc_qid = cq->queue_id;
15971         drq->subtype = subtype;
15972         drq->host_index = 0;
15973         drq->hba_index = 0;
15974         drq->entry_repost = LPFC_RQ_REPOST;
15975
15976         /* link the header and data RQs onto the parent cq child list */
15977         list_add_tail(&hrq->list, &cq->child_list);
15978         list_add_tail(&drq->list, &cq->child_list);
15979
15980 out:
15981         mempool_free(mbox, phba->mbox_mem_pool);
15982         return status;
15983 }
15984
15985 /**
15986  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15987  * @phba: HBA structure that indicates port to create a queue on.
15988  * @hrqp: The queue structure array to use to create the header receive queues.
15989  * @drqp: The queue structure array to use to create the data receive queues.
15990  * @cqp: The completion queue array to bind these receive queues to.
15991  *
15992  * This function creates a receive buffer queue pair , as detailed in @hrq and
15993  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15994  * to the HBA.
15995  *
15996  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15997  * struct is used to get the entry count that is necessary to determine the
15998  * number of pages to use for this queue. The @cq is used to indicate which
15999  * completion queue to bind received buffers that are posted to these queues to.
16000  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16001  * receive queue pair. This function is asynchronous and will wait for the
16002  * mailbox command to finish before continuing.
16003  *
16004  * On success this function will return a zero. If unable to allocate enough
16005  * memory this function will return -ENOMEM. If the queue create mailbox command
16006  * fails this function will return -ENXIO.
16007  **/
16008 int
16009 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16010                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16011                 uint32_t subtype)
16012 {
16013         struct lpfc_queue *hrq, *drq, *cq;
16014         struct lpfc_mbx_rq_create_v2 *rq_create;
16015         struct lpfc_dmabuf *dmabuf;
16016         LPFC_MBOXQ_t *mbox;
16017         int rc, length, alloclen, status = 0;
16018         int cnt, idx, numrq, page_idx = 0;
16019         uint32_t shdr_status, shdr_add_status;
16020         union lpfc_sli4_cfg_shdr *shdr;
16021         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16022
16023         numrq = phba->cfg_nvmet_mrq;
16024         /* sanity check on array memory */
16025         if (!hrqp || !drqp || !cqp || !numrq)
16026                 return -ENODEV;
16027         if (!phba->sli4_hba.pc_sli4_params.supported)
16028                 hw_page_size = SLI4_PAGE_SIZE;
16029
16030         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16031         if (!mbox)
16032                 return -ENOMEM;
16033
16034         length = sizeof(struct lpfc_mbx_rq_create_v2);
16035         length += ((2 * numrq * hrqp[0]->page_count) *
16036                    sizeof(struct dma_address));
16037
16038         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16039                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16040                                     LPFC_SLI4_MBX_NEMBED);
16041         if (alloclen < length) {
16042                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16043                                 "3099 Allocated DMA memory size (%d) is "
16044                                 "less than the requested DMA memory size "
16045                                 "(%d)\n", alloclen, length);
16046                 status = -ENOMEM;
16047                 goto out;
16048         }
16049
16050
16051
16052         rq_create = mbox->sge_array->addr[0];
16053         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16054
16055         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16056         cnt = 0;
16057
16058         for (idx = 0; idx < numrq; idx++) {
16059                 hrq = hrqp[idx];
16060                 drq = drqp[idx];
16061                 cq  = cqp[idx];
16062
16063                 /* sanity check on queue memory */
16064                 if (!hrq || !drq || !cq) {
16065                         status = -ENODEV;
16066                         goto out;
16067                 }
16068
16069                 if (hrq->entry_count != drq->entry_count) {
16070                         status = -EINVAL;
16071                         goto out;
16072                 }
16073
16074                 if (idx == 0) {
16075                         bf_set(lpfc_mbx_rq_create_num_pages,
16076                                &rq_create->u.request,
16077                                hrq->page_count);
16078                         bf_set(lpfc_mbx_rq_create_rq_cnt,
16079                                &rq_create->u.request, (numrq * 2));
16080                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16081                                1);
16082                         bf_set(lpfc_rq_context_base_cq,
16083                                &rq_create->u.request.context,
16084                                cq->queue_id);
16085                         bf_set(lpfc_rq_context_data_size,
16086                                &rq_create->u.request.context,
16087                                LPFC_NVMET_DATA_BUF_SIZE);
16088                         bf_set(lpfc_rq_context_hdr_size,
16089                                &rq_create->u.request.context,
16090                                LPFC_HDR_BUF_SIZE);
16091                         bf_set(lpfc_rq_context_rqe_count_1,
16092                                &rq_create->u.request.context,
16093                                hrq->entry_count);
16094                         bf_set(lpfc_rq_context_rqe_size,
16095                                &rq_create->u.request.context,
16096                                LPFC_RQE_SIZE_8);
16097                         bf_set(lpfc_rq_context_page_size,
16098                                &rq_create->u.request.context,
16099                                (PAGE_SIZE/SLI4_PAGE_SIZE));
16100                 }
16101                 rc = 0;
16102                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16103                         memset(dmabuf->virt, 0, hw_page_size);
16104                         cnt = page_idx + dmabuf->buffer_tag;
16105                         rq_create->u.request.page[cnt].addr_lo =
16106                                         putPaddrLow(dmabuf->phys);
16107                         rq_create->u.request.page[cnt].addr_hi =
16108                                         putPaddrHigh(dmabuf->phys);
16109                         rc++;
16110                 }
16111                 page_idx += rc;
16112
16113                 rc = 0;
16114                 list_for_each_entry(dmabuf, &drq->page_list, list) {
16115                         memset(dmabuf->virt, 0, hw_page_size);
16116                         cnt = page_idx + dmabuf->buffer_tag;
16117                         rq_create->u.request.page[cnt].addr_lo =
16118                                         putPaddrLow(dmabuf->phys);
16119                         rq_create->u.request.page[cnt].addr_hi =
16120                                         putPaddrHigh(dmabuf->phys);
16121                         rc++;
16122                 }
16123                 page_idx += rc;
16124
16125                 hrq->db_format = LPFC_DB_RING_FORMAT;
16126                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16127                 hrq->type = LPFC_HRQ;
16128                 hrq->assoc_qid = cq->queue_id;
16129                 hrq->subtype = subtype;
16130                 hrq->host_index = 0;
16131                 hrq->hba_index = 0;
16132                 hrq->entry_repost = LPFC_RQ_REPOST;
16133
16134                 drq->db_format = LPFC_DB_RING_FORMAT;
16135                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16136                 drq->type = LPFC_DRQ;
16137                 drq->assoc_qid = cq->queue_id;
16138                 drq->subtype = subtype;
16139                 drq->host_index = 0;
16140                 drq->hba_index = 0;
16141                 drq->entry_repost = LPFC_RQ_REPOST;
16142
16143                 list_add_tail(&hrq->list, &cq->child_list);
16144                 list_add_tail(&drq->list, &cq->child_list);
16145         }
16146
16147         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16148         /* The IOCTL status is embedded in the mailbox subheader. */
16149         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16150         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16151         if (shdr_status || shdr_add_status || rc) {
16152                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16153                                 "3120 RQ_CREATE mailbox failed with "
16154                                 "status x%x add_status x%x, mbx status x%x\n",
16155                                 shdr_status, shdr_add_status, rc);
16156                 status = -ENXIO;
16157                 goto out;
16158         }
16159         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16160         if (rc == 0xFFFF) {
16161                 status = -ENXIO;
16162                 goto out;
16163         }
16164
16165         /* Initialize all RQs with associated queue id */
16166         for (idx = 0; idx < numrq; idx++) {
16167                 hrq = hrqp[idx];
16168                 hrq->queue_id = rc + (2 * idx);
16169                 drq = drqp[idx];
16170                 drq->queue_id = rc + (2 * idx) + 1;
16171         }
16172
16173 out:
16174         lpfc_sli4_mbox_cmd_free(phba, mbox);
16175         return status;
16176 }
16177
16178 /**
16179  * lpfc_eq_destroy - Destroy an event Queue on the HBA
16180  * @eq: The queue structure associated with the queue to destroy.
16181  *
16182  * This function destroys a queue, as detailed in @eq by sending an mailbox
16183  * command, specific to the type of queue, to the HBA.
16184  *
16185  * The @eq struct is used to get the queue ID of the queue to destroy.
16186  *
16187  * On success this function will return a zero. If the queue destroy mailbox
16188  * command fails this function will return -ENXIO.
16189  **/
16190 int
16191 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16192 {
16193         LPFC_MBOXQ_t *mbox;
16194         int rc, length, status = 0;
16195         uint32_t shdr_status, shdr_add_status;
16196         union lpfc_sli4_cfg_shdr *shdr;
16197
16198         /* sanity check on queue memory */
16199         if (!eq)
16200                 return -ENODEV;
16201         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16202         if (!mbox)
16203                 return -ENOMEM;
16204         length = (sizeof(struct lpfc_mbx_eq_destroy) -
16205                   sizeof(struct lpfc_sli4_cfg_mhdr));
16206         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16207                          LPFC_MBOX_OPCODE_EQ_DESTROY,
16208                          length, LPFC_SLI4_MBX_EMBED);
16209         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16210                eq->queue_id);
16211         mbox->vport = eq->phba->pport;
16212         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16213
16214         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16215         /* The IOCTL status is embedded in the mailbox subheader. */
16216         shdr = (union lpfc_sli4_cfg_shdr *)
16217                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16218         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16219         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16220         if (shdr_status || shdr_add_status || rc) {
16221                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16222                                 "2505 EQ_DESTROY mailbox failed with "
16223                                 "status x%x add_status x%x, mbx status x%x\n",
16224                                 shdr_status, shdr_add_status, rc);
16225                 status = -ENXIO;
16226         }
16227
16228         /* Remove eq from any list */
16229         list_del_init(&eq->list);
16230         mempool_free(mbox, eq->phba->mbox_mem_pool);
16231         return status;
16232 }
16233
16234 /**
16235  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16236  * @cq: The queue structure associated with the queue to destroy.
16237  *
16238  * This function destroys a queue, as detailed in @cq by sending an mailbox
16239  * command, specific to the type of queue, to the HBA.
16240  *
16241  * The @cq struct is used to get the queue ID of the queue to destroy.
16242  *
16243  * On success this function will return a zero. If the queue destroy mailbox
16244  * command fails this function will return -ENXIO.
16245  **/
16246 int
16247 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16248 {
16249         LPFC_MBOXQ_t *mbox;
16250         int rc, length, status = 0;
16251         uint32_t shdr_status, shdr_add_status;
16252         union lpfc_sli4_cfg_shdr *shdr;
16253
16254         /* sanity check on queue memory */
16255         if (!cq)
16256                 return -ENODEV;
16257         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16258         if (!mbox)
16259                 return -ENOMEM;
16260         length = (sizeof(struct lpfc_mbx_cq_destroy) -
16261                   sizeof(struct lpfc_sli4_cfg_mhdr));
16262         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16263                          LPFC_MBOX_OPCODE_CQ_DESTROY,
16264                          length, LPFC_SLI4_MBX_EMBED);
16265         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16266                cq->queue_id);
16267         mbox->vport = cq->phba->pport;
16268         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16269         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16270         /* The IOCTL status is embedded in the mailbox subheader. */
16271         shdr = (union lpfc_sli4_cfg_shdr *)
16272                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16273         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16274         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16275         if (shdr_status || shdr_add_status || rc) {
16276                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16277                                 "2506 CQ_DESTROY mailbox failed with "
16278                                 "status x%x add_status x%x, mbx status x%x\n",
16279                                 shdr_status, shdr_add_status, rc);
16280                 status = -ENXIO;
16281         }
16282         /* Remove cq from any list */
16283         list_del_init(&cq->list);
16284         mempool_free(mbox, cq->phba->mbox_mem_pool);
16285         return status;
16286 }
16287
16288 /**
16289  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16290  * @qm: The queue structure associated with the queue to destroy.
16291  *
16292  * This function destroys a queue, as detailed in @mq by sending an mailbox
16293  * command, specific to the type of queue, to the HBA.
16294  *
16295  * The @mq struct is used to get the queue ID of the queue to destroy.
16296  *
16297  * On success this function will return a zero. If the queue destroy mailbox
16298  * command fails this function will return -ENXIO.
16299  **/
16300 int
16301 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16302 {
16303         LPFC_MBOXQ_t *mbox;
16304         int rc, length, status = 0;
16305         uint32_t shdr_status, shdr_add_status;
16306         union lpfc_sli4_cfg_shdr *shdr;
16307
16308         /* sanity check on queue memory */
16309         if (!mq)
16310                 return -ENODEV;
16311         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16312         if (!mbox)
16313                 return -ENOMEM;
16314         length = (sizeof(struct lpfc_mbx_mq_destroy) -
16315                   sizeof(struct lpfc_sli4_cfg_mhdr));
16316         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16317                          LPFC_MBOX_OPCODE_MQ_DESTROY,
16318                          length, LPFC_SLI4_MBX_EMBED);
16319         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16320                mq->queue_id);
16321         mbox->vport = mq->phba->pport;
16322         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16323         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16324         /* The IOCTL status is embedded in the mailbox subheader. */
16325         shdr = (union lpfc_sli4_cfg_shdr *)
16326                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16327         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16328         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16329         if (shdr_status || shdr_add_status || rc) {
16330                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16331                                 "2507 MQ_DESTROY mailbox failed with "
16332                                 "status x%x add_status x%x, mbx status x%x\n",
16333                                 shdr_status, shdr_add_status, rc);
16334                 status = -ENXIO;
16335         }
16336         /* Remove mq from any list */
16337         list_del_init(&mq->list);
16338         mempool_free(mbox, mq->phba->mbox_mem_pool);
16339         return status;
16340 }
16341
16342 /**
16343  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16344  * @wq: The queue structure associated with the queue to destroy.
16345  *
16346  * This function destroys a queue, as detailed in @wq by sending an mailbox
16347  * command, specific to the type of queue, to the HBA.
16348  *
16349  * The @wq struct is used to get the queue ID of the queue to destroy.
16350  *
16351  * On success this function will return a zero. If the queue destroy mailbox
16352  * command fails this function will return -ENXIO.
16353  **/
16354 int
16355 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16356 {
16357         LPFC_MBOXQ_t *mbox;
16358         int rc, length, status = 0;
16359         uint32_t shdr_status, shdr_add_status;
16360         union lpfc_sli4_cfg_shdr *shdr;
16361
16362         /* sanity check on queue memory */
16363         if (!wq)
16364                 return -ENODEV;
16365         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16366         if (!mbox)
16367                 return -ENOMEM;
16368         length = (sizeof(struct lpfc_mbx_wq_destroy) -
16369                   sizeof(struct lpfc_sli4_cfg_mhdr));
16370         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16371                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16372                          length, LPFC_SLI4_MBX_EMBED);
16373         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16374                wq->queue_id);
16375         mbox->vport = wq->phba->pport;
16376         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16377         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16378         shdr = (union lpfc_sli4_cfg_shdr *)
16379                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16380         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16381         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16382         if (shdr_status || shdr_add_status || rc) {
16383                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16384                                 "2508 WQ_DESTROY mailbox failed with "
16385                                 "status x%x add_status x%x, mbx status x%x\n",
16386                                 shdr_status, shdr_add_status, rc);
16387                 status = -ENXIO;
16388         }
16389         /* Remove wq from any list */
16390         list_del_init(&wq->list);
16391         kfree(wq->pring);
16392         wq->pring = NULL;
16393         mempool_free(mbox, wq->phba->mbox_mem_pool);
16394         return status;
16395 }
16396
16397 /**
16398  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16399  * @rq: The queue structure associated with the queue to destroy.
16400  *
16401  * This function destroys a queue, as detailed in @rq by sending an mailbox
16402  * command, specific to the type of queue, to the HBA.
16403  *
16404  * The @rq struct is used to get the queue ID of the queue to destroy.
16405  *
16406  * On success this function will return a zero. If the queue destroy mailbox
16407  * command fails this function will return -ENXIO.
16408  **/
16409 int
16410 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16411                 struct lpfc_queue *drq)
16412 {
16413         LPFC_MBOXQ_t *mbox;
16414         int rc, length, status = 0;
16415         uint32_t shdr_status, shdr_add_status;
16416         union lpfc_sli4_cfg_shdr *shdr;
16417
16418         /* sanity check on queue memory */
16419         if (!hrq || !drq)
16420                 return -ENODEV;
16421         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16422         if (!mbox)
16423                 return -ENOMEM;
16424         length = (sizeof(struct lpfc_mbx_rq_destroy) -
16425                   sizeof(struct lpfc_sli4_cfg_mhdr));
16426         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16427                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16428                          length, LPFC_SLI4_MBX_EMBED);
16429         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16430                hrq->queue_id);
16431         mbox->vport = hrq->phba->pport;
16432         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16433         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16434         /* The IOCTL status is embedded in the mailbox subheader. */
16435         shdr = (union lpfc_sli4_cfg_shdr *)
16436                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16437         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16438         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16439         if (shdr_status || shdr_add_status || rc) {
16440                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16441                                 "2509 RQ_DESTROY mailbox failed with "
16442                                 "status x%x add_status x%x, mbx status x%x\n",
16443                                 shdr_status, shdr_add_status, rc);
16444                 if (rc != MBX_TIMEOUT)
16445                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
16446                 return -ENXIO;
16447         }
16448         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16449                drq->queue_id);
16450         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16451         shdr = (union lpfc_sli4_cfg_shdr *)
16452                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16453         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16454         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16455         if (shdr_status || shdr_add_status || rc) {
16456                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16457                                 "2510 RQ_DESTROY mailbox failed with "
16458                                 "status x%x add_status x%x, mbx status x%x\n",
16459                                 shdr_status, shdr_add_status, rc);
16460                 status = -ENXIO;
16461         }
16462         list_del_init(&hrq->list);
16463         list_del_init(&drq->list);
16464         mempool_free(mbox, hrq->phba->mbox_mem_pool);
16465         return status;
16466 }
16467
16468 /**
16469  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16470  * @phba: The virtual port for which this call being executed.
16471  * @pdma_phys_addr0: Physical address of the 1st SGL page.
16472  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16473  * @xritag: the xritag that ties this io to the SGL pages.
16474  *
16475  * This routine will post the sgl pages for the IO that has the xritag
16476  * that is in the iocbq structure. The xritag is assigned during iocbq
16477  * creation and persists for as long as the driver is loaded.
16478  * if the caller has fewer than 256 scatter gather segments to map then
16479  * pdma_phys_addr1 should be 0.
16480  * If the caller needs to map more than 256 scatter gather segment then
16481  * pdma_phys_addr1 should be a valid physical address.
16482  * physical address for SGLs must be 64 byte aligned.
16483  * If you are going to map 2 SGL's then the first one must have 256 entries
16484  * the second sgl can have between 1 and 256 entries.
16485  *
16486  * Return codes:
16487  *      0 - Success
16488  *      -ENXIO, -ENOMEM - Failure
16489  **/
16490 int
16491 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16492                 dma_addr_t pdma_phys_addr0,
16493                 dma_addr_t pdma_phys_addr1,
16494                 uint16_t xritag)
16495 {
16496         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16497         LPFC_MBOXQ_t *mbox;
16498         int rc;
16499         uint32_t shdr_status, shdr_add_status;
16500         uint32_t mbox_tmo;
16501         union lpfc_sli4_cfg_shdr *shdr;
16502
16503         if (xritag == NO_XRI) {
16504                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16505                                 "0364 Invalid param:\n");
16506                 return -EINVAL;
16507         }
16508
16509         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16510         if (!mbox)
16511                 return -ENOMEM;
16512
16513         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16514                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16515                         sizeof(struct lpfc_mbx_post_sgl_pages) -
16516                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16517
16518         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16519                                 &mbox->u.mqe.un.post_sgl_pages;
16520         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16521         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16522
16523         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16524                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16525         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16526                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16527
16528         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16529                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16530         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16531                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16532         if (!phba->sli4_hba.intr_enable)
16533                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16534         else {
16535                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16536                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16537         }
16538         /* The IOCTL status is embedded in the mailbox subheader. */
16539         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16540         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16541         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16542         if (rc != MBX_TIMEOUT)
16543                 mempool_free(mbox, phba->mbox_mem_pool);
16544         if (shdr_status || shdr_add_status || rc) {
16545                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16546                                 "2511 POST_SGL mailbox failed with "
16547                                 "status x%x add_status x%x, mbx status x%x\n",
16548                                 shdr_status, shdr_add_status, rc);
16549         }
16550         return 0;
16551 }
16552
16553 /**
16554  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
16555  * @phba: pointer to lpfc hba data structure.
16556  *
16557  * This routine is invoked to post rpi header templates to the
16558  * HBA consistent with the SLI-4 interface spec.  This routine
16559  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16560  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
16561  *
16562  * Returns
16563  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16564  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
16565  **/
16566 static uint16_t
16567 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16568 {
16569         unsigned long xri;
16570
16571         /*
16572          * Fetch the next logical xri.  Because this index is logical,
16573          * the driver starts at 0 each time.
16574          */
16575         spin_lock_irq(&phba->hbalock);
16576         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16577                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
16578         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16579                 spin_unlock_irq(&phba->hbalock);
16580                 return NO_XRI;
16581         } else {
16582                 set_bit(xri, phba->sli4_hba.xri_bmask);
16583                 phba->sli4_hba.max_cfg_param.xri_used++;
16584         }
16585         spin_unlock_irq(&phba->hbalock);
16586         return xri;
16587 }
16588
16589 /**
16590  * lpfc_sli4_free_xri - Release an xri for reuse.
16591  * @phba: pointer to lpfc hba data structure.
16592  *
16593  * This routine is invoked to release an xri to the pool of
16594  * available rpis maintained by the driver.
16595  **/
16596 static void
16597 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16598 {
16599         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
16600                 phba->sli4_hba.max_cfg_param.xri_used--;
16601         }
16602 }
16603
16604 /**
16605  * lpfc_sli4_free_xri - Release an xri for reuse.
16606  * @phba: pointer to lpfc hba data structure.
16607  *
16608  * This routine is invoked to release an xri to the pool of
16609  * available rpis maintained by the driver.
16610  **/
16611 void
16612 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16613 {
16614         spin_lock_irq(&phba->hbalock);
16615         __lpfc_sli4_free_xri(phba, xri);
16616         spin_unlock_irq(&phba->hbalock);
16617 }
16618
16619 /**
16620  * lpfc_sli4_next_xritag - Get an xritag for the io
16621  * @phba: Pointer to HBA context object.
16622  *
16623  * This function gets an xritag for the iocb. If there is no unused xritag
16624  * it will return 0xffff.
16625  * The function returns the allocated xritag if successful, else returns zero.
16626  * Zero is not a valid xritag.
16627  * The caller is not required to hold any lock.
16628  **/
16629 uint16_t
16630 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16631 {
16632         uint16_t xri_index;
16633
16634         xri_index = lpfc_sli4_alloc_xri(phba);
16635         if (xri_index == NO_XRI)
16636                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16637                                 "2004 Failed to allocate XRI.last XRITAG is %d"
16638                                 " Max XRI is %d, Used XRI is %d\n",
16639                                 xri_index,
16640                                 phba->sli4_hba.max_cfg_param.max_xri,
16641                                 phba->sli4_hba.max_cfg_param.xri_used);
16642         return xri_index;
16643 }
16644
16645 /**
16646  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
16647  * @phba: pointer to lpfc hba data structure.
16648  * @post_sgl_list: pointer to els sgl entry list.
16649  * @count: number of els sgl entries on the list.
16650  *
16651  * This routine is invoked to post a block of driver's sgl pages to the
16652  * HBA using non-embedded mailbox command. No Lock is held. This routine
16653  * is only called when the driver is loading and after all IO has been
16654  * stopped.
16655  **/
16656 static int
16657 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
16658                             struct list_head *post_sgl_list,
16659                             int post_cnt)
16660 {
16661         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
16662         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16663         struct sgl_page_pairs *sgl_pg_pairs;
16664         void *viraddr;
16665         LPFC_MBOXQ_t *mbox;
16666         uint32_t reqlen, alloclen, pg_pairs;
16667         uint32_t mbox_tmo;
16668         uint16_t xritag_start = 0;
16669         int rc = 0;
16670         uint32_t shdr_status, shdr_add_status;
16671         union lpfc_sli4_cfg_shdr *shdr;
16672
16673         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
16674                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16675         if (reqlen > SLI4_PAGE_SIZE) {
16676                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16677                                 "2559 Block sgl registration required DMA "
16678                                 "size (%d) great than a page\n", reqlen);
16679                 return -ENOMEM;
16680         }
16681
16682         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16683         if (!mbox)
16684                 return -ENOMEM;
16685
16686         /* Allocate DMA memory and set up the non-embedded mailbox command */
16687         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16688                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16689                          LPFC_SLI4_MBX_NEMBED);
16690
16691         if (alloclen < reqlen) {
16692                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16693                                 "0285 Allocated DMA memory size (%d) is "
16694                                 "less than the requested DMA memory "
16695                                 "size (%d)\n", alloclen, reqlen);
16696                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16697                 return -ENOMEM;
16698         }
16699         /* Set up the SGL pages in the non-embedded DMA pages */
16700         viraddr = mbox->sge_array->addr[0];
16701         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16702         sgl_pg_pairs = &sgl->sgl_pg_pairs;
16703
16704         pg_pairs = 0;
16705         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
16706                 /* Set up the sge entry */
16707                 sgl_pg_pairs->sgl_pg0_addr_lo =
16708                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16709                 sgl_pg_pairs->sgl_pg0_addr_hi =
16710                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16711                 sgl_pg_pairs->sgl_pg1_addr_lo =
16712                                 cpu_to_le32(putPaddrLow(0));
16713                 sgl_pg_pairs->sgl_pg1_addr_hi =
16714                                 cpu_to_le32(putPaddrHigh(0));
16715
16716                 /* Keep the first xritag on the list */
16717                 if (pg_pairs == 0)
16718                         xritag_start = sglq_entry->sli4_xritag;
16719                 sgl_pg_pairs++;
16720                 pg_pairs++;
16721         }
16722
16723         /* Complete initialization and perform endian conversion. */
16724         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16725         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
16726         sgl->word0 = cpu_to_le32(sgl->word0);
16727
16728         if (!phba->sli4_hba.intr_enable)
16729                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16730         else {
16731                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16732                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16733         }
16734         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16735         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16736         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16737         if (rc != MBX_TIMEOUT)
16738                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16739         if (shdr_status || shdr_add_status || rc) {
16740                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16741                                 "2513 POST_SGL_BLOCK mailbox command failed "
16742                                 "status x%x add_status x%x mbx status x%x\n",
16743                                 shdr_status, shdr_add_status, rc);
16744                 rc = -ENXIO;
16745         }
16746         return rc;
16747 }
16748
16749 /**
16750  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16751  * @phba: pointer to lpfc hba data structure.
16752  * @sblist: pointer to scsi buffer list.
16753  * @count: number of scsi buffers on the list.
16754  *
16755  * This routine is invoked to post a block of @count scsi sgl pages from a
16756  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16757  * No Lock is held.
16758  *
16759  **/
16760 int
16761 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16762                               struct list_head *sblist,
16763                               int count)
16764 {
16765         struct lpfc_scsi_buf *psb;
16766         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16767         struct sgl_page_pairs *sgl_pg_pairs;
16768         void *viraddr;
16769         LPFC_MBOXQ_t *mbox;
16770         uint32_t reqlen, alloclen, pg_pairs;
16771         uint32_t mbox_tmo;
16772         uint16_t xritag_start = 0;
16773         int rc = 0;
16774         uint32_t shdr_status, shdr_add_status;
16775         dma_addr_t pdma_phys_bpl1;
16776         union lpfc_sli4_cfg_shdr *shdr;
16777
16778         /* Calculate the requested length of the dma memory */
16779         reqlen = count * sizeof(struct sgl_page_pairs) +
16780                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16781         if (reqlen > SLI4_PAGE_SIZE) {
16782                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16783                                 "0217 Block sgl registration required DMA "
16784                                 "size (%d) great than a page\n", reqlen);
16785                 return -ENOMEM;
16786         }
16787         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16788         if (!mbox) {
16789                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16790                                 "0283 Failed to allocate mbox cmd memory\n");
16791                 return -ENOMEM;
16792         }
16793
16794         /* Allocate DMA memory and set up the non-embedded mailbox command */
16795         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16796                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16797                                 LPFC_SLI4_MBX_NEMBED);
16798
16799         if (alloclen < reqlen) {
16800                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16801                                 "2561 Allocated DMA memory size (%d) is "
16802                                 "less than the requested DMA memory "
16803                                 "size (%d)\n", alloclen, reqlen);
16804                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16805                 return -ENOMEM;
16806         }
16807
16808         /* Get the first SGE entry from the non-embedded DMA memory */
16809         viraddr = mbox->sge_array->addr[0];
16810
16811         /* Set up the SGL pages in the non-embedded DMA pages */
16812         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16813         sgl_pg_pairs = &sgl->sgl_pg_pairs;
16814
16815         pg_pairs = 0;
16816         list_for_each_entry(psb, sblist, list) {
16817                 /* Set up the sge entry */
16818                 sgl_pg_pairs->sgl_pg0_addr_lo =
16819                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16820                 sgl_pg_pairs->sgl_pg0_addr_hi =
16821                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16822                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16823                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16824                 else
16825                         pdma_phys_bpl1 = 0;
16826                 sgl_pg_pairs->sgl_pg1_addr_lo =
16827                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16828                 sgl_pg_pairs->sgl_pg1_addr_hi =
16829                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16830                 /* Keep the first xritag on the list */
16831                 if (pg_pairs == 0)
16832                         xritag_start = psb->cur_iocbq.sli4_xritag;
16833                 sgl_pg_pairs++;
16834                 pg_pairs++;
16835         }
16836         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16837         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16838         /* Perform endian conversion if necessary */
16839         sgl->word0 = cpu_to_le32(sgl->word0);
16840
16841         if (!phba->sli4_hba.intr_enable)
16842                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16843         else {
16844                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16845                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16846         }
16847         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16848         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16849         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16850         if (rc != MBX_TIMEOUT)
16851                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16852         if (shdr_status || shdr_add_status || rc) {
16853                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16854                                 "2564 POST_SGL_BLOCK mailbox command failed "
16855                                 "status x%x add_status x%x mbx status x%x\n",
16856                                 shdr_status, shdr_add_status, rc);
16857                 rc = -ENXIO;
16858         }
16859         return rc;
16860 }
16861
16862 /**
16863  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16864  * @phba: pointer to lpfc_hba struct that the frame was received on
16865  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16866  *
16867  * This function checks the fields in the @fc_hdr to see if the FC frame is a
16868  * valid type of frame that the LPFC driver will handle. This function will
16869  * return a zero if the frame is a valid frame or a non zero value when the
16870  * frame does not pass the check.
16871  **/
16872 static int
16873 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16874 {
16875         /*  make rctl_names static to save stack space */
16876         struct fc_vft_header *fc_vft_hdr;
16877         uint32_t *header = (uint32_t *) fc_hdr;
16878
16879 #define FC_RCTL_MDS_DIAGS       0xF4
16880
16881         switch (fc_hdr->fh_r_ctl) {
16882         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
16883         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
16884         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
16885         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
16886         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
16887         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
16888         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
16889         case FC_RCTL_DD_CMD_STATUS:     /* command status */
16890         case FC_RCTL_ELS_REQ:   /* extended link services request */
16891         case FC_RCTL_ELS_REP:   /* extended link services reply */
16892         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
16893         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
16894         case FC_RCTL_BA_NOP:    /* basic link service NOP */
16895         case FC_RCTL_BA_ABTS:   /* basic link service abort */
16896         case FC_RCTL_BA_RMC:    /* remove connection */
16897         case FC_RCTL_BA_ACC:    /* basic accept */
16898         case FC_RCTL_BA_RJT:    /* basic reject */
16899         case FC_RCTL_BA_PRMT:
16900         case FC_RCTL_ACK_1:     /* acknowledge_1 */
16901         case FC_RCTL_ACK_0:     /* acknowledge_0 */
16902         case FC_RCTL_P_RJT:     /* port reject */
16903         case FC_RCTL_F_RJT:     /* fabric reject */
16904         case FC_RCTL_P_BSY:     /* port busy */
16905         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
16906         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
16907         case FC_RCTL_LCR:       /* link credit reset */
16908         case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
16909         case FC_RCTL_END:       /* end */
16910                 break;
16911         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
16912                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16913                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16914                 return lpfc_fc_frame_check(phba, fc_hdr);
16915         default:
16916                 goto drop;
16917         }
16918
16919 #define FC_TYPE_VENDOR_UNIQUE   0xFF
16920
16921         switch (fc_hdr->fh_type) {
16922         case FC_TYPE_BLS:
16923         case FC_TYPE_ELS:
16924         case FC_TYPE_FCP:
16925         case FC_TYPE_CT:
16926         case FC_TYPE_NVME:
16927         case FC_TYPE_VENDOR_UNIQUE:
16928                 break;
16929         case FC_TYPE_IP:
16930         case FC_TYPE_ILS:
16931         default:
16932                 goto drop;
16933         }
16934
16935         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16936                         "2538 Received frame rctl:x%x, type:x%x, "
16937                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16938                         fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16939                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16940                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16941                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16942                         be32_to_cpu(header[6]));
16943         return 0;
16944 drop:
16945         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16946                         "2539 Dropped frame rctl:x%x type:x%x\n",
16947                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16948         return 1;
16949 }
16950
16951 /**
16952  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16953  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16954  *
16955  * This function processes the FC header to retrieve the VFI from the VF
16956  * header, if one exists. This function will return the VFI if one exists
16957  * or 0 if no VSAN Header exists.
16958  **/
16959 static uint32_t
16960 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16961 {
16962         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16963
16964         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16965                 return 0;
16966         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16967 }
16968
16969 /**
16970  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16971  * @phba: Pointer to the HBA structure to search for the vport on
16972  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16973  * @fcfi: The FC Fabric ID that the frame came from
16974  *
16975  * This function searches the @phba for a vport that matches the content of the
16976  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16977  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16978  * returns the matching vport pointer or NULL if unable to match frame to a
16979  * vport.
16980  **/
16981 static struct lpfc_vport *
16982 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16983                        uint16_t fcfi, uint32_t did)
16984 {
16985         struct lpfc_vport **vports;
16986         struct lpfc_vport *vport = NULL;
16987         int i;
16988
16989         if (did == Fabric_DID)
16990                 return phba->pport;
16991         if ((phba->pport->fc_flag & FC_PT2PT) &&
16992                 !(phba->link_state == LPFC_HBA_READY))
16993                 return phba->pport;
16994
16995         vports = lpfc_create_vport_work_array(phba);
16996         if (vports != NULL) {
16997                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16998                         if (phba->fcf.fcfi == fcfi &&
16999                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17000                             vports[i]->fc_myDID == did) {
17001                                 vport = vports[i];
17002                                 break;
17003                         }
17004                 }
17005         }
17006         lpfc_destroy_vport_work_array(phba, vports);
17007         return vport;
17008 }
17009
17010 /**
17011  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17012  * @vport: The vport to work on.
17013  *
17014  * This function updates the receive sequence time stamp for this vport. The
17015  * receive sequence time stamp indicates the time that the last frame of the
17016  * the sequence that has been idle for the longest amount of time was received.
17017  * the driver uses this time stamp to indicate if any received sequences have
17018  * timed out.
17019  **/
17020 static void
17021 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17022 {
17023         struct lpfc_dmabuf *h_buf;
17024         struct hbq_dmabuf *dmabuf = NULL;
17025
17026         /* get the oldest sequence on the rcv list */
17027         h_buf = list_get_first(&vport->rcv_buffer_list,
17028                                struct lpfc_dmabuf, list);
17029         if (!h_buf)
17030                 return;
17031         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17032         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17033 }
17034
17035 /**
17036  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17037  * @vport: The vport that the received sequences were sent to.
17038  *
17039  * This function cleans up all outstanding received sequences. This is called
17040  * by the driver when a link event or user action invalidates all the received
17041  * sequences.
17042  **/
17043 void
17044 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17045 {
17046         struct lpfc_dmabuf *h_buf, *hnext;
17047         struct lpfc_dmabuf *d_buf, *dnext;
17048         struct hbq_dmabuf *dmabuf = NULL;
17049
17050         /* start with the oldest sequence on the rcv list */
17051         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17052                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17053                 list_del_init(&dmabuf->hbuf.list);
17054                 list_for_each_entry_safe(d_buf, dnext,
17055                                          &dmabuf->dbuf.list, list) {
17056                         list_del_init(&d_buf->list);
17057                         lpfc_in_buf_free(vport->phba, d_buf);
17058                 }
17059                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17060         }
17061 }
17062
17063 /**
17064  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17065  * @vport: The vport that the received sequences were sent to.
17066  *
17067  * This function determines whether any received sequences have timed out by
17068  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17069  * indicates that there is at least one timed out sequence this routine will
17070  * go through the received sequences one at a time from most inactive to most
17071  * active to determine which ones need to be cleaned up. Once it has determined
17072  * that a sequence needs to be cleaned up it will simply free up the resources
17073  * without sending an abort.
17074  **/
17075 void
17076 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17077 {
17078         struct lpfc_dmabuf *h_buf, *hnext;
17079         struct lpfc_dmabuf *d_buf, *dnext;
17080         struct hbq_dmabuf *dmabuf = NULL;
17081         unsigned long timeout;
17082         int abort_count = 0;
17083
17084         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17085                    vport->rcv_buffer_time_stamp);
17086         if (list_empty(&vport->rcv_buffer_list) ||
17087             time_before(jiffies, timeout))
17088                 return;
17089         /* start with the oldest sequence on the rcv list */
17090         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17091                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17092                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17093                            dmabuf->time_stamp);
17094                 if (time_before(jiffies, timeout))
17095                         break;
17096                 abort_count++;
17097                 list_del_init(&dmabuf->hbuf.list);
17098                 list_for_each_entry_safe(d_buf, dnext,
17099                                          &dmabuf->dbuf.list, list) {
17100                         list_del_init(&d_buf->list);
17101                         lpfc_in_buf_free(vport->phba, d_buf);
17102                 }
17103                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17104         }
17105         if (abort_count)
17106                 lpfc_update_rcv_time_stamp(vport);
17107 }
17108
17109 /**
17110  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17111  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17112  *
17113  * This function searches through the existing incomplete sequences that have
17114  * been sent to this @vport. If the frame matches one of the incomplete
17115  * sequences then the dbuf in the @dmabuf is added to the list of frames that
17116  * make up that sequence. If no sequence is found that matches this frame then
17117  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17118  * This function returns a pointer to the first dmabuf in the sequence list that
17119  * the frame was linked to.
17120  **/
17121 static struct hbq_dmabuf *
17122 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17123 {
17124         struct fc_frame_header *new_hdr;
17125         struct fc_frame_header *temp_hdr;
17126         struct lpfc_dmabuf *d_buf;
17127         struct lpfc_dmabuf *h_buf;
17128         struct hbq_dmabuf *seq_dmabuf = NULL;
17129         struct hbq_dmabuf *temp_dmabuf = NULL;
17130         uint8_t found = 0;
17131
17132         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17133         dmabuf->time_stamp = jiffies;
17134         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17135
17136         /* Use the hdr_buf to find the sequence that this frame belongs to */
17137         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17138                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17139                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17140                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17141                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17142                         continue;
17143                 /* found a pending sequence that matches this frame */
17144                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17145                 break;
17146         }
17147         if (!seq_dmabuf) {
17148                 /*
17149                  * This indicates first frame received for this sequence.
17150                  * Queue the buffer on the vport's rcv_buffer_list.
17151                  */
17152                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17153                 lpfc_update_rcv_time_stamp(vport);
17154                 return dmabuf;
17155         }
17156         temp_hdr = seq_dmabuf->hbuf.virt;
17157         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17158                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17159                 list_del_init(&seq_dmabuf->hbuf.list);
17160                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17161                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17162                 lpfc_update_rcv_time_stamp(vport);
17163                 return dmabuf;
17164         }
17165         /* move this sequence to the tail to indicate a young sequence */
17166         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17167         seq_dmabuf->time_stamp = jiffies;
17168         lpfc_update_rcv_time_stamp(vport);
17169         if (list_empty(&seq_dmabuf->dbuf.list)) {
17170                 temp_hdr = dmabuf->hbuf.virt;
17171                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17172                 return seq_dmabuf;
17173         }
17174         /* find the correct place in the sequence to insert this frame */
17175         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17176         while (!found) {
17177                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17178                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17179                 /*
17180                  * If the frame's sequence count is greater than the frame on
17181                  * the list then insert the frame right after this frame
17182                  */
17183                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17184                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17185                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
17186                         found = 1;
17187                         break;
17188                 }
17189
17190                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17191                         break;
17192                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
17193         }
17194
17195         if (found)
17196                 return seq_dmabuf;
17197         return NULL;
17198 }
17199
17200 /**
17201  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17202  * @vport: pointer to a vitural port
17203  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17204  *
17205  * This function tries to abort from the partially assembed sequence, described
17206  * by the information from basic abbort @dmabuf. It checks to see whether such
17207  * partially assembled sequence held by the driver. If so, it shall free up all
17208  * the frames from the partially assembled sequence.
17209  *
17210  * Return
17211  * true  -- if there is matching partially assembled sequence present and all
17212  *          the frames freed with the sequence;
17213  * false -- if there is no matching partially assembled sequence present so
17214  *          nothing got aborted in the lower layer driver
17215  **/
17216 static bool
17217 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17218                             struct hbq_dmabuf *dmabuf)
17219 {
17220         struct fc_frame_header *new_hdr;
17221         struct fc_frame_header *temp_hdr;
17222         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17223         struct hbq_dmabuf *seq_dmabuf = NULL;
17224
17225         /* Use the hdr_buf to find the sequence that matches this frame */
17226         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17227         INIT_LIST_HEAD(&dmabuf->hbuf.list);
17228         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17229         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17230                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17231                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17232                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17233                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17234                         continue;
17235                 /* found a pending sequence that matches this frame */
17236                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17237                 break;
17238         }
17239
17240         /* Free up all the frames from the partially assembled sequence */
17241         if (seq_dmabuf) {
17242                 list_for_each_entry_safe(d_buf, n_buf,
17243                                          &seq_dmabuf->dbuf.list, list) {
17244                         list_del_init(&d_buf->list);
17245                         lpfc_in_buf_free(vport->phba, d_buf);
17246                 }
17247                 return true;
17248         }
17249         return false;
17250 }
17251
17252 /**
17253  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17254  * @vport: pointer to a vitural port
17255  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17256  *
17257  * This function tries to abort from the assembed sequence from upper level
17258  * protocol, described by the information from basic abbort @dmabuf. It
17259  * checks to see whether such pending context exists at upper level protocol.
17260  * If so, it shall clean up the pending context.
17261  *
17262  * Return
17263  * true  -- if there is matching pending context of the sequence cleaned
17264  *          at ulp;
17265  * false -- if there is no matching pending context of the sequence present
17266  *          at ulp.
17267  **/
17268 static bool
17269 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17270 {
17271         struct lpfc_hba *phba = vport->phba;
17272         int handled;
17273
17274         /* Accepting abort at ulp with SLI4 only */
17275         if (phba->sli_rev < LPFC_SLI_REV4)
17276                 return false;
17277
17278         /* Register all caring upper level protocols to attend abort */
17279         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17280         if (handled)
17281                 return true;
17282
17283         return false;
17284 }
17285
17286 /**
17287  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
17288  * @phba: Pointer to HBA context object.
17289  * @cmd_iocbq: pointer to the command iocbq structure.
17290  * @rsp_iocbq: pointer to the response iocbq structure.
17291  *
17292  * This function handles the sequence abort response iocb command complete
17293  * event. It properly releases the memory allocated to the sequence abort
17294  * accept iocb.
17295  **/
17296 static void
17297 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
17298                              struct lpfc_iocbq *cmd_iocbq,
17299                              struct lpfc_iocbq *rsp_iocbq)
17300 {
17301         struct lpfc_nodelist *ndlp;
17302
17303         if (cmd_iocbq) {
17304                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17305                 lpfc_nlp_put(ndlp);
17306                 lpfc_nlp_not_used(ndlp);
17307                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
17308         }
17309
17310         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17311         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17312                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17313                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
17314                         rsp_iocbq->iocb.ulpStatus,
17315                         rsp_iocbq->iocb.un.ulpWord[4]);
17316 }
17317
17318 /**
17319  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17320  * @phba: Pointer to HBA context object.
17321  * @xri: xri id in transaction.
17322  *
17323  * This function validates the xri maps to the known range of XRIs allocated an
17324  * used by the driver.
17325  **/
17326 uint16_t
17327 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17328                       uint16_t xri)
17329 {
17330         uint16_t i;
17331
17332         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17333                 if (xri == phba->sli4_hba.xri_ids[i])
17334                         return i;
17335         }
17336         return NO_XRI;
17337 }
17338
17339 /**
17340  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
17341  * @phba: Pointer to HBA context object.
17342  * @fc_hdr: pointer to a FC frame header.
17343  *
17344  * This function sends a basic response to a previous unsol sequence abort
17345  * event after aborting the sequence handling.
17346  **/
17347 void
17348 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17349                         struct fc_frame_header *fc_hdr, bool aborted)
17350 {
17351         struct lpfc_hba *phba = vport->phba;
17352         struct lpfc_iocbq *ctiocb = NULL;
17353         struct lpfc_nodelist *ndlp;
17354         uint16_t oxid, rxid, xri, lxri;
17355         uint32_t sid, fctl;
17356         IOCB_t *icmd;
17357         int rc;
17358
17359         if (!lpfc_is_link_up(phba))
17360                 return;
17361
17362         sid = sli4_sid_from_fc_hdr(fc_hdr);
17363         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
17364         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
17365
17366         ndlp = lpfc_findnode_did(vport, sid);
17367         if (!ndlp) {
17368                 ndlp = lpfc_nlp_init(vport, sid);
17369                 if (!ndlp) {
17370                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17371                                          "1268 Failed to allocate ndlp for "
17372                                          "oxid:x%x SID:x%x\n", oxid, sid);
17373                         return;
17374                 }
17375                 /* Put ndlp onto pport node list */
17376                 lpfc_enqueue_node(vport, ndlp);
17377         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17378                 /* re-setup ndlp without removing from node list */
17379                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17380                 if (!ndlp) {
17381                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17382                                          "3275 Failed to active ndlp found "
17383                                          "for oxid:x%x SID:x%x\n", oxid, sid);
17384                         return;
17385                 }
17386         }
17387
17388         /* Allocate buffer for rsp iocb */
17389         ctiocb = lpfc_sli_get_iocbq(phba);
17390         if (!ctiocb)
17391                 return;
17392
17393         /* Extract the F_CTL field from FC_HDR */
17394         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17395
17396         icmd = &ctiocb->iocb;
17397         icmd->un.xseq64.bdl.bdeSize = 0;
17398         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
17399         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17400         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17401         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17402
17403         /* Fill in the rest of iocb fields */
17404         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17405         icmd->ulpBdeCount = 0;
17406         icmd->ulpLe = 1;
17407         icmd->ulpClass = CLASS3;
17408         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
17409         ctiocb->context1 = lpfc_nlp_get(ndlp);
17410
17411         ctiocb->iocb_cmpl = NULL;
17412         ctiocb->vport = phba->pport;
17413         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
17414         ctiocb->sli4_lxritag = NO_XRI;
17415         ctiocb->sli4_xritag = NO_XRI;
17416
17417         if (fctl & FC_FC_EX_CTX)
17418                 /* Exchange responder sent the abort so we
17419                  * own the oxid.
17420                  */
17421                 xri = oxid;
17422         else
17423                 xri = rxid;
17424         lxri = lpfc_sli4_xri_inrange(phba, xri);
17425         if (lxri != NO_XRI)
17426                 lpfc_set_rrq_active(phba, ndlp, lxri,
17427                         (xri == oxid) ? rxid : oxid, 0);
17428         /* For BA_ABTS from exchange responder, if the logical xri with
17429          * the oxid maps to the FCP XRI range, the port no longer has
17430          * that exchange context, send a BLS_RJT. Override the IOCB for
17431          * a BA_RJT.
17432          */
17433         if ((fctl & FC_FC_EX_CTX) &&
17434             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
17435                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17436                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17437                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17438                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17439         }
17440
17441         /* If BA_ABTS failed to abort a partially assembled receive sequence,
17442          * the driver no longer has that exchange, send a BLS_RJT. Override
17443          * the IOCB for a BA_RJT.
17444          */
17445         if (aborted == false) {
17446                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17447                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17448                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17449                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17450         }
17451
17452         if (fctl & FC_FC_EX_CTX) {
17453                 /* ABTS sent by responder to CT exchange, construction
17454                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17455                  * field and RX_ID from ABTS for RX_ID field.
17456                  */
17457                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
17458         } else {
17459                 /* ABTS sent by initiator to CT exchange, construction
17460                  * of BA_ACC will need to allocate a new XRI as for the
17461                  * XRI_TAG field.
17462                  */
17463                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
17464         }
17465         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
17466         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
17467
17468         /* Xmit CT abts response on exchange <xid> */
17469         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17470                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17471                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
17472
17473         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17474         if (rc == IOCB_ERROR) {
17475                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17476                                  "2925 Failed to issue CT ABTS RSP x%x on "
17477                                  "xri x%x, Data x%x\n",
17478                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17479                                  phba->link_state);
17480                 lpfc_nlp_put(ndlp);
17481                 ctiocb->context1 = NULL;
17482                 lpfc_sli_release_iocbq(phba, ctiocb);
17483         }
17484 }
17485
17486 /**
17487  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17488  * @vport: Pointer to the vport on which this sequence was received
17489  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17490  *
17491  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17492  * receive sequence is only partially assembed by the driver, it shall abort
17493  * the partially assembled frames for the sequence. Otherwise, if the
17494  * unsolicited receive sequence has been completely assembled and passed to
17495  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17496  * unsolicited sequence has been aborted. After that, it will issue a basic
17497  * accept to accept the abort.
17498  **/
17499 static void
17500 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17501                              struct hbq_dmabuf *dmabuf)
17502 {
17503         struct lpfc_hba *phba = vport->phba;
17504         struct fc_frame_header fc_hdr;
17505         uint32_t fctl;
17506         bool aborted;
17507
17508         /* Make a copy of fc_hdr before the dmabuf being released */
17509         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
17510         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
17511
17512         if (fctl & FC_FC_EX_CTX) {
17513                 /* ABTS by responder to exchange, no cleanup needed */
17514                 aborted = true;
17515         } else {
17516                 /* ABTS by initiator to exchange, need to do cleanup */
17517                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17518                 if (aborted == false)
17519                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
17520         }
17521         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17522
17523         if (phba->nvmet_support) {
17524                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17525                 return;
17526         }
17527
17528         /* Respond with BA_ACC or BA_RJT accordingly */
17529         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
17530 }
17531
17532 /**
17533  * lpfc_seq_complete - Indicates if a sequence is complete
17534  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17535  *
17536  * This function checks the sequence, starting with the frame described by
17537  * @dmabuf, to see if all the frames associated with this sequence are present.
17538  * the frames associated with this sequence are linked to the @dmabuf using the
17539  * dbuf list. This function looks for two major things. 1) That the first frame
17540  * has a sequence count of zero. 2) There is a frame with last frame of sequence
17541  * set. 3) That there are no holes in the sequence count. The function will
17542  * return 1 when the sequence is complete, otherwise it will return 0.
17543  **/
17544 static int
17545 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17546 {
17547         struct fc_frame_header *hdr;
17548         struct lpfc_dmabuf *d_buf;
17549         struct hbq_dmabuf *seq_dmabuf;
17550         uint32_t fctl;
17551         int seq_count = 0;
17552
17553         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17554         /* make sure first fame of sequence has a sequence count of zero */
17555         if (hdr->fh_seq_cnt != seq_count)
17556                 return 0;
17557         fctl = (hdr->fh_f_ctl[0] << 16 |
17558                 hdr->fh_f_ctl[1] << 8 |
17559                 hdr->fh_f_ctl[2]);
17560         /* If last frame of sequence we can return success. */
17561         if (fctl & FC_FC_END_SEQ)
17562                 return 1;
17563         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17564                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17565                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17566                 /* If there is a hole in the sequence count then fail. */
17567                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
17568                         return 0;
17569                 fctl = (hdr->fh_f_ctl[0] << 16 |
17570                         hdr->fh_f_ctl[1] << 8 |
17571                         hdr->fh_f_ctl[2]);
17572                 /* If last frame of sequence we can return success. */
17573                 if (fctl & FC_FC_END_SEQ)
17574                         return 1;
17575         }
17576         return 0;
17577 }
17578
17579 /**
17580  * lpfc_prep_seq - Prep sequence for ULP processing
17581  * @vport: Pointer to the vport on which this sequence was received
17582  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17583  *
17584  * This function takes a sequence, described by a list of frames, and creates
17585  * a list of iocbq structures to describe the sequence. This iocbq list will be
17586  * used to issue to the generic unsolicited sequence handler. This routine
17587  * returns a pointer to the first iocbq in the list. If the function is unable
17588  * to allocate an iocbq then it throw out the received frames that were not
17589  * able to be described and return a pointer to the first iocbq. If unable to
17590  * allocate any iocbqs (including the first) this function will return NULL.
17591  **/
17592 static struct lpfc_iocbq *
17593 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17594 {
17595         struct hbq_dmabuf *hbq_buf;
17596         struct lpfc_dmabuf *d_buf, *n_buf;
17597         struct lpfc_iocbq *first_iocbq, *iocbq;
17598         struct fc_frame_header *fc_hdr;
17599         uint32_t sid;
17600         uint32_t len, tot_len;
17601         struct ulp_bde64 *pbde;
17602
17603         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17604         /* remove from receive buffer list */
17605         list_del_init(&seq_dmabuf->hbuf.list);
17606         lpfc_update_rcv_time_stamp(vport);
17607         /* get the Remote Port's SID */
17608         sid = sli4_sid_from_fc_hdr(fc_hdr);
17609         tot_len = 0;
17610         /* Get an iocbq struct to fill in. */
17611         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17612         if (first_iocbq) {
17613                 /* Initialize the first IOCB. */
17614                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
17615                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
17616                 first_iocbq->vport = vport;
17617
17618                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17619                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17620                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17621                         first_iocbq->iocb.un.rcvels.parmRo =
17622                                 sli4_did_from_fc_hdr(fc_hdr);
17623                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17624                 } else
17625                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
17626                 first_iocbq->iocb.ulpContext = NO_XRI;
17627                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17628                         be16_to_cpu(fc_hdr->fh_ox_id);
17629                 /* iocbq is prepped for internal consumption.  Physical vpi. */
17630                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17631                         vport->phba->vpi_ids[vport->vpi];
17632                 /* put the first buffer into the first IOCBq */
17633                 tot_len = bf_get(lpfc_rcqe_length,
17634                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17635
17636                 first_iocbq->context2 = &seq_dmabuf->dbuf;
17637                 first_iocbq->context3 = NULL;
17638                 first_iocbq->iocb.ulpBdeCount = 1;
17639                 if (tot_len > LPFC_DATA_BUF_SIZE)
17640                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17641                                                         LPFC_DATA_BUF_SIZE;
17642                 else
17643                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17644
17645                 first_iocbq->iocb.un.rcvels.remoteID = sid;
17646
17647                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17648         }
17649         iocbq = first_iocbq;
17650         /*
17651          * Each IOCBq can have two Buffers assigned, so go through the list
17652          * of buffers for this sequence and save two buffers in each IOCBq
17653          */
17654         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17655                 if (!iocbq) {
17656                         lpfc_in_buf_free(vport->phba, d_buf);
17657                         continue;
17658                 }
17659                 if (!iocbq->context3) {
17660                         iocbq->context3 = d_buf;
17661                         iocbq->iocb.ulpBdeCount++;
17662                         /* We need to get the size out of the right CQE */
17663                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17664                         len = bf_get(lpfc_rcqe_length,
17665                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
17666                         pbde = (struct ulp_bde64 *)
17667                                         &iocbq->iocb.unsli3.sli3Words[4];
17668                         if (len > LPFC_DATA_BUF_SIZE)
17669                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17670                         else
17671                                 pbde->tus.f.bdeSize = len;
17672
17673                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17674                         tot_len += len;
17675                 } else {
17676                         iocbq = lpfc_sli_get_iocbq(vport->phba);
17677                         if (!iocbq) {
17678                                 if (first_iocbq) {
17679                                         first_iocbq->iocb.ulpStatus =
17680                                                         IOSTAT_FCP_RSP_ERROR;
17681                                         first_iocbq->iocb.un.ulpWord[4] =
17682                                                         IOERR_NO_RESOURCES;
17683                                 }
17684                                 lpfc_in_buf_free(vport->phba, d_buf);
17685                                 continue;
17686                         }
17687                         /* We need to get the size out of the right CQE */
17688                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17689                         len = bf_get(lpfc_rcqe_length,
17690                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
17691                         iocbq->context2 = d_buf;
17692                         iocbq->context3 = NULL;
17693                         iocbq->iocb.ulpBdeCount = 1;
17694                         if (len > LPFC_DATA_BUF_SIZE)
17695                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17696                                                         LPFC_DATA_BUF_SIZE;
17697                         else
17698                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17699
17700                         tot_len += len;
17701                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17702
17703                         iocbq->iocb.un.rcvels.remoteID = sid;
17704                         list_add_tail(&iocbq->list, &first_iocbq->list);
17705                 }
17706         }
17707         return first_iocbq;
17708 }
17709
17710 static void
17711 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17712                           struct hbq_dmabuf *seq_dmabuf)
17713 {
17714         struct fc_frame_header *fc_hdr;
17715         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17716         struct lpfc_hba *phba = vport->phba;
17717
17718         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17719         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17720         if (!iocbq) {
17721                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17722                                 "2707 Ring %d handler: Failed to allocate "
17723                                 "iocb Rctl x%x Type x%x received\n",
17724                                 LPFC_ELS_RING,
17725                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17726                 return;
17727         }
17728         if (!lpfc_complete_unsol_iocb(phba,
17729                                       phba->sli4_hba.els_wq->pring,
17730                                       iocbq, fc_hdr->fh_r_ctl,
17731                                       fc_hdr->fh_type))
17732                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17733                                 "2540 Ring %d handler: unexpected Rctl "
17734                                 "x%x Type x%x received\n",
17735                                 LPFC_ELS_RING,
17736                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17737
17738         /* Free iocb created in lpfc_prep_seq */
17739         list_for_each_entry_safe(curr_iocb, next_iocb,
17740                 &iocbq->list, list) {
17741                 list_del_init(&curr_iocb->list);
17742                 lpfc_sli_release_iocbq(phba, curr_iocb);
17743         }
17744         lpfc_sli_release_iocbq(phba, iocbq);
17745 }
17746
17747 static void
17748 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17749                             struct lpfc_iocbq *rspiocb)
17750 {
17751         struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17752
17753         if (pcmd && pcmd->virt)
17754                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17755         kfree(pcmd);
17756         lpfc_sli_release_iocbq(phba, cmdiocb);
17757 }
17758
17759 static void
17760 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17761                               struct hbq_dmabuf *dmabuf)
17762 {
17763         struct fc_frame_header *fc_hdr;
17764         struct lpfc_hba *phba = vport->phba;
17765         struct lpfc_iocbq *iocbq = NULL;
17766         union  lpfc_wqe *wqe;
17767         struct lpfc_dmabuf *pcmd = NULL;
17768         uint32_t frame_len;
17769         int rc;
17770
17771         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17772         frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17773
17774         /* Send the received frame back */
17775         iocbq = lpfc_sli_get_iocbq(phba);
17776         if (!iocbq)
17777                 goto exit;
17778
17779         /* Allocate buffer for command payload */
17780         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17781         if (pcmd)
17782                 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
17783                                             &pcmd->phys);
17784         if (!pcmd || !pcmd->virt)
17785                 goto exit;
17786
17787         INIT_LIST_HEAD(&pcmd->list);
17788
17789         /* copyin the payload */
17790         memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17791
17792         /* fill in BDE's for command */
17793         iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17794         iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17795         iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17796         iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17797
17798         iocbq->context2 = pcmd;
17799         iocbq->vport = vport;
17800         iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17801         iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17802
17803         /*
17804          * Setup rest of the iocb as though it were a WQE
17805          * Build the SEND_FRAME WQE
17806          */
17807         wqe = (union lpfc_wqe *)&iocbq->iocb;
17808
17809         wqe->send_frame.frame_len = frame_len;
17810         wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17811         wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17812         wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17813         wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17814         wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17815         wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17816
17817         iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17818         iocbq->iocb.ulpLe = 1;
17819         iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17820         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17821         if (rc == IOCB_ERROR)
17822                 goto exit;
17823
17824         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17825         return;
17826
17827 exit:
17828         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17829                         "2023 Unable to process MDS loopback frame\n");
17830         if (pcmd && pcmd->virt)
17831                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17832         kfree(pcmd);
17833         if (iocbq)
17834                 lpfc_sli_release_iocbq(phba, iocbq);
17835         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17836 }
17837
17838 /**
17839  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17840  * @phba: Pointer to HBA context object.
17841  *
17842  * This function is called with no lock held. This function processes all
17843  * the received buffers and gives it to upper layers when a received buffer
17844  * indicates that it is the final frame in the sequence. The interrupt
17845  * service routine processes received buffers at interrupt contexts.
17846  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17847  * appropriate receive function when the final frame in a sequence is received.
17848  **/
17849 void
17850 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17851                                  struct hbq_dmabuf *dmabuf)
17852 {
17853         struct hbq_dmabuf *seq_dmabuf;
17854         struct fc_frame_header *fc_hdr;
17855         struct lpfc_vport *vport;
17856         uint32_t fcfi;
17857         uint32_t did;
17858
17859         /* Process each received buffer */
17860         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17861
17862         /* check to see if this a valid type of frame */
17863         if (lpfc_fc_frame_check(phba, fc_hdr)) {
17864                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17865                 return;
17866         }
17867
17868         if ((bf_get(lpfc_cqe_code,
17869                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17870                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17871                               &dmabuf->cq_event.cqe.rcqe_cmpl);
17872         else
17873                 fcfi = bf_get(lpfc_rcqe_fcf_id,
17874                               &dmabuf->cq_event.cqe.rcqe_cmpl);
17875
17876         if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17877                 vport = phba->pport;
17878                 /* Handle MDS Loopback frames */
17879                 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17880                 return;
17881         }
17882
17883         /* d_id this frame is directed to */
17884         did = sli4_did_from_fc_hdr(fc_hdr);
17885
17886         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
17887         if (!vport) {
17888                 /* throw out the frame */
17889                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17890                 return;
17891         }
17892
17893         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17894         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17895                 (did != Fabric_DID)) {
17896                 /*
17897                  * Throw out the frame if we are not pt2pt.
17898                  * The pt2pt protocol allows for discovery frames
17899                  * to be received without a registered VPI.
17900                  */
17901                 if (!(vport->fc_flag & FC_PT2PT) ||
17902                         (phba->link_state == LPFC_HBA_READY)) {
17903                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17904                         return;
17905                 }
17906         }
17907
17908         /* Handle the basic abort sequence (BA_ABTS) event */
17909         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17910                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17911                 return;
17912         }
17913
17914         /* Link this frame */
17915         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17916         if (!seq_dmabuf) {
17917                 /* unable to add frame to vport - throw it out */
17918                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17919                 return;
17920         }
17921         /* If not last frame in sequence continue processing frames. */
17922         if (!lpfc_seq_complete(seq_dmabuf))
17923                 return;
17924
17925         /* Send the complete sequence to the upper layer protocol */
17926         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
17927 }
17928
17929 /**
17930  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17931  * @phba: pointer to lpfc hba data structure.
17932  *
17933  * This routine is invoked to post rpi header templates to the
17934  * HBA consistent with the SLI-4 interface spec.  This routine
17935  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17936  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17937  *
17938  * This routine does not require any locks.  It's usage is expected
17939  * to be driver load or reset recovery when the driver is
17940  * sequential.
17941  *
17942  * Return codes
17943  *      0 - successful
17944  *      -EIO - The mailbox failed to complete successfully.
17945  *      When this error occurs, the driver is not guaranteed
17946  *      to have any rpi regions posted to the device and
17947  *      must either attempt to repost the regions or take a
17948  *      fatal error.
17949  **/
17950 int
17951 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17952 {
17953         struct lpfc_rpi_hdr *rpi_page;
17954         uint32_t rc = 0;
17955         uint16_t lrpi = 0;
17956
17957         /* SLI4 ports that support extents do not require RPI headers. */
17958         if (!phba->sli4_hba.rpi_hdrs_in_use)
17959                 goto exit;
17960         if (phba->sli4_hba.extents_in_use)
17961                 return -EIO;
17962
17963         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
17964                 /*
17965                  * Assign the rpi headers a physical rpi only if the driver
17966                  * has not initialized those resources.  A port reset only
17967                  * needs the headers posted.
17968                  */
17969                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17970                     LPFC_RPI_RSRC_RDY)
17971                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17972
17973                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17974                 if (rc != MBX_SUCCESS) {
17975                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17976                                         "2008 Error %d posting all rpi "
17977                                         "headers\n", rc);
17978                         rc = -EIO;
17979                         break;
17980                 }
17981         }
17982
17983  exit:
17984         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17985                LPFC_RPI_RSRC_RDY);
17986         return rc;
17987 }
17988
17989 /**
17990  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17991  * @phba: pointer to lpfc hba data structure.
17992  * @rpi_page:  pointer to the rpi memory region.
17993  *
17994  * This routine is invoked to post a single rpi header to the
17995  * HBA consistent with the SLI-4 interface spec.  This memory region
17996  * maps up to 64 rpi context regions.
17997  *
17998  * Return codes
17999  *      0 - successful
18000  *      -ENOMEM - No available memory
18001  *      -EIO - The mailbox failed to complete successfully.
18002  **/
18003 int
18004 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18005 {
18006         LPFC_MBOXQ_t *mboxq;
18007         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18008         uint32_t rc = 0;
18009         uint32_t shdr_status, shdr_add_status;
18010         union lpfc_sli4_cfg_shdr *shdr;
18011
18012         /* SLI4 ports that support extents do not require RPI headers. */
18013         if (!phba->sli4_hba.rpi_hdrs_in_use)
18014                 return rc;
18015         if (phba->sli4_hba.extents_in_use)
18016                 return -EIO;
18017
18018         /* The port is notified of the header region via a mailbox command. */
18019         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18020         if (!mboxq) {
18021                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18022                                 "2001 Unable to allocate memory for issuing "
18023                                 "SLI_CONFIG_SPECIAL mailbox command\n");
18024                 return -ENOMEM;
18025         }
18026
18027         /* Post all rpi memory regions to the port. */
18028         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
18029         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18030                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18031                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
18032                          sizeof(struct lpfc_sli4_cfg_mhdr),
18033                          LPFC_SLI4_MBX_EMBED);
18034
18035
18036         /* Post the physical rpi to the port for this rpi header. */
18037         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18038                rpi_page->start_rpi);
18039         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18040                hdr_tmpl, rpi_page->page_count);
18041
18042         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18043         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
18044         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18045         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18046         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18047         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18048         if (rc != MBX_TIMEOUT)
18049                 mempool_free(mboxq, phba->mbox_mem_pool);
18050         if (shdr_status || shdr_add_status || rc) {
18051                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18052                                 "2514 POST_RPI_HDR mailbox failed with "
18053                                 "status x%x add_status x%x, mbx status x%x\n",
18054                                 shdr_status, shdr_add_status, rc);
18055                 rc = -ENXIO;
18056         } else {
18057                 /*
18058                  * The next_rpi stores the next logical module-64 rpi value used
18059                  * to post physical rpis in subsequent rpi postings.
18060                  */
18061                 spin_lock_irq(&phba->hbalock);
18062                 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18063                 spin_unlock_irq(&phba->hbalock);
18064         }
18065         return rc;
18066 }
18067
18068 /**
18069  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18070  * @phba: pointer to lpfc hba data structure.
18071  *
18072  * This routine is invoked to post rpi header templates to the
18073  * HBA consistent with the SLI-4 interface spec.  This routine
18074  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18075  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18076  *
18077  * Returns
18078  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18079  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
18080  **/
18081 int
18082 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18083 {
18084         unsigned long rpi;
18085         uint16_t max_rpi, rpi_limit;
18086         uint16_t rpi_remaining, lrpi = 0;
18087         struct lpfc_rpi_hdr *rpi_hdr;
18088         unsigned long iflag;
18089
18090         /*
18091          * Fetch the next logical rpi.  Because this index is logical,
18092          * the  driver starts at 0 each time.
18093          */
18094         spin_lock_irqsave(&phba->hbalock, iflag);
18095         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18096         rpi_limit = phba->sli4_hba.next_rpi;
18097
18098         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18099         if (rpi >= rpi_limit)
18100                 rpi = LPFC_RPI_ALLOC_ERROR;
18101         else {
18102                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18103                 phba->sli4_hba.max_cfg_param.rpi_used++;
18104                 phba->sli4_hba.rpi_count++;
18105         }
18106         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18107                         "0001 rpi:%x max:%x lim:%x\n",
18108                         (int) rpi, max_rpi, rpi_limit);
18109
18110         /*
18111          * Don't try to allocate more rpi header regions if the device limit
18112          * has been exhausted.
18113          */
18114         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18115             (phba->sli4_hba.rpi_count >= max_rpi)) {
18116                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18117                 return rpi;
18118         }
18119
18120         /*
18121          * RPI header postings are not required for SLI4 ports capable of
18122          * extents.
18123          */
18124         if (!phba->sli4_hba.rpi_hdrs_in_use) {
18125                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18126                 return rpi;
18127         }
18128
18129         /*
18130          * If the driver is running low on rpi resources, allocate another
18131          * page now.  Note that the next_rpi value is used because
18132          * it represents how many are actually in use whereas max_rpi notes
18133          * how many are supported max by the device.
18134          */
18135         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
18136         spin_unlock_irqrestore(&phba->hbalock, iflag);
18137         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18138                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18139                 if (!rpi_hdr) {
18140                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18141                                         "2002 Error Could not grow rpi "
18142                                         "count\n");
18143                 } else {
18144                         lrpi = rpi_hdr->start_rpi;
18145                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18146                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18147                 }
18148         }
18149
18150         return rpi;
18151 }
18152
18153 /**
18154  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18155  * @phba: pointer to lpfc hba data structure.
18156  *
18157  * This routine is invoked to release an rpi to the pool of
18158  * available rpis maintained by the driver.
18159  **/
18160 static void
18161 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18162 {
18163         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18164                 phba->sli4_hba.rpi_count--;
18165                 phba->sli4_hba.max_cfg_param.rpi_used--;
18166         }
18167 }
18168
18169 /**
18170  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18171  * @phba: pointer to lpfc hba data structure.
18172  *
18173  * This routine is invoked to release an rpi to the pool of
18174  * available rpis maintained by the driver.
18175  **/
18176 void
18177 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18178 {
18179         spin_lock_irq(&phba->hbalock);
18180         __lpfc_sli4_free_rpi(phba, rpi);
18181         spin_unlock_irq(&phba->hbalock);
18182 }
18183
18184 /**
18185  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18186  * @phba: pointer to lpfc hba data structure.
18187  *
18188  * This routine is invoked to remove the memory region that
18189  * provided rpi via a bitmask.
18190  **/
18191 void
18192 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18193 {
18194         kfree(phba->sli4_hba.rpi_bmask);
18195         kfree(phba->sli4_hba.rpi_ids);
18196         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
18197 }
18198
18199 /**
18200  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18201  * @phba: pointer to lpfc hba data structure.
18202  *
18203  * This routine is invoked to remove the memory region that
18204  * provided rpi via a bitmask.
18205  **/
18206 int
18207 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18208         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
18209 {
18210         LPFC_MBOXQ_t *mboxq;
18211         struct lpfc_hba *phba = ndlp->phba;
18212         int rc;
18213
18214         /* The port is notified of the header region via a mailbox command. */
18215         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18216         if (!mboxq)
18217                 return -ENOMEM;
18218
18219         /* Post all rpi memory regions to the port. */
18220         lpfc_resume_rpi(mboxq, ndlp);
18221         if (cmpl) {
18222                 mboxq->mbox_cmpl = cmpl;
18223                 mboxq->context1 = arg;
18224                 mboxq->context2 = ndlp;
18225         } else
18226                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18227         mboxq->vport = ndlp->vport;
18228         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18229         if (rc == MBX_NOT_FINISHED) {
18230                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18231                                 "2010 Resume RPI Mailbox failed "
18232                                 "status %d, mbxStatus x%x\n", rc,
18233                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18234                 mempool_free(mboxq, phba->mbox_mem_pool);
18235                 return -EIO;
18236         }
18237         return 0;
18238 }
18239
18240 /**
18241  * lpfc_sli4_init_vpi - Initialize a vpi with the port
18242  * @vport: Pointer to the vport for which the vpi is being initialized
18243  *
18244  * This routine is invoked to activate a vpi with the port.
18245  *
18246  * Returns:
18247  *    0 success
18248  *    -Evalue otherwise
18249  **/
18250 int
18251 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
18252 {
18253         LPFC_MBOXQ_t *mboxq;
18254         int rc = 0;
18255         int retval = MBX_SUCCESS;
18256         uint32_t mbox_tmo;
18257         struct lpfc_hba *phba = vport->phba;
18258         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18259         if (!mboxq)
18260                 return -ENOMEM;
18261         lpfc_init_vpi(phba, mboxq, vport->vpi);
18262         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
18263         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
18264         if (rc != MBX_SUCCESS) {
18265                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
18266                                 "2022 INIT VPI Mailbox failed "
18267                                 "status %d, mbxStatus x%x\n", rc,
18268                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18269                 retval = -EIO;
18270         }
18271         if (rc != MBX_TIMEOUT)
18272                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
18273
18274         return retval;
18275 }
18276
18277 /**
18278  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18279  * @phba: pointer to lpfc hba data structure.
18280  * @mboxq: Pointer to mailbox object.
18281  *
18282  * This routine is invoked to manually add a single FCF record. The caller
18283  * must pass a completely initialized FCF_Record.  This routine takes
18284  * care of the nonembedded mailbox operations.
18285  **/
18286 static void
18287 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18288 {
18289         void *virt_addr;
18290         union lpfc_sli4_cfg_shdr *shdr;
18291         uint32_t shdr_status, shdr_add_status;
18292
18293         virt_addr = mboxq->sge_array->addr[0];
18294         /* The IOCTL status is embedded in the mailbox subheader. */
18295         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18296         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18297         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18298
18299         if ((shdr_status || shdr_add_status) &&
18300                 (shdr_status != STATUS_FCF_IN_USE))
18301                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18302                         "2558 ADD_FCF_RECORD mailbox failed with "
18303                         "status x%x add_status x%x\n",
18304                         shdr_status, shdr_add_status);
18305
18306         lpfc_sli4_mbox_cmd_free(phba, mboxq);
18307 }
18308
18309 /**
18310  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18311  * @phba: pointer to lpfc hba data structure.
18312  * @fcf_record:  pointer to the initialized fcf record to add.
18313  *
18314  * This routine is invoked to manually add a single FCF record. The caller
18315  * must pass a completely initialized FCF_Record.  This routine takes
18316  * care of the nonembedded mailbox operations.
18317  **/
18318 int
18319 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18320 {
18321         int rc = 0;
18322         LPFC_MBOXQ_t *mboxq;
18323         uint8_t *bytep;
18324         void *virt_addr;
18325         struct lpfc_mbx_sge sge;
18326         uint32_t alloc_len, req_len;
18327         uint32_t fcfindex;
18328
18329         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18330         if (!mboxq) {
18331                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18332                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18333                 return -ENOMEM;
18334         }
18335
18336         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18337                   sizeof(uint32_t);
18338
18339         /* Allocate DMA memory and set up the non-embedded mailbox command */
18340         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18341                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18342                                      req_len, LPFC_SLI4_MBX_NEMBED);
18343         if (alloc_len < req_len) {
18344                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18345                         "2523 Allocated DMA memory size (x%x) is "
18346                         "less than the requested DMA memory "
18347                         "size (x%x)\n", alloc_len, req_len);
18348                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18349                 return -ENOMEM;
18350         }
18351
18352         /*
18353          * Get the first SGE entry from the non-embedded DMA memory.  This
18354          * routine only uses a single SGE.
18355          */
18356         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
18357         virt_addr = mboxq->sge_array->addr[0];
18358         /*
18359          * Configure the FCF record for FCFI 0.  This is the driver's
18360          * hardcoded default and gets used in nonFIP mode.
18361          */
18362         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18363         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18364         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18365
18366         /*
18367          * Copy the fcf_index and the FCF Record Data. The data starts after
18368          * the FCoE header plus word10. The data copy needs to be endian
18369          * correct.
18370          */
18371         bytep += sizeof(uint32_t);
18372         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18373         mboxq->vport = phba->pport;
18374         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18375         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18376         if (rc == MBX_NOT_FINISHED) {
18377                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18378                         "2515 ADD_FCF_RECORD mailbox failed with "
18379                         "status 0x%x\n", rc);
18380                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18381                 rc = -EIO;
18382         } else
18383                 rc = 0;
18384
18385         return rc;
18386 }
18387
18388 /**
18389  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18390  * @phba: pointer to lpfc hba data structure.
18391  * @fcf_record:  pointer to the fcf record to write the default data.
18392  * @fcf_index: FCF table entry index.
18393  *
18394  * This routine is invoked to build the driver's default FCF record.  The
18395  * values used are hardcoded.  This routine handles memory initialization.
18396  *
18397  **/
18398 void
18399 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18400                                 struct fcf_record *fcf_record,
18401                                 uint16_t fcf_index)
18402 {
18403         memset(fcf_record, 0, sizeof(struct fcf_record));
18404         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18405         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18406         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18407         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18408         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18409         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18410         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18411         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18412         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18413         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18414         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18415         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18416         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
18417         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
18418         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18419         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18420                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18421         /* Set the VLAN bit map */
18422         if (phba->valid_vlan) {
18423                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18424                         = 1 << (phba->vlan_id % 8);
18425         }
18426 }
18427
18428 /**
18429  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
18430  * @phba: pointer to lpfc hba data structure.
18431  * @fcf_index: FCF table entry offset.
18432  *
18433  * This routine is invoked to scan the entire FCF table by reading FCF
18434  * record and processing it one at a time starting from the @fcf_index
18435  * for initial FCF discovery or fast FCF failover rediscovery.
18436  *
18437  * Return 0 if the mailbox command is submitted successfully, none 0
18438  * otherwise.
18439  **/
18440 int
18441 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18442 {
18443         int rc = 0, error;
18444         LPFC_MBOXQ_t *mboxq;
18445
18446         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
18447         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
18448         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18449         if (!mboxq) {
18450                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18451                                 "2000 Failed to allocate mbox for "
18452                                 "READ_FCF cmd\n");
18453                 error = -ENOMEM;
18454                 goto fail_fcf_scan;
18455         }
18456         /* Construct the read FCF record mailbox command */
18457         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18458         if (rc) {
18459                 error = -EINVAL;
18460                 goto fail_fcf_scan;
18461         }
18462         /* Issue the mailbox command asynchronously */
18463         mboxq->vport = phba->pport;
18464         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
18465
18466         spin_lock_irq(&phba->hbalock);
18467         phba->hba_flag |= FCF_TS_INPROG;
18468         spin_unlock_irq(&phba->hbalock);
18469
18470         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18471         if (rc == MBX_NOT_FINISHED)
18472                 error = -EIO;
18473         else {
18474                 /* Reset eligible FCF count for new scan */
18475                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
18476                         phba->fcf.eligible_fcf_cnt = 0;
18477                 error = 0;
18478         }
18479 fail_fcf_scan:
18480         if (error) {
18481                 if (mboxq)
18482                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
18483                 /* FCF scan failed, clear FCF_TS_INPROG flag */
18484                 spin_lock_irq(&phba->hbalock);
18485                 phba->hba_flag &= ~FCF_TS_INPROG;
18486                 spin_unlock_irq(&phba->hbalock);
18487         }
18488         return error;
18489 }
18490
18491 /**
18492  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
18493  * @phba: pointer to lpfc hba data structure.
18494  * @fcf_index: FCF table entry offset.
18495  *
18496  * This routine is invoked to read an FCF record indicated by @fcf_index
18497  * and to use it for FLOGI roundrobin FCF failover.
18498  *
18499  * Return 0 if the mailbox command is submitted successfully, none 0
18500  * otherwise.
18501  **/
18502 int
18503 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18504 {
18505         int rc = 0, error;
18506         LPFC_MBOXQ_t *mboxq;
18507
18508         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18509         if (!mboxq) {
18510                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18511                                 "2763 Failed to allocate mbox for "
18512                                 "READ_FCF cmd\n");
18513                 error = -ENOMEM;
18514                 goto fail_fcf_read;
18515         }
18516         /* Construct the read FCF record mailbox command */
18517         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18518         if (rc) {
18519                 error = -EINVAL;
18520                 goto fail_fcf_read;
18521         }
18522         /* Issue the mailbox command asynchronously */
18523         mboxq->vport = phba->pport;
18524         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18525         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18526         if (rc == MBX_NOT_FINISHED)
18527                 error = -EIO;
18528         else
18529                 error = 0;
18530
18531 fail_fcf_read:
18532         if (error && mboxq)
18533                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18534         return error;
18535 }
18536
18537 /**
18538  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18539  * @phba: pointer to lpfc hba data structure.
18540  * @fcf_index: FCF table entry offset.
18541  *
18542  * This routine is invoked to read an FCF record indicated by @fcf_index to
18543  * determine whether it's eligible for FLOGI roundrobin failover list.
18544  *
18545  * Return 0 if the mailbox command is submitted successfully, none 0
18546  * otherwise.
18547  **/
18548 int
18549 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18550 {
18551         int rc = 0, error;
18552         LPFC_MBOXQ_t *mboxq;
18553
18554         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18555         if (!mboxq) {
18556                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18557                                 "2758 Failed to allocate mbox for "
18558                                 "READ_FCF cmd\n");
18559                                 error = -ENOMEM;
18560                                 goto fail_fcf_read;
18561         }
18562         /* Construct the read FCF record mailbox command */
18563         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18564         if (rc) {
18565                 error = -EINVAL;
18566                 goto fail_fcf_read;
18567         }
18568         /* Issue the mailbox command asynchronously */
18569         mboxq->vport = phba->pport;
18570         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18571         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18572         if (rc == MBX_NOT_FINISHED)
18573                 error = -EIO;
18574         else
18575                 error = 0;
18576
18577 fail_fcf_read:
18578         if (error && mboxq)
18579                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18580         return error;
18581 }
18582
18583 /**
18584  * lpfc_check_next_fcf_pri_level
18585  * phba pointer to the lpfc_hba struct for this port.
18586  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18587  * routine when the rr_bmask is empty. The FCF indecies are put into the
18588  * rr_bmask based on their priority level. Starting from the highest priority
18589  * to the lowest. The most likely FCF candidate will be in the highest
18590  * priority group. When this routine is called it searches the fcf_pri list for
18591  * next lowest priority group and repopulates the rr_bmask with only those
18592  * fcf_indexes.
18593  * returns:
18594  * 1=success 0=failure
18595  **/
18596 static int
18597 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18598 {
18599         uint16_t next_fcf_pri;
18600         uint16_t last_index;
18601         struct lpfc_fcf_pri *fcf_pri;
18602         int rc;
18603         int ret = 0;
18604
18605         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18606                         LPFC_SLI4_FCF_TBL_INDX_MAX);
18607         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18608                         "3060 Last IDX %d\n", last_index);
18609
18610         /* Verify the priority list has 2 or more entries */
18611         spin_lock_irq(&phba->hbalock);
18612         if (list_empty(&phba->fcf.fcf_pri_list) ||
18613             list_is_singular(&phba->fcf.fcf_pri_list)) {
18614                 spin_unlock_irq(&phba->hbalock);
18615                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18616                         "3061 Last IDX %d\n", last_index);
18617                 return 0; /* Empty rr list */
18618         }
18619         spin_unlock_irq(&phba->hbalock);
18620
18621         next_fcf_pri = 0;
18622         /*
18623          * Clear the rr_bmask and set all of the bits that are at this
18624          * priority.
18625          */
18626         memset(phba->fcf.fcf_rr_bmask, 0,
18627                         sizeof(*phba->fcf.fcf_rr_bmask));
18628         spin_lock_irq(&phba->hbalock);
18629         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18630                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18631                         continue;
18632                 /*
18633                  * the 1st priority that has not FLOGI failed
18634                  * will be the highest.
18635                  */
18636                 if (!next_fcf_pri)
18637                         next_fcf_pri = fcf_pri->fcf_rec.priority;
18638                 spin_unlock_irq(&phba->hbalock);
18639                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18640                         rc = lpfc_sli4_fcf_rr_index_set(phba,
18641                                                 fcf_pri->fcf_rec.fcf_index);
18642                         if (rc)
18643                                 return 0;
18644                 }
18645                 spin_lock_irq(&phba->hbalock);
18646         }
18647         /*
18648          * if next_fcf_pri was not set above and the list is not empty then
18649          * we have failed flogis on all of them. So reset flogi failed
18650          * and start at the beginning.
18651          */
18652         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18653                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18654                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18655                         /*
18656                          * the 1st priority that has not FLOGI failed
18657                          * will be the highest.
18658                          */
18659                         if (!next_fcf_pri)
18660                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
18661                         spin_unlock_irq(&phba->hbalock);
18662                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18663                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
18664                                                 fcf_pri->fcf_rec.fcf_index);
18665                                 if (rc)
18666                                         return 0;
18667                         }
18668                         spin_lock_irq(&phba->hbalock);
18669                 }
18670         } else
18671                 ret = 1;
18672         spin_unlock_irq(&phba->hbalock);
18673
18674         return ret;
18675 }
18676 /**
18677  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18678  * @phba: pointer to lpfc hba data structure.
18679  *
18680  * This routine is to get the next eligible FCF record index in a round
18681  * robin fashion. If the next eligible FCF record index equals to the
18682  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
18683  * shall be returned, otherwise, the next eligible FCF record's index
18684  * shall be returned.
18685  **/
18686 uint16_t
18687 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18688 {
18689         uint16_t next_fcf_index;
18690
18691 initial_priority:
18692         /* Search start from next bit of currently registered FCF index */
18693         next_fcf_index = phba->fcf.current_rec.fcf_indx;
18694
18695 next_priority:
18696         /* Determine the next fcf index to check */
18697         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
18698         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18699                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
18700                                        next_fcf_index);
18701
18702         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
18703         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18704                 /*
18705                  * If we have wrapped then we need to clear the bits that
18706                  * have been tested so that we can detect when we should
18707                  * change the priority level.
18708                  */
18709                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18710                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
18711         }
18712
18713
18714         /* Check roundrobin failover list empty condition */
18715         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18716                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18717                 /*
18718                  * If next fcf index is not found check if there are lower
18719                  * Priority level fcf's in the fcf_priority list.
18720                  * Set up the rr_bmask with all of the avaiable fcf bits
18721                  * at that level and continue the selection process.
18722                  */
18723                 if (lpfc_check_next_fcf_pri_level(phba))
18724                         goto initial_priority;
18725                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18726                                 "2844 No roundrobin failover FCF available\n");
18727
18728                 return LPFC_FCOE_FCF_NEXT_NONE;
18729         }
18730
18731         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18732                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
18733                 LPFC_FCF_FLOGI_FAILED) {
18734                 if (list_is_singular(&phba->fcf.fcf_pri_list))
18735                         return LPFC_FCOE_FCF_NEXT_NONE;
18736
18737                 goto next_priority;
18738         }
18739
18740         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18741                         "2845 Get next roundrobin failover FCF (x%x)\n",
18742                         next_fcf_index);
18743
18744         return next_fcf_index;
18745 }
18746
18747 /**
18748  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18749  * @phba: pointer to lpfc hba data structure.
18750  *
18751  * This routine sets the FCF record index in to the eligible bmask for
18752  * roundrobin failover search. It checks to make sure that the index
18753  * does not go beyond the range of the driver allocated bmask dimension
18754  * before setting the bit.
18755  *
18756  * Returns 0 if the index bit successfully set, otherwise, it returns
18757  * -EINVAL.
18758  **/
18759 int
18760 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18761 {
18762         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18763                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18764                                 "2610 FCF (x%x) reached driver's book "
18765                                 "keeping dimension:x%x\n",
18766                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18767                 return -EINVAL;
18768         }
18769         /* Set the eligible FCF record index bmask */
18770         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18771
18772         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18773                         "2790 Set FCF (x%x) to roundrobin FCF failover "
18774                         "bmask\n", fcf_index);
18775
18776         return 0;
18777 }
18778
18779 /**
18780  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
18781  * @phba: pointer to lpfc hba data structure.
18782  *
18783  * This routine clears the FCF record index from the eligible bmask for
18784  * roundrobin failover search. It checks to make sure that the index
18785  * does not go beyond the range of the driver allocated bmask dimension
18786  * before clearing the bit.
18787  **/
18788 void
18789 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18790 {
18791         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
18792         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18793                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18794                                 "2762 FCF (x%x) reached driver's book "
18795                                 "keeping dimension:x%x\n",
18796                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18797                 return;
18798         }
18799         /* Clear the eligible FCF record index bmask */
18800         spin_lock_irq(&phba->hbalock);
18801         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18802                                  list) {
18803                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18804                         list_del_init(&fcf_pri->list);
18805                         break;
18806                 }
18807         }
18808         spin_unlock_irq(&phba->hbalock);
18809         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18810
18811         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18812                         "2791 Clear FCF (x%x) from roundrobin failover "
18813                         "bmask\n", fcf_index);
18814 }
18815
18816 /**
18817  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18818  * @phba: pointer to lpfc hba data structure.
18819  *
18820  * This routine is the completion routine for the rediscover FCF table mailbox
18821  * command. If the mailbox command returned failure, it will try to stop the
18822  * FCF rediscover wait timer.
18823  **/
18824 static void
18825 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18826 {
18827         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18828         uint32_t shdr_status, shdr_add_status;
18829
18830         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18831
18832         shdr_status = bf_get(lpfc_mbox_hdr_status,
18833                              &redisc_fcf->header.cfg_shdr.response);
18834         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18835                              &redisc_fcf->header.cfg_shdr.response);
18836         if (shdr_status || shdr_add_status) {
18837                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18838                                 "2746 Requesting for FCF rediscovery failed "
18839                                 "status x%x add_status x%x\n",
18840                                 shdr_status, shdr_add_status);
18841                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
18842                         spin_lock_irq(&phba->hbalock);
18843                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
18844                         spin_unlock_irq(&phba->hbalock);
18845                         /*
18846                          * CVL event triggered FCF rediscover request failed,
18847                          * last resort to re-try current registered FCF entry.
18848                          */
18849                         lpfc_retry_pport_discovery(phba);
18850                 } else {
18851                         spin_lock_irq(&phba->hbalock);
18852                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
18853                         spin_unlock_irq(&phba->hbalock);
18854                         /*
18855                          * DEAD FCF event triggered FCF rediscover request
18856                          * failed, last resort to fail over as a link down
18857                          * to FCF registration.
18858                          */
18859                         lpfc_sli4_fcf_dead_failthrough(phba);
18860                 }
18861         } else {
18862                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18863                                 "2775 Start FCF rediscover quiescent timer\n");
18864                 /*
18865                  * Start FCF rediscovery wait timer for pending FCF
18866                  * before rescan FCF record table.
18867                  */
18868                 lpfc_fcf_redisc_wait_start_timer(phba);
18869         }
18870
18871         mempool_free(mbox, phba->mbox_mem_pool);
18872 }
18873
18874 /**
18875  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
18876  * @phba: pointer to lpfc hba data structure.
18877  *
18878  * This routine is invoked to request for rediscovery of the entire FCF table
18879  * by the port.
18880  **/
18881 int
18882 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18883 {
18884         LPFC_MBOXQ_t *mbox;
18885         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18886         int rc, length;
18887
18888         /* Cancel retry delay timers to all vports before FCF rediscover */
18889         lpfc_cancel_all_vport_retry_delay_timer(phba);
18890
18891         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18892         if (!mbox) {
18893                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18894                                 "2745 Failed to allocate mbox for "
18895                                 "requesting FCF rediscover.\n");
18896                 return -ENOMEM;
18897         }
18898
18899         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18900                   sizeof(struct lpfc_sli4_cfg_mhdr));
18901         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18902                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18903                          length, LPFC_SLI4_MBX_EMBED);
18904
18905         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18906         /* Set count to 0 for invalidating the entire FCF database */
18907         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18908
18909         /* Issue the mailbox command asynchronously */
18910         mbox->vport = phba->pport;
18911         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18912         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18913
18914         if (rc == MBX_NOT_FINISHED) {
18915                 mempool_free(mbox, phba->mbox_mem_pool);
18916                 return -EIO;
18917         }
18918         return 0;
18919 }
18920
18921 /**
18922  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18923  * @phba: pointer to lpfc hba data structure.
18924  *
18925  * This function is the failover routine as a last resort to the FCF DEAD
18926  * event when driver failed to perform fast FCF failover.
18927  **/
18928 void
18929 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18930 {
18931         uint32_t link_state;
18932
18933         /*
18934          * Last resort as FCF DEAD event failover will treat this as
18935          * a link down, but save the link state because we don't want
18936          * it to be changed to Link Down unless it is already down.
18937          */
18938         link_state = phba->link_state;
18939         lpfc_linkdown(phba);
18940         phba->link_state = link_state;
18941
18942         /* Unregister FCF if no devices connected to it */
18943         lpfc_unregister_unused_fcf(phba);
18944 }
18945
18946 /**
18947  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
18948  * @phba: pointer to lpfc hba data structure.
18949  * @rgn23_data: pointer to configure region 23 data.
18950  *
18951  * This function gets SLI3 port configure region 23 data through memory dump
18952  * mailbox command. When it successfully retrieves data, the size of the data
18953  * will be returned, otherwise, 0 will be returned.
18954  **/
18955 static uint32_t
18956 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18957 {
18958         LPFC_MBOXQ_t *pmb = NULL;
18959         MAILBOX_t *mb;
18960         uint32_t offset = 0;
18961         int rc;
18962
18963         if (!rgn23_data)
18964                 return 0;
18965
18966         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18967         if (!pmb) {
18968                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18969                                 "2600 failed to allocate mailbox memory\n");
18970                 return 0;
18971         }
18972         mb = &pmb->u.mb;
18973
18974         do {
18975                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18976                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18977
18978                 if (rc != MBX_SUCCESS) {
18979                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
18980                                         "2601 failed to read config "
18981                                         "region 23, rc 0x%x Status 0x%x\n",
18982                                         rc, mb->mbxStatus);
18983                         mb->un.varDmp.word_cnt = 0;
18984                 }
18985                 /*
18986                  * dump mem may return a zero when finished or we got a
18987                  * mailbox error, either way we are done.
18988                  */
18989                 if (mb->un.varDmp.word_cnt == 0)
18990                         break;
18991                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18992                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18993
18994                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18995                                        rgn23_data + offset,
18996                                        mb->un.varDmp.word_cnt);
18997                 offset += mb->un.varDmp.word_cnt;
18998         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18999
19000         mempool_free(pmb, phba->mbox_mem_pool);
19001         return offset;
19002 }
19003
19004 /**
19005  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19006  * @phba: pointer to lpfc hba data structure.
19007  * @rgn23_data: pointer to configure region 23 data.
19008  *
19009  * This function gets SLI4 port configure region 23 data through memory dump
19010  * mailbox command. When it successfully retrieves data, the size of the data
19011  * will be returned, otherwise, 0 will be returned.
19012  **/
19013 static uint32_t
19014 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19015 {
19016         LPFC_MBOXQ_t *mboxq = NULL;
19017         struct lpfc_dmabuf *mp = NULL;
19018         struct lpfc_mqe *mqe;
19019         uint32_t data_length = 0;
19020         int rc;
19021
19022         if (!rgn23_data)
19023                 return 0;
19024
19025         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19026         if (!mboxq) {
19027                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19028                                 "3105 failed to allocate mailbox memory\n");
19029                 return 0;
19030         }
19031
19032         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19033                 goto out;
19034         mqe = &mboxq->u.mqe;
19035         mp = (struct lpfc_dmabuf *) mboxq->context1;
19036         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19037         if (rc)
19038                 goto out;
19039         data_length = mqe->un.mb_words[5];
19040         if (data_length == 0)
19041                 goto out;
19042         if (data_length > DMP_RGN23_SIZE) {
19043                 data_length = 0;
19044                 goto out;
19045         }
19046         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19047 out:
19048         mempool_free(mboxq, phba->mbox_mem_pool);
19049         if (mp) {
19050                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19051                 kfree(mp);
19052         }
19053         return data_length;
19054 }
19055
19056 /**
19057  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19058  * @phba: pointer to lpfc hba data structure.
19059  *
19060  * This function read region 23 and parse TLV for port status to
19061  * decide if the user disaled the port. If the TLV indicates the
19062  * port is disabled, the hba_flag is set accordingly.
19063  **/
19064 void
19065 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19066 {
19067         uint8_t *rgn23_data = NULL;
19068         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19069         uint32_t offset = 0;
19070
19071         /* Get adapter Region 23 data */
19072         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19073         if (!rgn23_data)
19074                 goto out;
19075
19076         if (phba->sli_rev < LPFC_SLI_REV4)
19077                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19078         else {
19079                 if_type = bf_get(lpfc_sli_intf_if_type,
19080                                  &phba->sli4_hba.sli_intf);
19081                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19082                         goto out;
19083                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19084         }
19085
19086         if (!data_size)
19087                 goto out;
19088
19089         /* Check the region signature first */
19090         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19091                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19092                         "2619 Config region 23 has bad signature\n");
19093                         goto out;
19094         }
19095         offset += 4;
19096
19097         /* Check the data structure version */
19098         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19099                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19100                         "2620 Config region 23 has bad version\n");
19101                 goto out;
19102         }
19103         offset += 4;
19104
19105         /* Parse TLV entries in the region */
19106         while (offset < data_size) {
19107                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19108                         break;
19109                 /*
19110                  * If the TLV is not driver specific TLV or driver id is
19111                  * not linux driver id, skip the record.
19112                  */
19113                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19114                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19115                     (rgn23_data[offset + 3] != 0)) {
19116                         offset += rgn23_data[offset + 1] * 4 + 4;
19117                         continue;
19118                 }
19119
19120                 /* Driver found a driver specific TLV in the config region */
19121                 sub_tlv_len = rgn23_data[offset + 1] * 4;
19122                 offset += 4;
19123                 tlv_offset = 0;
19124
19125                 /*
19126                  * Search for configured port state sub-TLV.
19127                  */
19128                 while ((offset < data_size) &&
19129                         (tlv_offset < sub_tlv_len)) {
19130                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19131                                 offset += 4;
19132                                 tlv_offset += 4;
19133                                 break;
19134                         }
19135                         if (rgn23_data[offset] != PORT_STE_TYPE) {
19136                                 offset += rgn23_data[offset + 1] * 4 + 4;
19137                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19138                                 continue;
19139                         }
19140
19141                         /* This HBA contains PORT_STE configured */
19142                         if (!rgn23_data[offset + 2])
19143                                 phba->hba_flag |= LINK_DISABLED;
19144
19145                         goto out;
19146                 }
19147         }
19148
19149 out:
19150         kfree(rgn23_data);
19151         return;
19152 }
19153
19154 /**
19155  * lpfc_wr_object - write an object to the firmware
19156  * @phba: HBA structure that indicates port to create a queue on.
19157  * @dmabuf_list: list of dmabufs to write to the port.
19158  * @size: the total byte value of the objects to write to the port.
19159  * @offset: the current offset to be used to start the transfer.
19160  *
19161  * This routine will create a wr_object mailbox command to send to the port.
19162  * the mailbox command will be constructed using the dma buffers described in
19163  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19164  * BDEs that the imbedded mailbox can support. The @offset variable will be
19165  * used to indicate the starting offset of the transfer and will also return
19166  * the offset after the write object mailbox has completed. @size is used to
19167  * determine the end of the object and whether the eof bit should be set.
19168  *
19169  * Return 0 is successful and offset will contain the the new offset to use
19170  * for the next write.
19171  * Return negative value for error cases.
19172  **/
19173 int
19174 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19175                uint32_t size, uint32_t *offset)
19176 {
19177         struct lpfc_mbx_wr_object *wr_object;
19178         LPFC_MBOXQ_t *mbox;
19179         int rc = 0, i = 0;
19180         uint32_t shdr_status, shdr_add_status;
19181         uint32_t mbox_tmo;
19182         union lpfc_sli4_cfg_shdr *shdr;
19183         struct lpfc_dmabuf *dmabuf;
19184         uint32_t written = 0;
19185
19186         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19187         if (!mbox)
19188                 return -ENOMEM;
19189
19190         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19191                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
19192                         sizeof(struct lpfc_mbx_wr_object) -
19193                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19194
19195         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19196         wr_object->u.request.write_offset = *offset;
19197         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19198         wr_object->u.request.object_name[0] =
19199                 cpu_to_le32(wr_object->u.request.object_name[0]);
19200         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19201         list_for_each_entry(dmabuf, dmabuf_list, list) {
19202                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19203                         break;
19204                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19205                 wr_object->u.request.bde[i].addrHigh =
19206                         putPaddrHigh(dmabuf->phys);
19207                 if (written + SLI4_PAGE_SIZE >= size) {
19208                         wr_object->u.request.bde[i].tus.f.bdeSize =
19209                                 (size - written);
19210                         written += (size - written);
19211                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
19212                 } else {
19213                         wr_object->u.request.bde[i].tus.f.bdeSize =
19214                                 SLI4_PAGE_SIZE;
19215                         written += SLI4_PAGE_SIZE;
19216                 }
19217                 i++;
19218         }
19219         wr_object->u.request.bde_count = i;
19220         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19221         if (!phba->sli4_hba.intr_enable)
19222                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19223         else {
19224                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
19225                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19226         }
19227         /* The IOCTL status is embedded in the mailbox subheader. */
19228         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
19229         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19230         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19231         if (rc != MBX_TIMEOUT)
19232                 mempool_free(mbox, phba->mbox_mem_pool);
19233         if (shdr_status || shdr_add_status || rc) {
19234                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19235                                 "3025 Write Object mailbox failed with "
19236                                 "status x%x add_status x%x, mbx status x%x\n",
19237                                 shdr_status, shdr_add_status, rc);
19238                 rc = -ENXIO;
19239                 *offset = shdr_add_status;
19240         } else
19241                 *offset += wr_object->u.response.actual_write_length;
19242         return rc;
19243 }
19244
19245 /**
19246  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19247  * @vport: pointer to vport data structure.
19248  *
19249  * This function iterate through the mailboxq and clean up all REG_LOGIN
19250  * and REG_VPI mailbox commands associated with the vport. This function
19251  * is called when driver want to restart discovery of the vport due to
19252  * a Clear Virtual Link event.
19253  **/
19254 void
19255 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19256 {
19257         struct lpfc_hba *phba = vport->phba;
19258         LPFC_MBOXQ_t *mb, *nextmb;
19259         struct lpfc_dmabuf *mp;
19260         struct lpfc_nodelist *ndlp;
19261         struct lpfc_nodelist *act_mbx_ndlp = NULL;
19262         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
19263         LIST_HEAD(mbox_cmd_list);
19264         uint8_t restart_loop;
19265
19266         /* Clean up internally queued mailbox commands with the vport */
19267         spin_lock_irq(&phba->hbalock);
19268         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19269                 if (mb->vport != vport)
19270                         continue;
19271
19272                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19273                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
19274                         continue;
19275
19276                 list_del(&mb->list);
19277                 list_add_tail(&mb->list, &mbox_cmd_list);
19278         }
19279         /* Clean up active mailbox command with the vport */
19280         mb = phba->sli.mbox_active;
19281         if (mb && (mb->vport == vport)) {
19282                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19283                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
19284                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19285                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19286                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
19287                         /* Put reference count for delayed processing */
19288                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19289                         /* Unregister the RPI when mailbox complete */
19290                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19291                 }
19292         }
19293         /* Cleanup any mailbox completions which are not yet processed */
19294         do {
19295                 restart_loop = 0;
19296                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19297                         /*
19298                          * If this mailox is already processed or it is
19299                          * for another vport ignore it.
19300                          */
19301                         if ((mb->vport != vport) ||
19302                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19303                                 continue;
19304
19305                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19306                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19307                                 continue;
19308
19309                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19310                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19311                                 ndlp = (struct lpfc_nodelist *)mb->context2;
19312                                 /* Unregister the RPI when mailbox complete */
19313                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19314                                 restart_loop = 1;
19315                                 spin_unlock_irq(&phba->hbalock);
19316                                 spin_lock(shost->host_lock);
19317                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19318                                 spin_unlock(shost->host_lock);
19319                                 spin_lock_irq(&phba->hbalock);
19320                                 break;
19321                         }
19322                 }
19323         } while (restart_loop);
19324
19325         spin_unlock_irq(&phba->hbalock);
19326
19327         /* Release the cleaned-up mailbox commands */
19328         while (!list_empty(&mbox_cmd_list)) {
19329                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
19330                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19331                         mp = (struct lpfc_dmabuf *) (mb->context1);
19332                         if (mp) {
19333                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19334                                 kfree(mp);
19335                         }
19336                         ndlp = (struct lpfc_nodelist *) mb->context2;
19337                         mb->context2 = NULL;
19338                         if (ndlp) {
19339                                 spin_lock(shost->host_lock);
19340                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19341                                 spin_unlock(shost->host_lock);
19342                                 lpfc_nlp_put(ndlp);
19343                         }
19344                 }
19345                 mempool_free(mb, phba->mbox_mem_pool);
19346         }
19347
19348         /* Release the ndlp with the cleaned-up active mailbox command */
19349         if (act_mbx_ndlp) {
19350                 spin_lock(shost->host_lock);
19351                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19352                 spin_unlock(shost->host_lock);
19353                 lpfc_nlp_put(act_mbx_ndlp);
19354         }
19355 }
19356
19357 /**
19358  * lpfc_drain_txq - Drain the txq
19359  * @phba: Pointer to HBA context object.
19360  *
19361  * This function attempt to submit IOCBs on the txq
19362  * to the adapter.  For SLI4 adapters, the txq contains
19363  * ELS IOCBs that have been deferred because the there
19364  * are no SGLs.  This congestion can occur with large
19365  * vport counts during node discovery.
19366  **/
19367
19368 uint32_t
19369 lpfc_drain_txq(struct lpfc_hba *phba)
19370 {
19371         LIST_HEAD(completions);
19372         struct lpfc_sli_ring *pring;
19373         struct lpfc_iocbq *piocbq = NULL;
19374         unsigned long iflags = 0;
19375         char *fail_msg = NULL;
19376         struct lpfc_sglq *sglq;
19377         union lpfc_wqe128 wqe;
19378         uint32_t txq_cnt = 0;
19379         struct lpfc_queue *wq;
19380
19381         if (phba->link_flag & LS_MDS_LOOPBACK) {
19382                 /* MDS WQE are posted only to first WQ*/
19383                 wq = phba->sli4_hba.fcp_wq[0];
19384                 if (unlikely(!wq))
19385                         return 0;
19386                 pring = wq->pring;
19387         } else {
19388                 wq = phba->sli4_hba.els_wq;
19389                 if (unlikely(!wq))
19390                         return 0;
19391                 pring = lpfc_phba_elsring(phba);
19392         }
19393
19394         if (unlikely(!pring) || list_empty(&pring->txq))
19395                 return 0;
19396
19397         spin_lock_irqsave(&pring->ring_lock, iflags);
19398         list_for_each_entry(piocbq, &pring->txq, list) {
19399                 txq_cnt++;
19400         }
19401
19402         if (txq_cnt > pring->txq_max)
19403                 pring->txq_max = txq_cnt;
19404
19405         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19406
19407         while (!list_empty(&pring->txq)) {
19408                 spin_lock_irqsave(&pring->ring_lock, iflags);
19409
19410                 piocbq = lpfc_sli_ringtx_get(phba, pring);
19411                 if (!piocbq) {
19412                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19413                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19414                                 "2823 txq empty and txq_cnt is %d\n ",
19415                                 txq_cnt);
19416                         break;
19417                 }
19418                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
19419                 if (!sglq) {
19420                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
19421                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19422                         break;
19423                 }
19424                 txq_cnt--;
19425
19426                 /* The xri and iocb resources secured,
19427                  * attempt to issue request
19428                  */
19429                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
19430                 piocbq->sli4_xritag = sglq->sli4_xritag;
19431                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19432                         fail_msg = "to convert bpl to sgl";
19433                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
19434                         fail_msg = "to convert iocb to wqe";
19435                 else if (lpfc_sli4_wq_put(wq, &wqe))
19436                         fail_msg = " - Wq is full";
19437                 else
19438                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19439
19440                 if (fail_msg) {
19441                         /* Failed means we can't issue and need to cancel */
19442                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19443                                         "2822 IOCB failed %s iotag 0x%x "
19444                                         "xri 0x%x\n",
19445                                         fail_msg,
19446                                         piocbq->iotag, piocbq->sli4_xritag);
19447                         list_add_tail(&piocbq->list, &completions);
19448                 }
19449                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19450         }
19451
19452         /* Cancel all the IOCBs that cannot be issued */
19453         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19454                                 IOERR_SLI_ABORTED);
19455
19456         return txq_cnt;
19457 }
19458
19459 /**
19460  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19461  * @phba: Pointer to HBA context object.
19462  * @pwqe: Pointer to command WQE.
19463  * @sglq: Pointer to the scatter gather queue object.
19464  *
19465  * This routine converts the bpl or bde that is in the WQE
19466  * to a sgl list for the sli4 hardware. The physical address
19467  * of the bpl/bde is converted back to a virtual address.
19468  * If the WQE contains a BPL then the list of BDE's is
19469  * converted to sli4_sge's. If the WQE contains a single
19470  * BDE then it is converted to a single sli_sge.
19471  * The WQE is still in cpu endianness so the contents of
19472  * the bpl can be used without byte swapping.
19473  *
19474  * Returns valid XRI = Success, NO_XRI = Failure.
19475  */
19476 static uint16_t
19477 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19478                  struct lpfc_sglq *sglq)
19479 {
19480         uint16_t xritag = NO_XRI;
19481         struct ulp_bde64 *bpl = NULL;
19482         struct ulp_bde64 bde;
19483         struct sli4_sge *sgl  = NULL;
19484         struct lpfc_dmabuf *dmabuf;
19485         union lpfc_wqe128 *wqe;
19486         int numBdes = 0;
19487         int i = 0;
19488         uint32_t offset = 0; /* accumulated offset in the sg request list */
19489         int inbound = 0; /* number of sg reply entries inbound from firmware */
19490         uint32_t cmd;
19491
19492         if (!pwqeq || !sglq)
19493                 return xritag;
19494
19495         sgl  = (struct sli4_sge *)sglq->sgl;
19496         wqe = &pwqeq->wqe;
19497         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19498
19499         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19500         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19501                 return sglq->sli4_xritag;
19502         numBdes = pwqeq->rsvd2;
19503         if (numBdes) {
19504                 /* The addrHigh and addrLow fields within the WQE
19505                  * have not been byteswapped yet so there is no
19506                  * need to swap them back.
19507                  */
19508                 if (pwqeq->context3)
19509                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19510                 else
19511                         return xritag;
19512
19513                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
19514                 if (!bpl)
19515                         return xritag;
19516
19517                 for (i = 0; i < numBdes; i++) {
19518                         /* Should already be byte swapped. */
19519                         sgl->addr_hi = bpl->addrHigh;
19520                         sgl->addr_lo = bpl->addrLow;
19521
19522                         sgl->word2 = le32_to_cpu(sgl->word2);
19523                         if ((i+1) == numBdes)
19524                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
19525                         else
19526                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
19527                         /* swap the size field back to the cpu so we
19528                          * can assign it to the sgl.
19529                          */
19530                         bde.tus.w = le32_to_cpu(bpl->tus.w);
19531                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19532                         /* The offsets in the sgl need to be accumulated
19533                          * separately for the request and reply lists.
19534                          * The request is always first, the reply follows.
19535                          */
19536                         switch (cmd) {
19537                         case CMD_GEN_REQUEST64_WQE:
19538                                 /* add up the reply sg entries */
19539                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19540                                         inbound++;
19541                                 /* first inbound? reset the offset */
19542                                 if (inbound == 1)
19543                                         offset = 0;
19544                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19545                                 bf_set(lpfc_sli4_sge_type, sgl,
19546                                         LPFC_SGE_TYPE_DATA);
19547                                 offset += bde.tus.f.bdeSize;
19548                                 break;
19549                         case CMD_FCP_TRSP64_WQE:
19550                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19551                                 bf_set(lpfc_sli4_sge_type, sgl,
19552                                         LPFC_SGE_TYPE_DATA);
19553                                 break;
19554                         case CMD_FCP_TSEND64_WQE:
19555                         case CMD_FCP_TRECEIVE64_WQE:
19556                                 bf_set(lpfc_sli4_sge_type, sgl,
19557                                         bpl->tus.f.bdeFlags);
19558                                 if (i < 3)
19559                                         offset = 0;
19560                                 else
19561                                         offset += bde.tus.f.bdeSize;
19562                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19563                                 break;
19564                         }
19565                         sgl->word2 = cpu_to_le32(sgl->word2);
19566                         bpl++;
19567                         sgl++;
19568                 }
19569         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19570                 /* The addrHigh and addrLow fields of the BDE have not
19571                  * been byteswapped yet so they need to be swapped
19572                  * before putting them in the sgl.
19573                  */
19574                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19575                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19576                 sgl->word2 = le32_to_cpu(sgl->word2);
19577                 bf_set(lpfc_sli4_sge_last, sgl, 1);
19578                 sgl->word2 = cpu_to_le32(sgl->word2);
19579                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19580         }
19581         return sglq->sli4_xritag;
19582 }
19583
19584 /**
19585  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19586  * @phba: Pointer to HBA context object.
19587  * @ring_number: Base sli ring number
19588  * @pwqe: Pointer to command WQE.
19589  **/
19590 int
19591 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19592                     struct lpfc_iocbq *pwqe)
19593 {
19594         union lpfc_wqe128 *wqe = &pwqe->wqe;
19595         struct lpfc_nvmet_rcv_ctx *ctxp;
19596         struct lpfc_queue *wq;
19597         struct lpfc_sglq *sglq;
19598         struct lpfc_sli_ring *pring;
19599         unsigned long iflags;
19600         uint32_t ret = 0;
19601
19602         /* NVME_LS and NVME_LS ABTS requests. */
19603         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19604                 pring =  phba->sli4_hba.nvmels_wq->pring;
19605                 spin_lock_irqsave(&pring->ring_lock, iflags);
19606                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19607                 if (!sglq) {
19608                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19609                         return WQE_BUSY;
19610                 }
19611                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19612                 pwqe->sli4_xritag = sglq->sli4_xritag;
19613                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19614                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19615                         return WQE_ERROR;
19616                 }
19617                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19618                        pwqe->sli4_xritag);
19619                 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19620                 if (ret) {
19621                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19622                         return ret;
19623                 }
19624
19625                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19626                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19627                 return 0;
19628         }
19629
19630         /* NVME_FCREQ and NVME_ABTS requests */
19631         if (pwqe->iocb_flag & LPFC_IO_NVME) {
19632                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19633                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19634
19635                 spin_lock_irqsave(&pring->ring_lock, iflags);
19636                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19637                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19638                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
19639                 ret = lpfc_sli4_wq_put(wq, wqe);
19640                 if (ret) {
19641                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19642                         return ret;
19643                 }
19644                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19645                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19646                 return 0;
19647         }
19648
19649         /* NVMET requests */
19650         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19651                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19652                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19653
19654                 spin_lock_irqsave(&pring->ring_lock, iflags);
19655                 ctxp = pwqe->context2;
19656                 sglq = ctxp->ctxbuf->sglq;
19657                 if (pwqe->sli4_xritag ==  NO_XRI) {
19658                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
19659                         pwqe->sli4_xritag = sglq->sli4_xritag;
19660                 }
19661                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19662                        pwqe->sli4_xritag);
19663                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19664                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19665                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
19666                 ret = lpfc_sli4_wq_put(wq, wqe);
19667                 if (ret) {
19668                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19669                         return ret;
19670                 }
19671                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19672                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19673                 return 0;
19674         }
19675         return WQE_ERROR;
19676 }