]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: cleanup: Remove unused FCP_XRI_ABORT_EVENT slowpath event
[linux.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/lockdep.h>
30
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport_fc.h>
36 #include <scsi/fc/fc_fs.h>
37 #include <linux/aer.h>
38 #ifdef CONFIG_X86
39 #include <asm/set_memory.h>
40 #endif
41
42 #include <linux/nvme-fc-driver.h>
43
44 #include "lpfc_hw4.h"
45 #include "lpfc_hw.h"
46 #include "lpfc_sli.h"
47 #include "lpfc_sli4.h"
48 #include "lpfc_nl.h"
49 #include "lpfc_disc.h"
50 #include "lpfc.h"
51 #include "lpfc_scsi.h"
52 #include "lpfc_nvme.h"
53 #include "lpfc_nvmet.h"
54 #include "lpfc_crtn.h"
55 #include "lpfc_logmsg.h"
56 #include "lpfc_compat.h"
57 #include "lpfc_debugfs.h"
58 #include "lpfc_vport.h"
59 #include "lpfc_version.h"
60
61 /* There are only four IOCB completion types. */
62 typedef enum _lpfc_iocb_type {
63         LPFC_UNKNOWN_IOCB,
64         LPFC_UNSOL_IOCB,
65         LPFC_SOL_IOCB,
66         LPFC_ABORT_IOCB
67 } lpfc_iocb_type;
68
69
70 /* Provide function prototypes local to this module. */
71 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
72                                   uint32_t);
73 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
74                               uint8_t *, uint32_t *);
75 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
76                                                          struct lpfc_iocbq *);
77 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
78                                       struct hbq_dmabuf *);
79 static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
80                                           struct hbq_dmabuf *dmabuf);
81 static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
82                                     struct lpfc_cqe *);
83 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
84                                        int);
85 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
86                                      struct lpfc_eqe *eqe, uint32_t qidx);
87 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
88 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
89 static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
90                                    struct lpfc_sli_ring *pring,
91                                    struct lpfc_iocbq *cmdiocb);
92
93 static IOCB_t *
94 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
95 {
96         return &iocbq->iocb;
97 }
98
99 #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
100 /**
101  * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
102  * @srcp: Source memory pointer.
103  * @destp: Destination memory pointer.
104  * @cnt: Number of words required to be copied.
105  *       Must be a multiple of sizeof(uint64_t)
106  *
107  * This function is used for copying data between driver memory
108  * and the SLI WQ. This function also changes the endianness
109  * of each word if native endianness is different from SLI
110  * endianness. This function can be called with or without
111  * lock.
112  **/
113 void
114 lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
115 {
116         uint64_t *src = srcp;
117         uint64_t *dest = destp;
118         int i;
119
120         for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
121                 *dest++ = *src++;
122 }
123 #else
124 #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
125 #endif
126
127 /**
128  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
129  * @q: The Work Queue to operate on.
130  * @wqe: The work Queue Entry to put on the Work queue.
131  *
132  * This routine will copy the contents of @wqe to the next available entry on
133  * the @q. This function will then ring the Work Queue Doorbell to signal the
134  * HBA to start processing the Work Queue Entry. This function returns 0 if
135  * successful. If no entries are available on @q then this function will return
136  * -ENOMEM.
137  * The caller is expected to hold the hbalock when calling this routine.
138  **/
139 static int
140 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
141 {
142         union lpfc_wqe *temp_wqe;
143         struct lpfc_register doorbell;
144         uint32_t host_index;
145         uint32_t idx;
146         uint32_t i = 0;
147         uint8_t *tmp;
148         u32 if_type;
149
150         /* sanity check on queue memory */
151         if (unlikely(!q))
152                 return -ENOMEM;
153         temp_wqe = q->qe[q->host_index].wqe;
154
155         /* If the host has not yet processed the next entry then we are done */
156         idx = ((q->host_index + 1) % q->entry_count);
157         if (idx == q->hba_index) {
158                 q->WQ_overflow++;
159                 return -EBUSY;
160         }
161         q->WQ_posted++;
162         /* set consumption flag every once in a while */
163         if (!((q->host_index + 1) % q->entry_repost))
164                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
165         else
166                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
167         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
168                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
169         lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
170         if (q->dpp_enable && q->phba->cfg_enable_dpp) {
171                 /* write to DPP aperture taking advatage of Combined Writes */
172                 tmp = (uint8_t *)temp_wqe;
173 #ifdef __raw_writeq
174                 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
175                         __raw_writeq(*((uint64_t *)(tmp + i)),
176                                         q->dpp_regaddr + i);
177 #else
178                 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
179                         __raw_writel(*((uint32_t *)(tmp + i)),
180                                         q->dpp_regaddr + i);
181 #endif
182         }
183         /* ensure WQE bcopy and DPP flushed before doorbell write */
184         wmb();
185
186         /* Update the host index before invoking device */
187         host_index = q->host_index;
188
189         q->host_index = idx;
190
191         /* Ring Doorbell */
192         doorbell.word0 = 0;
193         if (q->db_format == LPFC_DB_LIST_FORMAT) {
194                 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
195                         bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
196                         bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
197                         bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
198                             q->dpp_id);
199                         bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
200                             q->queue_id);
201                 } else {
202                         bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
203                         bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
204
205                         /* Leave bits <23:16> clear for if_type 6 dpp */
206                         if_type = bf_get(lpfc_sli_intf_if_type,
207                                          &q->phba->sli4_hba.sli_intf);
208                         if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
209                                 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
210                                        host_index);
211                 }
212         } else if (q->db_format == LPFC_DB_RING_FORMAT) {
213                 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
214                 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
215         } else {
216                 return -EINVAL;
217         }
218         writel(doorbell.word0, q->db_regaddr);
219
220         return 0;
221 }
222
223 /**
224  * lpfc_sli4_wq_release - Updates internal hba index for WQ
225  * @q: The Work Queue to operate on.
226  * @index: The index to advance the hba index to.
227  *
228  * This routine will update the HBA index of a queue to reflect consumption of
229  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
230  * an entry the host calls this function to update the queue's internal
231  * pointers. This routine returns the number of entries that were consumed by
232  * the HBA.
233  **/
234 static uint32_t
235 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
236 {
237         uint32_t released = 0;
238
239         /* sanity check on queue memory */
240         if (unlikely(!q))
241                 return 0;
242
243         if (q->hba_index == index)
244                 return 0;
245         do {
246                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
247                 released++;
248         } while (q->hba_index != index);
249         return released;
250 }
251
252 /**
253  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
254  * @q: The Mailbox Queue to operate on.
255  * @wqe: The Mailbox Queue Entry to put on the Work queue.
256  *
257  * This routine will copy the contents of @mqe to the next available entry on
258  * the @q. This function will then ring the Work Queue Doorbell to signal the
259  * HBA to start processing the Work Queue Entry. This function returns 0 if
260  * successful. If no entries are available on @q then this function will return
261  * -ENOMEM.
262  * The caller is expected to hold the hbalock when calling this routine.
263  **/
264 static uint32_t
265 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
266 {
267         struct lpfc_mqe *temp_mqe;
268         struct lpfc_register doorbell;
269
270         /* sanity check on queue memory */
271         if (unlikely(!q))
272                 return -ENOMEM;
273         temp_mqe = q->qe[q->host_index].mqe;
274
275         /* If the host has not yet processed the next entry then we are done */
276         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
277                 return -ENOMEM;
278         lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
279         /* Save off the mailbox pointer for completion */
280         q->phba->mbox = (MAILBOX_t *)temp_mqe;
281
282         /* Update the host index before invoking device */
283         q->host_index = ((q->host_index + 1) % q->entry_count);
284
285         /* Ring Doorbell */
286         doorbell.word0 = 0;
287         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
288         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
289         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
290         return 0;
291 }
292
293 /**
294  * lpfc_sli4_mq_release - Updates internal hba index for MQ
295  * @q: The Mailbox Queue to operate on.
296  *
297  * This routine will update the HBA index of a queue to reflect consumption of
298  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
299  * an entry the host calls this function to update the queue's internal
300  * pointers. This routine returns the number of entries that were consumed by
301  * the HBA.
302  **/
303 static uint32_t
304 lpfc_sli4_mq_release(struct lpfc_queue *q)
305 {
306         /* sanity check on queue memory */
307         if (unlikely(!q))
308                 return 0;
309
310         /* Clear the mailbox pointer for completion */
311         q->phba->mbox = NULL;
312         q->hba_index = ((q->hba_index + 1) % q->entry_count);
313         return 1;
314 }
315
316 /**
317  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
318  * @q: The Event Queue to get the first valid EQE from
319  *
320  * This routine will get the first valid Event Queue Entry from @q, update
321  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
322  * the Queue (no more work to do), or the Queue is full of EQEs that have been
323  * processed, but not popped back to the HBA then this routine will return NULL.
324  **/
325 static struct lpfc_eqe *
326 lpfc_sli4_eq_get(struct lpfc_queue *q)
327 {
328         struct lpfc_hba *phba;
329         struct lpfc_eqe *eqe;
330         uint32_t idx;
331
332         /* sanity check on queue memory */
333         if (unlikely(!q))
334                 return NULL;
335         phba = q->phba;
336         eqe = q->qe[q->hba_index].eqe;
337
338         /* If the next EQE is not valid then we are done */
339         if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
340                 return NULL;
341         /* If the host has not yet processed the next entry then we are done */
342         idx = ((q->hba_index + 1) % q->entry_count);
343         if (idx == q->host_index)
344                 return NULL;
345
346         q->hba_index = idx;
347         /* if the index wrapped around, toggle the valid bit */
348         if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
349                 q->qe_valid = (q->qe_valid) ? 0 : 1;
350
351
352         /*
353          * insert barrier for instruction interlock : data from the hardware
354          * must have the valid bit checked before it can be copied and acted
355          * upon. Speculative instructions were allowing a bcopy at the start
356          * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
357          * after our return, to copy data before the valid bit check above
358          * was done. As such, some of the copied data was stale. The barrier
359          * ensures the check is before any data is copied.
360          */
361         mb();
362         return eqe;
363 }
364
365 /**
366  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
367  * @q: The Event Queue to disable interrupts
368  *
369  **/
370 inline void
371 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
372 {
373         struct lpfc_register doorbell;
374
375         doorbell.word0 = 0;
376         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
377         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
378         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
379                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
380         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
381         writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
382 }
383
384 /**
385  * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
386  * @q: The Event Queue to disable interrupts
387  *
388  **/
389 inline void
390 lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
391 {
392         struct lpfc_register doorbell;
393
394         doorbell.word0 = 0;
395         bf_set(lpfc_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->ctx_buf);
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->ctx_ndlp;
2495                 lpfc_nlp_put(ndlp);
2496                 pmb->ctx_buf = NULL;
2497                 pmb->ctx_ndlp = NULL;
2498         }
2499
2500         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2501                 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2502
2503                 /* Check to see if there are any deferred events to process */
2504                 if (ndlp) {
2505                         lpfc_printf_vlog(
2506                                 vport,
2507                                 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2508                                 "1438 UNREG cmpl deferred mbox x%x "
2509                                 "on NPort x%x Data: x%x x%x %p\n",
2510                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2511                                 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2512
2513                         if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2514                             (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
2515                                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2516                                 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2517                                 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2518                         } else {
2519                                 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2520                         }
2521                 }
2522                 pmb->ctx_ndlp = NULL;
2523         }
2524
2525         /* Check security permission status on INIT_LINK mailbox command */
2526         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2527             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2528                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2529                                 "2860 SLI authentication is required "
2530                                 "for INIT_LINK but has not done yet\n");
2531
2532         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2533                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2534         else
2535                 mempool_free(pmb, phba->mbox_mem_pool);
2536 }
2537  /**
2538  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2539  * @phba: Pointer to HBA context object.
2540  * @pmb: Pointer to mailbox object.
2541  *
2542  * This function is the unreg rpi mailbox completion handler. It
2543  * frees the memory resources associated with the completed mailbox
2544  * command. An additional refrenece is put on the ndlp to prevent
2545  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2546  * the unreg mailbox command completes, this routine puts the
2547  * reference back.
2548  *
2549  **/
2550 void
2551 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2552 {
2553         struct lpfc_vport  *vport = pmb->vport;
2554         struct lpfc_nodelist *ndlp;
2555
2556         ndlp = pmb->ctx_ndlp;
2557         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2558                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2559                     (bf_get(lpfc_sli_intf_if_type,
2560                      &phba->sli4_hba.sli_intf) >=
2561                      LPFC_SLI_INTF_IF_TYPE_2)) {
2562                         if (ndlp) {
2563                                 lpfc_printf_vlog(
2564                                         vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2565                                          "0010 UNREG_LOGIN vpi:%x "
2566                                          "rpi:%x DID:%x defer x%x flg x%x "
2567                                          "map:%x %p\n",
2568                                          vport->vpi, ndlp->nlp_rpi,
2569                                          ndlp->nlp_DID, ndlp->nlp_defer_did,
2570                                          ndlp->nlp_flag,
2571                                          ndlp->nlp_usg_map, ndlp);
2572                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2573                                 lpfc_nlp_put(ndlp);
2574
2575                                 /* Check to see if there are any deferred
2576                                  * events to process
2577                                  */
2578                                 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2579                                     (ndlp->nlp_defer_did !=
2580                                     NLP_EVT_NOTHING_PENDING)) {
2581                                         lpfc_printf_vlog(
2582                                                 vport, KERN_INFO, LOG_DISCOVERY,
2583                                                 "4111 UNREG cmpl deferred "
2584                                                 "clr x%x on "
2585                                                 "NPort x%x Data: x%x %p\n",
2586                                                 ndlp->nlp_rpi, ndlp->nlp_DID,
2587                                                 ndlp->nlp_defer_did, ndlp);
2588                                         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2589                                         ndlp->nlp_defer_did =
2590                                                 NLP_EVT_NOTHING_PENDING;
2591                                         lpfc_issue_els_plogi(
2592                                                 vport, ndlp->nlp_DID, 0);
2593                                 } else {
2594                                         ndlp->nlp_flag &= ~NLP_UNREG_INP;
2595                                 }
2596                         }
2597                 }
2598         }
2599
2600         mempool_free(pmb, phba->mbox_mem_pool);
2601 }
2602
2603 /**
2604  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2605  * @phba: Pointer to HBA context object.
2606  *
2607  * This function is called with no lock held. This function processes all
2608  * the completed mailbox commands and gives it to upper layers. The interrupt
2609  * service routine processes mailbox completion interrupt and adds completed
2610  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2611  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2612  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2613  * function returns the mailbox commands to the upper layer by calling the
2614  * completion handler function of each mailbox.
2615  **/
2616 int
2617 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2618 {
2619         MAILBOX_t *pmbox;
2620         LPFC_MBOXQ_t *pmb;
2621         int rc;
2622         LIST_HEAD(cmplq);
2623
2624         phba->sli.slistat.mbox_event++;
2625
2626         /* Get all completed mailboxe buffers into the cmplq */
2627         spin_lock_irq(&phba->hbalock);
2628         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2629         spin_unlock_irq(&phba->hbalock);
2630
2631         /* Get a Mailbox buffer to setup mailbox commands for callback */
2632         do {
2633                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2634                 if (pmb == NULL)
2635                         break;
2636
2637                 pmbox = &pmb->u.mb;
2638
2639                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2640                         if (pmb->vport) {
2641                                 lpfc_debugfs_disc_trc(pmb->vport,
2642                                         LPFC_DISC_TRC_MBOX_VPORT,
2643                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2644                                         (uint32_t)pmbox->mbxCommand,
2645                                         pmbox->un.varWords[0],
2646                                         pmbox->un.varWords[1]);
2647                         }
2648                         else {
2649                                 lpfc_debugfs_disc_trc(phba->pport,
2650                                         LPFC_DISC_TRC_MBOX,
2651                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2652                                         (uint32_t)pmbox->mbxCommand,
2653                                         pmbox->un.varWords[0],
2654                                         pmbox->un.varWords[1]);
2655                         }
2656                 }
2657
2658                 /*
2659                  * It is a fatal error if unknown mbox command completion.
2660                  */
2661                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2662                     MBX_SHUTDOWN) {
2663                         /* Unknown mailbox command compl */
2664                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2665                                         "(%d):0323 Unknown Mailbox command "
2666                                         "x%x (x%x/x%x) Cmpl\n",
2667                                         pmb->vport ? pmb->vport->vpi : 0,
2668                                         pmbox->mbxCommand,
2669                                         lpfc_sli_config_mbox_subsys_get(phba,
2670                                                                         pmb),
2671                                         lpfc_sli_config_mbox_opcode_get(phba,
2672                                                                         pmb));
2673                         phba->link_state = LPFC_HBA_ERROR;
2674                         phba->work_hs = HS_FFER3;
2675                         lpfc_handle_eratt(phba);
2676                         continue;
2677                 }
2678
2679                 if (pmbox->mbxStatus) {
2680                         phba->sli.slistat.mbox_stat_err++;
2681                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2682                                 /* Mbox cmd cmpl error - RETRYing */
2683                                 lpfc_printf_log(phba, KERN_INFO,
2684                                         LOG_MBOX | LOG_SLI,
2685                                         "(%d):0305 Mbox cmd cmpl "
2686                                         "error - RETRYing Data: x%x "
2687                                         "(x%x/x%x) x%x x%x x%x\n",
2688                                         pmb->vport ? pmb->vport->vpi : 0,
2689                                         pmbox->mbxCommand,
2690                                         lpfc_sli_config_mbox_subsys_get(phba,
2691                                                                         pmb),
2692                                         lpfc_sli_config_mbox_opcode_get(phba,
2693                                                                         pmb),
2694                                         pmbox->mbxStatus,
2695                                         pmbox->un.varWords[0],
2696                                         pmb->vport->port_state);
2697                                 pmbox->mbxStatus = 0;
2698                                 pmbox->mbxOwner = OWN_HOST;
2699                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2700                                 if (rc != MBX_NOT_FINISHED)
2701                                         continue;
2702                         }
2703                 }
2704
2705                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2706                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2707                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2708                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2709                                 "x%x x%x x%x\n",
2710                                 pmb->vport ? pmb->vport->vpi : 0,
2711                                 pmbox->mbxCommand,
2712                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2713                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2714                                 pmb->mbox_cmpl,
2715                                 *((uint32_t *) pmbox),
2716                                 pmbox->un.varWords[0],
2717                                 pmbox->un.varWords[1],
2718                                 pmbox->un.varWords[2],
2719                                 pmbox->un.varWords[3],
2720                                 pmbox->un.varWords[4],
2721                                 pmbox->un.varWords[5],
2722                                 pmbox->un.varWords[6],
2723                                 pmbox->un.varWords[7],
2724                                 pmbox->un.varWords[8],
2725                                 pmbox->un.varWords[9],
2726                                 pmbox->un.varWords[10]);
2727
2728                 if (pmb->mbox_cmpl)
2729                         pmb->mbox_cmpl(phba,pmb);
2730         } while (1);
2731         return 0;
2732 }
2733
2734 /**
2735  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2736  * @phba: Pointer to HBA context object.
2737  * @pring: Pointer to driver SLI ring object.
2738  * @tag: buffer tag.
2739  *
2740  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2741  * is set in the tag the buffer is posted for a particular exchange,
2742  * the function will return the buffer without replacing the buffer.
2743  * If the buffer is for unsolicited ELS or CT traffic, this function
2744  * returns the buffer and also posts another buffer to the firmware.
2745  **/
2746 static struct lpfc_dmabuf *
2747 lpfc_sli_get_buff(struct lpfc_hba *phba,
2748                   struct lpfc_sli_ring *pring,
2749                   uint32_t tag)
2750 {
2751         struct hbq_dmabuf *hbq_entry;
2752
2753         if (tag & QUE_BUFTAG_BIT)
2754                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2755         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2756         if (!hbq_entry)
2757                 return NULL;
2758         return &hbq_entry->dbuf;
2759 }
2760
2761 /**
2762  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2763  * @phba: Pointer to HBA context object.
2764  * @pring: Pointer to driver SLI ring object.
2765  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2766  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2767  * @fch_type: the type for the first frame of the sequence.
2768  *
2769  * This function is called with no lock held. This function uses the r_ctl and
2770  * type of the received sequence to find the correct callback function to call
2771  * to process the sequence.
2772  **/
2773 static int
2774 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2775                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2776                          uint32_t fch_type)
2777 {
2778         int i;
2779
2780         switch (fch_type) {
2781         case FC_TYPE_NVME:
2782                 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
2783                 return 1;
2784         default:
2785                 break;
2786         }
2787
2788         /* unSolicited Responses */
2789         if (pring->prt[0].profile) {
2790                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2791                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2792                                                                         saveq);
2793                 return 1;
2794         }
2795         /* We must search, based on rctl / type
2796            for the right routine */
2797         for (i = 0; i < pring->num_mask; i++) {
2798                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2799                     (pring->prt[i].type == fch_type)) {
2800                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2801                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2802                                                 (phba, pring, saveq);
2803                         return 1;
2804                 }
2805         }
2806         return 0;
2807 }
2808
2809 /**
2810  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2811  * @phba: Pointer to HBA context object.
2812  * @pring: Pointer to driver SLI ring object.
2813  * @saveq: Pointer to the unsolicited iocb.
2814  *
2815  * This function is called with no lock held by the ring event handler
2816  * when there is an unsolicited iocb posted to the response ring by the
2817  * firmware. This function gets the buffer associated with the iocbs
2818  * and calls the event handler for the ring. This function handles both
2819  * qring buffers and hbq buffers.
2820  * When the function returns 1 the caller can free the iocb object otherwise
2821  * upper layer functions will free the iocb objects.
2822  **/
2823 static int
2824 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2825                             struct lpfc_iocbq *saveq)
2826 {
2827         IOCB_t           * irsp;
2828         WORD5            * w5p;
2829         uint32_t           Rctl, Type;
2830         struct lpfc_iocbq *iocbq;
2831         struct lpfc_dmabuf *dmzbuf;
2832
2833         irsp = &(saveq->iocb);
2834
2835         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2836                 if (pring->lpfc_sli_rcv_async_status)
2837                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2838                 else
2839                         lpfc_printf_log(phba,
2840                                         KERN_WARNING,
2841                                         LOG_SLI,
2842                                         "0316 Ring %d handler: unexpected "
2843                                         "ASYNC_STATUS iocb received evt_code "
2844                                         "0x%x\n",
2845                                         pring->ringno,
2846                                         irsp->un.asyncstat.evt_code);
2847                 return 1;
2848         }
2849
2850         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2851                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2852                 if (irsp->ulpBdeCount > 0) {
2853                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2854                                         irsp->un.ulpWord[3]);
2855                         lpfc_in_buf_free(phba, dmzbuf);
2856                 }
2857
2858                 if (irsp->ulpBdeCount > 1) {
2859                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2860                                         irsp->unsli3.sli3Words[3]);
2861                         lpfc_in_buf_free(phba, dmzbuf);
2862                 }
2863
2864                 if (irsp->ulpBdeCount > 2) {
2865                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2866                                 irsp->unsli3.sli3Words[7]);
2867                         lpfc_in_buf_free(phba, dmzbuf);
2868                 }
2869
2870                 return 1;
2871         }
2872
2873         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2874                 if (irsp->ulpBdeCount != 0) {
2875                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2876                                                 irsp->un.ulpWord[3]);
2877                         if (!saveq->context2)
2878                                 lpfc_printf_log(phba,
2879                                         KERN_ERR,
2880                                         LOG_SLI,
2881                                         "0341 Ring %d Cannot find buffer for "
2882                                         "an unsolicited iocb. tag 0x%x\n",
2883                                         pring->ringno,
2884                                         irsp->un.ulpWord[3]);
2885                 }
2886                 if (irsp->ulpBdeCount == 2) {
2887                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2888                                                 irsp->unsli3.sli3Words[7]);
2889                         if (!saveq->context3)
2890                                 lpfc_printf_log(phba,
2891                                         KERN_ERR,
2892                                         LOG_SLI,
2893                                         "0342 Ring %d Cannot find buffer for an"
2894                                         " unsolicited iocb. tag 0x%x\n",
2895                                         pring->ringno,
2896                                         irsp->unsli3.sli3Words[7]);
2897                 }
2898                 list_for_each_entry(iocbq, &saveq->list, list) {
2899                         irsp = &(iocbq->iocb);
2900                         if (irsp->ulpBdeCount != 0) {
2901                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2902                                                         irsp->un.ulpWord[3]);
2903                                 if (!iocbq->context2)
2904                                         lpfc_printf_log(phba,
2905                                                 KERN_ERR,
2906                                                 LOG_SLI,
2907                                                 "0343 Ring %d Cannot find "
2908                                                 "buffer for an unsolicited iocb"
2909                                                 ". tag 0x%x\n", pring->ringno,
2910                                                 irsp->un.ulpWord[3]);
2911                         }
2912                         if (irsp->ulpBdeCount == 2) {
2913                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2914                                                 irsp->unsli3.sli3Words[7]);
2915                                 if (!iocbq->context3)
2916                                         lpfc_printf_log(phba,
2917                                                 KERN_ERR,
2918                                                 LOG_SLI,
2919                                                 "0344 Ring %d Cannot find "
2920                                                 "buffer for an unsolicited "
2921                                                 "iocb. tag 0x%x\n",
2922                                                 pring->ringno,
2923                                                 irsp->unsli3.sli3Words[7]);
2924                         }
2925                 }
2926         }
2927         if (irsp->ulpBdeCount != 0 &&
2928             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2929              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2930                 int found = 0;
2931
2932                 /* search continue save q for same XRI */
2933                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2934                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2935                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
2936                                 list_add_tail(&saveq->list, &iocbq->list);
2937                                 found = 1;
2938                                 break;
2939                         }
2940                 }
2941                 if (!found)
2942                         list_add_tail(&saveq->clist,
2943                                       &pring->iocb_continue_saveq);
2944                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2945                         list_del_init(&iocbq->clist);
2946                         saveq = iocbq;
2947                         irsp = &(saveq->iocb);
2948                 } else
2949                         return 0;
2950         }
2951         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2952             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2953             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2954                 Rctl = FC_RCTL_ELS_REQ;
2955                 Type = FC_TYPE_ELS;
2956         } else {
2957                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2958                 Rctl = w5p->hcsw.Rctl;
2959                 Type = w5p->hcsw.Type;
2960
2961                 /* Firmware Workaround */
2962                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2963                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2964                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2965                         Rctl = FC_RCTL_ELS_REQ;
2966                         Type = FC_TYPE_ELS;
2967                         w5p->hcsw.Rctl = Rctl;
2968                         w5p->hcsw.Type = Type;
2969                 }
2970         }
2971
2972         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2973                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2974                                 "0313 Ring %d handler: unexpected Rctl x%x "
2975                                 "Type x%x received\n",
2976                                 pring->ringno, Rctl, Type);
2977
2978         return 1;
2979 }
2980
2981 /**
2982  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2983  * @phba: Pointer to HBA context object.
2984  * @pring: Pointer to driver SLI ring object.
2985  * @prspiocb: Pointer to response iocb object.
2986  *
2987  * This function looks up the iocb_lookup table to get the command iocb
2988  * corresponding to the given response iocb using the iotag of the
2989  * response iocb. This function is called with the hbalock held
2990  * for sli3 devices or the ring_lock for sli4 devices.
2991  * This function returns the command iocb object if it finds the command
2992  * iocb else returns NULL.
2993  **/
2994 static struct lpfc_iocbq *
2995 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2996                       struct lpfc_sli_ring *pring,
2997                       struct lpfc_iocbq *prspiocb)
2998 {
2999         struct lpfc_iocbq *cmd_iocb = NULL;
3000         uint16_t iotag;
3001         lockdep_assert_held(&phba->hbalock);
3002
3003         iotag = prspiocb->iocb.ulpIoTag;
3004
3005         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3006                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3007                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3008                         /* remove from txcmpl queue list */
3009                         list_del_init(&cmd_iocb->list);
3010                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3011                         return cmd_iocb;
3012                 }
3013         }
3014
3015         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3016                         "0317 iotag x%x is out of "
3017                         "range: max iotag x%x wd0 x%x\n",
3018                         iotag, phba->sli.last_iotag,
3019                         *(((uint32_t *) &prspiocb->iocb) + 7));
3020         return NULL;
3021 }
3022
3023 /**
3024  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3025  * @phba: Pointer to HBA context object.
3026  * @pring: Pointer to driver SLI ring object.
3027  * @iotag: IOCB tag.
3028  *
3029  * This function looks up the iocb_lookup table to get the command iocb
3030  * corresponding to the given iotag. This function is called with the
3031  * hbalock held.
3032  * This function returns the command iocb object if it finds the command
3033  * iocb else returns NULL.
3034  **/
3035 static struct lpfc_iocbq *
3036 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3037                              struct lpfc_sli_ring *pring, uint16_t iotag)
3038 {
3039         struct lpfc_iocbq *cmd_iocb = NULL;
3040
3041         lockdep_assert_held(&phba->hbalock);
3042         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3043                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
3044                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3045                         /* remove from txcmpl queue list */
3046                         list_del_init(&cmd_iocb->list);
3047                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3048                         return cmd_iocb;
3049                 }
3050         }
3051
3052         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3053                         "0372 iotag x%x lookup error: max iotag (x%x) "
3054                         "iocb_flag x%x\n",
3055                         iotag, phba->sli.last_iotag,
3056                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3057         return NULL;
3058 }
3059
3060 /**
3061  * lpfc_sli_process_sol_iocb - process solicited iocb completion
3062  * @phba: Pointer to HBA context object.
3063  * @pring: Pointer to driver SLI ring object.
3064  * @saveq: Pointer to the response iocb to be processed.
3065  *
3066  * This function is called by the ring event handler for non-fcp
3067  * rings when there is a new response iocb in the response ring.
3068  * The caller is not required to hold any locks. This function
3069  * gets the command iocb associated with the response iocb and
3070  * calls the completion handler for the command iocb. If there
3071  * is no completion handler, the function will free the resources
3072  * associated with command iocb. If the response iocb is for
3073  * an already aborted command iocb, the status of the completion
3074  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3075  * This function always returns 1.
3076  **/
3077 static int
3078 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3079                           struct lpfc_iocbq *saveq)
3080 {
3081         struct lpfc_iocbq *cmdiocbp;
3082         int rc = 1;
3083         unsigned long iflag;
3084
3085         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
3086         if (phba->sli_rev == LPFC_SLI_REV4)
3087                 spin_lock_irqsave(&pring->ring_lock, iflag);
3088         else
3089                 spin_lock_irqsave(&phba->hbalock, iflag);
3090         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3091         if (phba->sli_rev == LPFC_SLI_REV4)
3092                 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3093         else
3094                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3095
3096         if (cmdiocbp) {
3097                 if (cmdiocbp->iocb_cmpl) {
3098                         /*
3099                          * If an ELS command failed send an event to mgmt
3100                          * application.
3101                          */
3102                         if (saveq->iocb.ulpStatus &&
3103                              (pring->ringno == LPFC_ELS_RING) &&
3104                              (cmdiocbp->iocb.ulpCommand ==
3105                                 CMD_ELS_REQUEST64_CR))
3106                                 lpfc_send_els_failure_event(phba,
3107                                         cmdiocbp, saveq);
3108
3109                         /*
3110                          * Post all ELS completions to the worker thread.
3111                          * All other are passed to the completion callback.
3112                          */
3113                         if (pring->ringno == LPFC_ELS_RING) {
3114                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3115                                     (cmdiocbp->iocb_flag &
3116                                                         LPFC_DRIVER_ABORTED)) {
3117                                         spin_lock_irqsave(&phba->hbalock,
3118                                                           iflag);
3119                                         cmdiocbp->iocb_flag &=
3120                                                 ~LPFC_DRIVER_ABORTED;
3121                                         spin_unlock_irqrestore(&phba->hbalock,
3122                                                                iflag);
3123                                         saveq->iocb.ulpStatus =
3124                                                 IOSTAT_LOCAL_REJECT;
3125                                         saveq->iocb.un.ulpWord[4] =
3126                                                 IOERR_SLI_ABORTED;
3127
3128                                         /* Firmware could still be in progress
3129                                          * of DMAing payload, so don't free data
3130                                          * buffer till after a hbeat.
3131                                          */
3132                                         spin_lock_irqsave(&phba->hbalock,
3133                                                           iflag);
3134                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3135                                         spin_unlock_irqrestore(&phba->hbalock,
3136                                                                iflag);
3137                                 }
3138                                 if (phba->sli_rev == LPFC_SLI_REV4) {
3139                                         if (saveq->iocb_flag &
3140                                             LPFC_EXCHANGE_BUSY) {
3141                                                 /* Set cmdiocb flag for the
3142                                                  * exchange busy so sgl (xri)
3143                                                  * will not be released until
3144                                                  * the abort xri is received
3145                                                  * from hba.
3146                                                  */
3147                                                 spin_lock_irqsave(
3148                                                         &phba->hbalock, iflag);
3149                                                 cmdiocbp->iocb_flag |=
3150                                                         LPFC_EXCHANGE_BUSY;
3151                                                 spin_unlock_irqrestore(
3152                                                         &phba->hbalock, iflag);
3153                                         }
3154                                         if (cmdiocbp->iocb_flag &
3155                                             LPFC_DRIVER_ABORTED) {
3156                                                 /*
3157                                                  * Clear LPFC_DRIVER_ABORTED
3158                                                  * bit in case it was driver
3159                                                  * initiated abort.
3160                                                  */
3161                                                 spin_lock_irqsave(
3162                                                         &phba->hbalock, iflag);
3163                                                 cmdiocbp->iocb_flag &=
3164                                                         ~LPFC_DRIVER_ABORTED;
3165                                                 spin_unlock_irqrestore(
3166                                                         &phba->hbalock, iflag);
3167                                                 cmdiocbp->iocb.ulpStatus =
3168                                                         IOSTAT_LOCAL_REJECT;
3169                                                 cmdiocbp->iocb.un.ulpWord[4] =
3170                                                         IOERR_ABORT_REQUESTED;
3171                                                 /*
3172                                                  * For SLI4, irsiocb contains
3173                                                  * NO_XRI in sli_xritag, it
3174                                                  * shall not affect releasing
3175                                                  * sgl (xri) process.
3176                                                  */
3177                                                 saveq->iocb.ulpStatus =
3178                                                         IOSTAT_LOCAL_REJECT;
3179                                                 saveq->iocb.un.ulpWord[4] =
3180                                                         IOERR_SLI_ABORTED;
3181                                                 spin_lock_irqsave(
3182                                                         &phba->hbalock, iflag);
3183                                                 saveq->iocb_flag |=
3184                                                         LPFC_DELAY_MEM_FREE;
3185                                                 spin_unlock_irqrestore(
3186                                                         &phba->hbalock, iflag);
3187                                         }
3188                                 }
3189                         }
3190                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3191                 } else
3192                         lpfc_sli_release_iocbq(phba, cmdiocbp);
3193         } else {
3194                 /*
3195                  * Unknown initiating command based on the response iotag.
3196                  * This could be the case on the ELS ring because of
3197                  * lpfc_els_abort().
3198                  */
3199                 if (pring->ringno != LPFC_ELS_RING) {
3200                         /*
3201                          * Ring <ringno> handler: unexpected completion IoTag
3202                          * <IoTag>
3203                          */
3204                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3205                                          "0322 Ring %d handler: "
3206                                          "unexpected completion IoTag x%x "
3207                                          "Data: x%x x%x x%x x%x\n",
3208                                          pring->ringno,
3209                                          saveq->iocb.ulpIoTag,
3210                                          saveq->iocb.ulpStatus,
3211                                          saveq->iocb.un.ulpWord[4],
3212                                          saveq->iocb.ulpCommand,
3213                                          saveq->iocb.ulpContext);
3214                 }
3215         }
3216
3217         return rc;
3218 }
3219
3220 /**
3221  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3222  * @phba: Pointer to HBA context object.
3223  * @pring: Pointer to driver SLI ring object.
3224  *
3225  * This function is called from the iocb ring event handlers when
3226  * put pointer is ahead of the get pointer for a ring. This function signal
3227  * an error attention condition to the worker thread and the worker
3228  * thread will transition the HBA to offline state.
3229  **/
3230 static void
3231 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3232 {
3233         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3234         /*
3235          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3236          * rsp ring <portRspMax>
3237          */
3238         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3239                         "0312 Ring %d handler: portRspPut %d "
3240                         "is bigger than rsp ring %d\n",
3241                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
3242                         pring->sli.sli3.numRiocb);
3243
3244         phba->link_state = LPFC_HBA_ERROR;
3245
3246         /*
3247          * All error attention handlers are posted to
3248          * worker thread
3249          */
3250         phba->work_ha |= HA_ERATT;
3251         phba->work_hs = HS_FFER3;
3252
3253         lpfc_worker_wake_up(phba);
3254
3255         return;
3256 }
3257
3258 /**
3259  * lpfc_poll_eratt - Error attention polling timer timeout handler
3260  * @ptr: Pointer to address of HBA context object.
3261  *
3262  * This function is invoked by the Error Attention polling timer when the
3263  * timer times out. It will check the SLI Error Attention register for
3264  * possible attention events. If so, it will post an Error Attention event
3265  * and wake up worker thread to process it. Otherwise, it will set up the
3266  * Error Attention polling timer for the next poll.
3267  **/
3268 void lpfc_poll_eratt(struct timer_list *t)
3269 {
3270         struct lpfc_hba *phba;
3271         uint32_t eratt = 0;
3272         uint64_t sli_intr, cnt;
3273
3274         phba = from_timer(phba, t, eratt_poll);
3275
3276         /* Here we will also keep track of interrupts per sec of the hba */
3277         sli_intr = phba->sli.slistat.sli_intr;
3278
3279         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3280                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3281                         sli_intr);
3282         else
3283                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3284
3285         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3286         do_div(cnt, phba->eratt_poll_interval);
3287         phba->sli.slistat.sli_ips = cnt;
3288
3289         phba->sli.slistat.sli_prev_intr = sli_intr;
3290
3291         /* Check chip HA register for error event */
3292         eratt = lpfc_sli_check_eratt(phba);
3293
3294         if (eratt)
3295                 /* Tell the worker thread there is work to do */
3296                 lpfc_worker_wake_up(phba);
3297         else
3298                 /* Restart the timer for next eratt poll */
3299                 mod_timer(&phba->eratt_poll,
3300                           jiffies +
3301                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3302         return;
3303 }
3304
3305
3306 /**
3307  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3308  * @phba: Pointer to HBA context object.
3309  * @pring: Pointer to driver SLI ring object.
3310  * @mask: Host attention register mask for this ring.
3311  *
3312  * This function is called from the interrupt context when there is a ring
3313  * event for the fcp ring. The caller does not hold any lock.
3314  * The function processes each response iocb in the response ring until it
3315  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3316  * LE bit set. The function will call the completion handler of the command iocb
3317  * if the response iocb indicates a completion for a command iocb or it is
3318  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3319  * function if this is an unsolicited iocb.
3320  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3321  * to check it explicitly.
3322  */
3323 int
3324 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3325                                 struct lpfc_sli_ring *pring, uint32_t mask)
3326 {
3327         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3328         IOCB_t *irsp = NULL;
3329         IOCB_t *entry = NULL;
3330         struct lpfc_iocbq *cmdiocbq = NULL;
3331         struct lpfc_iocbq rspiocbq;
3332         uint32_t status;
3333         uint32_t portRspPut, portRspMax;
3334         int rc = 1;
3335         lpfc_iocb_type type;
3336         unsigned long iflag;
3337         uint32_t rsp_cmpl = 0;
3338
3339         spin_lock_irqsave(&phba->hbalock, iflag);
3340         pring->stats.iocb_event++;
3341
3342         /*
3343          * The next available response entry should never exceed the maximum
3344          * entries.  If it does, treat it as an adapter hardware error.
3345          */
3346         portRspMax = pring->sli.sli3.numRiocb;
3347         portRspPut = le32_to_cpu(pgp->rspPutInx);
3348         if (unlikely(portRspPut >= portRspMax)) {
3349                 lpfc_sli_rsp_pointers_error(phba, pring);
3350                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3351                 return 1;
3352         }
3353         if (phba->fcp_ring_in_use) {
3354                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3355                 return 1;
3356         } else
3357                 phba->fcp_ring_in_use = 1;
3358
3359         rmb();
3360         while (pring->sli.sli3.rspidx != portRspPut) {
3361                 /*
3362                  * Fetch an entry off the ring and copy it into a local data
3363                  * structure.  The copy involves a byte-swap since the
3364                  * network byte order and pci byte orders are different.
3365                  */
3366                 entry = lpfc_resp_iocb(phba, pring);
3367                 phba->last_completion_time = jiffies;
3368
3369                 if (++pring->sli.sli3.rspidx >= portRspMax)
3370                         pring->sli.sli3.rspidx = 0;
3371
3372                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3373                                       (uint32_t *) &rspiocbq.iocb,
3374                                       phba->iocb_rsp_size);
3375                 INIT_LIST_HEAD(&(rspiocbq.list));
3376                 irsp = &rspiocbq.iocb;
3377
3378                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3379                 pring->stats.iocb_rsp++;
3380                 rsp_cmpl++;
3381
3382                 if (unlikely(irsp->ulpStatus)) {
3383                         /*
3384                          * If resource errors reported from HBA, reduce
3385                          * queuedepths of the SCSI device.
3386                          */
3387                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3388                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3389                              IOERR_NO_RESOURCES)) {
3390                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3391                                 phba->lpfc_rampdown_queue_depth(phba);
3392                                 spin_lock_irqsave(&phba->hbalock, iflag);
3393                         }
3394
3395                         /* Rsp ring <ringno> error: IOCB */
3396                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3397                                         "0336 Rsp Ring %d error: IOCB Data: "
3398                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3399                                         pring->ringno,
3400                                         irsp->un.ulpWord[0],
3401                                         irsp->un.ulpWord[1],
3402                                         irsp->un.ulpWord[2],
3403                                         irsp->un.ulpWord[3],
3404                                         irsp->un.ulpWord[4],
3405                                         irsp->un.ulpWord[5],
3406                                         *(uint32_t *)&irsp->un1,
3407                                         *((uint32_t *)&irsp->un1 + 1));
3408                 }
3409
3410                 switch (type) {
3411                 case LPFC_ABORT_IOCB:
3412                 case LPFC_SOL_IOCB:
3413                         /*
3414                          * Idle exchange closed via ABTS from port.  No iocb
3415                          * resources need to be recovered.
3416                          */
3417                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3418                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3419                                                 "0333 IOCB cmd 0x%x"
3420                                                 " processed. Skipping"
3421                                                 " completion\n",
3422                                                 irsp->ulpCommand);
3423                                 break;
3424                         }
3425
3426                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3427                                                          &rspiocbq);
3428                         if (unlikely(!cmdiocbq))
3429                                 break;
3430                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3431                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3432                         if (cmdiocbq->iocb_cmpl) {
3433                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3434                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3435                                                       &rspiocbq);
3436                                 spin_lock_irqsave(&phba->hbalock, iflag);
3437                         }
3438                         break;
3439                 case LPFC_UNSOL_IOCB:
3440                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3441                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3442                         spin_lock_irqsave(&phba->hbalock, iflag);
3443                         break;
3444                 default:
3445                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3446                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3447                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3448                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3449                                        MAX_MSG_DATA);
3450                                 dev_warn(&((phba->pcidev)->dev),
3451                                          "lpfc%d: %s\n",
3452                                          phba->brd_no, adaptermsg);
3453                         } else {
3454                                 /* Unknown IOCB command */
3455                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3456                                                 "0334 Unknown IOCB command "
3457                                                 "Data: x%x, x%x x%x x%x x%x\n",
3458                                                 type, irsp->ulpCommand,
3459                                                 irsp->ulpStatus,
3460                                                 irsp->ulpIoTag,
3461                                                 irsp->ulpContext);
3462                         }
3463                         break;
3464                 }
3465
3466                 /*
3467                  * The response IOCB has been processed.  Update the ring
3468                  * pointer in SLIM.  If the port response put pointer has not
3469                  * been updated, sync the pgp->rspPutInx and fetch the new port
3470                  * response put pointer.
3471                  */
3472                 writel(pring->sli.sli3.rspidx,
3473                         &phba->host_gp[pring->ringno].rspGetInx);
3474
3475                 if (pring->sli.sli3.rspidx == portRspPut)
3476                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3477         }
3478
3479         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3480                 pring->stats.iocb_rsp_full++;
3481                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3482                 writel(status, phba->CAregaddr);
3483                 readl(phba->CAregaddr);
3484         }
3485         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3486                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3487                 pring->stats.iocb_cmd_empty++;
3488
3489                 /* Force update of the local copy of cmdGetInx */
3490                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3491                 lpfc_sli_resume_iocb(phba, pring);
3492
3493                 if ((pring->lpfc_sli_cmd_available))
3494                         (pring->lpfc_sli_cmd_available) (phba, pring);
3495
3496         }
3497
3498         phba->fcp_ring_in_use = 0;
3499         spin_unlock_irqrestore(&phba->hbalock, iflag);
3500         return rc;
3501 }
3502
3503 /**
3504  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3505  * @phba: Pointer to HBA context object.
3506  * @pring: Pointer to driver SLI ring object.
3507  * @rspiocbp: Pointer to driver response IOCB object.
3508  *
3509  * This function is called from the worker thread when there is a slow-path
3510  * response IOCB to process. This function chains all the response iocbs until
3511  * seeing the iocb with the LE bit set. The function will call
3512  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3513  * completion of a command iocb. The function will call the
3514  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3515  * The function frees the resources or calls the completion handler if this
3516  * iocb is an abort completion. The function returns NULL when the response
3517  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3518  * this function shall chain the iocb on to the iocb_continueq and return the
3519  * response iocb passed in.
3520  **/
3521 static struct lpfc_iocbq *
3522 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3523                         struct lpfc_iocbq *rspiocbp)
3524 {
3525         struct lpfc_iocbq *saveq;
3526         struct lpfc_iocbq *cmdiocbp;
3527         struct lpfc_iocbq *next_iocb;
3528         IOCB_t *irsp = NULL;
3529         uint32_t free_saveq;
3530         uint8_t iocb_cmd_type;
3531         lpfc_iocb_type type;
3532         unsigned long iflag;
3533         int rc;
3534
3535         spin_lock_irqsave(&phba->hbalock, iflag);
3536         /* First add the response iocb to the countinueq list */
3537         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3538         pring->iocb_continueq_cnt++;
3539
3540         /* Now, determine whether the list is completed for processing */
3541         irsp = &rspiocbp->iocb;
3542         if (irsp->ulpLe) {
3543                 /*
3544                  * By default, the driver expects to free all resources
3545                  * associated with this iocb completion.
3546                  */
3547                 free_saveq = 1;
3548                 saveq = list_get_first(&pring->iocb_continueq,
3549                                        struct lpfc_iocbq, list);
3550                 irsp = &(saveq->iocb);
3551                 list_del_init(&pring->iocb_continueq);
3552                 pring->iocb_continueq_cnt = 0;
3553
3554                 pring->stats.iocb_rsp++;
3555
3556                 /*
3557                  * If resource errors reported from HBA, reduce
3558                  * queuedepths of the SCSI device.
3559                  */
3560                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3561                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3562                      IOERR_NO_RESOURCES)) {
3563                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3564                         phba->lpfc_rampdown_queue_depth(phba);
3565                         spin_lock_irqsave(&phba->hbalock, iflag);
3566                 }
3567
3568                 if (irsp->ulpStatus) {
3569                         /* Rsp ring <ringno> error: IOCB */
3570                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3571                                         "0328 Rsp Ring %d error: "
3572                                         "IOCB Data: "
3573                                         "x%x x%x x%x x%x "
3574                                         "x%x x%x x%x x%x "
3575                                         "x%x x%x x%x x%x "
3576                                         "x%x x%x x%x x%x\n",
3577                                         pring->ringno,
3578                                         irsp->un.ulpWord[0],
3579                                         irsp->un.ulpWord[1],
3580                                         irsp->un.ulpWord[2],
3581                                         irsp->un.ulpWord[3],
3582                                         irsp->un.ulpWord[4],
3583                                         irsp->un.ulpWord[5],
3584                                         *(((uint32_t *) irsp) + 6),
3585                                         *(((uint32_t *) irsp) + 7),
3586                                         *(((uint32_t *) irsp) + 8),
3587                                         *(((uint32_t *) irsp) + 9),
3588                                         *(((uint32_t *) irsp) + 10),
3589                                         *(((uint32_t *) irsp) + 11),
3590                                         *(((uint32_t *) irsp) + 12),
3591                                         *(((uint32_t *) irsp) + 13),
3592                                         *(((uint32_t *) irsp) + 14),
3593                                         *(((uint32_t *) irsp) + 15));
3594                 }
3595
3596                 /*
3597                  * Fetch the IOCB command type and call the correct completion
3598                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3599                  * get freed back to the lpfc_iocb_list by the discovery
3600                  * kernel thread.
3601                  */
3602                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3603                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3604                 switch (type) {
3605                 case LPFC_SOL_IOCB:
3606                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3607                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3608                         spin_lock_irqsave(&phba->hbalock, iflag);
3609                         break;
3610
3611                 case LPFC_UNSOL_IOCB:
3612                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3613                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3614                         spin_lock_irqsave(&phba->hbalock, iflag);
3615                         if (!rc)
3616                                 free_saveq = 0;
3617                         break;
3618
3619                 case LPFC_ABORT_IOCB:
3620                         cmdiocbp = NULL;
3621                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3622                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3623                                                                  saveq);
3624                         if (cmdiocbp) {
3625                                 /* Call the specified completion routine */
3626                                 if (cmdiocbp->iocb_cmpl) {
3627                                         spin_unlock_irqrestore(&phba->hbalock,
3628                                                                iflag);
3629                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3630                                                               saveq);
3631                                         spin_lock_irqsave(&phba->hbalock,
3632                                                           iflag);
3633                                 } else
3634                                         __lpfc_sli_release_iocbq(phba,
3635                                                                  cmdiocbp);
3636                         }
3637                         break;
3638
3639                 case LPFC_UNKNOWN_IOCB:
3640                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3641                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3642                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3643                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3644                                        MAX_MSG_DATA);
3645                                 dev_warn(&((phba->pcidev)->dev),
3646                                          "lpfc%d: %s\n",
3647                                          phba->brd_no, adaptermsg);
3648                         } else {
3649                                 /* Unknown IOCB command */
3650                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3651                                                 "0335 Unknown IOCB "
3652                                                 "command Data: x%x "
3653                                                 "x%x x%x x%x\n",
3654                                                 irsp->ulpCommand,
3655                                                 irsp->ulpStatus,
3656                                                 irsp->ulpIoTag,
3657                                                 irsp->ulpContext);
3658                         }
3659                         break;
3660                 }
3661
3662                 if (free_saveq) {
3663                         list_for_each_entry_safe(rspiocbp, next_iocb,
3664                                                  &saveq->list, list) {
3665                                 list_del_init(&rspiocbp->list);
3666                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3667                         }
3668                         __lpfc_sli_release_iocbq(phba, saveq);
3669                 }
3670                 rspiocbp = NULL;
3671         }
3672         spin_unlock_irqrestore(&phba->hbalock, iflag);
3673         return rspiocbp;
3674 }
3675
3676 /**
3677  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3678  * @phba: Pointer to HBA context object.
3679  * @pring: Pointer to driver SLI ring object.
3680  * @mask: Host attention register mask for this ring.
3681  *
3682  * This routine wraps the actual slow_ring event process routine from the
3683  * API jump table function pointer from the lpfc_hba struct.
3684  **/
3685 void
3686 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3687                                 struct lpfc_sli_ring *pring, uint32_t mask)
3688 {
3689         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3690 }
3691
3692 /**
3693  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3694  * @phba: Pointer to HBA context object.
3695  * @pring: Pointer to driver SLI ring object.
3696  * @mask: Host attention register mask for this ring.
3697  *
3698  * This function is called from the worker thread when there is a ring event
3699  * for non-fcp rings. The caller does not hold any lock. The function will
3700  * remove each response iocb in the response ring and calls the handle
3701  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3702  **/
3703 static void
3704 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3705                                    struct lpfc_sli_ring *pring, uint32_t mask)
3706 {
3707         struct lpfc_pgp *pgp;
3708         IOCB_t *entry;
3709         IOCB_t *irsp = NULL;
3710         struct lpfc_iocbq *rspiocbp = NULL;
3711         uint32_t portRspPut, portRspMax;
3712         unsigned long iflag;
3713         uint32_t status;
3714
3715         pgp = &phba->port_gp[pring->ringno];
3716         spin_lock_irqsave(&phba->hbalock, iflag);
3717         pring->stats.iocb_event++;
3718
3719         /*
3720          * The next available response entry should never exceed the maximum
3721          * entries.  If it does, treat it as an adapter hardware error.
3722          */
3723         portRspMax = pring->sli.sli3.numRiocb;
3724         portRspPut = le32_to_cpu(pgp->rspPutInx);
3725         if (portRspPut >= portRspMax) {
3726                 /*
3727                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3728                  * rsp ring <portRspMax>
3729                  */
3730                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3731                                 "0303 Ring %d handler: portRspPut %d "
3732                                 "is bigger than rsp ring %d\n",
3733                                 pring->ringno, portRspPut, portRspMax);
3734
3735                 phba->link_state = LPFC_HBA_ERROR;
3736                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3737
3738                 phba->work_hs = HS_FFER3;
3739                 lpfc_handle_eratt(phba);
3740
3741                 return;
3742         }
3743
3744         rmb();
3745         while (pring->sli.sli3.rspidx != portRspPut) {
3746                 /*
3747                  * Build a completion list and call the appropriate handler.
3748                  * The process is to get the next available response iocb, get
3749                  * a free iocb from the list, copy the response data into the
3750                  * free iocb, insert to the continuation list, and update the
3751                  * next response index to slim.  This process makes response
3752                  * iocb's in the ring available to DMA as fast as possible but
3753                  * pays a penalty for a copy operation.  Since the iocb is
3754                  * only 32 bytes, this penalty is considered small relative to
3755                  * the PCI reads for register values and a slim write.  When
3756                  * the ulpLe field is set, the entire Command has been
3757                  * received.
3758                  */
3759                 entry = lpfc_resp_iocb(phba, pring);
3760
3761                 phba->last_completion_time = jiffies;
3762                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3763                 if (rspiocbp == NULL) {
3764                         printk(KERN_ERR "%s: out of buffers! Failing "
3765                                "completion.\n", __func__);
3766                         break;
3767                 }
3768
3769                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3770                                       phba->iocb_rsp_size);
3771                 irsp = &rspiocbp->iocb;
3772
3773                 if (++pring->sli.sli3.rspidx >= portRspMax)
3774                         pring->sli.sli3.rspidx = 0;
3775
3776                 if (pring->ringno == LPFC_ELS_RING) {
3777                         lpfc_debugfs_slow_ring_trc(phba,
3778                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3779                                 *(((uint32_t *) irsp) + 4),
3780                                 *(((uint32_t *) irsp) + 6),
3781                                 *(((uint32_t *) irsp) + 7));
3782                 }
3783
3784                 writel(pring->sli.sli3.rspidx,
3785                         &phba->host_gp[pring->ringno].rspGetInx);
3786
3787                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3788                 /* Handle the response IOCB */
3789                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3790                 spin_lock_irqsave(&phba->hbalock, iflag);
3791
3792                 /*
3793                  * If the port response put pointer has not been updated, sync
3794                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3795                  * response put pointer.
3796                  */
3797                 if (pring->sli.sli3.rspidx == portRspPut) {
3798                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3799                 }
3800         } /* while (pring->sli.sli3.rspidx != portRspPut) */
3801
3802         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3803                 /* At least one response entry has been freed */
3804                 pring->stats.iocb_rsp_full++;
3805                 /* SET RxRE_RSP in Chip Att register */
3806                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3807                 writel(status, phba->CAregaddr);
3808                 readl(phba->CAregaddr); /* flush */
3809         }
3810         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3811                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3812                 pring->stats.iocb_cmd_empty++;
3813
3814                 /* Force update of the local copy of cmdGetInx */
3815                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3816                 lpfc_sli_resume_iocb(phba, pring);
3817
3818                 if ((pring->lpfc_sli_cmd_available))
3819                         (pring->lpfc_sli_cmd_available) (phba, pring);
3820
3821         }
3822
3823         spin_unlock_irqrestore(&phba->hbalock, iflag);
3824         return;
3825 }
3826
3827 /**
3828  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3829  * @phba: Pointer to HBA context object.
3830  * @pring: Pointer to driver SLI ring object.
3831  * @mask: Host attention register mask for this ring.
3832  *
3833  * This function is called from the worker thread when there is a pending
3834  * ELS response iocb on the driver internal slow-path response iocb worker
3835  * queue. The caller does not hold any lock. The function will remove each
3836  * response iocb from the response worker queue and calls the handle
3837  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3838  **/
3839 static void
3840 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3841                                    struct lpfc_sli_ring *pring, uint32_t mask)
3842 {
3843         struct lpfc_iocbq *irspiocbq;
3844         struct hbq_dmabuf *dmabuf;
3845         struct lpfc_cq_event *cq_event;
3846         unsigned long iflag;
3847         int count = 0;
3848
3849         spin_lock_irqsave(&phba->hbalock, iflag);
3850         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3851         spin_unlock_irqrestore(&phba->hbalock, iflag);
3852         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3853                 /* Get the response iocb from the head of work queue */
3854                 spin_lock_irqsave(&phba->hbalock, iflag);
3855                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3856                                  cq_event, struct lpfc_cq_event, list);
3857                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3858
3859                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3860                 case CQE_CODE_COMPL_WQE:
3861                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3862                                                  cq_event);
3863                         /* Translate ELS WCQE to response IOCBQ */
3864                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3865                                                                    irspiocbq);
3866                         if (irspiocbq)
3867                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3868                                                            irspiocbq);
3869                         count++;
3870                         break;
3871                 case CQE_CODE_RECEIVE:
3872                 case CQE_CODE_RECEIVE_V1:
3873                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3874                                               cq_event);
3875                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3876                         count++;
3877                         break;
3878                 default:
3879                         break;
3880                 }
3881
3882                 /* Limit the number of events to 64 to avoid soft lockups */
3883                 if (count == 64)
3884                         break;
3885         }
3886 }
3887
3888 /**
3889  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3890  * @phba: Pointer to HBA context object.
3891  * @pring: Pointer to driver SLI ring object.
3892  *
3893  * This function aborts all iocbs in the given ring and frees all the iocb
3894  * objects in txq. This function issues an abort iocb for all the iocb commands
3895  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3896  * the return of this function. The caller is not required to hold any locks.
3897  **/
3898 void
3899 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3900 {
3901         LIST_HEAD(completions);
3902         struct lpfc_iocbq *iocb, *next_iocb;
3903
3904         if (pring->ringno == LPFC_ELS_RING) {
3905                 lpfc_fabric_abort_hba(phba);
3906         }
3907
3908         /* Error everything on txq and txcmplq
3909          * First do the txq.
3910          */
3911         if (phba->sli_rev >= LPFC_SLI_REV4) {
3912                 spin_lock_irq(&pring->ring_lock);
3913                 list_splice_init(&pring->txq, &completions);
3914                 pring->txq_cnt = 0;
3915                 spin_unlock_irq(&pring->ring_lock);
3916
3917                 spin_lock_irq(&phba->hbalock);
3918                 /* Next issue ABTS for everything on the txcmplq */
3919                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3920                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3921                 spin_unlock_irq(&phba->hbalock);
3922         } else {
3923                 spin_lock_irq(&phba->hbalock);
3924                 list_splice_init(&pring->txq, &completions);
3925                 pring->txq_cnt = 0;
3926
3927                 /* Next issue ABTS for everything on the txcmplq */
3928                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3929                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3930                 spin_unlock_irq(&phba->hbalock);
3931         }
3932
3933         /* Cancel all the IOCBs from the completions list */
3934         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3935                               IOERR_SLI_ABORTED);
3936 }
3937
3938 /**
3939  * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3940  * @phba: Pointer to HBA context object.
3941  * @pring: Pointer to driver SLI ring object.
3942  *
3943  * This function aborts all iocbs in the given ring and frees all the iocb
3944  * objects in txq. This function issues an abort iocb for all the iocb commands
3945  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3946  * the return of this function. The caller is not required to hold any locks.
3947  **/
3948 void
3949 lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3950 {
3951         LIST_HEAD(completions);
3952         struct lpfc_iocbq *iocb, *next_iocb;
3953
3954         if (pring->ringno == LPFC_ELS_RING)
3955                 lpfc_fabric_abort_hba(phba);
3956
3957         spin_lock_irq(&phba->hbalock);
3958         /* Next issue ABTS for everything on the txcmplq */
3959         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3960                 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3961         spin_unlock_irq(&phba->hbalock);
3962 }
3963
3964
3965 /**
3966  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3967  * @phba: Pointer to HBA context object.
3968  * @pring: Pointer to driver SLI ring object.
3969  *
3970  * This function aborts all iocbs in FCP rings and frees all the iocb
3971  * objects in txq. This function issues an abort iocb for all the iocb commands
3972  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3973  * the return of this function. The caller is not required to hold any locks.
3974  **/
3975 void
3976 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3977 {
3978         struct lpfc_sli *psli = &phba->sli;
3979         struct lpfc_sli_ring  *pring;
3980         uint32_t i;
3981
3982         /* Look on all the FCP Rings for the iotag */
3983         if (phba->sli_rev >= LPFC_SLI_REV4) {
3984                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
3985                         pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
3986                         lpfc_sli_abort_iocb_ring(phba, pring);
3987                 }
3988         } else {
3989                 pring = &psli->sli3_ring[LPFC_FCP_RING];
3990                 lpfc_sli_abort_iocb_ring(phba, pring);
3991         }
3992 }
3993
3994 /**
3995  * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3996  * @phba: Pointer to HBA context object.
3997  *
3998  * This function aborts all wqes in NVME rings. This function issues an
3999  * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
4000  * the txcmplq is not guaranteed to complete before the return of this
4001  * function. The caller is not required to hold any locks.
4002  **/
4003 void
4004 lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
4005 {
4006         struct lpfc_sli_ring  *pring;
4007         uint32_t i;
4008
4009         if ((phba->sli_rev < LPFC_SLI_REV4) ||
4010             !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
4011                 return;
4012
4013         /* Abort all IO on each NVME ring. */
4014         for (i = 0; i < phba->cfg_hdw_queue; i++) {
4015                 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
4016                 lpfc_sli_abort_wqe_ring(phba, pring);
4017         }
4018 }
4019
4020
4021 /**
4022  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
4023  * @phba: Pointer to HBA context object.
4024  *
4025  * This function flushes all iocbs in the fcp ring and frees all the iocb
4026  * objects in txq and txcmplq. This function will not issue abort iocbs
4027  * for all the iocb commands in txcmplq, they will just be returned with
4028  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4029  * slot has been permanently disabled.
4030  **/
4031 void
4032 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
4033 {
4034         LIST_HEAD(txq);
4035         LIST_HEAD(txcmplq);
4036         struct lpfc_sli *psli = &phba->sli;
4037         struct lpfc_sli_ring  *pring;
4038         uint32_t i;
4039         struct lpfc_iocbq *piocb, *next_iocb;
4040
4041         spin_lock_irq(&phba->hbalock);
4042         /* Indicate the I/O queues are flushed */
4043         phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
4044         spin_unlock_irq(&phba->hbalock);
4045
4046         /* Look on all the FCP Rings for the iotag */
4047         if (phba->sli_rev >= LPFC_SLI_REV4) {
4048                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
4049                         pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
4050
4051                         spin_lock_irq(&pring->ring_lock);
4052                         /* Retrieve everything on txq */
4053                         list_splice_init(&pring->txq, &txq);
4054                         list_for_each_entry_safe(piocb, next_iocb,
4055                                                  &pring->txcmplq, list)
4056                                 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4057                         /* Retrieve everything on the txcmplq */
4058                         list_splice_init(&pring->txcmplq, &txcmplq);
4059                         pring->txq_cnt = 0;
4060                         pring->txcmplq_cnt = 0;
4061                         spin_unlock_irq(&pring->ring_lock);
4062
4063                         /* Flush the txq */
4064                         lpfc_sli_cancel_iocbs(phba, &txq,
4065                                               IOSTAT_LOCAL_REJECT,
4066                                               IOERR_SLI_DOWN);
4067                         /* Flush the txcmpq */
4068                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
4069                                               IOSTAT_LOCAL_REJECT,
4070                                               IOERR_SLI_DOWN);
4071                 }
4072         } else {
4073                 pring = &psli->sli3_ring[LPFC_FCP_RING];
4074
4075                 spin_lock_irq(&phba->hbalock);
4076                 /* Retrieve everything on txq */
4077                 list_splice_init(&pring->txq, &txq);
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->txq_cnt = 0;
4084                 pring->txcmplq_cnt = 0;
4085                 spin_unlock_irq(&phba->hbalock);
4086
4087                 /* Flush the txq */
4088                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4089                                       IOERR_SLI_DOWN);
4090                 /* Flush the txcmpq */
4091                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4092                                       IOERR_SLI_DOWN);
4093         }
4094 }
4095
4096 /**
4097  * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4098  * @phba: Pointer to HBA context object.
4099  *
4100  * This function flushes all wqes in the nvme rings and frees all resources
4101  * in the txcmplq. This function does not issue abort wqes for the IO
4102  * commands in txcmplq, they will just be returned with
4103  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4104  * slot has been permanently disabled.
4105  **/
4106 void
4107 lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4108 {
4109         LIST_HEAD(txcmplq);
4110         struct lpfc_sli_ring  *pring;
4111         uint32_t i;
4112         struct lpfc_iocbq *piocb, *next_iocb;
4113
4114         if ((phba->sli_rev < LPFC_SLI_REV4) ||
4115             !(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
4116                 return;
4117
4118         /* Hint to other driver operations that a flush is in progress. */
4119         spin_lock_irq(&phba->hbalock);
4120         phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4121         spin_unlock_irq(&phba->hbalock);
4122
4123         /* Cycle through all NVME rings and complete each IO with
4124          * a local driver reason code.  This is a flush so no
4125          * abort exchange to FW.
4126          */
4127         for (i = 0; i < phba->cfg_hdw_queue; i++) {
4128                 pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
4129
4130                 spin_lock_irq(&pring->ring_lock);
4131                 list_for_each_entry_safe(piocb, next_iocb,
4132                                          &pring->txcmplq, list)
4133                         piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4134                 /* Retrieve everything on the txcmplq */
4135                 list_splice_init(&pring->txcmplq, &txcmplq);
4136                 pring->txcmplq_cnt = 0;
4137                 spin_unlock_irq(&pring->ring_lock);
4138
4139                 /* Flush the txcmpq &&&PAE */
4140                 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4141                                       IOSTAT_LOCAL_REJECT,
4142                                       IOERR_SLI_DOWN);
4143         }
4144 }
4145
4146 /**
4147  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4148  * @phba: Pointer to HBA context object.
4149  * @mask: Bit mask to be checked.
4150  *
4151  * This function reads the host status register and compares
4152  * with the provided bit mask to check if HBA completed
4153  * the restart. This function will wait in a loop for the
4154  * HBA to complete restart. If the HBA does not restart within
4155  * 15 iterations, the function will reset the HBA again. The
4156  * function returns 1 when HBA fail to restart otherwise returns
4157  * zero.
4158  **/
4159 static int
4160 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4161 {
4162         uint32_t status;
4163         int i = 0;
4164         int retval = 0;
4165
4166         /* Read the HBA Host Status Register */
4167         if (lpfc_readl(phba->HSregaddr, &status))
4168                 return 1;
4169
4170         /*
4171          * Check status register every 100ms for 5 retries, then every
4172          * 500ms for 5, then every 2.5 sec for 5, then reset board and
4173          * every 2.5 sec for 4.
4174          * Break our of the loop if errors occurred during init.
4175          */
4176         while (((status & mask) != mask) &&
4177                !(status & HS_FFERM) &&
4178                i++ < 20) {
4179
4180                 if (i <= 5)
4181                         msleep(10);
4182                 else if (i <= 10)
4183                         msleep(500);
4184                 else
4185                         msleep(2500);
4186
4187                 if (i == 15) {
4188                                 /* Do post */
4189                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4190                         lpfc_sli_brdrestart(phba);
4191                 }
4192                 /* Read the HBA Host Status Register */
4193                 if (lpfc_readl(phba->HSregaddr, &status)) {
4194                         retval = 1;
4195                         break;
4196                 }
4197         }
4198
4199         /* Check to see if any errors occurred during init */
4200         if ((status & HS_FFERM) || (i >= 20)) {
4201                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4202                                 "2751 Adapter failed to restart, "
4203                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4204                                 status,
4205                                 readl(phba->MBslimaddr + 0xa8),
4206                                 readl(phba->MBslimaddr + 0xac));
4207                 phba->link_state = LPFC_HBA_ERROR;
4208                 retval = 1;
4209         }
4210
4211         return retval;
4212 }
4213
4214 /**
4215  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4216  * @phba: Pointer to HBA context object.
4217  * @mask: Bit mask to be checked.
4218  *
4219  * This function checks the host status register to check if HBA is
4220  * ready. This function will wait in a loop for the HBA to be ready
4221  * If the HBA is not ready , the function will will reset the HBA PCI
4222  * function again. The function returns 1 when HBA fail to be ready
4223  * otherwise returns zero.
4224  **/
4225 static int
4226 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4227 {
4228         uint32_t status;
4229         int retval = 0;
4230
4231         /* Read the HBA Host Status Register */
4232         status = lpfc_sli4_post_status_check(phba);
4233
4234         if (status) {
4235                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4236                 lpfc_sli_brdrestart(phba);
4237                 status = lpfc_sli4_post_status_check(phba);
4238         }
4239
4240         /* Check to see if any errors occurred during init */
4241         if (status) {
4242                 phba->link_state = LPFC_HBA_ERROR;
4243                 retval = 1;
4244         } else
4245                 phba->sli4_hba.intr_enable = 0;
4246
4247         return retval;
4248 }
4249
4250 /**
4251  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4252  * @phba: Pointer to HBA context object.
4253  * @mask: Bit mask to be checked.
4254  *
4255  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4256  * from the API jump table function pointer from the lpfc_hba struct.
4257  **/
4258 int
4259 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4260 {
4261         return phba->lpfc_sli_brdready(phba, mask);
4262 }
4263
4264 #define BARRIER_TEST_PATTERN (0xdeadbeef)
4265
4266 /**
4267  * lpfc_reset_barrier - Make HBA ready for HBA reset
4268  * @phba: Pointer to HBA context object.
4269  *
4270  * This function is called before resetting an HBA. This function is called
4271  * with hbalock held and requests HBA to quiesce DMAs before a reset.
4272  **/
4273 void lpfc_reset_barrier(struct lpfc_hba *phba)
4274 {
4275         uint32_t __iomem *resp_buf;
4276         uint32_t __iomem *mbox_buf;
4277         volatile uint32_t mbox;
4278         uint32_t hc_copy, ha_copy, resp_data;
4279         int  i;
4280         uint8_t hdrtype;
4281
4282         lockdep_assert_held(&phba->hbalock);
4283
4284         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4285         if (hdrtype != 0x80 ||
4286             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4287              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4288                 return;
4289
4290         /*
4291          * Tell the other part of the chip to suspend temporarily all
4292          * its DMA activity.
4293          */
4294         resp_buf = phba->MBslimaddr;
4295
4296         /* Disable the error attention */
4297         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4298                 return;
4299         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4300         readl(phba->HCregaddr); /* flush */
4301         phba->link_flag |= LS_IGNORE_ERATT;
4302
4303         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4304                 return;
4305         if (ha_copy & HA_ERATT) {
4306                 /* Clear Chip error bit */
4307                 writel(HA_ERATT, phba->HAregaddr);
4308                 phba->pport->stopped = 1;
4309         }
4310
4311         mbox = 0;
4312         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4313         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4314
4315         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4316         mbox_buf = phba->MBslimaddr;
4317         writel(mbox, mbox_buf);
4318
4319         for (i = 0; i < 50; i++) {
4320                 if (lpfc_readl((resp_buf + 1), &resp_data))
4321                         return;
4322                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4323                         mdelay(1);
4324                 else
4325                         break;
4326         }
4327         resp_data = 0;
4328         if (lpfc_readl((resp_buf + 1), &resp_data))
4329                 return;
4330         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4331                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4332                     phba->pport->stopped)
4333                         goto restore_hc;
4334                 else
4335                         goto clear_errat;
4336         }
4337
4338         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4339         resp_data = 0;
4340         for (i = 0; i < 500; i++) {
4341                 if (lpfc_readl(resp_buf, &resp_data))
4342                         return;
4343                 if (resp_data != mbox)
4344                         mdelay(1);
4345                 else
4346                         break;
4347         }
4348
4349 clear_errat:
4350
4351         while (++i < 500) {
4352                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4353                         return;
4354                 if (!(ha_copy & HA_ERATT))
4355                         mdelay(1);
4356                 else
4357                         break;
4358         }
4359
4360         if (readl(phba->HAregaddr) & HA_ERATT) {
4361                 writel(HA_ERATT, phba->HAregaddr);
4362                 phba->pport->stopped = 1;
4363         }
4364
4365 restore_hc:
4366         phba->link_flag &= ~LS_IGNORE_ERATT;
4367         writel(hc_copy, phba->HCregaddr);
4368         readl(phba->HCregaddr); /* flush */
4369 }
4370
4371 /**
4372  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4373  * @phba: Pointer to HBA context object.
4374  *
4375  * This function issues a kill_board mailbox command and waits for
4376  * the error attention interrupt. This function is called for stopping
4377  * the firmware processing. The caller is not required to hold any
4378  * locks. This function calls lpfc_hba_down_post function to free
4379  * any pending commands after the kill. The function will return 1 when it
4380  * fails to kill the board else will return 0.
4381  **/
4382 int
4383 lpfc_sli_brdkill(struct lpfc_hba *phba)
4384 {
4385         struct lpfc_sli *psli;
4386         LPFC_MBOXQ_t *pmb;
4387         uint32_t status;
4388         uint32_t ha_copy;
4389         int retval;
4390         int i = 0;
4391
4392         psli = &phba->sli;
4393
4394         /* Kill HBA */
4395         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4396                         "0329 Kill HBA Data: x%x x%x\n",
4397                         phba->pport->port_state, psli->sli_flag);
4398
4399         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4400         if (!pmb)
4401                 return 1;
4402
4403         /* Disable the error attention */
4404         spin_lock_irq(&phba->hbalock);
4405         if (lpfc_readl(phba->HCregaddr, &status)) {
4406                 spin_unlock_irq(&phba->hbalock);
4407                 mempool_free(pmb, phba->mbox_mem_pool);
4408                 return 1;
4409         }
4410         status &= ~HC_ERINT_ENA;
4411         writel(status, phba->HCregaddr);
4412         readl(phba->HCregaddr); /* flush */
4413         phba->link_flag |= LS_IGNORE_ERATT;
4414         spin_unlock_irq(&phba->hbalock);
4415
4416         lpfc_kill_board(phba, pmb);
4417         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4418         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4419
4420         if (retval != MBX_SUCCESS) {
4421                 if (retval != MBX_BUSY)
4422                         mempool_free(pmb, phba->mbox_mem_pool);
4423                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4424                                 "2752 KILL_BOARD command failed retval %d\n",
4425                                 retval);
4426                 spin_lock_irq(&phba->hbalock);
4427                 phba->link_flag &= ~LS_IGNORE_ERATT;
4428                 spin_unlock_irq(&phba->hbalock);
4429                 return 1;
4430         }
4431
4432         spin_lock_irq(&phba->hbalock);
4433         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4434         spin_unlock_irq(&phba->hbalock);
4435
4436         mempool_free(pmb, phba->mbox_mem_pool);
4437
4438         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4439          * attention every 100ms for 3 seconds. If we don't get ERATT after
4440          * 3 seconds we still set HBA_ERROR state because the status of the
4441          * board is now undefined.
4442          */
4443         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4444                 return 1;
4445         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4446                 mdelay(100);
4447                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4448                         return 1;
4449         }
4450
4451         del_timer_sync(&psli->mbox_tmo);
4452         if (ha_copy & HA_ERATT) {
4453                 writel(HA_ERATT, phba->HAregaddr);
4454                 phba->pport->stopped = 1;
4455         }
4456         spin_lock_irq(&phba->hbalock);
4457         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4458         psli->mbox_active = NULL;
4459         phba->link_flag &= ~LS_IGNORE_ERATT;
4460         spin_unlock_irq(&phba->hbalock);
4461
4462         lpfc_hba_down_post(phba);
4463         phba->link_state = LPFC_HBA_ERROR;
4464
4465         return ha_copy & HA_ERATT ? 0 : 1;
4466 }
4467
4468 /**
4469  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4470  * @phba: Pointer to HBA context object.
4471  *
4472  * This function resets the HBA by writing HC_INITFF to the control
4473  * register. After the HBA resets, this function resets all the iocb ring
4474  * indices. This function disables PCI layer parity checking during
4475  * the reset.
4476  * This function returns 0 always.
4477  * The caller is not required to hold any locks.
4478  **/
4479 int
4480 lpfc_sli_brdreset(struct lpfc_hba *phba)
4481 {
4482         struct lpfc_sli *psli;
4483         struct lpfc_sli_ring *pring;
4484         uint16_t cfg_value;
4485         int i;
4486
4487         psli = &phba->sli;
4488
4489         /* Reset HBA */
4490         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4491                         "0325 Reset HBA Data: x%x x%x\n",
4492                         (phba->pport) ? phba->pport->port_state : 0,
4493                         psli->sli_flag);
4494
4495         /* perform board reset */
4496         phba->fc_eventTag = 0;
4497         phba->link_events = 0;
4498         if (phba->pport) {
4499                 phba->pport->fc_myDID = 0;
4500                 phba->pport->fc_prevDID = 0;
4501         }
4502
4503         /* Turn off parity checking and serr during the physical reset */
4504         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4505         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4506                               (cfg_value &
4507                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4508
4509         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4510
4511         /* Now toggle INITFF bit in the Host Control Register */
4512         writel(HC_INITFF, phba->HCregaddr);
4513         mdelay(1);
4514         readl(phba->HCregaddr); /* flush */
4515         writel(0, phba->HCregaddr);
4516         readl(phba->HCregaddr); /* flush */
4517
4518         /* Restore PCI cmd register */
4519         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4520
4521         /* Initialize relevant SLI info */
4522         for (i = 0; i < psli->num_rings; i++) {
4523                 pring = &psli->sli3_ring[i];
4524                 pring->flag = 0;
4525                 pring->sli.sli3.rspidx = 0;
4526                 pring->sli.sli3.next_cmdidx  = 0;
4527                 pring->sli.sli3.local_getidx = 0;
4528                 pring->sli.sli3.cmdidx = 0;
4529                 pring->missbufcnt = 0;
4530         }
4531
4532         phba->link_state = LPFC_WARM_START;
4533         return 0;
4534 }
4535
4536 /**
4537  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4538  * @phba: Pointer to HBA context object.
4539  *
4540  * This function resets a SLI4 HBA. This function disables PCI layer parity
4541  * checking during resets the device. The caller is not required to hold
4542  * any locks.
4543  *
4544  * This function returns 0 always.
4545  **/
4546 int
4547 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4548 {
4549         struct lpfc_sli *psli = &phba->sli;
4550         uint16_t cfg_value;
4551         int rc = 0;
4552
4553         /* Reset HBA */
4554         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4555                         "0295 Reset HBA Data: x%x x%x x%x\n",
4556                         phba->pport->port_state, psli->sli_flag,
4557                         phba->hba_flag);
4558
4559         /* perform board reset */
4560         phba->fc_eventTag = 0;
4561         phba->link_events = 0;
4562         phba->pport->fc_myDID = 0;
4563         phba->pport->fc_prevDID = 0;
4564
4565         spin_lock_irq(&phba->hbalock);
4566         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4567         phba->fcf.fcf_flag = 0;
4568         spin_unlock_irq(&phba->hbalock);
4569
4570         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4571         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4572                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4573                 return rc;
4574         }
4575
4576         /* Now physically reset the device */
4577         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4578                         "0389 Performing PCI function reset!\n");
4579
4580         /* Turn off parity checking and serr during the physical reset */
4581         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4582         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4583                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4584
4585         /* Perform FCoE PCI function reset before freeing queue memory */
4586         rc = lpfc_pci_function_reset(phba);
4587
4588         /* Restore PCI cmd register */
4589         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4590
4591         return rc;
4592 }
4593
4594 /**
4595  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4596  * @phba: Pointer to HBA context object.
4597  *
4598  * This function is called in the SLI initialization code path to
4599  * restart the HBA. The caller is not required to hold any lock.
4600  * This function writes MBX_RESTART mailbox command to the SLIM and
4601  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4602  * function to free any pending commands. The function enables
4603  * POST only during the first initialization. The function returns zero.
4604  * The function does not guarantee completion of MBX_RESTART mailbox
4605  * command before the return of this function.
4606  **/
4607 static int
4608 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4609 {
4610         MAILBOX_t *mb;
4611         struct lpfc_sli *psli;
4612         volatile uint32_t word0;
4613         void __iomem *to_slim;
4614         uint32_t hba_aer_enabled;
4615
4616         spin_lock_irq(&phba->hbalock);
4617
4618         /* Take PCIe device Advanced Error Reporting (AER) state */
4619         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4620
4621         psli = &phba->sli;
4622
4623         /* Restart HBA */
4624         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4625                         "0337 Restart HBA Data: x%x x%x\n",
4626                         (phba->pport) ? phba->pport->port_state : 0,
4627                         psli->sli_flag);
4628
4629         word0 = 0;
4630         mb = (MAILBOX_t *) &word0;
4631         mb->mbxCommand = MBX_RESTART;
4632         mb->mbxHc = 1;
4633
4634         lpfc_reset_barrier(phba);
4635
4636         to_slim = phba->MBslimaddr;
4637         writel(*(uint32_t *) mb, to_slim);
4638         readl(to_slim); /* flush */
4639
4640         /* Only skip post after fc_ffinit is completed */
4641         if (phba->pport && phba->pport->port_state)
4642                 word0 = 1;      /* This is really setting up word1 */
4643         else
4644                 word0 = 0;      /* This is really setting up word1 */
4645         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4646         writel(*(uint32_t *) mb, to_slim);
4647         readl(to_slim); /* flush */
4648
4649         lpfc_sli_brdreset(phba);
4650         if (phba->pport)
4651                 phba->pport->stopped = 0;
4652         phba->link_state = LPFC_INIT_START;
4653         phba->hba_flag = 0;
4654         spin_unlock_irq(&phba->hbalock);
4655
4656         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4657         psli->stats_start = ktime_get_seconds();
4658
4659         /* Give the INITFF and Post time to settle. */
4660         mdelay(100);
4661
4662         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4663         if (hba_aer_enabled)
4664                 pci_disable_pcie_error_reporting(phba->pcidev);
4665
4666         lpfc_hba_down_post(phba);
4667
4668         return 0;
4669 }
4670
4671 /**
4672  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4673  * @phba: Pointer to HBA context object.
4674  *
4675  * This function is called in the SLI initialization code path to restart
4676  * a SLI4 HBA. The caller is not required to hold any lock.
4677  * At the end of the function, it calls lpfc_hba_down_post function to
4678  * free any pending commands.
4679  **/
4680 static int
4681 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4682 {
4683         struct lpfc_sli *psli = &phba->sli;
4684         uint32_t hba_aer_enabled;
4685         int rc;
4686
4687         /* Restart HBA */
4688         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4689                         "0296 Restart HBA Data: x%x x%x\n",
4690                         phba->pport->port_state, psli->sli_flag);
4691
4692         /* Take PCIe device Advanced Error Reporting (AER) state */
4693         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4694
4695         rc = lpfc_sli4_brdreset(phba);
4696         if (rc)
4697                 return rc;
4698
4699         spin_lock_irq(&phba->hbalock);
4700         phba->pport->stopped = 0;
4701         phba->link_state = LPFC_INIT_START;
4702         phba->hba_flag = 0;
4703         spin_unlock_irq(&phba->hbalock);
4704
4705         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4706         psli->stats_start = ktime_get_seconds();
4707
4708         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4709         if (hba_aer_enabled)
4710                 pci_disable_pcie_error_reporting(phba->pcidev);
4711
4712         lpfc_hba_down_post(phba);
4713         lpfc_sli4_queue_destroy(phba);
4714
4715         return rc;
4716 }
4717
4718 /**
4719  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4720  * @phba: Pointer to HBA context object.
4721  *
4722  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4723  * API jump table function pointer from the lpfc_hba struct.
4724 **/
4725 int
4726 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4727 {
4728         return phba->lpfc_sli_brdrestart(phba);
4729 }
4730
4731 /**
4732  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4733  * @phba: Pointer to HBA context object.
4734  *
4735  * This function is called after a HBA restart to wait for successful
4736  * restart of the HBA. Successful restart of the HBA is indicated by
4737  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4738  * iteration, the function will restart the HBA again. The function returns
4739  * zero if HBA successfully restarted else returns negative error code.
4740  **/
4741 int
4742 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4743 {
4744         uint32_t status, i = 0;
4745
4746         /* Read the HBA Host Status Register */
4747         if (lpfc_readl(phba->HSregaddr, &status))
4748                 return -EIO;
4749
4750         /* Check status register to see what current state is */
4751         i = 0;
4752         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4753
4754                 /* Check every 10ms for 10 retries, then every 100ms for 90
4755                  * retries, then every 1 sec for 50 retires for a total of
4756                  * ~60 seconds before reset the board again and check every
4757                  * 1 sec for 50 retries. The up to 60 seconds before the
4758                  * board ready is required by the Falcon FIPS zeroization
4759                  * complete, and any reset the board in between shall cause
4760                  * restart of zeroization, further delay the board ready.
4761                  */
4762                 if (i++ >= 200) {
4763                         /* Adapter failed to init, timeout, status reg
4764                            <status> */
4765                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4766                                         "0436 Adapter failed to init, "
4767                                         "timeout, status reg x%x, "
4768                                         "FW Data: A8 x%x AC x%x\n", status,
4769                                         readl(phba->MBslimaddr + 0xa8),
4770                                         readl(phba->MBslimaddr + 0xac));
4771                         phba->link_state = LPFC_HBA_ERROR;
4772                         return -ETIMEDOUT;
4773                 }
4774
4775                 /* Check to see if any errors occurred during init */
4776                 if (status & HS_FFERM) {
4777                         /* ERROR: During chipset initialization */
4778                         /* Adapter failed to init, chipset, status reg
4779                            <status> */
4780                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4781                                         "0437 Adapter failed to init, "
4782                                         "chipset, status reg x%x, "
4783                                         "FW Data: A8 x%x AC x%x\n", status,
4784                                         readl(phba->MBslimaddr + 0xa8),
4785                                         readl(phba->MBslimaddr + 0xac));
4786                         phba->link_state = LPFC_HBA_ERROR;
4787                         return -EIO;
4788                 }
4789
4790                 if (i <= 10)
4791                         msleep(10);
4792                 else if (i <= 100)
4793                         msleep(100);
4794                 else
4795                         msleep(1000);
4796
4797                 if (i == 150) {
4798                         /* Do post */
4799                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4800                         lpfc_sli_brdrestart(phba);
4801                 }
4802                 /* Read the HBA Host Status Register */
4803                 if (lpfc_readl(phba->HSregaddr, &status))
4804                         return -EIO;
4805         }
4806
4807         /* Check to see if any errors occurred during init */
4808         if (status & HS_FFERM) {
4809                 /* ERROR: During chipset initialization */
4810                 /* Adapter failed to init, chipset, status reg <status> */
4811                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4812                                 "0438 Adapter failed to init, chipset, "
4813                                 "status reg x%x, "
4814                                 "FW Data: A8 x%x AC x%x\n", status,
4815                                 readl(phba->MBslimaddr + 0xa8),
4816                                 readl(phba->MBslimaddr + 0xac));
4817                 phba->link_state = LPFC_HBA_ERROR;
4818                 return -EIO;
4819         }
4820
4821         /* Clear all interrupt enable conditions */
4822         writel(0, phba->HCregaddr);
4823         readl(phba->HCregaddr); /* flush */
4824
4825         /* setup host attn register */
4826         writel(0xffffffff, phba->HAregaddr);
4827         readl(phba->HAregaddr); /* flush */
4828         return 0;
4829 }
4830
4831 /**
4832  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4833  *
4834  * This function calculates and returns the number of HBQs required to be
4835  * configured.
4836  **/
4837 int
4838 lpfc_sli_hbq_count(void)
4839 {
4840         return ARRAY_SIZE(lpfc_hbq_defs);
4841 }
4842
4843 /**
4844  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4845  *
4846  * This function adds the number of hbq entries in every HBQ to get
4847  * the total number of hbq entries required for the HBA and returns
4848  * the total count.
4849  **/
4850 static int
4851 lpfc_sli_hbq_entry_count(void)
4852 {
4853         int  hbq_count = lpfc_sli_hbq_count();
4854         int  count = 0;
4855         int  i;
4856
4857         for (i = 0; i < hbq_count; ++i)
4858                 count += lpfc_hbq_defs[i]->entry_count;
4859         return count;
4860 }
4861
4862 /**
4863  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4864  *
4865  * This function calculates amount of memory required for all hbq entries
4866  * to be configured and returns the total memory required.
4867  **/
4868 int
4869 lpfc_sli_hbq_size(void)
4870 {
4871         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4872 }
4873
4874 /**
4875  * lpfc_sli_hbq_setup - configure and initialize HBQs
4876  * @phba: Pointer to HBA context object.
4877  *
4878  * This function is called during the SLI initialization to configure
4879  * all the HBQs and post buffers to the HBQ. The caller is not
4880  * required to hold any locks. This function will return zero if successful
4881  * else it will return negative error code.
4882  **/
4883 static int
4884 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4885 {
4886         int  hbq_count = lpfc_sli_hbq_count();
4887         LPFC_MBOXQ_t *pmb;
4888         MAILBOX_t *pmbox;
4889         uint32_t hbqno;
4890         uint32_t hbq_entry_index;
4891
4892                                 /* Get a Mailbox buffer to setup mailbox
4893                                  * commands for HBA initialization
4894                                  */
4895         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4896
4897         if (!pmb)
4898                 return -ENOMEM;
4899
4900         pmbox = &pmb->u.mb;
4901
4902         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4903         phba->link_state = LPFC_INIT_MBX_CMDS;
4904         phba->hbq_in_use = 1;
4905
4906         hbq_entry_index = 0;
4907         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4908                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4909                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4910                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4911                 phba->hbqs[hbqno].entry_count =
4912                         lpfc_hbq_defs[hbqno]->entry_count;
4913                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4914                         hbq_entry_index, pmb);
4915                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4916
4917                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4918                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4919                            mbxStatus <status>, ring <num> */
4920
4921                         lpfc_printf_log(phba, KERN_ERR,
4922                                         LOG_SLI | LOG_VPORT,
4923                                         "1805 Adapter failed to init. "
4924                                         "Data: x%x x%x x%x\n",
4925                                         pmbox->mbxCommand,
4926                                         pmbox->mbxStatus, hbqno);
4927
4928                         phba->link_state = LPFC_HBA_ERROR;
4929                         mempool_free(pmb, phba->mbox_mem_pool);
4930                         return -ENXIO;
4931                 }
4932         }
4933         phba->hbq_count = hbq_count;
4934
4935         mempool_free(pmb, phba->mbox_mem_pool);
4936
4937         /* Initially populate or replenish the HBQs */
4938         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4939                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4940         return 0;
4941 }
4942
4943 /**
4944  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4945  * @phba: Pointer to HBA context object.
4946  *
4947  * This function is called during the SLI initialization to configure
4948  * all the HBQs and post buffers to the HBQ. The caller is not
4949  * required to hold any locks. This function will return zero if successful
4950  * else it will return negative error code.
4951  **/
4952 static int
4953 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4954 {
4955         phba->hbq_in_use = 1;
4956         phba->hbqs[LPFC_ELS_HBQ].entry_count =
4957                 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4958         phba->hbq_count = 1;
4959         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4960         /* Initially populate or replenish the HBQs */
4961         return 0;
4962 }
4963
4964 /**
4965  * lpfc_sli_config_port - Issue config port mailbox command
4966  * @phba: Pointer to HBA context object.
4967  * @sli_mode: sli mode - 2/3
4968  *
4969  * This function is called by the sli initialization code path
4970  * to issue config_port mailbox command. This function restarts the
4971  * HBA firmware and issues a config_port mailbox command to configure
4972  * the SLI interface in the sli mode specified by sli_mode
4973  * variable. The caller is not required to hold any locks.
4974  * The function returns 0 if successful, else returns negative error
4975  * code.
4976  **/
4977 int
4978 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4979 {
4980         LPFC_MBOXQ_t *pmb;
4981         uint32_t resetcount = 0, rc = 0, done = 0;
4982
4983         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4984         if (!pmb) {
4985                 phba->link_state = LPFC_HBA_ERROR;
4986                 return -ENOMEM;
4987         }
4988
4989         phba->sli_rev = sli_mode;
4990         while (resetcount < 2 && !done) {
4991                 spin_lock_irq(&phba->hbalock);
4992                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4993                 spin_unlock_irq(&phba->hbalock);
4994                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4995                 lpfc_sli_brdrestart(phba);
4996                 rc = lpfc_sli_chipset_init(phba);
4997                 if (rc)
4998                         break;
4999
5000                 spin_lock_irq(&phba->hbalock);
5001                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5002                 spin_unlock_irq(&phba->hbalock);
5003                 resetcount++;
5004
5005                 /* Call pre CONFIG_PORT mailbox command initialization.  A
5006                  * value of 0 means the call was successful.  Any other
5007                  * nonzero value is a failure, but if ERESTART is returned,
5008                  * the driver may reset the HBA and try again.
5009                  */
5010                 rc = lpfc_config_port_prep(phba);
5011                 if (rc == -ERESTART) {
5012                         phba->link_state = LPFC_LINK_UNKNOWN;
5013                         continue;
5014                 } else if (rc)
5015                         break;
5016
5017                 phba->link_state = LPFC_INIT_MBX_CMDS;
5018                 lpfc_config_port(phba, pmb);
5019                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5020                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5021                                         LPFC_SLI3_HBQ_ENABLED |
5022                                         LPFC_SLI3_CRP_ENABLED |
5023                                         LPFC_SLI3_DSS_ENABLED);
5024                 if (rc != MBX_SUCCESS) {
5025                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5026                                 "0442 Adapter failed to init, mbxCmd x%x "
5027                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
5028                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
5029                         spin_lock_irq(&phba->hbalock);
5030                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
5031                         spin_unlock_irq(&phba->hbalock);
5032                         rc = -ENXIO;
5033                 } else {
5034                         /* Allow asynchronous mailbox command to go through */
5035                         spin_lock_irq(&phba->hbalock);
5036                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5037                         spin_unlock_irq(&phba->hbalock);
5038                         done = 1;
5039
5040                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5041                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
5042                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5043                                         "3110 Port did not grant ASABT\n");
5044                 }
5045         }
5046         if (!done) {
5047                 rc = -EINVAL;
5048                 goto do_prep_failed;
5049         }
5050         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5051                 if (!pmb->u.mb.un.varCfgPort.cMA) {
5052                         rc = -ENXIO;
5053                         goto do_prep_failed;
5054                 }
5055                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
5056                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
5057                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5058                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5059                                 phba->max_vpi : phba->max_vports;
5060
5061                 } else
5062                         phba->max_vpi = 0;
5063                 phba->fips_level = 0;
5064                 phba->fips_spec_rev = 0;
5065                 if (pmb->u.mb.un.varCfgPort.gdss) {
5066                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
5067                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5068                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5069                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5070                                         "2850 Security Crypto Active. FIPS x%d "
5071                                         "(Spec Rev: x%d)",
5072                                         phba->fips_level, phba->fips_spec_rev);
5073                 }
5074                 if (pmb->u.mb.un.varCfgPort.sec_err) {
5075                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5076                                         "2856 Config Port Security Crypto "
5077                                         "Error: x%x ",
5078                                         pmb->u.mb.un.varCfgPort.sec_err);
5079                 }
5080                 if (pmb->u.mb.un.varCfgPort.gerbm)
5081                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
5082                 if (pmb->u.mb.un.varCfgPort.gcrp)
5083                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
5084
5085                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5086                 phba->port_gp = phba->mbox->us.s3_pgp.port;
5087
5088                 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5089                         if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5090                                 phba->cfg_enable_bg = 0;
5091                                 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5092                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5093                                                 "0443 Adapter did not grant "
5094                                                 "BlockGuard\n");
5095                         }
5096                 }
5097         } else {
5098                 phba->hbq_get = NULL;
5099                 phba->port_gp = phba->mbox->us.s2.port;
5100                 phba->max_vpi = 0;
5101         }
5102 do_prep_failed:
5103         mempool_free(pmb, phba->mbox_mem_pool);
5104         return rc;
5105 }
5106
5107
5108 /**
5109  * lpfc_sli_hba_setup - SLI initialization function
5110  * @phba: Pointer to HBA context object.
5111  *
5112  * This function is the main SLI initialization function. This function
5113  * is called by the HBA initialization code, HBA reset code and HBA
5114  * error attention handler code. Caller is not required to hold any
5115  * locks. This function issues config_port mailbox command to configure
5116  * the SLI, setup iocb rings and HBQ rings. In the end the function
5117  * calls the config_port_post function to issue init_link mailbox
5118  * command and to start the discovery. The function will return zero
5119  * if successful, else it will return negative error code.
5120  **/
5121 int
5122 lpfc_sli_hba_setup(struct lpfc_hba *phba)
5123 {
5124         uint32_t rc;
5125         int  mode = 3, i;
5126         int longs;
5127
5128         switch (phba->cfg_sli_mode) {
5129         case 2:
5130                 if (phba->cfg_enable_npiv) {
5131                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5132                                 "1824 NPIV enabled: Override sli_mode "
5133                                 "parameter (%d) to auto (0).\n",
5134                                 phba->cfg_sli_mode);
5135                         break;
5136                 }
5137                 mode = 2;
5138                 break;
5139         case 0:
5140         case 3:
5141                 break;
5142         default:
5143                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5144                                 "1819 Unrecognized sli_mode parameter: %d.\n",
5145                                 phba->cfg_sli_mode);
5146
5147                 break;
5148         }
5149         phba->fcp_embed_io = 0; /* SLI4 FC support only */
5150
5151         rc = lpfc_sli_config_port(phba, mode);
5152
5153         if (rc && phba->cfg_sli_mode == 3)
5154                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5155                                 "1820 Unable to select SLI-3.  "
5156                                 "Not supported by adapter.\n");
5157         if (rc && mode != 2)
5158                 rc = lpfc_sli_config_port(phba, 2);
5159         else if (rc && mode == 2)
5160                 rc = lpfc_sli_config_port(phba, 3);
5161         if (rc)
5162                 goto lpfc_sli_hba_setup_error;
5163
5164         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5165         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5166                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5167                 if (!rc) {
5168                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5169                                         "2709 This device supports "
5170                                         "Advanced Error Reporting (AER)\n");
5171                         spin_lock_irq(&phba->hbalock);
5172                         phba->hba_flag |= HBA_AER_ENABLED;
5173                         spin_unlock_irq(&phba->hbalock);
5174                 } else {
5175                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5176                                         "2708 This device does not support "
5177                                         "Advanced Error Reporting (AER): %d\n",
5178                                         rc);
5179                         phba->cfg_aer_support = 0;
5180                 }
5181         }
5182
5183         if (phba->sli_rev == 3) {
5184                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5185                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5186         } else {
5187                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5188                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5189                 phba->sli3_options = 0;
5190         }
5191
5192         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5193                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5194                         phba->sli_rev, phba->max_vpi);
5195         rc = lpfc_sli_ring_map(phba);
5196
5197         if (rc)
5198                 goto lpfc_sli_hba_setup_error;
5199
5200         /* Initialize VPIs. */
5201         if (phba->sli_rev == LPFC_SLI_REV3) {
5202                 /*
5203                  * The VPI bitmask and physical ID array are allocated
5204                  * and initialized once only - at driver load.  A port
5205                  * reset doesn't need to reinitialize this memory.
5206                  */
5207                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5208                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5209                         phba->vpi_bmask = kcalloc(longs,
5210                                                   sizeof(unsigned long),
5211                                                   GFP_KERNEL);
5212                         if (!phba->vpi_bmask) {
5213                                 rc = -ENOMEM;
5214                                 goto lpfc_sli_hba_setup_error;
5215                         }
5216
5217                         phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5218                                                 sizeof(uint16_t),
5219                                                 GFP_KERNEL);
5220                         if (!phba->vpi_ids) {
5221                                 kfree(phba->vpi_bmask);
5222                                 rc = -ENOMEM;
5223                                 goto lpfc_sli_hba_setup_error;
5224                         }
5225                         for (i = 0; i < phba->max_vpi; i++)
5226                                 phba->vpi_ids[i] = i;
5227                 }
5228         }
5229
5230         /* Init HBQs */
5231         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5232                 rc = lpfc_sli_hbq_setup(phba);
5233                 if (rc)
5234                         goto lpfc_sli_hba_setup_error;
5235         }
5236         spin_lock_irq(&phba->hbalock);
5237         phba->sli.sli_flag |= LPFC_PROCESS_LA;
5238         spin_unlock_irq(&phba->hbalock);
5239
5240         rc = lpfc_config_port_post(phba);
5241         if (rc)
5242                 goto lpfc_sli_hba_setup_error;
5243
5244         return rc;
5245
5246 lpfc_sli_hba_setup_error:
5247         phba->link_state = LPFC_HBA_ERROR;
5248         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5249                         "0445 Firmware initialization failed\n");
5250         return rc;
5251 }
5252
5253 /**
5254  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5255  * @phba: Pointer to HBA context object.
5256  * @mboxq: mailbox pointer.
5257  * This function issue a dump mailbox command to read config region
5258  * 23 and parse the records in the region and populate driver
5259  * data structure.
5260  **/
5261 static int
5262 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5263 {
5264         LPFC_MBOXQ_t *mboxq;
5265         struct lpfc_dmabuf *mp;
5266         struct lpfc_mqe *mqe;
5267         uint32_t data_length;
5268         int rc;
5269
5270         /* Program the default value of vlan_id and fc_map */
5271         phba->valid_vlan = 0;
5272         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5273         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5274         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5275
5276         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5277         if (!mboxq)
5278                 return -ENOMEM;
5279
5280         mqe = &mboxq->u.mqe;
5281         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5282                 rc = -ENOMEM;
5283                 goto out_free_mboxq;
5284         }
5285
5286         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
5287         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5288
5289         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5290                         "(%d):2571 Mailbox cmd x%x Status x%x "
5291                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5292                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5293                         "CQ: x%x x%x x%x x%x\n",
5294                         mboxq->vport ? mboxq->vport->vpi : 0,
5295                         bf_get(lpfc_mqe_command, mqe),
5296                         bf_get(lpfc_mqe_status, mqe),
5297                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5298                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5299                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5300                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5301                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5302                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5303                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5304                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5305                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5306                         mboxq->mcqe.word0,
5307                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5308                         mboxq->mcqe.trailer);
5309
5310         if (rc) {
5311                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5312                 kfree(mp);
5313                 rc = -EIO;
5314                 goto out_free_mboxq;
5315         }
5316         data_length = mqe->un.mb_words[5];
5317         if (data_length > DMP_RGN23_SIZE) {
5318                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5319                 kfree(mp);
5320                 rc = -EIO;
5321                 goto out_free_mboxq;
5322         }
5323
5324         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5325         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5326         kfree(mp);
5327         rc = 0;
5328
5329 out_free_mboxq:
5330         mempool_free(mboxq, phba->mbox_mem_pool);
5331         return rc;
5332 }
5333
5334 /**
5335  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5336  * @phba: pointer to lpfc hba data structure.
5337  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5338  * @vpd: pointer to the memory to hold resulting port vpd data.
5339  * @vpd_size: On input, the number of bytes allocated to @vpd.
5340  *            On output, the number of data bytes in @vpd.
5341  *
5342  * This routine executes a READ_REV SLI4 mailbox command.  In
5343  * addition, this routine gets the port vpd data.
5344  *
5345  * Return codes
5346  *      0 - successful
5347  *      -ENOMEM - could not allocated memory.
5348  **/
5349 static int
5350 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5351                     uint8_t *vpd, uint32_t *vpd_size)
5352 {
5353         int rc = 0;
5354         uint32_t dma_size;
5355         struct lpfc_dmabuf *dmabuf;
5356         struct lpfc_mqe *mqe;
5357
5358         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5359         if (!dmabuf)
5360                 return -ENOMEM;
5361
5362         /*
5363          * Get a DMA buffer for the vpd data resulting from the READ_REV
5364          * mailbox command.
5365          */
5366         dma_size = *vpd_size;
5367         dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5368                                            &dmabuf->phys, GFP_KERNEL);
5369         if (!dmabuf->virt) {
5370                 kfree(dmabuf);
5371                 return -ENOMEM;
5372         }
5373
5374         /*
5375          * The SLI4 implementation of READ_REV conflicts at word1,
5376          * bits 31:16 and SLI4 adds vpd functionality not present
5377          * in SLI3.  This code corrects the conflicts.
5378          */
5379         lpfc_read_rev(phba, mboxq);
5380         mqe = &mboxq->u.mqe;
5381         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5382         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5383         mqe->un.read_rev.word1 &= 0x0000FFFF;
5384         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5385         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5386
5387         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5388         if (rc) {
5389                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5390                                   dmabuf->virt, dmabuf->phys);
5391                 kfree(dmabuf);
5392                 return -EIO;
5393         }
5394
5395         /*
5396          * The available vpd length cannot be bigger than the
5397          * DMA buffer passed to the port.  Catch the less than
5398          * case and update the caller's size.
5399          */
5400         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5401                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5402
5403         memcpy(vpd, dmabuf->virt, *vpd_size);
5404
5405         dma_free_coherent(&phba->pcidev->dev, dma_size,
5406                           dmabuf->virt, dmabuf->phys);
5407         kfree(dmabuf);
5408         return 0;
5409 }
5410
5411 /**
5412  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5413  * @phba: pointer to lpfc hba data structure.
5414  *
5415  * This routine retrieves SLI4 device physical port name this PCI function
5416  * is attached to.
5417  *
5418  * Return codes
5419  *      0 - successful
5420  *      otherwise - failed to retrieve physical port name
5421  **/
5422 static int
5423 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5424 {
5425         LPFC_MBOXQ_t *mboxq;
5426         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5427         struct lpfc_controller_attribute *cntl_attr;
5428         struct lpfc_mbx_get_port_name *get_port_name;
5429         void *virtaddr = NULL;
5430         uint32_t alloclen, reqlen;
5431         uint32_t shdr_status, shdr_add_status;
5432         union lpfc_sli4_cfg_shdr *shdr;
5433         char cport_name = 0;
5434         int rc;
5435
5436         /* We assume nothing at this point */
5437         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5438         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5439
5440         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5441         if (!mboxq)
5442                 return -ENOMEM;
5443         /* obtain link type and link number via READ_CONFIG */
5444         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5445         lpfc_sli4_read_config(phba);
5446         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5447                 goto retrieve_ppname;
5448
5449         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5450         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5451         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5452                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5453                         LPFC_SLI4_MBX_NEMBED);
5454         if (alloclen < reqlen) {
5455                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5456                                 "3084 Allocated DMA memory size (%d) is "
5457                                 "less than the requested DMA memory size "
5458                                 "(%d)\n", alloclen, reqlen);
5459                 rc = -ENOMEM;
5460                 goto out_free_mboxq;
5461         }
5462         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5463         virtaddr = mboxq->sge_array->addr[0];
5464         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5465         shdr = &mbx_cntl_attr->cfg_shdr;
5466         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5467         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5468         if (shdr_status || shdr_add_status || rc) {
5469                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5470                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5471                                 "rc:x%x, status:x%x, add_status:x%x\n",
5472                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5473                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5474                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5475                                 rc, shdr_status, shdr_add_status);
5476                 rc = -ENXIO;
5477                 goto out_free_mboxq;
5478         }
5479         cntl_attr = &mbx_cntl_attr->cntl_attr;
5480         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5481         phba->sli4_hba.lnk_info.lnk_tp =
5482                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5483         phba->sli4_hba.lnk_info.lnk_no =
5484                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5485         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5486                         "3086 lnk_type:%d, lnk_numb:%d\n",
5487                         phba->sli4_hba.lnk_info.lnk_tp,
5488                         phba->sli4_hba.lnk_info.lnk_no);
5489
5490 retrieve_ppname:
5491         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5492                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5493                 sizeof(struct lpfc_mbx_get_port_name) -
5494                 sizeof(struct lpfc_sli4_cfg_mhdr),
5495                 LPFC_SLI4_MBX_EMBED);
5496         get_port_name = &mboxq->u.mqe.un.get_port_name;
5497         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5498         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5499         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5500                 phba->sli4_hba.lnk_info.lnk_tp);
5501         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5502         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5503         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5504         if (shdr_status || shdr_add_status || rc) {
5505                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5506                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5507                                 "rc:x%x, status:x%x, add_status:x%x\n",
5508                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5509                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5510                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5511                                 rc, shdr_status, shdr_add_status);
5512                 rc = -ENXIO;
5513                 goto out_free_mboxq;
5514         }
5515         switch (phba->sli4_hba.lnk_info.lnk_no) {
5516         case LPFC_LINK_NUMBER_0:
5517                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5518                                 &get_port_name->u.response);
5519                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5520                 break;
5521         case LPFC_LINK_NUMBER_1:
5522                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5523                                 &get_port_name->u.response);
5524                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5525                 break;
5526         case LPFC_LINK_NUMBER_2:
5527                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5528                                 &get_port_name->u.response);
5529                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5530                 break;
5531         case LPFC_LINK_NUMBER_3:
5532                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5533                                 &get_port_name->u.response);
5534                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5535                 break;
5536         default:
5537                 break;
5538         }
5539
5540         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5541                 phba->Port[0] = cport_name;
5542                 phba->Port[1] = '\0';
5543                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5544                                 "3091 SLI get port name: %s\n", phba->Port);
5545         }
5546
5547 out_free_mboxq:
5548         if (rc != MBX_TIMEOUT) {
5549                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5550                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5551                 else
5552                         mempool_free(mboxq, phba->mbox_mem_pool);
5553         }
5554         return rc;
5555 }
5556
5557 /**
5558  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5559  * @phba: pointer to lpfc hba data structure.
5560  *
5561  * This routine is called to explicitly arm the SLI4 device's completion and
5562  * event queues
5563  **/
5564 static void
5565 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5566 {
5567         int qidx;
5568         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
5569         struct lpfc_sli4_hdw_queue *qp;
5570
5571         sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5572         sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5573         if (sli4_hba->nvmels_cq)
5574                 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
5575                                                 LPFC_QUEUE_REARM);
5576
5577         qp = sli4_hba->hdwq;
5578         if (sli4_hba->hdwq) {
5579                 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
5580                         sli4_hba->sli4_cq_release(qp[qidx].fcp_cq,
5581                                                 LPFC_QUEUE_REARM);
5582                         sli4_hba->sli4_cq_release(qp[qidx].nvme_cq,
5583                                                 LPFC_QUEUE_REARM);
5584                 }
5585
5586                 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++)
5587                         sli4_hba->sli4_eq_release(qp[qidx].hba_eq,
5588                                                 LPFC_QUEUE_REARM);
5589         }
5590
5591         if (phba->nvmet_support) {
5592                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5593                         sli4_hba->sli4_cq_release(
5594                                 sli4_hba->nvmet_cqset[qidx],
5595                                 LPFC_QUEUE_REARM);
5596                 }
5597         }
5598 }
5599
5600 /**
5601  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5602  * @phba: Pointer to HBA context object.
5603  * @type: The resource extent type.
5604  * @extnt_count: buffer to hold port available extent count.
5605  * @extnt_size: buffer to hold element count per extent.
5606  *
5607  * This function calls the port and retrievs the number of available
5608  * extents and their size for a particular extent type.
5609  *
5610  * Returns: 0 if successful.  Nonzero otherwise.
5611  **/
5612 int
5613 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5614                                uint16_t *extnt_count, uint16_t *extnt_size)
5615 {
5616         int rc = 0;
5617         uint32_t length;
5618         uint32_t mbox_tmo;
5619         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5620         LPFC_MBOXQ_t *mbox;
5621
5622         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5623         if (!mbox)
5624                 return -ENOMEM;
5625
5626         /* Find out how many extents are available for this resource type */
5627         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5628                   sizeof(struct lpfc_sli4_cfg_mhdr));
5629         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5630                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5631                          length, LPFC_SLI4_MBX_EMBED);
5632
5633         /* Send an extents count of 0 - the GET doesn't use it. */
5634         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5635                                         LPFC_SLI4_MBX_EMBED);
5636         if (unlikely(rc)) {
5637                 rc = -EIO;
5638                 goto err_exit;
5639         }
5640
5641         if (!phba->sli4_hba.intr_enable)
5642                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5643         else {
5644                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5645                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5646         }
5647         if (unlikely(rc)) {
5648                 rc = -EIO;
5649                 goto err_exit;
5650         }
5651
5652         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5653         if (bf_get(lpfc_mbox_hdr_status,
5654                    &rsrc_info->header.cfg_shdr.response)) {
5655                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5656                                 "2930 Failed to get resource extents "
5657                                 "Status 0x%x Add'l Status 0x%x\n",
5658                                 bf_get(lpfc_mbox_hdr_status,
5659                                        &rsrc_info->header.cfg_shdr.response),
5660                                 bf_get(lpfc_mbox_hdr_add_status,
5661                                        &rsrc_info->header.cfg_shdr.response));
5662                 rc = -EIO;
5663                 goto err_exit;
5664         }
5665
5666         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5667                               &rsrc_info->u.rsp);
5668         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5669                              &rsrc_info->u.rsp);
5670
5671         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5672                         "3162 Retrieved extents type-%d from port: count:%d, "
5673                         "size:%d\n", type, *extnt_count, *extnt_size);
5674
5675 err_exit:
5676         mempool_free(mbox, phba->mbox_mem_pool);
5677         return rc;
5678 }
5679
5680 /**
5681  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5682  * @phba: Pointer to HBA context object.
5683  * @type: The extent type to check.
5684  *
5685  * This function reads the current available extents from the port and checks
5686  * if the extent count or extent size has changed since the last access.
5687  * Callers use this routine post port reset to understand if there is a
5688  * extent reprovisioning requirement.
5689  *
5690  * Returns:
5691  *   -Error: error indicates problem.
5692  *   1: Extent count or size has changed.
5693  *   0: No changes.
5694  **/
5695 static int
5696 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5697 {
5698         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5699         uint16_t size_diff, rsrc_ext_size;
5700         int rc = 0;
5701         struct lpfc_rsrc_blks *rsrc_entry;
5702         struct list_head *rsrc_blk_list = NULL;
5703
5704         size_diff = 0;
5705         curr_ext_cnt = 0;
5706         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5707                                             &rsrc_ext_cnt,
5708                                             &rsrc_ext_size);
5709         if (unlikely(rc))
5710                 return -EIO;
5711
5712         switch (type) {
5713         case LPFC_RSC_TYPE_FCOE_RPI:
5714                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5715                 break;
5716         case LPFC_RSC_TYPE_FCOE_VPI:
5717                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5718                 break;
5719         case LPFC_RSC_TYPE_FCOE_XRI:
5720                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5721                 break;
5722         case LPFC_RSC_TYPE_FCOE_VFI:
5723                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5724                 break;
5725         default:
5726                 break;
5727         }
5728
5729         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5730                 curr_ext_cnt++;
5731                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5732                         size_diff++;
5733         }
5734
5735         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5736                 rc = 1;
5737
5738         return rc;
5739 }
5740
5741 /**
5742  * lpfc_sli4_cfg_post_extnts -
5743  * @phba: Pointer to HBA context object.
5744  * @extnt_cnt - number of available extents.
5745  * @type - the extent type (rpi, xri, vfi, vpi).
5746  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5747  * @mbox - pointer to the caller's allocated mailbox structure.
5748  *
5749  * This function executes the extents allocation request.  It also
5750  * takes care of the amount of memory needed to allocate or get the
5751  * allocated extents. It is the caller's responsibility to evaluate
5752  * the response.
5753  *
5754  * Returns:
5755  *   -Error:  Error value describes the condition found.
5756  *   0: if successful
5757  **/
5758 static int
5759 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5760                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5761 {
5762         int rc = 0;
5763         uint32_t req_len;
5764         uint32_t emb_len;
5765         uint32_t alloc_len, mbox_tmo;
5766
5767         /* Calculate the total requested length of the dma memory */
5768         req_len = extnt_cnt * sizeof(uint16_t);
5769
5770         /*
5771          * Calculate the size of an embedded mailbox.  The uint32_t
5772          * accounts for extents-specific word.
5773          */
5774         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5775                 sizeof(uint32_t);
5776
5777         /*
5778          * Presume the allocation and response will fit into an embedded
5779          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5780          */
5781         *emb = LPFC_SLI4_MBX_EMBED;
5782         if (req_len > emb_len) {
5783                 req_len = extnt_cnt * sizeof(uint16_t) +
5784                         sizeof(union lpfc_sli4_cfg_shdr) +
5785                         sizeof(uint32_t);
5786                 *emb = LPFC_SLI4_MBX_NEMBED;
5787         }
5788
5789         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5790                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5791                                      req_len, *emb);
5792         if (alloc_len < req_len) {
5793                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5794                         "2982 Allocated DMA memory size (x%x) is "
5795                         "less than the requested DMA memory "
5796                         "size (x%x)\n", alloc_len, req_len);
5797                 return -ENOMEM;
5798         }
5799         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5800         if (unlikely(rc))
5801                 return -EIO;
5802
5803         if (!phba->sli4_hba.intr_enable)
5804                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5805         else {
5806                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5807                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5808         }
5809
5810         if (unlikely(rc))
5811                 rc = -EIO;
5812         return rc;
5813 }
5814
5815 /**
5816  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5817  * @phba: Pointer to HBA context object.
5818  * @type:  The resource extent type to allocate.
5819  *
5820  * This function allocates the number of elements for the specified
5821  * resource type.
5822  **/
5823 static int
5824 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5825 {
5826         bool emb = false;
5827         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5828         uint16_t rsrc_id, rsrc_start, j, k;
5829         uint16_t *ids;
5830         int i, rc;
5831         unsigned long longs;
5832         unsigned long *bmask;
5833         struct lpfc_rsrc_blks *rsrc_blks;
5834         LPFC_MBOXQ_t *mbox;
5835         uint32_t length;
5836         struct lpfc_id_range *id_array = NULL;
5837         void *virtaddr = NULL;
5838         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5839         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5840         struct list_head *ext_blk_list;
5841
5842         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5843                                             &rsrc_cnt,
5844                                             &rsrc_size);
5845         if (unlikely(rc))
5846                 return -EIO;
5847
5848         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5849                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5850                         "3009 No available Resource Extents "
5851                         "for resource type 0x%x: Count: 0x%x, "
5852                         "Size 0x%x\n", type, rsrc_cnt,
5853                         rsrc_size);
5854                 return -ENOMEM;
5855         }
5856
5857         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5858                         "2903 Post resource extents type-0x%x: "
5859                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5860
5861         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5862         if (!mbox)
5863                 return -ENOMEM;
5864
5865         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5866         if (unlikely(rc)) {
5867                 rc = -EIO;
5868                 goto err_exit;
5869         }
5870
5871         /*
5872          * Figure out where the response is located.  Then get local pointers
5873          * to the response data.  The port does not guarantee to respond to
5874          * all extents counts request so update the local variable with the
5875          * allocated count from the port.
5876          */
5877         if (emb == LPFC_SLI4_MBX_EMBED) {
5878                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5879                 id_array = &rsrc_ext->u.rsp.id[0];
5880                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5881         } else {
5882                 virtaddr = mbox->sge_array->addr[0];
5883                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5884                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5885                 id_array = &n_rsrc->id;
5886         }
5887
5888         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5889         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5890
5891         /*
5892          * Based on the resource size and count, correct the base and max
5893          * resource values.
5894          */
5895         length = sizeof(struct lpfc_rsrc_blks);
5896         switch (type) {
5897         case LPFC_RSC_TYPE_FCOE_RPI:
5898                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
5899                                                    sizeof(unsigned long),
5900                                                    GFP_KERNEL);
5901                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5902                         rc = -ENOMEM;
5903                         goto err_exit;
5904                 }
5905                 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
5906                                                  sizeof(uint16_t),
5907                                                  GFP_KERNEL);
5908                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5909                         kfree(phba->sli4_hba.rpi_bmask);
5910                         rc = -ENOMEM;
5911                         goto err_exit;
5912                 }
5913
5914                 /*
5915                  * The next_rpi was initialized with the maximum available
5916                  * count but the port may allocate a smaller number.  Catch
5917                  * that case and update the next_rpi.
5918                  */
5919                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5920
5921                 /* Initialize local ptrs for common extent processing later. */
5922                 bmask = phba->sli4_hba.rpi_bmask;
5923                 ids = phba->sli4_hba.rpi_ids;
5924                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5925                 break;
5926         case LPFC_RSC_TYPE_FCOE_VPI:
5927                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
5928                                           GFP_KERNEL);
5929                 if (unlikely(!phba->vpi_bmask)) {
5930                         rc = -ENOMEM;
5931                         goto err_exit;
5932                 }
5933                 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
5934                                          GFP_KERNEL);
5935                 if (unlikely(!phba->vpi_ids)) {
5936                         kfree(phba->vpi_bmask);
5937                         rc = -ENOMEM;
5938                         goto err_exit;
5939                 }
5940
5941                 /* Initialize local ptrs for common extent processing later. */
5942                 bmask = phba->vpi_bmask;
5943                 ids = phba->vpi_ids;
5944                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5945                 break;
5946         case LPFC_RSC_TYPE_FCOE_XRI:
5947                 phba->sli4_hba.xri_bmask = kcalloc(longs,
5948                                                    sizeof(unsigned long),
5949                                                    GFP_KERNEL);
5950                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5951                         rc = -ENOMEM;
5952                         goto err_exit;
5953                 }
5954                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5955                 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
5956                                                  sizeof(uint16_t),
5957                                                  GFP_KERNEL);
5958                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5959                         kfree(phba->sli4_hba.xri_bmask);
5960                         rc = -ENOMEM;
5961                         goto err_exit;
5962                 }
5963
5964                 /* Initialize local ptrs for common extent processing later. */
5965                 bmask = phba->sli4_hba.xri_bmask;
5966                 ids = phba->sli4_hba.xri_ids;
5967                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5968                 break;
5969         case LPFC_RSC_TYPE_FCOE_VFI:
5970                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
5971                                                    sizeof(unsigned long),
5972                                                    GFP_KERNEL);
5973                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5974                         rc = -ENOMEM;
5975                         goto err_exit;
5976                 }
5977                 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
5978                                                  sizeof(uint16_t),
5979                                                  GFP_KERNEL);
5980                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5981                         kfree(phba->sli4_hba.vfi_bmask);
5982                         rc = -ENOMEM;
5983                         goto err_exit;
5984                 }
5985
5986                 /* Initialize local ptrs for common extent processing later. */
5987                 bmask = phba->sli4_hba.vfi_bmask;
5988                 ids = phba->sli4_hba.vfi_ids;
5989                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5990                 break;
5991         default:
5992                 /* Unsupported Opcode.  Fail call. */
5993                 id_array = NULL;
5994                 bmask = NULL;
5995                 ids = NULL;
5996                 ext_blk_list = NULL;
5997                 goto err_exit;
5998         }
5999
6000         /*
6001          * Complete initializing the extent configuration with the
6002          * allocated ids assigned to this function.  The bitmask serves
6003          * as an index into the array and manages the available ids.  The
6004          * array just stores the ids communicated to the port via the wqes.
6005          */
6006         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6007                 if ((i % 2) == 0)
6008                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6009                                          &id_array[k]);
6010                 else
6011                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6012                                          &id_array[k]);
6013
6014                 rsrc_blks = kzalloc(length, GFP_KERNEL);
6015                 if (unlikely(!rsrc_blks)) {
6016                         rc = -ENOMEM;
6017                         kfree(bmask);
6018                         kfree(ids);
6019                         goto err_exit;
6020                 }
6021                 rsrc_blks->rsrc_start = rsrc_id;
6022                 rsrc_blks->rsrc_size = rsrc_size;
6023                 list_add_tail(&rsrc_blks->list, ext_blk_list);
6024                 rsrc_start = rsrc_id;
6025                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6026                         phba->sli4_hba.io_xri_start = rsrc_start +
6027                                 lpfc_sli4_get_iocb_cnt(phba);
6028                 }
6029
6030                 while (rsrc_id < (rsrc_start + rsrc_size)) {
6031                         ids[j] = rsrc_id;
6032                         rsrc_id++;
6033                         j++;
6034                 }
6035                 /* Entire word processed.  Get next word.*/
6036                 if ((i % 2) == 1)
6037                         k++;
6038         }
6039  err_exit:
6040         lpfc_sli4_mbox_cmd_free(phba, mbox);
6041         return rc;
6042 }
6043
6044
6045
6046 /**
6047  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6048  * @phba: Pointer to HBA context object.
6049  * @type: the extent's type.
6050  *
6051  * This function deallocates all extents of a particular resource type.
6052  * SLI4 does not allow for deallocating a particular extent range.  It
6053  * is the caller's responsibility to release all kernel memory resources.
6054  **/
6055 static int
6056 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6057 {
6058         int rc;
6059         uint32_t length, mbox_tmo = 0;
6060         LPFC_MBOXQ_t *mbox;
6061         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6062         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6063
6064         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6065         if (!mbox)
6066                 return -ENOMEM;
6067
6068         /*
6069          * This function sends an embedded mailbox because it only sends the
6070          * the resource type.  All extents of this type are released by the
6071          * port.
6072          */
6073         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6074                   sizeof(struct lpfc_sli4_cfg_mhdr));
6075         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6076                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6077                          length, LPFC_SLI4_MBX_EMBED);
6078
6079         /* Send an extents count of 0 - the dealloc doesn't use it. */
6080         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6081                                         LPFC_SLI4_MBX_EMBED);
6082         if (unlikely(rc)) {
6083                 rc = -EIO;
6084                 goto out_free_mbox;
6085         }
6086         if (!phba->sli4_hba.intr_enable)
6087                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6088         else {
6089                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6090                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6091         }
6092         if (unlikely(rc)) {
6093                 rc = -EIO;
6094                 goto out_free_mbox;
6095         }
6096
6097         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6098         if (bf_get(lpfc_mbox_hdr_status,
6099                    &dealloc_rsrc->header.cfg_shdr.response)) {
6100                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6101                                 "2919 Failed to release resource extents "
6102                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
6103                                 "Resource memory not released.\n",
6104                                 type,
6105                                 bf_get(lpfc_mbox_hdr_status,
6106                                     &dealloc_rsrc->header.cfg_shdr.response),
6107                                 bf_get(lpfc_mbox_hdr_add_status,
6108                                     &dealloc_rsrc->header.cfg_shdr.response));
6109                 rc = -EIO;
6110                 goto out_free_mbox;
6111         }
6112
6113         /* Release kernel memory resources for the specific type. */
6114         switch (type) {
6115         case LPFC_RSC_TYPE_FCOE_VPI:
6116                 kfree(phba->vpi_bmask);
6117                 kfree(phba->vpi_ids);
6118                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6119                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6120                                     &phba->lpfc_vpi_blk_list, list) {
6121                         list_del_init(&rsrc_blk->list);
6122                         kfree(rsrc_blk);
6123                 }
6124                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6125                 break;
6126         case LPFC_RSC_TYPE_FCOE_XRI:
6127                 kfree(phba->sli4_hba.xri_bmask);
6128                 kfree(phba->sli4_hba.xri_ids);
6129                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6130                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
6131                         list_del_init(&rsrc_blk->list);
6132                         kfree(rsrc_blk);
6133                 }
6134                 break;
6135         case LPFC_RSC_TYPE_FCOE_VFI:
6136                 kfree(phba->sli4_hba.vfi_bmask);
6137                 kfree(phba->sli4_hba.vfi_ids);
6138                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6139                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6140                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6141                         list_del_init(&rsrc_blk->list);
6142                         kfree(rsrc_blk);
6143                 }
6144                 break;
6145         case LPFC_RSC_TYPE_FCOE_RPI:
6146                 /* RPI bitmask and physical id array are cleaned up earlier. */
6147                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6148                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6149                         list_del_init(&rsrc_blk->list);
6150                         kfree(rsrc_blk);
6151                 }
6152                 break;
6153         default:
6154                 break;
6155         }
6156
6157         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6158
6159  out_free_mbox:
6160         mempool_free(mbox, phba->mbox_mem_pool);
6161         return rc;
6162 }
6163
6164 static void
6165 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6166                   uint32_t feature)
6167 {
6168         uint32_t len;
6169
6170         len = sizeof(struct lpfc_mbx_set_feature) -
6171                 sizeof(struct lpfc_sli4_cfg_mhdr);
6172         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6173                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
6174                          LPFC_SLI4_MBX_EMBED);
6175
6176         switch (feature) {
6177         case LPFC_SET_UE_RECOVERY:
6178                 bf_set(lpfc_mbx_set_feature_UER,
6179                        &mbox->u.mqe.un.set_feature, 1);
6180                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6181                 mbox->u.mqe.un.set_feature.param_len = 8;
6182                 break;
6183         case LPFC_SET_MDS_DIAGS:
6184                 bf_set(lpfc_mbx_set_feature_mds,
6185                        &mbox->u.mqe.un.set_feature, 1);
6186                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6187                        &mbox->u.mqe.un.set_feature, 1);
6188                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6189                 mbox->u.mqe.un.set_feature.param_len = 8;
6190                 break;
6191         }
6192
6193         return;
6194 }
6195
6196 /**
6197  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6198  * @phba: Pointer to HBA context object.
6199  *
6200  * Disable FW logging into host memory on the adapter. To
6201  * be done before reading logs from the host memory.
6202  **/
6203 void
6204 lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6205 {
6206         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6207
6208         ras_fwlog->ras_active = false;
6209
6210         /* Disable FW logging to host memory */
6211         writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6212                phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
6213 }
6214
6215 /**
6216  * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6217  * @phba: Pointer to HBA context object.
6218  *
6219  * This function is called to free memory allocated for RAS FW logging
6220  * support in the driver.
6221  **/
6222 void
6223 lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6224 {
6225         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6226         struct lpfc_dmabuf *dmabuf, *next;
6227
6228         if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6229                 list_for_each_entry_safe(dmabuf, next,
6230                                     &ras_fwlog->fwlog_buff_list,
6231                                     list) {
6232                         list_del(&dmabuf->list);
6233                         dma_free_coherent(&phba->pcidev->dev,
6234                                           LPFC_RAS_MAX_ENTRY_SIZE,
6235                                           dmabuf->virt, dmabuf->phys);
6236                         kfree(dmabuf);
6237                 }
6238         }
6239
6240         if (ras_fwlog->lwpd.virt) {
6241                 dma_free_coherent(&phba->pcidev->dev,
6242                                   sizeof(uint32_t) * 2,
6243                                   ras_fwlog->lwpd.virt,
6244                                   ras_fwlog->lwpd.phys);
6245                 ras_fwlog->lwpd.virt = NULL;
6246         }
6247
6248         ras_fwlog->ras_active = false;
6249 }
6250
6251 /**
6252  * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6253  * @phba: Pointer to HBA context object.
6254  * @fwlog_buff_count: Count of buffers to be created.
6255  *
6256  * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6257  * to update FW log is posted to the adapter.
6258  * Buffer count is calculated based on module param ras_fwlog_buffsize
6259  * Size of each buffer posted to FW is 64K.
6260  **/
6261
6262 static int
6263 lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6264                         uint32_t fwlog_buff_count)
6265 {
6266         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6267         struct lpfc_dmabuf *dmabuf;
6268         int rc = 0, i = 0;
6269
6270         /* Initialize List */
6271         INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6272
6273         /* Allocate memory for the LWPD */
6274         ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6275                                             sizeof(uint32_t) * 2,
6276                                             &ras_fwlog->lwpd.phys,
6277                                             GFP_KERNEL);
6278         if (!ras_fwlog->lwpd.virt) {
6279                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6280                                 "6185 LWPD Memory Alloc Failed\n");
6281
6282                 return -ENOMEM;
6283         }
6284
6285         ras_fwlog->fw_buffcount = fwlog_buff_count;
6286         for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6287                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6288                                  GFP_KERNEL);
6289                 if (!dmabuf) {
6290                         rc = -ENOMEM;
6291                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6292                                         "6186 Memory Alloc failed FW logging");
6293                         goto free_mem;
6294                 }
6295
6296                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
6297                                                   LPFC_RAS_MAX_ENTRY_SIZE,
6298                                                   &dmabuf->phys,
6299                                                   GFP_KERNEL);
6300                 if (!dmabuf->virt) {
6301                         kfree(dmabuf);
6302                         rc = -ENOMEM;
6303                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6304                                         "6187 DMA Alloc Failed FW logging");
6305                         goto free_mem;
6306                 }
6307                 dmabuf->buffer_tag = i;
6308                 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6309         }
6310
6311 free_mem:
6312         if (rc)
6313                 lpfc_sli4_ras_dma_free(phba);
6314
6315         return rc;
6316 }
6317
6318 /**
6319  * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6320  * @phba: pointer to lpfc hba data structure.
6321  * @pmboxq: pointer to the driver internal queue element for mailbox command.
6322  *
6323  * Completion handler for driver's RAS MBX command to the device.
6324  **/
6325 static void
6326 lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6327 {
6328         MAILBOX_t *mb;
6329         union lpfc_sli4_cfg_shdr *shdr;
6330         uint32_t shdr_status, shdr_add_status;
6331         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6332
6333         mb = &pmb->u.mb;
6334
6335         shdr = (union lpfc_sli4_cfg_shdr *)
6336                 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6337         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6338         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6339
6340         if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
6341                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
6342                                 "6188 FW LOG mailbox "
6343                                 "completed with status x%x add_status x%x,"
6344                                 " mbx status x%x\n",
6345                                 shdr_status, shdr_add_status, mb->mbxStatus);
6346
6347                 ras_fwlog->ras_hwsupport = false;
6348                 goto disable_ras;
6349         }
6350
6351         ras_fwlog->ras_active = true;
6352         mempool_free(pmb, phba->mbox_mem_pool);
6353
6354         return;
6355
6356 disable_ras:
6357         /* Free RAS DMA memory */
6358         lpfc_sli4_ras_dma_free(phba);
6359         mempool_free(pmb, phba->mbox_mem_pool);
6360 }
6361
6362 /**
6363  * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6364  * @phba: pointer to lpfc hba data structure.
6365  * @fwlog_level: Logging verbosity level.
6366  * @fwlog_enable: Enable/Disable logging.
6367  *
6368  * Initialize memory and post mailbox command to enable FW logging in host
6369  * memory.
6370  **/
6371 int
6372 lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6373                          uint32_t fwlog_level,
6374                          uint32_t fwlog_enable)
6375 {
6376         struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6377         struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6378         struct lpfc_dmabuf *dmabuf;
6379         LPFC_MBOXQ_t *mbox;
6380         uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6381         int rc = 0;
6382
6383         fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6384                           phba->cfg_ras_fwlog_buffsize);
6385         fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6386
6387         /*
6388          * If re-enabling FW logging support use earlier allocated
6389          * DMA buffers while posting MBX command.
6390          **/
6391         if (!ras_fwlog->lwpd.virt) {
6392                 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6393                 if (rc) {
6394                         lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6395                                         "6189 FW Log Memory Allocation Failed");
6396                         return rc;
6397                 }
6398         }
6399
6400         /* Setup Mailbox command */
6401         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6402         if (!mbox) {
6403                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6404                                 "6190 RAS MBX Alloc Failed");
6405                 rc = -ENOMEM;
6406                 goto mem_free;
6407         }
6408
6409         ras_fwlog->fw_loglevel = fwlog_level;
6410         len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6411                 sizeof(struct lpfc_sli4_cfg_mhdr));
6412
6413         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6414                          LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6415                          len, LPFC_SLI4_MBX_EMBED);
6416
6417         mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6418         bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6419                fwlog_enable);
6420         bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6421                ras_fwlog->fw_loglevel);
6422         bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6423                ras_fwlog->fw_buffcount);
6424         bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6425                LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6426
6427         /* Update DMA buffer address */
6428         list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6429                 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6430
6431                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6432                         putPaddrLow(dmabuf->phys);
6433
6434                 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6435                         putPaddrHigh(dmabuf->phys);
6436         }
6437
6438         /* Update LPWD address */
6439         mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6440         mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6441
6442         mbox->vport = phba->pport;
6443         mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6444
6445         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6446
6447         if (rc == MBX_NOT_FINISHED) {
6448                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6449                                 "6191 FW-Log Mailbox failed. "
6450                                 "status %d mbxStatus : x%x", rc,
6451                                 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6452                 mempool_free(mbox, phba->mbox_mem_pool);
6453                 rc = -EIO;
6454                 goto mem_free;
6455         } else
6456                 rc = 0;
6457 mem_free:
6458         if (rc)
6459                 lpfc_sli4_ras_dma_free(phba);
6460
6461         return rc;
6462 }
6463
6464 /**
6465  * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6466  * @phba: Pointer to HBA context object.
6467  *
6468  * Check if RAS is supported on the adapter and initialize it.
6469  **/
6470 void
6471 lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6472 {
6473         /* Check RAS FW Log needs to be enabled or not */
6474         if (lpfc_check_fwlog_support(phba))
6475                 return;
6476
6477         lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6478                                  LPFC_RAS_ENABLE_LOGGING);
6479 }
6480
6481 /**
6482  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6483  * @phba: Pointer to HBA context object.
6484  *
6485  * This function allocates all SLI4 resource identifiers.
6486  **/
6487 int
6488 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6489 {
6490         int i, rc, error = 0;
6491         uint16_t count, base;
6492         unsigned long longs;
6493
6494         if (!phba->sli4_hba.rpi_hdrs_in_use)
6495                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6496         if (phba->sli4_hba.extents_in_use) {
6497                 /*
6498                  * The port supports resource extents. The XRI, VPI, VFI, RPI
6499                  * resource extent count must be read and allocated before
6500                  * provisioning the resource id arrays.
6501                  */
6502                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6503                     LPFC_IDX_RSRC_RDY) {
6504                         /*
6505                          * Extent-based resources are set - the driver could
6506                          * be in a port reset. Figure out if any corrective
6507                          * actions need to be taken.
6508                          */
6509                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6510                                                  LPFC_RSC_TYPE_FCOE_VFI);
6511                         if (rc != 0)
6512                                 error++;
6513                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6514                                                  LPFC_RSC_TYPE_FCOE_VPI);
6515                         if (rc != 0)
6516                                 error++;
6517                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6518                                                  LPFC_RSC_TYPE_FCOE_XRI);
6519                         if (rc != 0)
6520                                 error++;
6521                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6522                                                  LPFC_RSC_TYPE_FCOE_RPI);
6523                         if (rc != 0)
6524                                 error++;
6525
6526                         /*
6527                          * It's possible that the number of resources
6528                          * provided to this port instance changed between
6529                          * resets.  Detect this condition and reallocate
6530                          * resources.  Otherwise, there is no action.
6531                          */
6532                         if (error) {
6533                                 lpfc_printf_log(phba, KERN_INFO,
6534                                                 LOG_MBOX | LOG_INIT,
6535                                                 "2931 Detected extent resource "
6536                                                 "change.  Reallocating all "
6537                                                 "extents.\n");
6538                                 rc = lpfc_sli4_dealloc_extent(phba,
6539                                                  LPFC_RSC_TYPE_FCOE_VFI);
6540                                 rc = lpfc_sli4_dealloc_extent(phba,
6541                                                  LPFC_RSC_TYPE_FCOE_VPI);
6542                                 rc = lpfc_sli4_dealloc_extent(phba,
6543                                                  LPFC_RSC_TYPE_FCOE_XRI);
6544                                 rc = lpfc_sli4_dealloc_extent(phba,
6545                                                  LPFC_RSC_TYPE_FCOE_RPI);
6546                         } else
6547                                 return 0;
6548                 }
6549
6550                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6551                 if (unlikely(rc))
6552                         goto err_exit;
6553
6554                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6555                 if (unlikely(rc))
6556                         goto err_exit;
6557
6558                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6559                 if (unlikely(rc))
6560                         goto err_exit;
6561
6562                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6563                 if (unlikely(rc))
6564                         goto err_exit;
6565                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6566                        LPFC_IDX_RSRC_RDY);
6567                 return rc;
6568         } else {
6569                 /*
6570                  * The port does not support resource extents.  The XRI, VPI,
6571                  * VFI, RPI resource ids were determined from READ_CONFIG.
6572                  * Just allocate the bitmasks and provision the resource id
6573                  * arrays.  If a port reset is active, the resources don't
6574                  * need any action - just exit.
6575                  */
6576                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6577                     LPFC_IDX_RSRC_RDY) {
6578                         lpfc_sli4_dealloc_resource_identifiers(phba);
6579                         lpfc_sli4_remove_rpis(phba);
6580                 }
6581                 /* RPIs. */
6582                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6583                 if (count <= 0) {
6584                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6585                                         "3279 Invalid provisioning of "
6586                                         "rpi:%d\n", count);
6587                         rc = -EINVAL;
6588                         goto err_exit;
6589                 }
6590                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6591                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6592                 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6593                                                    sizeof(unsigned long),
6594                                                    GFP_KERNEL);
6595                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6596                         rc = -ENOMEM;
6597                         goto err_exit;
6598                 }
6599                 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
6600                                                  GFP_KERNEL);
6601                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6602                         rc = -ENOMEM;
6603                         goto free_rpi_bmask;
6604                 }
6605
6606                 for (i = 0; i < count; i++)
6607                         phba->sli4_hba.rpi_ids[i] = base + i;
6608
6609                 /* VPIs. */
6610                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6611                 if (count <= 0) {
6612                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6613                                         "3280 Invalid provisioning of "
6614                                         "vpi:%d\n", count);
6615                         rc = -EINVAL;
6616                         goto free_rpi_ids;
6617                 }
6618                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6619                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6620                 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6621                                           GFP_KERNEL);
6622                 if (unlikely(!phba->vpi_bmask)) {
6623                         rc = -ENOMEM;
6624                         goto free_rpi_ids;
6625                 }
6626                 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
6627                                         GFP_KERNEL);
6628                 if (unlikely(!phba->vpi_ids)) {
6629                         rc = -ENOMEM;
6630                         goto free_vpi_bmask;
6631                 }
6632
6633                 for (i = 0; i < count; i++)
6634                         phba->vpi_ids[i] = base + i;
6635
6636                 /* XRIs. */
6637                 count = phba->sli4_hba.max_cfg_param.max_xri;
6638                 if (count <= 0) {
6639                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6640                                         "3281 Invalid provisioning of "
6641                                         "xri:%d\n", count);
6642                         rc = -EINVAL;
6643                         goto free_vpi_ids;
6644                 }
6645                 base = phba->sli4_hba.max_cfg_param.xri_base;
6646                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6647                 phba->sli4_hba.xri_bmask = kcalloc(longs,
6648                                                    sizeof(unsigned long),
6649                                                    GFP_KERNEL);
6650                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6651                         rc = -ENOMEM;
6652                         goto free_vpi_ids;
6653                 }
6654                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6655                 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
6656                                                  GFP_KERNEL);
6657                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6658                         rc = -ENOMEM;
6659                         goto free_xri_bmask;
6660                 }
6661
6662                 for (i = 0; i < count; i++)
6663                         phba->sli4_hba.xri_ids[i] = base + i;
6664
6665                 /* VFIs. */
6666                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6667                 if (count <= 0) {
6668                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669                                         "3282 Invalid provisioning of "
6670                                         "vfi:%d\n", count);
6671                         rc = -EINVAL;
6672                         goto free_xri_ids;
6673                 }
6674                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6675                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6676                 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6677                                                    sizeof(unsigned long),
6678                                                    GFP_KERNEL);
6679                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6680                         rc = -ENOMEM;
6681                         goto free_xri_ids;
6682                 }
6683                 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
6684                                                  GFP_KERNEL);
6685                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6686                         rc = -ENOMEM;
6687                         goto free_vfi_bmask;
6688                 }
6689
6690                 for (i = 0; i < count; i++)
6691                         phba->sli4_hba.vfi_ids[i] = base + i;
6692
6693                 /*
6694                  * Mark all resources ready.  An HBA reset doesn't need
6695                  * to reset the initialization.
6696                  */
6697                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6698                        LPFC_IDX_RSRC_RDY);
6699                 return 0;
6700         }
6701
6702  free_vfi_bmask:
6703         kfree(phba->sli4_hba.vfi_bmask);
6704         phba->sli4_hba.vfi_bmask = NULL;
6705  free_xri_ids:
6706         kfree(phba->sli4_hba.xri_ids);
6707         phba->sli4_hba.xri_ids = NULL;
6708  free_xri_bmask:
6709         kfree(phba->sli4_hba.xri_bmask);
6710         phba->sli4_hba.xri_bmask = NULL;
6711  free_vpi_ids:
6712         kfree(phba->vpi_ids);
6713         phba->vpi_ids = NULL;
6714  free_vpi_bmask:
6715         kfree(phba->vpi_bmask);
6716         phba->vpi_bmask = NULL;
6717  free_rpi_ids:
6718         kfree(phba->sli4_hba.rpi_ids);
6719         phba->sli4_hba.rpi_ids = NULL;
6720  free_rpi_bmask:
6721         kfree(phba->sli4_hba.rpi_bmask);
6722         phba->sli4_hba.rpi_bmask = NULL;
6723  err_exit:
6724         return rc;
6725 }
6726
6727 /**
6728  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6729  * @phba: Pointer to HBA context object.
6730  *
6731  * This function allocates the number of elements for the specified
6732  * resource type.
6733  **/
6734 int
6735 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6736 {
6737         if (phba->sli4_hba.extents_in_use) {
6738                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6739                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6740                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6741                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6742         } else {
6743                 kfree(phba->vpi_bmask);
6744                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6745                 kfree(phba->vpi_ids);
6746                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6747                 kfree(phba->sli4_hba.xri_bmask);
6748                 kfree(phba->sli4_hba.xri_ids);
6749                 kfree(phba->sli4_hba.vfi_bmask);
6750                 kfree(phba->sli4_hba.vfi_ids);
6751                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6752                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6753         }
6754
6755         return 0;
6756 }
6757
6758 /**
6759  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6760  * @phba: Pointer to HBA context object.
6761  * @type: The resource extent type.
6762  * @extnt_count: buffer to hold port extent count response
6763  * @extnt_size: buffer to hold port extent size response.
6764  *
6765  * This function calls the port to read the host allocated extents
6766  * for a particular type.
6767  **/
6768 int
6769 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6770                                uint16_t *extnt_cnt, uint16_t *extnt_size)
6771 {
6772         bool emb;
6773         int rc = 0;
6774         uint16_t curr_blks = 0;
6775         uint32_t req_len, emb_len;
6776         uint32_t alloc_len, mbox_tmo;
6777         struct list_head *blk_list_head;
6778         struct lpfc_rsrc_blks *rsrc_blk;
6779         LPFC_MBOXQ_t *mbox;
6780         void *virtaddr = NULL;
6781         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6782         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6783         union  lpfc_sli4_cfg_shdr *shdr;
6784
6785         switch (type) {
6786         case LPFC_RSC_TYPE_FCOE_VPI:
6787                 blk_list_head = &phba->lpfc_vpi_blk_list;
6788                 break;
6789         case LPFC_RSC_TYPE_FCOE_XRI:
6790                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6791                 break;
6792         case LPFC_RSC_TYPE_FCOE_VFI:
6793                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6794                 break;
6795         case LPFC_RSC_TYPE_FCOE_RPI:
6796                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6797                 break;
6798         default:
6799                 return -EIO;
6800         }
6801
6802         /* Count the number of extents currently allocatd for this type. */
6803         list_for_each_entry(rsrc_blk, blk_list_head, list) {
6804                 if (curr_blks == 0) {
6805                         /*
6806                          * The GET_ALLOCATED mailbox does not return the size,
6807                          * just the count.  The size should be just the size
6808                          * stored in the current allocated block and all sizes
6809                          * for an extent type are the same so set the return
6810                          * value now.
6811                          */
6812                         *extnt_size = rsrc_blk->rsrc_size;
6813                 }
6814                 curr_blks++;
6815         }
6816
6817         /*
6818          * Calculate the size of an embedded mailbox.  The uint32_t
6819          * accounts for extents-specific word.
6820          */
6821         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6822                 sizeof(uint32_t);
6823
6824         /*
6825          * Presume the allocation and response will fit into an embedded
6826          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6827          */
6828         emb = LPFC_SLI4_MBX_EMBED;
6829         req_len = emb_len;
6830         if (req_len > emb_len) {
6831                 req_len = curr_blks * sizeof(uint16_t) +
6832                         sizeof(union lpfc_sli4_cfg_shdr) +
6833                         sizeof(uint32_t);
6834                 emb = LPFC_SLI4_MBX_NEMBED;
6835         }
6836
6837         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6838         if (!mbox)
6839                 return -ENOMEM;
6840         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6841
6842         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6843                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6844                                      req_len, emb);
6845         if (alloc_len < req_len) {
6846                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6847                         "2983 Allocated DMA memory size (x%x) is "
6848                         "less than the requested DMA memory "
6849                         "size (x%x)\n", alloc_len, req_len);
6850                 rc = -ENOMEM;
6851                 goto err_exit;
6852         }
6853         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6854         if (unlikely(rc)) {
6855                 rc = -EIO;
6856                 goto err_exit;
6857         }
6858
6859         if (!phba->sli4_hba.intr_enable)
6860                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6861         else {
6862                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6863                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6864         }
6865
6866         if (unlikely(rc)) {
6867                 rc = -EIO;
6868                 goto err_exit;
6869         }
6870
6871         /*
6872          * Figure out where the response is located.  Then get local pointers
6873          * to the response data.  The port does not guarantee to respond to
6874          * all extents counts request so update the local variable with the
6875          * allocated count from the port.
6876          */
6877         if (emb == LPFC_SLI4_MBX_EMBED) {
6878                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6879                 shdr = &rsrc_ext->header.cfg_shdr;
6880                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6881         } else {
6882                 virtaddr = mbox->sge_array->addr[0];
6883                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6884                 shdr = &n_rsrc->cfg_shdr;
6885                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6886         }
6887
6888         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6889                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6890                         "2984 Failed to read allocated resources "
6891                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6892                         type,
6893                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
6894                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6895                 rc = -EIO;
6896                 goto err_exit;
6897         }
6898  err_exit:
6899         lpfc_sli4_mbox_cmd_free(phba, mbox);
6900         return rc;
6901 }
6902
6903 /**
6904  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
6905  * @phba: pointer to lpfc hba data structure.
6906  * @pring: Pointer to driver SLI ring object.
6907  * @sgl_list: linked link of sgl buffers to post
6908  * @cnt: number of linked list buffers
6909  *
6910  * This routine walks the list of buffers that have been allocated and
6911  * repost them to the port by using SGL block post. This is needed after a
6912  * pci_function_reset/warm_start or start. It attempts to construct blocks
6913  * of buffer sgls which contains contiguous xris and uses the non-embedded
6914  * SGL block post mailbox commands to post them to the port. For single
6915  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6916  * mailbox command for posting.
6917  *
6918  * Returns: 0 = success, non-zero failure.
6919  **/
6920 static int
6921 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6922                           struct list_head *sgl_list, int cnt)
6923 {
6924         struct lpfc_sglq *sglq_entry = NULL;
6925         struct lpfc_sglq *sglq_entry_next = NULL;
6926         struct lpfc_sglq *sglq_entry_first = NULL;
6927         int status, total_cnt;
6928         int post_cnt = 0, num_posted = 0, block_cnt = 0;
6929         int last_xritag = NO_XRI;
6930         LIST_HEAD(prep_sgl_list);
6931         LIST_HEAD(blck_sgl_list);
6932         LIST_HEAD(allc_sgl_list);
6933         LIST_HEAD(post_sgl_list);
6934         LIST_HEAD(free_sgl_list);
6935
6936         spin_lock_irq(&phba->hbalock);
6937         spin_lock(&phba->sli4_hba.sgl_list_lock);
6938         list_splice_init(sgl_list, &allc_sgl_list);
6939         spin_unlock(&phba->sli4_hba.sgl_list_lock);
6940         spin_unlock_irq(&phba->hbalock);
6941
6942         total_cnt = cnt;
6943         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6944                                  &allc_sgl_list, list) {
6945                 list_del_init(&sglq_entry->list);
6946                 block_cnt++;
6947                 if ((last_xritag != NO_XRI) &&
6948                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
6949                         /* a hole in xri block, form a sgl posting block */
6950                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
6951                         post_cnt = block_cnt - 1;
6952                         /* prepare list for next posting block */
6953                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6954                         block_cnt = 1;
6955                 } else {
6956                         /* prepare list for next posting block */
6957                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6958                         /* enough sgls for non-embed sgl mbox command */
6959                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6960                                 list_splice_init(&prep_sgl_list,
6961                                                  &blck_sgl_list);
6962                                 post_cnt = block_cnt;
6963                                 block_cnt = 0;
6964                         }
6965                 }
6966                 num_posted++;
6967
6968                 /* keep track of last sgl's xritag */
6969                 last_xritag = sglq_entry->sli4_xritag;
6970
6971                 /* end of repost sgl list condition for buffers */
6972                 if (num_posted == total_cnt) {
6973                         if (post_cnt == 0) {
6974                                 list_splice_init(&prep_sgl_list,
6975                                                  &blck_sgl_list);
6976                                 post_cnt = block_cnt;
6977                         } else if (block_cnt == 1) {
6978                                 status = lpfc_sli4_post_sgl(phba,
6979                                                 sglq_entry->phys, 0,
6980                                                 sglq_entry->sli4_xritag);
6981                                 if (!status) {
6982                                         /* successful, put sgl to posted list */
6983                                         list_add_tail(&sglq_entry->list,
6984                                                       &post_sgl_list);
6985                                 } else {
6986                                         /* Failure, put sgl to free list */
6987                                         lpfc_printf_log(phba, KERN_WARNING,
6988                                                 LOG_SLI,
6989                                                 "3159 Failed to post "
6990                                                 "sgl, xritag:x%x\n",
6991                                                 sglq_entry->sli4_xritag);
6992                                         list_add_tail(&sglq_entry->list,
6993                                                       &free_sgl_list);
6994                                         total_cnt--;
6995                                 }
6996                         }
6997                 }
6998
6999                 /* continue until a nembed page worth of sgls */
7000                 if (post_cnt == 0)
7001                         continue;
7002
7003                 /* post the buffer list sgls as a block */
7004                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7005                                                  post_cnt);
7006
7007                 if (!status) {
7008                         /* success, put sgl list to posted sgl list */
7009                         list_splice_init(&blck_sgl_list, &post_sgl_list);
7010                 } else {
7011                         /* Failure, put sgl list to free sgl list */
7012                         sglq_entry_first = list_first_entry(&blck_sgl_list,
7013                                                             struct lpfc_sglq,
7014                                                             list);
7015                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
7016                                         "3160 Failed to post sgl-list, "
7017                                         "xritag:x%x-x%x\n",
7018                                         sglq_entry_first->sli4_xritag,
7019                                         (sglq_entry_first->sli4_xritag +
7020                                          post_cnt - 1));
7021                         list_splice_init(&blck_sgl_list, &free_sgl_list);
7022                         total_cnt -= post_cnt;
7023                 }
7024
7025                 /* don't reset xirtag due to hole in xri block */
7026                 if (block_cnt == 0)
7027                         last_xritag = NO_XRI;
7028
7029                 /* reset sgl post count for next round of posting */
7030                 post_cnt = 0;
7031         }
7032
7033         /* free the sgls failed to post */
7034         lpfc_free_sgl_list(phba, &free_sgl_list);
7035
7036         /* push sgls posted to the available list */
7037         if (!list_empty(&post_sgl_list)) {
7038                 spin_lock_irq(&phba->hbalock);
7039                 spin_lock(&phba->sli4_hba.sgl_list_lock);
7040                 list_splice_init(&post_sgl_list, sgl_list);
7041                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7042                 spin_unlock_irq(&phba->hbalock);
7043         } else {
7044                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7045                                 "3161 Failure to post sgl to port.\n");
7046                 return -EIO;
7047         }
7048
7049         /* return the number of XRIs actually posted */
7050         return total_cnt;
7051 }
7052
7053 /**
7054  * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
7055  * @phba: pointer to lpfc hba data structure.
7056  *
7057  * This routine walks the list of nvme buffers that have been allocated and
7058  * repost them to the port by using SGL block post. This is needed after a
7059  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7060  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
7061  * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
7062  *
7063  * Returns: 0 = success, non-zero failure.
7064  **/
7065 int
7066 lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
7067 {
7068         LIST_HEAD(post_nblist);
7069         int num_posted, rc = 0;
7070
7071         /* get all NVME buffers need to repost to a local list */
7072         lpfc_io_buf_flush(phba, &post_nblist);
7073
7074         /* post the list of nvme buffer sgls to port if available */
7075         if (!list_empty(&post_nblist)) {
7076                 num_posted = lpfc_sli4_post_io_sgl_list(
7077                         phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
7078                 /* failed to post any nvme buffer, return error */
7079                 if (num_posted == 0)
7080                         rc = -EIO;
7081         }
7082         return rc;
7083 }
7084
7085 void
7086 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7087 {
7088         uint32_t len;
7089
7090         len = sizeof(struct lpfc_mbx_set_host_data) -
7091                 sizeof(struct lpfc_sli4_cfg_mhdr);
7092         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7093                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7094                          LPFC_SLI4_MBX_EMBED);
7095
7096         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
7097         mbox->u.mqe.un.set_host_data.param_len =
7098                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
7099         snprintf(mbox->u.mqe.un.set_host_data.data,
7100                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7101                  "Linux %s v"LPFC_DRIVER_VERSION,
7102                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7103 }
7104
7105 int
7106 lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
7107                     struct lpfc_queue *drq, int count, int idx)
7108 {
7109         int rc, i;
7110         struct lpfc_rqe hrqe;
7111         struct lpfc_rqe drqe;
7112         struct lpfc_rqb *rqbp;
7113         unsigned long flags;
7114         struct rqb_dmabuf *rqb_buffer;
7115         LIST_HEAD(rqb_buf_list);
7116
7117         spin_lock_irqsave(&phba->hbalock, flags);
7118         rqbp = hrq->rqbp;
7119         for (i = 0; i < count; i++) {
7120                 /* IF RQ is already full, don't bother */
7121                 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7122                         break;
7123                 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7124                 if (!rqb_buffer)
7125                         break;
7126                 rqb_buffer->hrq = hrq;
7127                 rqb_buffer->drq = drq;
7128                 rqb_buffer->idx = idx;
7129                 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7130         }
7131         while (!list_empty(&rqb_buf_list)) {
7132                 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7133                                  hbuf.list);
7134
7135                 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7136                 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7137                 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7138                 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7139                 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7140                 if (rc < 0) {
7141                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7142                                         "6421 Cannot post to HRQ %d: %x %x %x "
7143                                         "DRQ %x %x\n",
7144                                         hrq->queue_id,
7145                                         hrq->host_index,
7146                                         hrq->hba_index,
7147                                         hrq->entry_count,
7148                                         drq->host_index,
7149                                         drq->hba_index);
7150                         rqbp->rqb_free_buffer(phba, rqb_buffer);
7151                 } else {
7152                         list_add_tail(&rqb_buffer->hbuf.list,
7153                                       &rqbp->rqb_buffer_list);
7154                         rqbp->buffer_count++;
7155                 }
7156         }
7157         spin_unlock_irqrestore(&phba->hbalock, flags);
7158         return 1;
7159 }
7160
7161 /**
7162  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
7163  * @phba: Pointer to HBA context object.
7164  *
7165  * This function is the main SLI4 device initialization PCI function. This
7166  * function is called by the HBA initialization code, HBA reset code and
7167  * HBA error attention handler code. Caller is not required to hold any
7168  * locks.
7169  **/
7170 int
7171 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7172 {
7173         int rc, i, cnt;
7174         LPFC_MBOXQ_t *mboxq;
7175         struct lpfc_mqe *mqe;
7176         uint8_t *vpd;
7177         uint32_t vpd_size;
7178         uint32_t ftr_rsp = 0;
7179         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7180         struct lpfc_vport *vport = phba->pport;
7181         struct lpfc_dmabuf *mp;
7182         struct lpfc_rqb *rqbp;
7183
7184         /* Perform a PCI function reset to start from clean */
7185         rc = lpfc_pci_function_reset(phba);
7186         if (unlikely(rc))
7187                 return -ENODEV;
7188
7189         /* Check the HBA Host Status Register for readyness */
7190         rc = lpfc_sli4_post_status_check(phba);
7191         if (unlikely(rc))
7192                 return -ENODEV;
7193         else {
7194                 spin_lock_irq(&phba->hbalock);
7195                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7196                 spin_unlock_irq(&phba->hbalock);
7197         }
7198
7199         /*
7200          * Allocate a single mailbox container for initializing the
7201          * port.
7202          */
7203         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7204         if (!mboxq)
7205                 return -ENOMEM;
7206
7207         /* Issue READ_REV to collect vpd and FW information. */
7208         vpd_size = SLI4_PAGE_SIZE;
7209         vpd = kzalloc(vpd_size, GFP_KERNEL);
7210         if (!vpd) {
7211                 rc = -ENOMEM;
7212                 goto out_free_mbox;
7213         }
7214
7215         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
7216         if (unlikely(rc)) {
7217                 kfree(vpd);
7218                 goto out_free_mbox;
7219         }
7220
7221         mqe = &mboxq->u.mqe;
7222         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
7223         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
7224                 phba->hba_flag |= HBA_FCOE_MODE;
7225                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7226         } else {
7227                 phba->hba_flag &= ~HBA_FCOE_MODE;
7228         }
7229
7230         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7231                 LPFC_DCBX_CEE_MODE)
7232                 phba->hba_flag |= HBA_FIP_SUPPORT;
7233         else
7234                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7235
7236         phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7237
7238         if (phba->sli_rev != LPFC_SLI_REV4) {
7239                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240                         "0376 READ_REV Error. SLI Level %d "
7241                         "FCoE enabled %d\n",
7242                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
7243                 rc = -EIO;
7244                 kfree(vpd);
7245                 goto out_free_mbox;
7246         }
7247
7248         /*
7249          * Continue initialization with default values even if driver failed
7250          * to read FCoE param config regions, only read parameters if the
7251          * board is FCoE
7252          */
7253         if (phba->hba_flag & HBA_FCOE_MODE &&
7254             lpfc_sli4_read_fcoe_params(phba))
7255                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7256                         "2570 Failed to read FCoE parameters\n");
7257
7258         /*
7259          * Retrieve sli4 device physical port name, failure of doing it
7260          * is considered as non-fatal.
7261          */
7262         rc = lpfc_sli4_retrieve_pport_name(phba);
7263         if (!rc)
7264                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7265                                 "3080 Successful retrieving SLI4 device "
7266                                 "physical port name: %s.\n", phba->Port);
7267
7268         /*
7269          * Evaluate the read rev and vpd data. Populate the driver
7270          * state with the results. If this routine fails, the failure
7271          * is not fatal as the driver will use generic values.
7272          */
7273         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7274         if (unlikely(!rc)) {
7275                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7276                                 "0377 Error %d parsing vpd. "
7277                                 "Using defaults.\n", rc);
7278                 rc = 0;
7279         }
7280         kfree(vpd);
7281
7282         /* Save information as VPD data */
7283         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7284         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
7285
7286         /*
7287          * This is because first G7 ASIC doesn't support the standard
7288          * 0x5a NVME cmd descriptor type/subtype
7289          */
7290         if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7291                         LPFC_SLI_INTF_IF_TYPE_6) &&
7292             (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7293             (phba->vpd.rev.smRev == 0) &&
7294             (phba->cfg_nvme_embed_cmd == 1))
7295                 phba->cfg_nvme_embed_cmd = 0;
7296
7297         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7298         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7299                                          &mqe->un.read_rev);
7300         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7301                                        &mqe->un.read_rev);
7302         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7303                                             &mqe->un.read_rev);
7304         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7305                                            &mqe->un.read_rev);
7306         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7307         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7308         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7309         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7310         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7311         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7312         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7313                         "(%d):0380 READ_REV Status x%x "
7314                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7315                         mboxq->vport ? mboxq->vport->vpi : 0,
7316                         bf_get(lpfc_mqe_status, mqe),
7317                         phba->vpd.rev.opFwName,
7318                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7319                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
7320
7321         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
7322         rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7323         if (phba->pport->cfg_lun_queue_depth > rc) {
7324                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7325                                 "3362 LUN queue depth changed from %d to %d\n",
7326                                 phba->pport->cfg_lun_queue_depth, rc);
7327                 phba->pport->cfg_lun_queue_depth = rc;
7328         }
7329
7330         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7331             LPFC_SLI_INTF_IF_TYPE_0) {
7332                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7333                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7334                 if (rc == MBX_SUCCESS) {
7335                         phba->hba_flag |= HBA_RECOVERABLE_UE;
7336                         /* Set 1Sec interval to detect UE */
7337                         phba->eratt_poll_interval = 1;
7338                         phba->sli4_hba.ue_to_sr = bf_get(
7339                                         lpfc_mbx_set_feature_UESR,
7340                                         &mboxq->u.mqe.un.set_feature);
7341                         phba->sli4_hba.ue_to_rp = bf_get(
7342                                         lpfc_mbx_set_feature_UERP,
7343                                         &mboxq->u.mqe.un.set_feature);
7344                 }
7345         }
7346
7347         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7348                 /* Enable MDS Diagnostics only if the SLI Port supports it */
7349                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7350                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7351                 if (rc != MBX_SUCCESS)
7352                         phba->mds_diags_support = 0;
7353         }
7354
7355         /*
7356          * Discover the port's supported feature set and match it against the
7357          * hosts requests.
7358          */
7359         lpfc_request_features(phba, mboxq);
7360         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7361         if (unlikely(rc)) {
7362                 rc = -EIO;
7363                 goto out_free_mbox;
7364         }
7365
7366         /*
7367          * The port must support FCP initiator mode as this is the
7368          * only mode running in the host.
7369          */
7370         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7371                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7372                                 "0378 No support for fcpi mode.\n");
7373                 ftr_rsp++;
7374         }
7375
7376         /* Performance Hints are ONLY for FCoE */
7377         if (phba->hba_flag & HBA_FCOE_MODE) {
7378                 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7379                         phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7380                 else
7381                         phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7382         }
7383
7384         /*
7385          * If the port cannot support the host's requested features
7386          * then turn off the global config parameters to disable the
7387          * feature in the driver.  This is not a fatal error.
7388          */
7389         if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7390                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7391                         phba->cfg_enable_bg = 0;
7392                         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
7393                         ftr_rsp++;
7394                 }
7395         }
7396
7397         if (phba->max_vpi && phba->cfg_enable_npiv &&
7398             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7399                 ftr_rsp++;
7400
7401         if (ftr_rsp) {
7402                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7403                                 "0379 Feature Mismatch Data: x%08x %08x "
7404                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7405                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7406                                 phba->cfg_enable_npiv, phba->max_vpi);
7407                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7408                         phba->cfg_enable_bg = 0;
7409                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7410                         phba->cfg_enable_npiv = 0;
7411         }
7412
7413         /* These SLI3 features are assumed in SLI4 */
7414         spin_lock_irq(&phba->hbalock);
7415         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7416         spin_unlock_irq(&phba->hbalock);
7417
7418         /*
7419          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
7420          * calls depends on these resources to complete port setup.
7421          */
7422         rc = lpfc_sli4_alloc_resource_identifiers(phba);
7423         if (rc) {
7424                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7425                                 "2920 Failed to alloc Resource IDs "
7426                                 "rc = x%x\n", rc);
7427                 goto out_free_mbox;
7428         }
7429
7430         lpfc_set_host_data(phba, mboxq);
7431
7432         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7433         if (rc) {
7434                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7435                                 "2134 Failed to set host os driver version %x",
7436                                 rc);
7437         }
7438
7439         /* Read the port's service parameters. */
7440         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7441         if (rc) {
7442                 phba->link_state = LPFC_HBA_ERROR;
7443                 rc = -ENOMEM;
7444                 goto out_free_mbox;
7445         }
7446
7447         mboxq->vport = vport;
7448         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7449         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
7450         if (rc == MBX_SUCCESS) {
7451                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7452                 rc = 0;
7453         }
7454
7455         /*
7456          * This memory was allocated by the lpfc_read_sparam routine. Release
7457          * it to the mbuf pool.
7458          */
7459         lpfc_mbuf_free(phba, mp->virt, mp->phys);
7460         kfree(mp);
7461         mboxq->ctx_buf = NULL;
7462         if (unlikely(rc)) {
7463                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7464                                 "0382 READ_SPARAM command failed "
7465                                 "status %d, mbxStatus x%x\n",
7466                                 rc, bf_get(lpfc_mqe_status, mqe));
7467                 phba->link_state = LPFC_HBA_ERROR;
7468                 rc = -EIO;
7469                 goto out_free_mbox;
7470         }
7471
7472         lpfc_update_vport_wwn(vport);
7473
7474         /* Update the fc_host data structures with new wwn. */
7475         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7476         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7477
7478         /* Create all the SLI4 queues */
7479         rc = lpfc_sli4_queue_create(phba);
7480         if (rc) {
7481                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7482                                 "3089 Failed to allocate queues\n");
7483                 rc = -ENODEV;
7484                 goto out_free_mbox;
7485         }
7486         /* Set up all the queues to the device */
7487         rc = lpfc_sli4_queue_setup(phba);
7488         if (unlikely(rc)) {
7489                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7490                                 "0381 Error %d during queue setup.\n ", rc);
7491                 goto out_stop_timers;
7492         }
7493         /* Initialize the driver internal SLI layer lists. */
7494         lpfc_sli4_setup(phba);
7495         lpfc_sli4_queue_init(phba);
7496
7497         /* update host els xri-sgl sizes and mappings */
7498         rc = lpfc_sli4_els_sgl_update(phba);
7499         if (unlikely(rc)) {
7500                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7501                                 "1400 Failed to update xri-sgl size and "
7502                                 "mapping: %d\n", rc);
7503                 goto out_destroy_queue;
7504         }
7505
7506         /* register the els sgl pool to the port */
7507         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7508                                        phba->sli4_hba.els_xri_cnt);
7509         if (unlikely(rc < 0)) {
7510                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7511                                 "0582 Error %d during els sgl post "
7512                                 "operation\n", rc);
7513                 rc = -ENODEV;
7514                 goto out_destroy_queue;
7515         }
7516         phba->sli4_hba.els_xri_cnt = rc;
7517
7518         if (phba->nvmet_support) {
7519                 /* update host nvmet xri-sgl sizes and mappings */
7520                 rc = lpfc_sli4_nvmet_sgl_update(phba);
7521                 if (unlikely(rc)) {
7522                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7523                                         "6308 Failed to update nvmet-sgl size "
7524                                         "and mapping: %d\n", rc);
7525                         goto out_destroy_queue;
7526                 }
7527
7528                 /* register the nvmet sgl pool to the port */
7529                 rc = lpfc_sli4_repost_sgl_list(
7530                         phba,
7531                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
7532                         phba->sli4_hba.nvmet_xri_cnt);
7533                 if (unlikely(rc < 0)) {
7534                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7535                                         "3117 Error %d during nvmet "
7536                                         "sgl post\n", rc);
7537                         rc = -ENODEV;
7538                         goto out_destroy_queue;
7539                 }
7540                 phba->sli4_hba.nvmet_xri_cnt = rc;
7541
7542                 cnt = phba->cfg_iocb_cnt * 1024;
7543                 /* We need 1 iocbq for every SGL, for IO processing */
7544                 cnt += phba->sli4_hba.nvmet_xri_cnt;
7545         } else {
7546                 /* update host common xri-sgl sizes and mappings */
7547                 rc = lpfc_sli4_io_sgl_update(phba);
7548                 if (unlikely(rc)) {
7549                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7550                                         "6082 Failed to update nvme-sgl size "
7551                                         "and mapping: %d\n", rc);
7552                         goto out_destroy_queue;
7553                 }
7554
7555                 /* register the allocated common sgl pool to the port */
7556                 rc = lpfc_sli4_repost_io_sgl_list(phba);
7557                 if (unlikely(rc)) {
7558                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7559                                         "6116 Error %d during nvme sgl post "
7560                                         "operation\n", rc);
7561                         /* Some NVME buffers were moved to abort nvme list */
7562                         /* A pci function reset will repost them */
7563                         rc = -ENODEV;
7564                         goto out_destroy_queue;
7565                 }
7566                 cnt = phba->cfg_iocb_cnt * 1024;
7567         }
7568
7569         if (!phba->sli.iocbq_lookup) {
7570                 /* Initialize and populate the iocb list per host */
7571                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7572                                 "2821 initialize iocb list %d total %d\n",
7573                                 phba->cfg_iocb_cnt, cnt);
7574                 rc = lpfc_init_iocb_list(phba, cnt);
7575                 if (rc) {
7576                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7577                                         "1413 Failed to init iocb list.\n");
7578                         goto out_destroy_queue;
7579                 }
7580         }
7581
7582         if (phba->nvmet_support)
7583                 lpfc_nvmet_create_targetport(phba);
7584
7585         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
7586                 /* Post initial buffers to all RQs created */
7587                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7588                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7589                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7590                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7591                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
7592                         rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
7593                         rqbp->buffer_count = 0;
7594
7595                         lpfc_post_rq_buffer(
7596                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7597                                 phba->sli4_hba.nvmet_mrq_data[i],
7598                                 phba->cfg_nvmet_mrq_post, i);
7599                 }
7600         }
7601
7602         /* Post the rpi header region to the device. */
7603         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7604         if (unlikely(rc)) {
7605                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7606                                 "0393 Error %d during rpi post operation\n",
7607                                 rc);
7608                 rc = -ENODEV;
7609                 goto out_destroy_queue;
7610         }
7611         lpfc_sli4_node_prep(phba);
7612
7613         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
7614                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
7615                         /*
7616                          * The FC Port needs to register FCFI (index 0)
7617                          */
7618                         lpfc_reg_fcfi(phba, mboxq);
7619                         mboxq->vport = phba->pport;
7620                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7621                         if (rc != MBX_SUCCESS)
7622                                 goto out_unset_queue;
7623                         rc = 0;
7624                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7625                                                 &mboxq->u.mqe.un.reg_fcfi);
7626                 } else {
7627                         /* We are a NVME Target mode with MRQ > 1 */
7628
7629                         /* First register the FCFI */
7630                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7631                         mboxq->vport = phba->pport;
7632                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7633                         if (rc != MBX_SUCCESS)
7634                                 goto out_unset_queue;
7635                         rc = 0;
7636                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7637                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
7638
7639                         /* Next register the MRQs */
7640                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7641                         mboxq->vport = phba->pport;
7642                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7643                         if (rc != MBX_SUCCESS)
7644                                 goto out_unset_queue;
7645                         rc = 0;
7646                 }
7647                 /* Check if the port is configured to be disabled */
7648                 lpfc_sli_read_link_ste(phba);
7649         }
7650
7651         /* Arm the CQs and then EQs on device */
7652         lpfc_sli4_arm_cqeq_intr(phba);
7653
7654         /* Indicate device interrupt mode */
7655         phba->sli4_hba.intr_enable = 1;
7656
7657         /* Allow asynchronous mailbox command to go through */
7658         spin_lock_irq(&phba->hbalock);
7659         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7660         spin_unlock_irq(&phba->hbalock);
7661
7662         /* Post receive buffers to the device */
7663         lpfc_sli4_rb_setup(phba);
7664
7665         /* Reset HBA FCF states after HBA reset */
7666         phba->fcf.fcf_flag = 0;
7667         phba->fcf.current_rec.flag = 0;
7668
7669         /* Start the ELS watchdog timer */
7670         mod_timer(&vport->els_tmofunc,
7671                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7672
7673         /* Start heart beat timer */
7674         mod_timer(&phba->hb_tmofunc,
7675                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7676         phba->hb_outstanding = 0;
7677         phba->last_completion_time = jiffies;
7678
7679         /* Start error attention (ERATT) polling timer */
7680         mod_timer(&phba->eratt_poll,
7681                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7682
7683         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7684         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7685                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7686                 if (!rc) {
7687                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7688                                         "2829 This device supports "
7689                                         "Advanced Error Reporting (AER)\n");
7690                         spin_lock_irq(&phba->hbalock);
7691                         phba->hba_flag |= HBA_AER_ENABLED;
7692                         spin_unlock_irq(&phba->hbalock);
7693                 } else {
7694                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7695                                         "2830 This device does not support "
7696                                         "Advanced Error Reporting (AER)\n");
7697                         phba->cfg_aer_support = 0;
7698                 }
7699                 rc = 0;
7700         }
7701
7702         /*
7703          * The port is ready, set the host's link state to LINK_DOWN
7704          * in preparation for link interrupts.
7705          */
7706         spin_lock_irq(&phba->hbalock);
7707         phba->link_state = LPFC_LINK_DOWN;
7708
7709         /* Check if physical ports are trunked */
7710         if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7711                 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7712         if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7713                 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7714         if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7715                 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7716         if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7717                 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
7718         spin_unlock_irq(&phba->hbalock);
7719
7720         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7721             (phba->hba_flag & LINK_DISABLED)) {
7722                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7723                                 "3103 Adapter Link is disabled.\n");
7724                 lpfc_down_link(phba, mboxq);
7725                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7726                 if (rc != MBX_SUCCESS) {
7727                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7728                                         "3104 Adapter failed to issue "
7729                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7730                         goto out_unset_queue;
7731                 }
7732         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7733                 /* don't perform init_link on SLI4 FC port loopback test */
7734                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7735                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7736                         if (rc)
7737                                 goto out_unset_queue;
7738                 }
7739         }
7740         mempool_free(mboxq, phba->mbox_mem_pool);
7741         return rc;
7742 out_unset_queue:
7743         /* Unset all the queues set up in this routine when error out */
7744         lpfc_sli4_queue_unset(phba);
7745 out_destroy_queue:
7746         lpfc_free_iocb_list(phba);
7747         lpfc_sli4_queue_destroy(phba);
7748 out_stop_timers:
7749         lpfc_stop_hba_timers(phba);
7750 out_free_mbox:
7751         mempool_free(mboxq, phba->mbox_mem_pool);
7752         return rc;
7753 }
7754
7755 /**
7756  * lpfc_mbox_timeout - Timeout call back function for mbox timer
7757  * @ptr: context object - pointer to hba structure.
7758  *
7759  * This is the callback function for mailbox timer. The mailbox
7760  * timer is armed when a new mailbox command is issued and the timer
7761  * is deleted when the mailbox complete. The function is called by
7762  * the kernel timer code when a mailbox does not complete within
7763  * expected time. This function wakes up the worker thread to
7764  * process the mailbox timeout and returns. All the processing is
7765  * done by the worker thread function lpfc_mbox_timeout_handler.
7766  **/
7767 void
7768 lpfc_mbox_timeout(struct timer_list *t)
7769 {
7770         struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
7771         unsigned long iflag;
7772         uint32_t tmo_posted;
7773
7774         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7775         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7776         if (!tmo_posted)
7777                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7778         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7779
7780         if (!tmo_posted)
7781                 lpfc_worker_wake_up(phba);
7782         return;
7783 }
7784
7785 /**
7786  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7787  *                                    are pending
7788  * @phba: Pointer to HBA context object.
7789  *
7790  * This function checks if any mailbox completions are present on the mailbox
7791  * completion queue.
7792  **/
7793 static bool
7794 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7795 {
7796
7797         uint32_t idx;
7798         struct lpfc_queue *mcq;
7799         struct lpfc_mcqe *mcqe;
7800         bool pending_completions = false;
7801         uint8_t qe_valid;
7802
7803         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7804                 return false;
7805
7806         /* Check for completions on mailbox completion queue */
7807
7808         mcq = phba->sli4_hba.mbx_cq;
7809         idx = mcq->hba_index;
7810         qe_valid = mcq->qe_valid;
7811         while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
7812                 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7813                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7814                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7815                         pending_completions = true;
7816                         break;
7817                 }
7818                 idx = (idx + 1) % mcq->entry_count;
7819                 if (mcq->hba_index == idx)
7820                         break;
7821
7822                 /* if the index wrapped around, toggle the valid bit */
7823                 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7824                         qe_valid = (qe_valid) ? 0 : 1;
7825         }
7826         return pending_completions;
7827
7828 }
7829
7830 /**
7831  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7832  *                                            that were missed.
7833  * @phba: Pointer to HBA context object.
7834  *
7835  * For sli4, it is possible to miss an interrupt. As such mbox completions
7836  * maybe missed causing erroneous mailbox timeouts to occur. This function
7837  * checks to see if mbox completions are on the mailbox completion queue
7838  * and will process all the completions associated with the eq for the
7839  * mailbox completion queue.
7840  **/
7841 bool
7842 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7843 {
7844         struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
7845         uint32_t eqidx;
7846         struct lpfc_queue *fpeq = NULL;
7847         struct lpfc_eqe *eqe;
7848         bool mbox_pending;
7849
7850         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7851                 return false;
7852
7853         /* Find the eq associated with the mcq */
7854
7855         if (sli4_hba->hdwq)
7856                 for (eqidx = 0; eqidx < phba->cfg_hdw_queue; eqidx++)
7857                         if (sli4_hba->hdwq[eqidx].hba_eq->queue_id ==
7858                             sli4_hba->mbx_cq->assoc_qid) {
7859                                 fpeq = sli4_hba->hdwq[eqidx].hba_eq;
7860                                 break;
7861                         }
7862         if (!fpeq)
7863                 return false;
7864
7865         /* Turn off interrupts from this EQ */
7866
7867         sli4_hba->sli4_eq_clr_intr(fpeq);
7868
7869         /* Check to see if a mbox completion is pending */
7870
7871         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7872
7873         /*
7874          * If a mbox completion is pending, process all the events on EQ
7875          * associated with the mbox completion queue (this could include
7876          * mailbox commands, async events, els commands, receive queue data
7877          * and fcp commands)
7878          */
7879
7880         if (mbox_pending)
7881                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7882                         lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7883                         fpeq->EQ_processed++;
7884                 }
7885
7886         /* Always clear and re-arm the EQ */
7887
7888         sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7889
7890         return mbox_pending;
7891
7892 }
7893
7894 /**
7895  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7896  * @phba: Pointer to HBA context object.
7897  *
7898  * This function is called from worker thread when a mailbox command times out.
7899  * The caller is not required to hold any locks. This function will reset the
7900  * HBA and recover all the pending commands.
7901  **/
7902 void
7903 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7904 {
7905         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7906         MAILBOX_t *mb = NULL;
7907
7908         struct lpfc_sli *psli = &phba->sli;
7909
7910         /* If the mailbox completed, process the completion and return */
7911         if (lpfc_sli4_process_missed_mbox_completions(phba))
7912                 return;
7913
7914         if (pmbox != NULL)
7915                 mb = &pmbox->u.mb;
7916         /* Check the pmbox pointer first.  There is a race condition
7917          * between the mbox timeout handler getting executed in the
7918          * worklist and the mailbox actually completing. When this
7919          * race condition occurs, the mbox_active will be NULL.
7920          */
7921         spin_lock_irq(&phba->hbalock);
7922         if (pmbox == NULL) {
7923                 lpfc_printf_log(phba, KERN_WARNING,
7924                                 LOG_MBOX | LOG_SLI,
7925                                 "0353 Active Mailbox cleared - mailbox timeout "
7926                                 "exiting\n");
7927                 spin_unlock_irq(&phba->hbalock);
7928                 return;
7929         }
7930
7931         /* Mbox cmd <mbxCommand> timeout */
7932         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7933                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7934                         mb->mbxCommand,
7935                         phba->pport->port_state,
7936                         phba->sli.sli_flag,
7937                         phba->sli.mbox_active);
7938         spin_unlock_irq(&phba->hbalock);
7939
7940         /* Setting state unknown so lpfc_sli_abort_iocb_ring
7941          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7942          * it to fail all outstanding SCSI IO.
7943          */
7944         spin_lock_irq(&phba->pport->work_port_lock);
7945         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7946         spin_unlock_irq(&phba->pport->work_port_lock);
7947         spin_lock_irq(&phba->hbalock);
7948         phba->link_state = LPFC_LINK_UNKNOWN;
7949         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7950         spin_unlock_irq(&phba->hbalock);
7951
7952         lpfc_sli_abort_fcp_rings(phba);
7953
7954         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7955                         "0345 Resetting board due to mailbox timeout\n");
7956
7957         /* Reset the HBA device */
7958         lpfc_reset_hba(phba);
7959 }
7960
7961 /**
7962  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7963  * @phba: Pointer to HBA context object.
7964  * @pmbox: Pointer to mailbox object.
7965  * @flag: Flag indicating how the mailbox need to be processed.
7966  *
7967  * This function is called by discovery code and HBA management code
7968  * to submit a mailbox command to firmware with SLI-3 interface spec. This
7969  * function gets the hbalock to protect the data structures.
7970  * The mailbox command can be submitted in polling mode, in which case
7971  * this function will wait in a polling loop for the completion of the
7972  * mailbox.
7973  * If the mailbox is submitted in no_wait mode (not polling) the
7974  * function will submit the command and returns immediately without waiting
7975  * for the mailbox completion. The no_wait is supported only when HBA
7976  * is in SLI2/SLI3 mode - interrupts are enabled.
7977  * The SLI interface allows only one mailbox pending at a time. If the
7978  * mailbox is issued in polling mode and there is already a mailbox
7979  * pending, then the function will return an error. If the mailbox is issued
7980  * in NO_WAIT mode and there is a mailbox pending already, the function
7981  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7982  * The sli layer owns the mailbox object until the completion of mailbox
7983  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7984  * return codes the caller owns the mailbox command after the return of
7985  * the function.
7986  **/
7987 static int
7988 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7989                        uint32_t flag)
7990 {
7991         MAILBOX_t *mbx;
7992         struct lpfc_sli *psli = &phba->sli;
7993         uint32_t status, evtctr;
7994         uint32_t ha_copy, hc_copy;
7995         int i;
7996         unsigned long timeout;
7997         unsigned long drvr_flag = 0;
7998         uint32_t word0, ldata;
7999         void __iomem *to_slim;
8000         int processing_queue = 0;
8001
8002         spin_lock_irqsave(&phba->hbalock, drvr_flag);
8003         if (!pmbox) {
8004                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8005                 /* processing mbox queue from intr_handler */
8006                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8007                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8008                         return MBX_SUCCESS;
8009                 }
8010                 processing_queue = 1;
8011                 pmbox = lpfc_mbox_get(phba);
8012                 if (!pmbox) {
8013                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8014                         return MBX_SUCCESS;
8015                 }
8016         }
8017
8018         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
8019                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
8020                 if(!pmbox->vport) {
8021                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8022                         lpfc_printf_log(phba, KERN_ERR,
8023                                         LOG_MBOX | LOG_VPORT,
8024                                         "1806 Mbox x%x failed. No vport\n",
8025                                         pmbox->u.mb.mbxCommand);
8026                         dump_stack();
8027                         goto out_not_finished;
8028                 }
8029         }
8030
8031         /* If the PCI channel is in offline state, do not post mbox. */
8032         if (unlikely(pci_channel_offline(phba->pcidev))) {
8033                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8034                 goto out_not_finished;
8035         }
8036
8037         /* If HBA has a deferred error attention, fail the iocb. */
8038         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8039                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8040                 goto out_not_finished;
8041         }
8042
8043         psli = &phba->sli;
8044
8045         mbx = &pmbox->u.mb;
8046         status = MBX_SUCCESS;
8047
8048         if (phba->link_state == LPFC_HBA_ERROR) {
8049                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8050
8051                 /* Mbox command <mbxCommand> cannot issue */
8052                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8053                                 "(%d):0311 Mailbox command x%x cannot "
8054                                 "issue Data: x%x x%x\n",
8055                                 pmbox->vport ? pmbox->vport->vpi : 0,
8056                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8057                 goto out_not_finished;
8058         }
8059
8060         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
8061                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8062                         !(hc_copy & HC_MBINT_ENA)) {
8063                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8064                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8065                                 "(%d):2528 Mailbox command x%x cannot "
8066                                 "issue Data: x%x x%x\n",
8067                                 pmbox->vport ? pmbox->vport->vpi : 0,
8068                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
8069                         goto out_not_finished;
8070                 }
8071         }
8072
8073         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8074                 /* Polling for a mbox command when another one is already active
8075                  * is not allowed in SLI. Also, the driver must have established
8076                  * SLI2 mode to queue and process multiple mbox commands.
8077                  */
8078
8079                 if (flag & MBX_POLL) {
8080                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8081
8082                         /* Mbox command <mbxCommand> cannot issue */
8083                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8084                                         "(%d):2529 Mailbox command x%x "
8085                                         "cannot issue Data: x%x x%x\n",
8086                                         pmbox->vport ? pmbox->vport->vpi : 0,
8087                                         pmbox->u.mb.mbxCommand,
8088                                         psli->sli_flag, flag);
8089                         goto out_not_finished;
8090                 }
8091
8092                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
8093                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8094                         /* Mbox command <mbxCommand> cannot issue */
8095                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8096                                         "(%d):2530 Mailbox command x%x "
8097                                         "cannot issue Data: x%x x%x\n",
8098                                         pmbox->vport ? pmbox->vport->vpi : 0,
8099                                         pmbox->u.mb.mbxCommand,
8100                                         psli->sli_flag, flag);
8101                         goto out_not_finished;
8102                 }
8103
8104                 /* Another mailbox command is still being processed, queue this
8105                  * command to be processed later.
8106                  */
8107                 lpfc_mbox_put(phba, pmbox);
8108
8109                 /* Mbox cmd issue - BUSY */
8110                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8111                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
8112                                 "x%x x%x x%x x%x\n",
8113                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
8114                                 mbx->mbxCommand,
8115                                 phba->pport ? phba->pport->port_state : 0xff,
8116                                 psli->sli_flag, flag);
8117
8118                 psli->slistat.mbox_busy++;
8119                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8120
8121                 if (pmbox->vport) {
8122                         lpfc_debugfs_disc_trc(pmbox->vport,
8123                                 LPFC_DISC_TRC_MBOX_VPORT,
8124                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
8125                                 (uint32_t)mbx->mbxCommand,
8126                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8127                 }
8128                 else {
8129                         lpfc_debugfs_disc_trc(phba->pport,
8130                                 LPFC_DISC_TRC_MBOX,
8131                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
8132                                 (uint32_t)mbx->mbxCommand,
8133                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8134                 }
8135
8136                 return MBX_BUSY;
8137         }
8138
8139         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8140
8141         /* If we are not polling, we MUST be in SLI2 mode */
8142         if (flag != MBX_POLL) {
8143                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
8144                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
8145                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8146                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8147                         /* Mbox command <mbxCommand> cannot issue */
8148                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8149                                         "(%d):2531 Mailbox command x%x "
8150                                         "cannot issue Data: x%x x%x\n",
8151                                         pmbox->vport ? pmbox->vport->vpi : 0,
8152                                         pmbox->u.mb.mbxCommand,
8153                                         psli->sli_flag, flag);
8154                         goto out_not_finished;
8155                 }
8156                 /* timeout active mbox command */
8157                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8158                                            1000);
8159                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
8160         }
8161
8162         /* Mailbox cmd <cmd> issue */
8163         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8164                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
8165                         "x%x\n",
8166                         pmbox->vport ? pmbox->vport->vpi : 0,
8167                         mbx->mbxCommand,
8168                         phba->pport ? phba->pport->port_state : 0xff,
8169                         psli->sli_flag, flag);
8170
8171         if (mbx->mbxCommand != MBX_HEARTBEAT) {
8172                 if (pmbox->vport) {
8173                         lpfc_debugfs_disc_trc(pmbox->vport,
8174                                 LPFC_DISC_TRC_MBOX_VPORT,
8175                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8176                                 (uint32_t)mbx->mbxCommand,
8177                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8178                 }
8179                 else {
8180                         lpfc_debugfs_disc_trc(phba->pport,
8181                                 LPFC_DISC_TRC_MBOX,
8182                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
8183                                 (uint32_t)mbx->mbxCommand,
8184                                 mbx->un.varWords[0], mbx->un.varWords[1]);
8185                 }
8186         }
8187
8188         psli->slistat.mbox_cmd++;
8189         evtctr = psli->slistat.mbox_event;
8190
8191         /* next set own bit for the adapter and copy over command word */
8192         mbx->mbxOwner = OWN_CHIP;
8193
8194         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8195                 /* Populate mbox extension offset word. */
8196                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
8197                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8198                                 = (uint8_t *)phba->mbox_ext
8199                                   - (uint8_t *)phba->mbox;
8200                 }
8201
8202                 /* Copy the mailbox extension data */
8203                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8204                         lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8205                                               (uint8_t *)phba->mbox_ext,
8206                                               pmbox->in_ext_byte_len);
8207                 }
8208                 /* Copy command data to host SLIM area */
8209                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
8210         } else {
8211                 /* Populate mbox extension offset word. */
8212                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
8213                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
8214                                 = MAILBOX_HBA_EXT_OFFSET;
8215
8216                 /* Copy the mailbox extension data */
8217                 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
8218                         lpfc_memcpy_to_slim(phba->MBslimaddr +
8219                                 MAILBOX_HBA_EXT_OFFSET,
8220                                 pmbox->ctx_buf, pmbox->in_ext_byte_len);
8221
8222                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8223                         /* copy command data into host mbox for cmpl */
8224                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8225                                               MAILBOX_CMD_SIZE);
8226
8227                 /* First copy mbox command data to HBA SLIM, skip past first
8228                    word */
8229                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
8230                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
8231                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
8232
8233                 /* Next copy over first word, with mbxOwner set */
8234                 ldata = *((uint32_t *)mbx);
8235                 to_slim = phba->MBslimaddr;
8236                 writel(ldata, to_slim);
8237                 readl(to_slim); /* flush */
8238
8239                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
8240                         /* switch over to host mailbox */
8241                         psli->sli_flag |= LPFC_SLI_ACTIVE;
8242         }
8243
8244         wmb();
8245
8246         switch (flag) {
8247         case MBX_NOWAIT:
8248                 /* Set up reference to mailbox command */
8249                 psli->mbox_active = pmbox;
8250                 /* Interrupt board to do it */
8251                 writel(CA_MBATT, phba->CAregaddr);
8252                 readl(phba->CAregaddr); /* flush */
8253                 /* Don't wait for it to finish, just return */
8254                 break;
8255
8256         case MBX_POLL:
8257                 /* Set up null reference to mailbox command */
8258                 psli->mbox_active = NULL;
8259                 /* Interrupt board to do it */
8260                 writel(CA_MBATT, phba->CAregaddr);
8261                 readl(phba->CAregaddr); /* flush */
8262
8263                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8264                         /* First read mbox status word */
8265                         word0 = *((uint32_t *)phba->mbox);
8266                         word0 = le32_to_cpu(word0);
8267                 } else {
8268                         /* First read mbox status word */
8269                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
8270                                 spin_unlock_irqrestore(&phba->hbalock,
8271                                                        drvr_flag);
8272                                 goto out_not_finished;
8273                         }
8274                 }
8275
8276                 /* Read the HBA Host Attention Register */
8277                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8278                         spin_unlock_irqrestore(&phba->hbalock,
8279                                                        drvr_flag);
8280                         goto out_not_finished;
8281                 }
8282                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8283                                                         1000) + jiffies;
8284                 i = 0;
8285                 /* Wait for command to complete */
8286                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8287                        (!(ha_copy & HA_MBATT) &&
8288                         (phba->link_state > LPFC_WARM_START))) {
8289                         if (time_after(jiffies, timeout)) {
8290                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8291                                 spin_unlock_irqrestore(&phba->hbalock,
8292                                                        drvr_flag);
8293                                 goto out_not_finished;
8294                         }
8295
8296                         /* Check if we took a mbox interrupt while we were
8297                            polling */
8298                         if (((word0 & OWN_CHIP) != OWN_CHIP)
8299                             && (evtctr != psli->slistat.mbox_event))
8300                                 break;
8301
8302                         if (i++ > 10) {
8303                                 spin_unlock_irqrestore(&phba->hbalock,
8304                                                        drvr_flag);
8305                                 msleep(1);
8306                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8307                         }
8308
8309                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8310                                 /* First copy command data */
8311                                 word0 = *((uint32_t *)phba->mbox);
8312                                 word0 = le32_to_cpu(word0);
8313                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
8314                                         MAILBOX_t *slimmb;
8315                                         uint32_t slimword0;
8316                                         /* Check real SLIM for any errors */
8317                                         slimword0 = readl(phba->MBslimaddr);
8318                                         slimmb = (MAILBOX_t *) & slimword0;
8319                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8320                                             && slimmb->mbxStatus) {
8321                                                 psli->sli_flag &=
8322                                                     ~LPFC_SLI_ACTIVE;
8323                                                 word0 = slimword0;
8324                                         }
8325                                 }
8326                         } else {
8327                                 /* First copy command data */
8328                                 word0 = readl(phba->MBslimaddr);
8329                         }
8330                         /* Read the HBA Host Attention Register */
8331                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8332                                 spin_unlock_irqrestore(&phba->hbalock,
8333                                                        drvr_flag);
8334                                 goto out_not_finished;
8335                         }
8336                 }
8337
8338                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8339                         /* copy results back to user */
8340                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8341                                                 MAILBOX_CMD_SIZE);
8342                         /* Copy the mailbox extension data */
8343                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8344                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
8345                                                       pmbox->ctx_buf,
8346                                                       pmbox->out_ext_byte_len);
8347                         }
8348                 } else {
8349                         /* First copy command data */
8350                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
8351                                                 MAILBOX_CMD_SIZE);
8352                         /* Copy the mailbox extension data */
8353                         if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8354                                 lpfc_memcpy_from_slim(
8355                                         pmbox->ctx_buf,
8356                                         phba->MBslimaddr +
8357                                         MAILBOX_HBA_EXT_OFFSET,
8358                                         pmbox->out_ext_byte_len);
8359                         }
8360                 }
8361
8362                 writel(HA_MBATT, phba->HAregaddr);
8363                 readl(phba->HAregaddr); /* flush */
8364
8365                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8366                 status = mbx->mbxStatus;
8367         }
8368
8369         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8370         return status;
8371
8372 out_not_finished:
8373         if (processing_queue) {
8374                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
8375                 lpfc_mbox_cmpl_put(phba, pmbox);
8376         }
8377         return MBX_NOT_FINISHED;
8378 }
8379
8380 /**
8381  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8382  * @phba: Pointer to HBA context object.
8383  *
8384  * The function blocks the posting of SLI4 asynchronous mailbox commands from
8385  * the driver internal pending mailbox queue. It will then try to wait out the
8386  * possible outstanding mailbox command before return.
8387  *
8388  * Returns:
8389  *      0 - the outstanding mailbox command completed; otherwise, the wait for
8390  *      the outstanding mailbox command timed out.
8391  **/
8392 static int
8393 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8394 {
8395         struct lpfc_sli *psli = &phba->sli;
8396         int rc = 0;
8397         unsigned long timeout = 0;
8398
8399         /* Mark the asynchronous mailbox command posting as blocked */
8400         spin_lock_irq(&phba->hbalock);
8401         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8402         /* Determine how long we might wait for the active mailbox
8403          * command to be gracefully completed by firmware.
8404          */
8405         if (phba->sli.mbox_active)
8406                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8407                                                 phba->sli.mbox_active) *
8408                                                 1000) + jiffies;
8409         spin_unlock_irq(&phba->hbalock);
8410
8411         /* Make sure the mailbox is really active */
8412         if (timeout)
8413                 lpfc_sli4_process_missed_mbox_completions(phba);
8414
8415         /* Wait for the outstnading mailbox command to complete */
8416         while (phba->sli.mbox_active) {
8417                 /* Check active mailbox complete status every 2ms */
8418                 msleep(2);
8419                 if (time_after(jiffies, timeout)) {
8420                         /* Timeout, marked the outstanding cmd not complete */
8421                         rc = 1;
8422                         break;
8423                 }
8424         }
8425
8426         /* Can not cleanly block async mailbox command, fails it */
8427         if (rc) {
8428                 spin_lock_irq(&phba->hbalock);
8429                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8430                 spin_unlock_irq(&phba->hbalock);
8431         }
8432         return rc;
8433 }
8434
8435 /**
8436  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8437  * @phba: Pointer to HBA context object.
8438  *
8439  * The function unblocks and resume posting of SLI4 asynchronous mailbox
8440  * commands from the driver internal pending mailbox queue. It makes sure
8441  * that there is no outstanding mailbox command before resuming posting
8442  * asynchronous mailbox commands. If, for any reason, there is outstanding
8443  * mailbox command, it will try to wait it out before resuming asynchronous
8444  * mailbox command posting.
8445  **/
8446 static void
8447 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8448 {
8449         struct lpfc_sli *psli = &phba->sli;
8450
8451         spin_lock_irq(&phba->hbalock);
8452         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8453                 /* Asynchronous mailbox posting is not blocked, do nothing */
8454                 spin_unlock_irq(&phba->hbalock);
8455                 return;
8456         }
8457
8458         /* Outstanding synchronous mailbox command is guaranteed to be done,
8459          * successful or timeout, after timing-out the outstanding mailbox
8460          * command shall always be removed, so just unblock posting async
8461          * mailbox command and resume
8462          */
8463         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8464         spin_unlock_irq(&phba->hbalock);
8465
8466         /* wake up worker thread to post asynchronlous mailbox command */
8467         lpfc_worker_wake_up(phba);
8468 }
8469
8470 /**
8471  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8472  * @phba: Pointer to HBA context object.
8473  * @mboxq: Pointer to mailbox object.
8474  *
8475  * The function waits for the bootstrap mailbox register ready bit from
8476  * port for twice the regular mailbox command timeout value.
8477  *
8478  *      0 - no timeout on waiting for bootstrap mailbox register ready.
8479  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8480  **/
8481 static int
8482 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8483 {
8484         uint32_t db_ready;
8485         unsigned long timeout;
8486         struct lpfc_register bmbx_reg;
8487
8488         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8489                                    * 1000) + jiffies;
8490
8491         do {
8492                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8493                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8494                 if (!db_ready)
8495                         msleep(2);
8496
8497                 if (time_after(jiffies, timeout))
8498                         return MBXERR_ERROR;
8499         } while (!db_ready);
8500
8501         return 0;
8502 }
8503
8504 /**
8505  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8506  * @phba: Pointer to HBA context object.
8507  * @mboxq: Pointer to mailbox object.
8508  *
8509  * The function posts a mailbox to the port.  The mailbox is expected
8510  * to be comletely filled in and ready for the port to operate on it.
8511  * This routine executes a synchronous completion operation on the
8512  * mailbox by polling for its completion.
8513  *
8514  * The caller must not be holding any locks when calling this routine.
8515  *
8516  * Returns:
8517  *      MBX_SUCCESS - mailbox posted successfully
8518  *      Any of the MBX error values.
8519  **/
8520 static int
8521 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8522 {
8523         int rc = MBX_SUCCESS;
8524         unsigned long iflag;
8525         uint32_t mcqe_status;
8526         uint32_t mbx_cmnd;
8527         struct lpfc_sli *psli = &phba->sli;
8528         struct lpfc_mqe *mb = &mboxq->u.mqe;
8529         struct lpfc_bmbx_create *mbox_rgn;
8530         struct dma_address *dma_address;
8531
8532         /*
8533          * Only one mailbox can be active to the bootstrap mailbox region
8534          * at a time and there is no queueing provided.
8535          */
8536         spin_lock_irqsave(&phba->hbalock, iflag);
8537         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8538                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8539                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8540                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
8541                                 "cannot issue Data: x%x x%x\n",
8542                                 mboxq->vport ? mboxq->vport->vpi : 0,
8543                                 mboxq->u.mb.mbxCommand,
8544                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8545                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8546                                 psli->sli_flag, MBX_POLL);
8547                 return MBXERR_ERROR;
8548         }
8549         /* The server grabs the token and owns it until release */
8550         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8551         phba->sli.mbox_active = mboxq;
8552         spin_unlock_irqrestore(&phba->hbalock, iflag);
8553
8554         /* wait for bootstrap mbox register for readyness */
8555         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8556         if (rc)
8557                 goto exit;
8558         /*
8559          * Initialize the bootstrap memory region to avoid stale data areas
8560          * in the mailbox post.  Then copy the caller's mailbox contents to
8561          * the bmbx mailbox region.
8562          */
8563         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8564         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8565         lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8566                                sizeof(struct lpfc_mqe));
8567
8568         /* Post the high mailbox dma address to the port and wait for ready. */
8569         dma_address = &phba->sli4_hba.bmbx.dma_address;
8570         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8571
8572         /* wait for bootstrap mbox register for hi-address write done */
8573         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8574         if (rc)
8575                 goto exit;
8576
8577         /* Post the low mailbox dma address to the port. */
8578         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
8579
8580         /* wait for bootstrap mbox register for low address write done */
8581         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8582         if (rc)
8583                 goto exit;
8584
8585         /*
8586          * Read the CQ to ensure the mailbox has completed.
8587          * If so, update the mailbox status so that the upper layers
8588          * can complete the request normally.
8589          */
8590         lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8591                                sizeof(struct lpfc_mqe));
8592         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8593         lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8594                                sizeof(struct lpfc_mcqe));
8595         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
8596         /*
8597          * When the CQE status indicates a failure and the mailbox status
8598          * indicates success then copy the CQE status into the mailbox status
8599          * (and prefix it with x4000).
8600          */
8601         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
8602                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8603                         bf_set(lpfc_mqe_status, mb,
8604                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
8605                 rc = MBXERR_ERROR;
8606         } else
8607                 lpfc_sli4_swap_str(phba, mboxq);
8608
8609         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8610                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
8611                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8612                         " x%x x%x CQ: x%x x%x x%x x%x\n",
8613                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8614                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8615                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8616                         bf_get(lpfc_mqe_status, mb),
8617                         mb->un.mb_words[0], mb->un.mb_words[1],
8618                         mb->un.mb_words[2], mb->un.mb_words[3],
8619                         mb->un.mb_words[4], mb->un.mb_words[5],
8620                         mb->un.mb_words[6], mb->un.mb_words[7],
8621                         mb->un.mb_words[8], mb->un.mb_words[9],
8622                         mb->un.mb_words[10], mb->un.mb_words[11],
8623                         mb->un.mb_words[12], mboxq->mcqe.word0,
8624                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
8625                         mboxq->mcqe.trailer);
8626 exit:
8627         /* We are holding the token, no needed for lock when release */
8628         spin_lock_irqsave(&phba->hbalock, iflag);
8629         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8630         phba->sli.mbox_active = NULL;
8631         spin_unlock_irqrestore(&phba->hbalock, iflag);
8632         return rc;
8633 }
8634
8635 /**
8636  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8637  * @phba: Pointer to HBA context object.
8638  * @pmbox: Pointer to mailbox object.
8639  * @flag: Flag indicating how the mailbox need to be processed.
8640  *
8641  * This function is called by discovery code and HBA management code to submit
8642  * a mailbox command to firmware with SLI-4 interface spec.
8643  *
8644  * Return codes the caller owns the mailbox command after the return of the
8645  * function.
8646  **/
8647 static int
8648 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8649                        uint32_t flag)
8650 {
8651         struct lpfc_sli *psli = &phba->sli;
8652         unsigned long iflags;
8653         int rc;
8654
8655         /* dump from issue mailbox command if setup */
8656         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8657
8658         rc = lpfc_mbox_dev_check(phba);
8659         if (unlikely(rc)) {
8660                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8661                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8662                                 "cannot issue Data: x%x x%x\n",
8663                                 mboxq->vport ? mboxq->vport->vpi : 0,
8664                                 mboxq->u.mb.mbxCommand,
8665                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8666                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8667                                 psli->sli_flag, flag);
8668                 goto out_not_finished;
8669         }
8670
8671         /* Detect polling mode and jump to a handler */
8672         if (!phba->sli4_hba.intr_enable) {
8673                 if (flag == MBX_POLL)
8674                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8675                 else
8676                         rc = -EIO;
8677                 if (rc != MBX_SUCCESS)
8678                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8679                                         "(%d):2541 Mailbox command x%x "
8680                                         "(x%x/x%x) failure: "
8681                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8682                                         "Data: x%x x%x\n,",
8683                                         mboxq->vport ? mboxq->vport->vpi : 0,
8684                                         mboxq->u.mb.mbxCommand,
8685                                         lpfc_sli_config_mbox_subsys_get(phba,
8686                                                                         mboxq),
8687                                         lpfc_sli_config_mbox_opcode_get(phba,
8688                                                                         mboxq),
8689                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8690                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8691                                         bf_get(lpfc_mcqe_ext_status,
8692                                                &mboxq->mcqe),
8693                                         psli->sli_flag, flag);
8694                 return rc;
8695         } else if (flag == MBX_POLL) {
8696                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8697                                 "(%d):2542 Try to issue mailbox command "
8698                                 "x%x (x%x/x%x) synchronously ahead of async "
8699                                 "mailbox command queue: x%x x%x\n",
8700                                 mboxq->vport ? mboxq->vport->vpi : 0,
8701                                 mboxq->u.mb.mbxCommand,
8702                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8703                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8704                                 psli->sli_flag, flag);
8705                 /* Try to block the asynchronous mailbox posting */
8706                 rc = lpfc_sli4_async_mbox_block(phba);
8707                 if (!rc) {
8708                         /* Successfully blocked, now issue sync mbox cmd */
8709                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8710                         if (rc != MBX_SUCCESS)
8711                                 lpfc_printf_log(phba, KERN_WARNING,
8712                                         LOG_MBOX | LOG_SLI,
8713                                         "(%d):2597 Sync Mailbox command "
8714                                         "x%x (x%x/x%x) failure: "
8715                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8716                                         "Data: x%x x%x\n,",
8717                                         mboxq->vport ? mboxq->vport->vpi : 0,
8718                                         mboxq->u.mb.mbxCommand,
8719                                         lpfc_sli_config_mbox_subsys_get(phba,
8720                                                                         mboxq),
8721                                         lpfc_sli_config_mbox_opcode_get(phba,
8722                                                                         mboxq),
8723                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8724                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8725                                         bf_get(lpfc_mcqe_ext_status,
8726                                                &mboxq->mcqe),
8727                                         psli->sli_flag, flag);
8728                         /* Unblock the async mailbox posting afterward */
8729                         lpfc_sli4_async_mbox_unblock(phba);
8730                 }
8731                 return rc;
8732         }
8733
8734         /* Now, interrupt mode asynchrous mailbox command */
8735         rc = lpfc_mbox_cmd_check(phba, mboxq);
8736         if (rc) {
8737                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8738                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
8739                                 "cannot issue Data: x%x x%x\n",
8740                                 mboxq->vport ? mboxq->vport->vpi : 0,
8741                                 mboxq->u.mb.mbxCommand,
8742                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8743                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8744                                 psli->sli_flag, flag);
8745                 goto out_not_finished;
8746         }
8747
8748         /* Put the mailbox command to the driver internal FIFO */
8749         psli->slistat.mbox_busy++;
8750         spin_lock_irqsave(&phba->hbalock, iflags);
8751         lpfc_mbox_put(phba, mboxq);
8752         spin_unlock_irqrestore(&phba->hbalock, iflags);
8753         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8754                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
8755                         "x%x (x%x/x%x) x%x x%x x%x\n",
8756                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8757                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8758                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8759                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8760                         phba->pport->port_state,
8761                         psli->sli_flag, MBX_NOWAIT);
8762         /* Wake up worker thread to transport mailbox command from head */
8763         lpfc_worker_wake_up(phba);
8764
8765         return MBX_BUSY;
8766
8767 out_not_finished:
8768         return MBX_NOT_FINISHED;
8769 }
8770
8771 /**
8772  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8773  * @phba: Pointer to HBA context object.
8774  *
8775  * This function is called by worker thread to send a mailbox command to
8776  * SLI4 HBA firmware.
8777  *
8778  **/
8779 int
8780 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8781 {
8782         struct lpfc_sli *psli = &phba->sli;
8783         LPFC_MBOXQ_t *mboxq;
8784         int rc = MBX_SUCCESS;
8785         unsigned long iflags;
8786         struct lpfc_mqe *mqe;
8787         uint32_t mbx_cmnd;
8788
8789         /* Check interrupt mode before post async mailbox command */
8790         if (unlikely(!phba->sli4_hba.intr_enable))
8791                 return MBX_NOT_FINISHED;
8792
8793         /* Check for mailbox command service token */
8794         spin_lock_irqsave(&phba->hbalock, iflags);
8795         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8796                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8797                 return MBX_NOT_FINISHED;
8798         }
8799         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8800                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8801                 return MBX_NOT_FINISHED;
8802         }
8803         if (unlikely(phba->sli.mbox_active)) {
8804                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8805                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8806                                 "0384 There is pending active mailbox cmd\n");
8807                 return MBX_NOT_FINISHED;
8808         }
8809         /* Take the mailbox command service token */
8810         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8811
8812         /* Get the next mailbox command from head of queue */
8813         mboxq = lpfc_mbox_get(phba);
8814
8815         /* If no more mailbox command waiting for post, we're done */
8816         if (!mboxq) {
8817                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8818                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8819                 return MBX_SUCCESS;
8820         }
8821         phba->sli.mbox_active = mboxq;
8822         spin_unlock_irqrestore(&phba->hbalock, iflags);
8823
8824         /* Check device readiness for posting mailbox command */
8825         rc = lpfc_mbox_dev_check(phba);
8826         if (unlikely(rc))
8827                 /* Driver clean routine will clean up pending mailbox */
8828                 goto out_not_finished;
8829
8830         /* Prepare the mbox command to be posted */
8831         mqe = &mboxq->u.mqe;
8832         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8833
8834         /* Start timer for the mbox_tmo and log some mailbox post messages */
8835         mod_timer(&psli->mbox_tmo, (jiffies +
8836                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8837
8838         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8839                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8840                         "x%x x%x\n",
8841                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8842                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8843                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8844                         phba->pport->port_state, psli->sli_flag);
8845
8846         if (mbx_cmnd != MBX_HEARTBEAT) {
8847                 if (mboxq->vport) {
8848                         lpfc_debugfs_disc_trc(mboxq->vport,
8849                                 LPFC_DISC_TRC_MBOX_VPORT,
8850                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8851                                 mbx_cmnd, mqe->un.mb_words[0],
8852                                 mqe->un.mb_words[1]);
8853                 } else {
8854                         lpfc_debugfs_disc_trc(phba->pport,
8855                                 LPFC_DISC_TRC_MBOX,
8856                                 "MBOX Send: cmd:x%x mb:x%x x%x",
8857                                 mbx_cmnd, mqe->un.mb_words[0],
8858                                 mqe->un.mb_words[1]);
8859                 }
8860         }
8861         psli->slistat.mbox_cmd++;
8862
8863         /* Post the mailbox command to the port */
8864         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8865         if (rc != MBX_SUCCESS) {
8866                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8867                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
8868                                 "cannot issue Data: x%x x%x\n",
8869                                 mboxq->vport ? mboxq->vport->vpi : 0,
8870                                 mboxq->u.mb.mbxCommand,
8871                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8872                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8873                                 psli->sli_flag, MBX_NOWAIT);
8874                 goto out_not_finished;
8875         }
8876
8877         return rc;
8878
8879 out_not_finished:
8880         spin_lock_irqsave(&phba->hbalock, iflags);
8881         if (phba->sli.mbox_active) {
8882                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8883                 __lpfc_mbox_cmpl_put(phba, mboxq);
8884                 /* Release the token */
8885                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8886                 phba->sli.mbox_active = NULL;
8887         }
8888         spin_unlock_irqrestore(&phba->hbalock, iflags);
8889
8890         return MBX_NOT_FINISHED;
8891 }
8892
8893 /**
8894  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8895  * @phba: Pointer to HBA context object.
8896  * @pmbox: Pointer to mailbox object.
8897  * @flag: Flag indicating how the mailbox need to be processed.
8898  *
8899  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8900  * the API jump table function pointer from the lpfc_hba struct.
8901  *
8902  * Return codes the caller owns the mailbox command after the return of the
8903  * function.
8904  **/
8905 int
8906 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8907 {
8908         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8909 }
8910
8911 /**
8912  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8913  * @phba: The hba struct for which this call is being executed.
8914  * @dev_grp: The HBA PCI-Device group number.
8915  *
8916  * This routine sets up the mbox interface API function jump table in @phba
8917  * struct.
8918  * Returns: 0 - success, -ENODEV - failure.
8919  **/
8920 int
8921 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8922 {
8923
8924         switch (dev_grp) {
8925         case LPFC_PCI_DEV_LP:
8926                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8927                 phba->lpfc_sli_handle_slow_ring_event =
8928                                 lpfc_sli_handle_slow_ring_event_s3;
8929                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8930                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8931                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8932                 break;
8933         case LPFC_PCI_DEV_OC:
8934                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8935                 phba->lpfc_sli_handle_slow_ring_event =
8936                                 lpfc_sli_handle_slow_ring_event_s4;
8937                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8938                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8939                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8940                 break;
8941         default:
8942                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8943                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
8944                                 dev_grp);
8945                 return -ENODEV;
8946                 break;
8947         }
8948         return 0;
8949 }
8950
8951 /**
8952  * __lpfc_sli_ringtx_put - Add an iocb to the txq
8953  * @phba: Pointer to HBA context object.
8954  * @pring: Pointer to driver SLI ring object.
8955  * @piocb: Pointer to address of newly added command iocb.
8956  *
8957  * This function is called with hbalock held to add a command
8958  * iocb to the txq when SLI layer cannot submit the command iocb
8959  * to the ring.
8960  **/
8961 void
8962 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8963                     struct lpfc_iocbq *piocb)
8964 {
8965         lockdep_assert_held(&phba->hbalock);
8966         /* Insert the caller's iocb in the txq tail for later processing. */
8967         list_add_tail(&piocb->list, &pring->txq);
8968 }
8969
8970 /**
8971  * lpfc_sli_next_iocb - Get the next iocb in the txq
8972  * @phba: Pointer to HBA context object.
8973  * @pring: Pointer to driver SLI ring object.
8974  * @piocb: Pointer to address of newly added command iocb.
8975  *
8976  * This function is called with hbalock held before a new
8977  * iocb is submitted to the firmware. This function checks
8978  * txq to flush the iocbs in txq to Firmware before
8979  * submitting new iocbs to the Firmware.
8980  * If there are iocbs in the txq which need to be submitted
8981  * to firmware, lpfc_sli_next_iocb returns the first element
8982  * of the txq after dequeuing it from txq.
8983  * If there is no iocb in the txq then the function will return
8984  * *piocb and *piocb is set to NULL. Caller needs to check
8985  * *piocb to find if there are more commands in the txq.
8986  **/
8987 static struct lpfc_iocbq *
8988 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8989                    struct lpfc_iocbq **piocb)
8990 {
8991         struct lpfc_iocbq * nextiocb;
8992
8993         lockdep_assert_held(&phba->hbalock);
8994
8995         nextiocb = lpfc_sli_ringtx_get(phba, pring);
8996         if (!nextiocb) {
8997                 nextiocb = *piocb;
8998                 *piocb = NULL;
8999         }
9000
9001         return nextiocb;
9002 }
9003
9004 /**
9005  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
9006  * @phba: Pointer to HBA context object.
9007  * @ring_number: SLI ring number to issue iocb on.
9008  * @piocb: Pointer to command iocb.
9009  * @flag: Flag indicating if this command can be put into txq.
9010  *
9011  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9012  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9013  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9014  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9015  * this function allows only iocbs for posting buffers. This function finds
9016  * next available slot in the command ring and posts the command to the
9017  * available slot and writes the port attention register to request HBA start
9018  * processing new iocb. If there is no slot available in the ring and
9019  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9020  * the function returns IOCB_BUSY.
9021  *
9022  * This function is called with hbalock held. The function will return success
9023  * after it successfully submit the iocb to firmware or after adding to the
9024  * txq.
9025  **/
9026 static int
9027 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
9028                     struct lpfc_iocbq *piocb, uint32_t flag)
9029 {
9030         struct lpfc_iocbq *nextiocb;
9031         IOCB_t *iocb;
9032         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
9033
9034         lockdep_assert_held(&phba->hbalock);
9035
9036         if (piocb->iocb_cmpl && (!piocb->vport) &&
9037            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9038            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9039                 lpfc_printf_log(phba, KERN_ERR,
9040                                 LOG_SLI | LOG_VPORT,
9041                                 "1807 IOCB x%x failed. No vport\n",
9042                                 piocb->iocb.ulpCommand);
9043                 dump_stack();
9044                 return IOCB_ERROR;
9045         }
9046
9047
9048         /* If the PCI channel is in offline state, do not post iocbs. */
9049         if (unlikely(pci_channel_offline(phba->pcidev)))
9050                 return IOCB_ERROR;
9051
9052         /* If HBA has a deferred error attention, fail the iocb. */
9053         if (unlikely(phba->hba_flag & DEFER_ERATT))
9054                 return IOCB_ERROR;
9055
9056         /*
9057          * We should never get an IOCB if we are in a < LINK_DOWN state
9058          */
9059         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9060                 return IOCB_ERROR;
9061
9062         /*
9063          * Check to see if we are blocking IOCB processing because of a
9064          * outstanding event.
9065          */
9066         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
9067                 goto iocb_busy;
9068
9069         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
9070                 /*
9071                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
9072                  * can be issued if the link is not up.
9073                  */
9074                 switch (piocb->iocb.ulpCommand) {
9075                 case CMD_GEN_REQUEST64_CR:
9076                 case CMD_GEN_REQUEST64_CX:
9077                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9078                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
9079                                         FC_RCTL_DD_UNSOL_CMD) ||
9080                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9081                                         MENLO_TRANSPORT_TYPE))
9082
9083                                 goto iocb_busy;
9084                         break;
9085                 case CMD_QUE_RING_BUF_CN:
9086                 case CMD_QUE_RING_BUF64_CN:
9087                         /*
9088                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9089                          * completion, iocb_cmpl MUST be 0.
9090                          */
9091                         if (piocb->iocb_cmpl)
9092                                 piocb->iocb_cmpl = NULL;
9093                         /*FALLTHROUGH*/
9094                 case CMD_CREATE_XRI_CR:
9095                 case CMD_CLOSE_XRI_CN:
9096                 case CMD_CLOSE_XRI_CX:
9097                         break;
9098                 default:
9099                         goto iocb_busy;
9100                 }
9101
9102         /*
9103          * For FCP commands, we must be in a state where we can process link
9104          * attention events.
9105          */
9106         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
9107                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
9108                 goto iocb_busy;
9109         }
9110
9111         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9112                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9113                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9114
9115         if (iocb)
9116                 lpfc_sli_update_ring(phba, pring);
9117         else
9118                 lpfc_sli_update_full_ring(phba, pring);
9119
9120         if (!piocb)
9121                 return IOCB_SUCCESS;
9122
9123         goto out_busy;
9124
9125  iocb_busy:
9126         pring->stats.iocb_cmd_delay++;
9127
9128  out_busy:
9129
9130         if (!(flag & SLI_IOCB_RET_IOCB)) {
9131                 __lpfc_sli_ringtx_put(phba, pring, piocb);
9132                 return IOCB_SUCCESS;
9133         }
9134
9135         return IOCB_BUSY;
9136 }
9137
9138 /**
9139  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9140  * @phba: Pointer to HBA context object.
9141  * @piocb: Pointer to command iocb.
9142  * @sglq: Pointer to the scatter gather queue object.
9143  *
9144  * This routine converts the bpl or bde that is in the IOCB
9145  * to a sgl list for the sli4 hardware. The physical address
9146  * of the bpl/bde is converted back to a virtual address.
9147  * If the IOCB contains a BPL then the list of BDE's is
9148  * converted to sli4_sge's. If the IOCB contains a single
9149  * BDE then it is converted to a single sli_sge.
9150  * The IOCB is still in cpu endianess so the contents of
9151  * the bpl can be used without byte swapping.
9152  *
9153  * Returns valid XRI = Success, NO_XRI = Failure.
9154 **/
9155 static uint16_t
9156 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9157                 struct lpfc_sglq *sglq)
9158 {
9159         uint16_t xritag = NO_XRI;
9160         struct ulp_bde64 *bpl = NULL;
9161         struct ulp_bde64 bde;
9162         struct sli4_sge *sgl  = NULL;
9163         struct lpfc_dmabuf *dmabuf;
9164         IOCB_t *icmd;
9165         int numBdes = 0;
9166         int i = 0;
9167         uint32_t offset = 0; /* accumulated offset in the sg request list */
9168         int inbound = 0; /* number of sg reply entries inbound from firmware */
9169
9170         if (!piocbq || !sglq)
9171                 return xritag;
9172
9173         sgl  = (struct sli4_sge *)sglq->sgl;
9174         icmd = &piocbq->iocb;
9175         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9176                 return sglq->sli4_xritag;
9177         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9178                 numBdes = icmd->un.genreq64.bdl.bdeSize /
9179                                 sizeof(struct ulp_bde64);
9180                 /* The addrHigh and addrLow fields within the IOCB
9181                  * have not been byteswapped yet so there is no
9182                  * need to swap them back.
9183                  */
9184                 if (piocbq->context3)
9185                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9186                 else
9187                         return xritag;
9188
9189                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
9190                 if (!bpl)
9191                         return xritag;
9192
9193                 for (i = 0; i < numBdes; i++) {
9194                         /* Should already be byte swapped. */
9195                         sgl->addr_hi = bpl->addrHigh;
9196                         sgl->addr_lo = bpl->addrLow;
9197
9198                         sgl->word2 = le32_to_cpu(sgl->word2);
9199                         if ((i+1) == numBdes)
9200                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
9201                         else
9202                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
9203                         /* swap the size field back to the cpu so we
9204                          * can assign it to the sgl.
9205                          */
9206                         bde.tus.w = le32_to_cpu(bpl->tus.w);
9207                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
9208                         /* The offsets in the sgl need to be accumulated
9209                          * separately for the request and reply lists.
9210                          * The request is always first, the reply follows.
9211                          */
9212                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9213                                 /* add up the reply sg entries */
9214                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9215                                         inbound++;
9216                                 /* first inbound? reset the offset */
9217                                 if (inbound == 1)
9218                                         offset = 0;
9219                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
9220                                 bf_set(lpfc_sli4_sge_type, sgl,
9221                                         LPFC_SGE_TYPE_DATA);
9222                                 offset += bde.tus.f.bdeSize;
9223                         }
9224                         sgl->word2 = cpu_to_le32(sgl->word2);
9225                         bpl++;
9226                         sgl++;
9227                 }
9228         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9229                         /* The addrHigh and addrLow fields of the BDE have not
9230                          * been byteswapped yet so they need to be swapped
9231                          * before putting them in the sgl.
9232                          */
9233                         sgl->addr_hi =
9234                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9235                         sgl->addr_lo =
9236                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
9237                         sgl->word2 = le32_to_cpu(sgl->word2);
9238                         bf_set(lpfc_sli4_sge_last, sgl, 1);
9239                         sgl->word2 = cpu_to_le32(sgl->word2);
9240                         sgl->sge_len =
9241                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
9242         }
9243         return sglq->sli4_xritag;
9244 }
9245
9246 /**
9247  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9248  * @phba: Pointer to HBA context object.
9249  * @piocb: Pointer to command iocb.
9250  * @wqe: Pointer to the work queue entry.
9251  *
9252  * This routine converts the iocb command to its Work Queue Entry
9253  * equivalent. The wqe pointer should not have any fields set when
9254  * this routine is called because it will memcpy over them.
9255  * This routine does not set the CQ_ID or the WQEC bits in the
9256  * wqe.
9257  *
9258  * Returns: 0 = Success, IOCB_ERROR = Failure.
9259  **/
9260 static int
9261 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
9262                 union lpfc_wqe128 *wqe)
9263 {
9264         uint32_t xmit_len = 0, total_len = 0;
9265         uint8_t ct = 0;
9266         uint32_t fip;
9267         uint32_t abort_tag;
9268         uint8_t command_type = ELS_COMMAND_NON_FIP;
9269         uint8_t cmnd;
9270         uint16_t xritag;
9271         uint16_t abrt_iotag;
9272         struct lpfc_iocbq *abrtiocbq;
9273         struct ulp_bde64 *bpl = NULL;
9274         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
9275         int numBdes, i;
9276         struct ulp_bde64 bde;
9277         struct lpfc_nodelist *ndlp;
9278         uint32_t *pcmd;
9279         uint32_t if_type;
9280
9281         fip = phba->hba_flag & HBA_FIP_SUPPORT;
9282         /* The fcp commands will set command type */
9283         if (iocbq->iocb_flag &  LPFC_IO_FCP)
9284                 command_type = FCP_COMMAND;
9285         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
9286                 command_type = ELS_COMMAND_FIP;
9287         else
9288                 command_type = ELS_COMMAND_NON_FIP;
9289
9290         if (phba->fcp_embed_io)
9291                 memset(wqe, 0, sizeof(union lpfc_wqe128));
9292         /* Some of the fields are in the right position already */
9293         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
9294         if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9295                 /* The ct field has moved so reset */
9296                 wqe->generic.wqe_com.word7 = 0;
9297                 wqe->generic.wqe_com.word10 = 0;
9298         }
9299
9300         abort_tag = (uint32_t) iocbq->iotag;
9301         xritag = iocbq->sli4_xritag;
9302         /* words0-2 bpl convert bde */
9303         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9304                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9305                                 sizeof(struct ulp_bde64);
9306                 bpl  = (struct ulp_bde64 *)
9307                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9308                 if (!bpl)
9309                         return IOCB_ERROR;
9310
9311                 /* Should already be byte swapped. */
9312                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
9313                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
9314                 /* swap the size field back to the cpu so we
9315                  * can assign it to the sgl.
9316                  */
9317                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
9318                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9319                 total_len = 0;
9320                 for (i = 0; i < numBdes; i++) {
9321                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
9322                         total_len += bde.tus.f.bdeSize;
9323                 }
9324         } else
9325                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
9326
9327         iocbq->iocb.ulpIoTag = iocbq->iotag;
9328         cmnd = iocbq->iocb.ulpCommand;
9329
9330         switch (iocbq->iocb.ulpCommand) {
9331         case CMD_ELS_REQUEST64_CR:
9332                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9333                         ndlp = iocbq->context_un.ndlp;
9334                 else
9335                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
9336                 if (!iocbq->iocb.ulpLe) {
9337                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9338                                 "2007 Only Limited Edition cmd Format"
9339                                 " supported 0x%x\n",
9340                                 iocbq->iocb.ulpCommand);
9341                         return IOCB_ERROR;
9342                 }
9343
9344                 wqe->els_req.payload_len = xmit_len;
9345                 /* Els_reguest64 has a TMO */
9346                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9347                         iocbq->iocb.ulpTimeout);
9348                 /* Need a VF for word 4 set the vf bit*/
9349                 bf_set(els_req64_vf, &wqe->els_req, 0);
9350                 /* And a VFID for word 12 */
9351                 bf_set(els_req64_vfid, &wqe->els_req, 0);
9352                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9353                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9354                        iocbq->iocb.ulpContext);
9355                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9356                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
9357                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
9358                 if (command_type == ELS_COMMAND_FIP)
9359                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9360                                         >> LPFC_FIP_ELS_ID_SHIFT);
9361                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9362                                         iocbq->context2)->virt);
9363                 if_type = bf_get(lpfc_sli_intf_if_type,
9364                                         &phba->sli4_hba.sli_intf);
9365                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9366                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
9367                                 *pcmd == ELS_CMD_SCR ||
9368                                 *pcmd == ELS_CMD_FDISC ||
9369                                 *pcmd == ELS_CMD_LOGO ||
9370                                 *pcmd == ELS_CMD_PLOGI)) {
9371                                 bf_set(els_req64_sp, &wqe->els_req, 1);
9372                                 bf_set(els_req64_sid, &wqe->els_req,
9373                                         iocbq->vport->fc_myDID);
9374                                 if ((*pcmd == ELS_CMD_FLOGI) &&
9375                                         !(phba->fc_topology ==
9376                                                 LPFC_TOPOLOGY_LOOP))
9377                                         bf_set(els_req64_sid, &wqe->els_req, 0);
9378                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9379                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9380                                         phba->vpi_ids[iocbq->vport->vpi]);
9381                         } else if (pcmd && iocbq->context1) {
9382                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9383                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9384                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9385                         }
9386                 }
9387                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9388                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9389                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9390                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9391                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9392                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9393                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9394                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
9395                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
9396                 break;
9397         case CMD_XMIT_SEQUENCE64_CX:
9398                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9399                        iocbq->iocb.un.ulpWord[3]);
9400                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
9401                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9402                 /* The entire sequence is transmitted for this IOCB */
9403                 xmit_len = total_len;
9404                 cmnd = CMD_XMIT_SEQUENCE64_CR;
9405                 if (phba->link_flag & LS_LOOPBACK_MODE)
9406                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
9407         case CMD_XMIT_SEQUENCE64_CR:
9408                 /* word3 iocb=io_tag32 wqe=reserved */
9409                 wqe->xmit_sequence.rsvd3 = 0;
9410                 /* word4 relative_offset memcpy */
9411                 /* word5 r_ctl/df_ctl memcpy */
9412                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9413                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9414                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9415                        LPFC_WQE_IOD_WRITE);
9416                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9417                        LPFC_WQE_LENLOC_WORD12);
9418                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
9419                 wqe->xmit_sequence.xmit_len = xmit_len;
9420                 command_type = OTHER_COMMAND;
9421                 break;
9422         case CMD_XMIT_BCAST64_CN:
9423                 /* word3 iocb=iotag32 wqe=seq_payload_len */
9424                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
9425                 /* word4 iocb=rsvd wqe=rsvd */
9426                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9427                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
9428                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
9429                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9430                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9431                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9432                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9433                        LPFC_WQE_LENLOC_WORD3);
9434                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
9435                 break;
9436         case CMD_FCP_IWRITE64_CR:
9437                 command_type = FCP_COMMAND_DATA_OUT;
9438                 /* word3 iocb=iotag wqe=payload_offset_len */
9439                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9440                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9441                        xmit_len + sizeof(struct fcp_rsp));
9442                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9443                        0);
9444                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9445                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9446                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9447                        iocbq->iocb.ulpFCP2Rcvy);
9448                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9449                 /* Always open the exchange */
9450                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9451                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9452                        LPFC_WQE_LENLOC_WORD4);
9453                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
9454                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
9455                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9456                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
9457                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9458                         if (iocbq->priority) {
9459                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9460                                        (iocbq->priority << 1));
9461                         } else {
9462                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9463                                        (phba->cfg_XLanePriority << 1));
9464                         }
9465                 }
9466                 /* Note, word 10 is already initialized to 0 */
9467
9468                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9469                 if (phba->cfg_enable_pbde)
9470                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9471                 else
9472                         bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9473
9474                 if (phba->fcp_embed_io) {
9475                         struct lpfc_scsi_buf *lpfc_cmd;
9476                         struct sli4_sge *sgl;
9477                         struct fcp_cmnd *fcp_cmnd;
9478                         uint32_t *ptr;
9479
9480                         /* 128 byte wqe support here */
9481
9482                         lpfc_cmd = iocbq->context1;
9483                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9484                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9485
9486                         /* Word 0-2 - FCP_CMND */
9487                         wqe->generic.bde.tus.f.bdeFlags =
9488                                 BUFF_TYPE_BDE_IMMED;
9489                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9490                         wqe->generic.bde.addrHigh = 0;
9491                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9492
9493                         bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9494                         bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
9495
9496                         /* Word 22-29  FCP CMND Payload */
9497                         ptr = &wqe->words[22];
9498                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9499                 }
9500                 break;
9501         case CMD_FCP_IREAD64_CR:
9502                 /* word3 iocb=iotag wqe=payload_offset_len */
9503                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9504                 bf_set(payload_offset_len, &wqe->fcp_iread,
9505                        xmit_len + sizeof(struct fcp_rsp));
9506                 bf_set(cmd_buff_len, &wqe->fcp_iread,
9507                        0);
9508                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9509                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9510                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9511                        iocbq->iocb.ulpFCP2Rcvy);
9512                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
9513                 /* Always open the exchange */
9514                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9515                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9516                        LPFC_WQE_LENLOC_WORD4);
9517                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
9518                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
9519                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9520                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
9521                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9522                         if (iocbq->priority) {
9523                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9524                                        (iocbq->priority << 1));
9525                         } else {
9526                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9527                                        (phba->cfg_XLanePriority << 1));
9528                         }
9529                 }
9530                 /* Note, word 10 is already initialized to 0 */
9531
9532                 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9533                 if (phba->cfg_enable_pbde)
9534                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9535                 else
9536                         bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9537
9538                 if (phba->fcp_embed_io) {
9539                         struct lpfc_scsi_buf *lpfc_cmd;
9540                         struct sli4_sge *sgl;
9541                         struct fcp_cmnd *fcp_cmnd;
9542                         uint32_t *ptr;
9543
9544                         /* 128 byte wqe support here */
9545
9546                         lpfc_cmd = iocbq->context1;
9547                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9548                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9549
9550                         /* Word 0-2 - FCP_CMND */
9551                         wqe->generic.bde.tus.f.bdeFlags =
9552                                 BUFF_TYPE_BDE_IMMED;
9553                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9554                         wqe->generic.bde.addrHigh = 0;
9555                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9556
9557                         bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9558                         bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
9559
9560                         /* Word 22-29  FCP CMND Payload */
9561                         ptr = &wqe->words[22];
9562                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9563                 }
9564                 break;
9565         case CMD_FCP_ICMND64_CR:
9566                 /* word3 iocb=iotag wqe=payload_offset_len */
9567                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9568                 bf_set(payload_offset_len, &wqe->fcp_icmd,
9569                        xmit_len + sizeof(struct fcp_rsp));
9570                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9571                        0);
9572                 /* word3 iocb=IO_TAG wqe=reserved */
9573                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
9574                 /* Always open the exchange */
9575                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9576                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9577                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9578                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9579                        LPFC_WQE_LENLOC_NONE);
9580                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9581                        iocbq->iocb.ulpFCP2Rcvy);
9582                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9583                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
9584                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9585                         if (iocbq->priority) {
9586                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9587                                        (iocbq->priority << 1));
9588                         } else {
9589                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9590                                        (phba->cfg_XLanePriority << 1));
9591                         }
9592                 }
9593                 /* Note, word 10 is already initialized to 0 */
9594
9595                 if (phba->fcp_embed_io) {
9596                         struct lpfc_scsi_buf *lpfc_cmd;
9597                         struct sli4_sge *sgl;
9598                         struct fcp_cmnd *fcp_cmnd;
9599                         uint32_t *ptr;
9600
9601                         /* 128 byte wqe support here */
9602
9603                         lpfc_cmd = iocbq->context1;
9604                         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
9605                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
9606
9607                         /* Word 0-2 - FCP_CMND */
9608                         wqe->generic.bde.tus.f.bdeFlags =
9609                                 BUFF_TYPE_BDE_IMMED;
9610                         wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9611                         wqe->generic.bde.addrHigh = 0;
9612                         wqe->generic.bde.addrLow =  88;  /* Word 22 */
9613
9614                         bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9615                         bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
9616
9617                         /* Word 22-29  FCP CMND Payload */
9618                         ptr = &wqe->words[22];
9619                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9620                 }
9621                 break;
9622         case CMD_GEN_REQUEST64_CR:
9623                 /* For this command calculate the xmit length of the
9624                  * request bde.
9625                  */
9626                 xmit_len = 0;
9627                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9628                         sizeof(struct ulp_bde64);
9629                 for (i = 0; i < numBdes; i++) {
9630                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9631                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9632                                 break;
9633                         xmit_len += bde.tus.f.bdeSize;
9634                 }
9635                 /* word3 iocb=IO_TAG wqe=request_payload_len */
9636                 wqe->gen_req.request_payload_len = xmit_len;
9637                 /* word4 iocb=parameter wqe=relative_offset memcpy */
9638                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
9639                 /* word6 context tag copied in memcpy */
9640                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
9641                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9642                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9643                                 "2015 Invalid CT %x command 0x%x\n",
9644                                 ct, iocbq->iocb.ulpCommand);
9645                         return IOCB_ERROR;
9646                 }
9647                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9648                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9649                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9650                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9651                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9652                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9653                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9654                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
9655                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
9656                 command_type = OTHER_COMMAND;
9657                 break;
9658         case CMD_XMIT_ELS_RSP64_CX:
9659                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9660                 /* words0-2 BDE memcpy */
9661                 /* word3 iocb=iotag32 wqe=response_payload_len */
9662                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
9663                 /* word4 */
9664                 wqe->xmit_els_rsp.word4 = 0;
9665                 /* word5 iocb=rsvd wge=did */
9666                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
9667                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
9668
9669                 if_type = bf_get(lpfc_sli_intf_if_type,
9670                                         &phba->sli4_hba.sli_intf);
9671                 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9672                         if (iocbq->vport->fc_flag & FC_PT2PT) {
9673                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9674                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9675                                         iocbq->vport->fc_myDID);
9676                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
9677                                         bf_set(wqe_els_did,
9678                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
9679                                 }
9680                         }
9681                 }
9682                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9683                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9684                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9685                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
9686                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9687                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
9688                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9689                                phba->vpi_ids[iocbq->vport->vpi]);
9690                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9691                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9692                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9693                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9694                        LPFC_WQE_LENLOC_WORD3);
9695                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9696                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9697                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9698                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9699                                         iocbq->context2)->virt);
9700                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9701                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9702                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9703                                         iocbq->vport->fc_myDID);
9704                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9705                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9706                                         phba->vpi_ids[phba->pport->vpi]);
9707                 }
9708                 command_type = OTHER_COMMAND;
9709                 break;
9710         case CMD_CLOSE_XRI_CN:
9711         case CMD_ABORT_XRI_CN:
9712         case CMD_ABORT_XRI_CX:
9713                 /* words 0-2 memcpy should be 0 rserved */
9714                 /* port will send abts */
9715                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9716                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9717                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9718                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9719                 } else
9720                         fip = 0;
9721
9722                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9723                         /*
9724                          * The link is down, or the command was ELS_FIP
9725                          * so the fw does not need to send abts
9726                          * on the wire.
9727                          */
9728                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9729                 else
9730                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9731                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9732                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9733                 wqe->abort_cmd.rsrvd5 = 0;
9734                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9735                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9736                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9737                 /*
9738                  * The abort handler will send us CMD_ABORT_XRI_CN or
9739                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9740                  */
9741                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9742                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9743                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9744                        LPFC_WQE_LENLOC_NONE);
9745                 cmnd = CMD_ABORT_XRI_CX;
9746                 command_type = OTHER_COMMAND;
9747                 xritag = 0;
9748                 break;
9749         case CMD_XMIT_BLS_RSP64_CX:
9750                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9751                 /* As BLS ABTS RSP WQE is very different from other WQEs,
9752                  * we re-construct this WQE here based on information in
9753                  * iocbq from scratch.
9754                  */
9755                 memset(wqe, 0, sizeof(union lpfc_wqe));
9756                 /* OX_ID is invariable to who sent ABTS to CT exchange */
9757                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9758                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9759                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9760                     LPFC_ABTS_UNSOL_INT) {
9761                         /* ABTS sent by initiator to CT exchange, the
9762                          * RX_ID field will be filled with the newly
9763                          * allocated responder XRI.
9764                          */
9765                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9766                                iocbq->sli4_xritag);
9767                 } else {
9768                         /* ABTS sent by responder to CT exchange, the
9769                          * RX_ID field will be filled with the responder
9770                          * RX_ID from ABTS.
9771                          */
9772                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9773                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9774                 }
9775                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9776                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9777
9778                 /* Use CT=VPI */
9779                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9780                         ndlp->nlp_DID);
9781                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9782                         iocbq->iocb.ulpContext);
9783                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9784                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9785                         phba->vpi_ids[phba->pport->vpi]);
9786                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9787                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9788                        LPFC_WQE_LENLOC_NONE);
9789                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9790                 command_type = OTHER_COMMAND;
9791                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9792                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9793                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9794                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9795                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9796                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9797                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9798                 }
9799
9800                 break;
9801         case CMD_SEND_FRAME:
9802                 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9803                 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9804                 return 0;
9805         case CMD_XRI_ABORTED_CX:
9806         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9807         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9808         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9809         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9810         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9811         default:
9812                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9813                                 "2014 Invalid command 0x%x\n",
9814                                 iocbq->iocb.ulpCommand);
9815                 return IOCB_ERROR;
9816                 break;
9817         }
9818
9819         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9820                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9821         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9822                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9823         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9824                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9825         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9826                               LPFC_IO_DIF_INSERT);
9827         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9828         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9829         wqe->generic.wqe_com.abort_tag = abort_tag;
9830         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9831         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9832         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9833         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9834         return 0;
9835 }
9836
9837 /**
9838  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9839  * @phba: Pointer to HBA context object.
9840  * @ring_number: SLI ring number to issue iocb on.
9841  * @piocb: Pointer to command iocb.
9842  * @flag: Flag indicating if this command can be put into txq.
9843  *
9844  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9845  * an iocb command to an HBA with SLI-4 interface spec.
9846  *
9847  * This function is called with hbalock held. The function will return success
9848  * after it successfully submit the iocb to firmware or after adding to the
9849  * txq.
9850  **/
9851 static int
9852 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9853                          struct lpfc_iocbq *piocb, uint32_t flag)
9854 {
9855         struct lpfc_sglq *sglq;
9856         union lpfc_wqe128 wqe;
9857         struct lpfc_queue *wq;
9858         struct lpfc_sli_ring *pring;
9859
9860         /* Get the WQ */
9861         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9862             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9863                 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq;
9864         } else {
9865                 wq = phba->sli4_hba.els_wq;
9866         }
9867
9868         /* Get corresponding ring */
9869         pring = wq->pring;
9870
9871         /*
9872          * The WQE can be either 64 or 128 bytes,
9873          */
9874
9875         lockdep_assert_held(&phba->hbalock);
9876
9877         if (piocb->sli4_xritag == NO_XRI) {
9878                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9879                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9880                         sglq = NULL;
9881                 else {
9882                         if (!list_empty(&pring->txq)) {
9883                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
9884                                         __lpfc_sli_ringtx_put(phba,
9885                                                 pring, piocb);
9886                                         return IOCB_SUCCESS;
9887                                 } else {
9888                                         return IOCB_BUSY;
9889                                 }
9890                         } else {
9891                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9892                                 if (!sglq) {
9893                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
9894                                                 __lpfc_sli_ringtx_put(phba,
9895                                                                 pring,
9896                                                                 piocb);
9897                                                 return IOCB_SUCCESS;
9898                                         } else
9899                                                 return IOCB_BUSY;
9900                                 }
9901                         }
9902                 }
9903         } else if (piocb->iocb_flag &  LPFC_IO_FCP)
9904                 /* These IO's already have an XRI and a mapped sgl. */
9905                 sglq = NULL;
9906         else {
9907                 /*
9908                  * This is a continuation of a commandi,(CX) so this
9909                  * sglq is on the active list
9910                  */
9911                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9912                 if (!sglq)
9913                         return IOCB_ERROR;
9914         }
9915
9916         if (sglq) {
9917                 piocb->sli4_lxritag = sglq->sli4_lxritag;
9918                 piocb->sli4_xritag = sglq->sli4_xritag;
9919                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9920                         return IOCB_ERROR;
9921         }
9922
9923         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
9924                 return IOCB_ERROR;
9925
9926         if (lpfc_sli4_wq_put(wq, &wqe))
9927                 return IOCB_ERROR;
9928         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9929
9930         return 0;
9931 }
9932
9933 /**
9934  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9935  *
9936  * This routine wraps the actual lockless version for issusing IOCB function
9937  * pointer from the lpfc_hba struct.
9938  *
9939  * Return codes:
9940  * IOCB_ERROR - Error
9941  * IOCB_SUCCESS - Success
9942  * IOCB_BUSY - Busy
9943  **/
9944 int
9945 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9946                 struct lpfc_iocbq *piocb, uint32_t flag)
9947 {
9948         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9949 }
9950
9951 /**
9952  * lpfc_sli_api_table_setup - Set up sli api function jump table
9953  * @phba: The hba struct for which this call is being executed.
9954  * @dev_grp: The HBA PCI-Device group number.
9955  *
9956  * This routine sets up the SLI interface API function jump table in @phba
9957  * struct.
9958  * Returns: 0 - success, -ENODEV - failure.
9959  **/
9960 int
9961 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9962 {
9963
9964         switch (dev_grp) {
9965         case LPFC_PCI_DEV_LP:
9966                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9967                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9968                 break;
9969         case LPFC_PCI_DEV_OC:
9970                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9971                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9972                 break;
9973         default:
9974                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9975                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
9976                                 dev_grp);
9977                 return -ENODEV;
9978                 break;
9979         }
9980         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9981         return 0;
9982 }
9983
9984 /**
9985  * lpfc_sli4_calc_ring - Calculates which ring to use
9986  * @phba: Pointer to HBA context object.
9987  * @piocb: Pointer to command iocb.
9988  *
9989  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9990  * hba_wqidx, thus we need to calculate the corresponding ring.
9991  * Since ABORTS must go on the same WQ of the command they are
9992  * aborting, we use command's hba_wqidx.
9993  */
9994 struct lpfc_sli_ring *
9995 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9996 {
9997         struct lpfc_scsi_buf *lpfc_cmd;
9998
9999         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
10000                 if (unlikely(!phba->sli4_hba.hdwq))
10001                         return NULL;
10002                 /*
10003                  * for abort iocb hba_wqidx should already
10004                  * be setup based on what work queue we used.
10005                  */
10006                 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
10007                         lpfc_cmd = (struct lpfc_scsi_buf *)piocb->context1;
10008                         piocb->hba_wqidx = lpfc_cmd->hdwq;
10009                 }
10010                 return phba->sli4_hba.hdwq[piocb->hba_wqidx].fcp_wq->pring;
10011         } else {
10012                 if (unlikely(!phba->sli4_hba.els_wq))
10013                         return NULL;
10014                 piocb->hba_wqidx = 0;
10015                 return phba->sli4_hba.els_wq->pring;
10016         }
10017 }
10018
10019 /**
10020  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
10021  * @phba: Pointer to HBA context object.
10022  * @pring: Pointer to driver SLI ring object.
10023  * @piocb: Pointer to command iocb.
10024  * @flag: Flag indicating if this command can be put into txq.
10025  *
10026  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10027  * function. This function gets the hbalock and calls
10028  * __lpfc_sli_issue_iocb function and will return the error returned
10029  * by __lpfc_sli_issue_iocb function. This wrapper is used by
10030  * functions which do not hold hbalock.
10031  **/
10032 int
10033 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10034                     struct lpfc_iocbq *piocb, uint32_t flag)
10035 {
10036         struct lpfc_hba_eq_hdl *hba_eq_hdl;
10037         struct lpfc_sli_ring *pring;
10038         struct lpfc_queue *fpeq;
10039         struct lpfc_eqe *eqe;
10040         unsigned long iflags;
10041         int rc, idx;
10042
10043         if (phba->sli_rev == LPFC_SLI_REV4) {
10044                 pring = lpfc_sli4_calc_ring(phba, piocb);
10045                 if (unlikely(pring == NULL))
10046                         return IOCB_ERROR;
10047
10048                 spin_lock_irqsave(&pring->ring_lock, iflags);
10049                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10050                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10051
10052                 if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
10053                         idx = piocb->hba_wqidx;
10054                         hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
10055
10056                         if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
10057
10058                                 /* Get associated EQ with this index */
10059                                 fpeq = phba->sli4_hba.hdwq[idx].hba_eq;
10060
10061                                 /* Turn off interrupts from this EQ */
10062                                 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
10063
10064                                 /*
10065                                  * Process all the events on FCP EQ
10066                                  */
10067                                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10068                                         lpfc_sli4_hba_handle_eqe(phba,
10069                                                 eqe, idx);
10070                                         fpeq->EQ_processed++;
10071                                 }
10072
10073                                 /* Always clear and re-arm the EQ */
10074                                 phba->sli4_hba.sli4_eq_release(fpeq,
10075                                         LPFC_QUEUE_REARM);
10076                         }
10077                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
10078                 }
10079         } else {
10080                 /* For now, SLI2/3 will still use hbalock */
10081                 spin_lock_irqsave(&phba->hbalock, iflags);
10082                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10083                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10084         }
10085         return rc;
10086 }
10087
10088 /**
10089  * lpfc_extra_ring_setup - Extra ring setup function
10090  * @phba: Pointer to HBA context object.
10091  *
10092  * This function is called while driver attaches with the
10093  * HBA to setup the extra ring. The extra ring is used
10094  * only when driver needs to support target mode functionality
10095  * or IP over FC functionalities.
10096  *
10097  * This function is called with no lock held. SLI3 only.
10098  **/
10099 static int
10100 lpfc_extra_ring_setup( struct lpfc_hba *phba)
10101 {
10102         struct lpfc_sli *psli;
10103         struct lpfc_sli_ring *pring;
10104
10105         psli = &phba->sli;
10106
10107         /* Adjust cmd/rsp ring iocb entries more evenly */
10108
10109         /* Take some away from the FCP ring */
10110         pring = &psli->sli3_ring[LPFC_FCP_RING];
10111         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10112         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10113         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10114         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10115
10116         /* and give them to the extra ring */
10117         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
10118
10119         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10120         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10121         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10122         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10123
10124         /* Setup default profile for this ring */
10125         pring->iotag_max = 4096;
10126         pring->num_mask = 1;
10127         pring->prt[0].profile = 0;      /* Mask 0 */
10128         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10129         pring->prt[0].type = phba->cfg_multi_ring_type;
10130         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10131         return 0;
10132 }
10133
10134 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10135  * @phba: Pointer to HBA context object.
10136  * @iocbq: Pointer to iocb object.
10137  *
10138  * The async_event handler calls this routine when it receives
10139  * an ASYNC_STATUS_CN event from the port.  The port generates
10140  * this event when an Abort Sequence request to an rport fails
10141  * twice in succession.  The abort could be originated by the
10142  * driver or by the port.  The ABTS could have been for an ELS
10143  * or FCP IO.  The port only generates this event when an ABTS
10144  * fails to complete after one retry.
10145  */
10146 static void
10147 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10148                           struct lpfc_iocbq *iocbq)
10149 {
10150         struct lpfc_nodelist *ndlp = NULL;
10151         uint16_t rpi = 0, vpi = 0;
10152         struct lpfc_vport *vport = NULL;
10153
10154         /* The rpi in the ulpContext is vport-sensitive. */
10155         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10156         rpi = iocbq->iocb.ulpContext;
10157
10158         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10159                         "3092 Port generated ABTS async event "
10160                         "on vpi %d rpi %d status 0x%x\n",
10161                         vpi, rpi, iocbq->iocb.ulpStatus);
10162
10163         vport = lpfc_find_vport_by_vpid(phba, vpi);
10164         if (!vport)
10165                 goto err_exit;
10166         ndlp = lpfc_findnode_rpi(vport, rpi);
10167         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10168                 goto err_exit;
10169
10170         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10171                 lpfc_sli_abts_recover_port(vport, ndlp);
10172         return;
10173
10174  err_exit:
10175         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10176                         "3095 Event Context not found, no "
10177                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10178                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10179                         vpi, rpi);
10180 }
10181
10182 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10183  * @phba: pointer to HBA context object.
10184  * @ndlp: nodelist pointer for the impacted rport.
10185  * @axri: pointer to the wcqe containing the failed exchange.
10186  *
10187  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10188  * port.  The port generates this event when an abort exchange request to an
10189  * rport fails twice in succession with no reply.  The abort could be originated
10190  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
10191  */
10192 void
10193 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10194                            struct lpfc_nodelist *ndlp,
10195                            struct sli4_wcqe_xri_aborted *axri)
10196 {
10197         struct lpfc_vport *vport;
10198         uint32_t ext_status = 0;
10199
10200         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
10201                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10202                                 "3115 Node Context not found, driver "
10203                                 "ignoring abts err event\n");
10204                 return;
10205         }
10206
10207         vport = ndlp->vport;
10208         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10209                         "3116 Port generated FCP XRI ABORT event on "
10210                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
10211                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
10212                         bf_get(lpfc_wcqe_xa_xri, axri),
10213                         bf_get(lpfc_wcqe_xa_status, axri),
10214                         axri->parameter);
10215
10216         /*
10217          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
10218          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10219          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10220          */
10221         ext_status = axri->parameter & IOERR_PARAM_MASK;
10222         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10223             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
10224                 lpfc_sli_abts_recover_port(vport, ndlp);
10225 }
10226
10227 /**
10228  * lpfc_sli_async_event_handler - ASYNC iocb handler function
10229  * @phba: Pointer to HBA context object.
10230  * @pring: Pointer to driver SLI ring object.
10231  * @iocbq: Pointer to iocb object.
10232  *
10233  * This function is called by the slow ring event handler
10234  * function when there is an ASYNC event iocb in the ring.
10235  * This function is called with no lock held.
10236  * Currently this function handles only temperature related
10237  * ASYNC events. The function decodes the temperature sensor
10238  * event message and posts events for the management applications.
10239  **/
10240 static void
10241 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10242         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10243 {
10244         IOCB_t *icmd;
10245         uint16_t evt_code;
10246         struct temp_event temp_event_data;
10247         struct Scsi_Host *shost;
10248         uint32_t *iocb_w;
10249
10250         icmd = &iocbq->iocb;
10251         evt_code = icmd->un.asyncstat.evt_code;
10252
10253         switch (evt_code) {
10254         case ASYNC_TEMP_WARN:
10255         case ASYNC_TEMP_SAFE:
10256                 temp_event_data.data = (uint32_t) icmd->ulpContext;
10257                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10258                 if (evt_code == ASYNC_TEMP_WARN) {
10259                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10260                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10261                                 "0347 Adapter is very hot, please take "
10262                                 "corrective action. temperature : %d Celsius\n",
10263                                 (uint32_t) icmd->ulpContext);
10264                 } else {
10265                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
10266                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10267                                 "0340 Adapter temperature is OK now. "
10268                                 "temperature : %d Celsius\n",
10269                                 (uint32_t) icmd->ulpContext);
10270                 }
10271
10272                 /* Send temperature change event to applications */
10273                 shost = lpfc_shost_from_vport(phba->pport);
10274                 fc_host_post_vendor_event(shost, fc_get_event_number(),
10275                         sizeof(temp_event_data), (char *) &temp_event_data,
10276                         LPFC_NL_VENDOR_ID);
10277                 break;
10278         case ASYNC_STATUS_CN:
10279                 lpfc_sli_abts_err_handler(phba, iocbq);
10280                 break;
10281         default:
10282                 iocb_w = (uint32_t *) icmd;
10283                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10284                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
10285                         " evt_code 0x%x\n"
10286                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
10287                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
10288                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
10289                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
10290                         pring->ringno, icmd->un.asyncstat.evt_code,
10291                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10292                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10293                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10294                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10295
10296                 break;
10297         }
10298 }
10299
10300
10301 /**
10302  * lpfc_sli4_setup - SLI ring setup function
10303  * @phba: Pointer to HBA context object.
10304  *
10305  * lpfc_sli_setup sets up rings of the SLI interface with
10306  * number of iocbs per ring and iotags. This function is
10307  * called while driver attach to the HBA and before the
10308  * interrupts are enabled. So there is no need for locking.
10309  *
10310  * This function always returns 0.
10311  **/
10312 int
10313 lpfc_sli4_setup(struct lpfc_hba *phba)
10314 {
10315         struct lpfc_sli_ring *pring;
10316
10317         pring = phba->sli4_hba.els_wq->pring;
10318         pring->num_mask = LPFC_MAX_RING_MASK;
10319         pring->prt[0].profile = 0;      /* Mask 0 */
10320         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10321         pring->prt[0].type = FC_TYPE_ELS;
10322         pring->prt[0].lpfc_sli_rcv_unsol_event =
10323             lpfc_els_unsol_event;
10324         pring->prt[1].profile = 0;      /* Mask 1 */
10325         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10326         pring->prt[1].type = FC_TYPE_ELS;
10327         pring->prt[1].lpfc_sli_rcv_unsol_event =
10328             lpfc_els_unsol_event;
10329         pring->prt[2].profile = 0;      /* Mask 2 */
10330         /* NameServer Inquiry */
10331         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10332         /* NameServer */
10333         pring->prt[2].type = FC_TYPE_CT;
10334         pring->prt[2].lpfc_sli_rcv_unsol_event =
10335             lpfc_ct_unsol_event;
10336         pring->prt[3].profile = 0;      /* Mask 3 */
10337         /* NameServer response */
10338         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10339         /* NameServer */
10340         pring->prt[3].type = FC_TYPE_CT;
10341         pring->prt[3].lpfc_sli_rcv_unsol_event =
10342             lpfc_ct_unsol_event;
10343         return 0;
10344 }
10345
10346 /**
10347  * lpfc_sli_setup - SLI ring setup function
10348  * @phba: Pointer to HBA context object.
10349  *
10350  * lpfc_sli_setup sets up rings of the SLI interface with
10351  * number of iocbs per ring and iotags. This function is
10352  * called while driver attach to the HBA and before the
10353  * interrupts are enabled. So there is no need for locking.
10354  *
10355  * This function always returns 0. SLI3 only.
10356  **/
10357 int
10358 lpfc_sli_setup(struct lpfc_hba *phba)
10359 {
10360         int i, totiocbsize = 0;
10361         struct lpfc_sli *psli = &phba->sli;
10362         struct lpfc_sli_ring *pring;
10363
10364         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
10365         psli->sli_flag = 0;
10366
10367         psli->iocbq_lookup = NULL;
10368         psli->iocbq_lookup_len = 0;
10369         psli->last_iotag = 0;
10370
10371         for (i = 0; i < psli->num_rings; i++) {
10372                 pring = &psli->sli3_ring[i];
10373                 switch (i) {
10374                 case LPFC_FCP_RING:     /* ring 0 - FCP */
10375                         /* numCiocb and numRiocb are used in config_port */
10376                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10377                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10378                         pring->sli.sli3.numCiocb +=
10379                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10380                         pring->sli.sli3.numRiocb +=
10381                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10382                         pring->sli.sli3.numCiocb +=
10383                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10384                         pring->sli.sli3.numRiocb +=
10385                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10386                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10387                                                         SLI3_IOCB_CMD_SIZE :
10388                                                         SLI2_IOCB_CMD_SIZE;
10389                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10390                                                         SLI3_IOCB_RSP_SIZE :
10391                                                         SLI2_IOCB_RSP_SIZE;
10392                         pring->iotag_ctr = 0;
10393                         pring->iotag_max =
10394                             (phba->cfg_hba_queue_depth * 2);
10395                         pring->fast_iotag = pring->iotag_max;
10396                         pring->num_mask = 0;
10397                         break;
10398                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
10399                         /* numCiocb and numRiocb are used in config_port */
10400                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10401                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10402                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10403                                                         SLI3_IOCB_CMD_SIZE :
10404                                                         SLI2_IOCB_CMD_SIZE;
10405                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10406                                                         SLI3_IOCB_RSP_SIZE :
10407                                                         SLI2_IOCB_RSP_SIZE;
10408                         pring->iotag_max = phba->cfg_hba_queue_depth;
10409                         pring->num_mask = 0;
10410                         break;
10411                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
10412                         /* numCiocb and numRiocb are used in config_port */
10413                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10414                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10415                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10416                                                         SLI3_IOCB_CMD_SIZE :
10417                                                         SLI2_IOCB_CMD_SIZE;
10418                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10419                                                         SLI3_IOCB_RSP_SIZE :
10420                                                         SLI2_IOCB_RSP_SIZE;
10421                         pring->fast_iotag = 0;
10422                         pring->iotag_ctr = 0;
10423                         pring->iotag_max = 4096;
10424                         pring->lpfc_sli_rcv_async_status =
10425                                 lpfc_sli_async_event_handler;
10426                         pring->num_mask = LPFC_MAX_RING_MASK;
10427                         pring->prt[0].profile = 0;      /* Mask 0 */
10428                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10429                         pring->prt[0].type = FC_TYPE_ELS;
10430                         pring->prt[0].lpfc_sli_rcv_unsol_event =
10431                             lpfc_els_unsol_event;
10432                         pring->prt[1].profile = 0;      /* Mask 1 */
10433                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
10434                         pring->prt[1].type = FC_TYPE_ELS;
10435                         pring->prt[1].lpfc_sli_rcv_unsol_event =
10436                             lpfc_els_unsol_event;
10437                         pring->prt[2].profile = 0;      /* Mask 2 */
10438                         /* NameServer Inquiry */
10439                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10440                         /* NameServer */
10441                         pring->prt[2].type = FC_TYPE_CT;
10442                         pring->prt[2].lpfc_sli_rcv_unsol_event =
10443                             lpfc_ct_unsol_event;
10444                         pring->prt[3].profile = 0;      /* Mask 3 */
10445                         /* NameServer response */
10446                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10447                         /* NameServer */
10448                         pring->prt[3].type = FC_TYPE_CT;
10449                         pring->prt[3].lpfc_sli_rcv_unsol_event =
10450                             lpfc_ct_unsol_event;
10451                         break;
10452                 }
10453                 totiocbsize += (pring->sli.sli3.numCiocb *
10454                         pring->sli.sli3.sizeCiocb) +
10455                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
10456         }
10457         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
10458                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
10459                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10460                        "SLI2 SLIM Data: x%x x%lx\n",
10461                        phba->brd_no, totiocbsize,
10462                        (unsigned long) MAX_SLIM_IOCB_SIZE);
10463         }
10464         if (phba->cfg_multi_ring_support == 2)
10465                 lpfc_extra_ring_setup(phba);
10466
10467         return 0;
10468 }
10469
10470 /**
10471  * lpfc_sli4_queue_init - Queue initialization function
10472  * @phba: Pointer to HBA context object.
10473  *
10474  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
10475  * ring. This function also initializes ring indices of each ring.
10476  * This function is called during the initialization of the SLI
10477  * interface of an HBA.
10478  * This function is called with no lock held and always returns
10479  * 1.
10480  **/
10481 void
10482 lpfc_sli4_queue_init(struct lpfc_hba *phba)
10483 {
10484         struct lpfc_sli *psli;
10485         struct lpfc_sli_ring *pring;
10486         int i;
10487
10488         psli = &phba->sli;
10489         spin_lock_irq(&phba->hbalock);
10490         INIT_LIST_HEAD(&psli->mboxq);
10491         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10492         /* Initialize list headers for txq and txcmplq as double linked lists */
10493         for (i = 0; i < phba->cfg_hdw_queue; i++) {
10494                 pring = phba->sli4_hba.hdwq[i].fcp_wq->pring;
10495                 pring->flag = 0;
10496                 pring->ringno = LPFC_FCP_RING;
10497                 INIT_LIST_HEAD(&pring->txq);
10498                 INIT_LIST_HEAD(&pring->txcmplq);
10499                 INIT_LIST_HEAD(&pring->iocb_continueq);
10500                 spin_lock_init(&pring->ring_lock);
10501         }
10502         pring = phba->sli4_hba.els_wq->pring;
10503         pring->flag = 0;
10504         pring->ringno = LPFC_ELS_RING;
10505         INIT_LIST_HEAD(&pring->txq);
10506         INIT_LIST_HEAD(&pring->txcmplq);
10507         INIT_LIST_HEAD(&pring->iocb_continueq);
10508         spin_lock_init(&pring->ring_lock);
10509
10510         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
10511                 for (i = 0; i < phba->cfg_hdw_queue; i++) {
10512                         pring = phba->sli4_hba.hdwq[i].nvme_wq->pring;
10513                                 pring->flag = 0;
10514                         pring->ringno = LPFC_FCP_RING;
10515                         INIT_LIST_HEAD(&pring->txq);
10516                         INIT_LIST_HEAD(&pring->txcmplq);
10517                         INIT_LIST_HEAD(&pring->iocb_continueq);
10518                         spin_lock_init(&pring->ring_lock);
10519                 }
10520                 pring = phba->sli4_hba.nvmels_wq->pring;
10521                 pring->flag = 0;
10522                 pring->ringno = LPFC_ELS_RING;
10523                 INIT_LIST_HEAD(&pring->txq);
10524                 INIT_LIST_HEAD(&pring->txcmplq);
10525                 INIT_LIST_HEAD(&pring->iocb_continueq);
10526                 spin_lock_init(&pring->ring_lock);
10527         }
10528
10529         spin_unlock_irq(&phba->hbalock);
10530 }
10531
10532 /**
10533  * lpfc_sli_queue_init - Queue initialization function
10534  * @phba: Pointer to HBA context object.
10535  *
10536  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10537  * ring. This function also initializes ring indices of each ring.
10538  * This function is called during the initialization of the SLI
10539  * interface of an HBA.
10540  * This function is called with no lock held and always returns
10541  * 1.
10542  **/
10543 void
10544 lpfc_sli_queue_init(struct lpfc_hba *phba)
10545 {
10546         struct lpfc_sli *psli;
10547         struct lpfc_sli_ring *pring;
10548         int i;
10549
10550         psli = &phba->sli;
10551         spin_lock_irq(&phba->hbalock);
10552         INIT_LIST_HEAD(&psli->mboxq);
10553         INIT_LIST_HEAD(&psli->mboxq_cmpl);
10554         /* Initialize list headers for txq and txcmplq as double linked lists */
10555         for (i = 0; i < psli->num_rings; i++) {
10556                 pring = &psli->sli3_ring[i];
10557                 pring->ringno = i;
10558                 pring->sli.sli3.next_cmdidx  = 0;
10559                 pring->sli.sli3.local_getidx = 0;
10560                 pring->sli.sli3.cmdidx = 0;
10561                 INIT_LIST_HEAD(&pring->iocb_continueq);
10562                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
10563                 INIT_LIST_HEAD(&pring->postbufq);
10564                 pring->flag = 0;
10565                 INIT_LIST_HEAD(&pring->txq);
10566                 INIT_LIST_HEAD(&pring->txcmplq);
10567                 spin_lock_init(&pring->ring_lock);
10568         }
10569         spin_unlock_irq(&phba->hbalock);
10570 }
10571
10572 /**
10573  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10574  * @phba: Pointer to HBA context object.
10575  *
10576  * This routine flushes the mailbox command subsystem. It will unconditionally
10577  * flush all the mailbox commands in the three possible stages in the mailbox
10578  * command sub-system: pending mailbox command queue; the outstanding mailbox
10579  * command; and completed mailbox command queue. It is caller's responsibility
10580  * to make sure that the driver is in the proper state to flush the mailbox
10581  * command sub-system. Namely, the posting of mailbox commands into the
10582  * pending mailbox command queue from the various clients must be stopped;
10583  * either the HBA is in a state that it will never works on the outstanding
10584  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10585  * mailbox command has been completed.
10586  **/
10587 static void
10588 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10589 {
10590         LIST_HEAD(completions);
10591         struct lpfc_sli *psli = &phba->sli;
10592         LPFC_MBOXQ_t *pmb;
10593         unsigned long iflag;
10594
10595         /* Disable softirqs, including timers from obtaining phba->hbalock */
10596         local_bh_disable();
10597
10598         /* Flush all the mailbox commands in the mbox system */
10599         spin_lock_irqsave(&phba->hbalock, iflag);
10600
10601         /* The pending mailbox command queue */
10602         list_splice_init(&phba->sli.mboxq, &completions);
10603         /* The outstanding active mailbox command */
10604         if (psli->mbox_active) {
10605                 list_add_tail(&psli->mbox_active->list, &completions);
10606                 psli->mbox_active = NULL;
10607                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10608         }
10609         /* The completed mailbox command queue */
10610         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10611         spin_unlock_irqrestore(&phba->hbalock, iflag);
10612
10613         /* Enable softirqs again, done with phba->hbalock */
10614         local_bh_enable();
10615
10616         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10617         while (!list_empty(&completions)) {
10618                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10619                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10620                 if (pmb->mbox_cmpl)
10621                         pmb->mbox_cmpl(phba, pmb);
10622         }
10623 }
10624
10625 /**
10626  * lpfc_sli_host_down - Vport cleanup function
10627  * @vport: Pointer to virtual port object.
10628  *
10629  * lpfc_sli_host_down is called to clean up the resources
10630  * associated with a vport before destroying virtual
10631  * port data structures.
10632  * This function does following operations:
10633  * - Free discovery resources associated with this virtual
10634  *   port.
10635  * - Free iocbs associated with this virtual port in
10636  *   the txq.
10637  * - Send abort for all iocb commands associated with this
10638  *   vport in txcmplq.
10639  *
10640  * This function is called with no lock held and always returns 1.
10641  **/
10642 int
10643 lpfc_sli_host_down(struct lpfc_vport *vport)
10644 {
10645         LIST_HEAD(completions);
10646         struct lpfc_hba *phba = vport->phba;
10647         struct lpfc_sli *psli = &phba->sli;
10648         struct lpfc_queue *qp = NULL;
10649         struct lpfc_sli_ring *pring;
10650         struct lpfc_iocbq *iocb, *next_iocb;
10651         int i;
10652         unsigned long flags = 0;
10653         uint16_t prev_pring_flag;
10654
10655         lpfc_cleanup_discovery_resources(vport);
10656
10657         spin_lock_irqsave(&phba->hbalock, flags);
10658
10659         /*
10660          * Error everything on the txq since these iocbs
10661          * have not been given to the FW yet.
10662          * Also issue ABTS for everything on the txcmplq
10663          */
10664         if (phba->sli_rev != LPFC_SLI_REV4) {
10665                 for (i = 0; i < psli->num_rings; i++) {
10666                         pring = &psli->sli3_ring[i];
10667                         prev_pring_flag = pring->flag;
10668                         /* Only slow rings */
10669                         if (pring->ringno == LPFC_ELS_RING) {
10670                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10671                                 /* Set the lpfc data pending flag */
10672                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10673                         }
10674                         list_for_each_entry_safe(iocb, next_iocb,
10675                                                  &pring->txq, list) {
10676                                 if (iocb->vport != vport)
10677                                         continue;
10678                                 list_move_tail(&iocb->list, &completions);
10679                         }
10680                         list_for_each_entry_safe(iocb, next_iocb,
10681                                                  &pring->txcmplq, list) {
10682                                 if (iocb->vport != vport)
10683                                         continue;
10684                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10685                         }
10686                         pring->flag = prev_pring_flag;
10687                 }
10688         } else {
10689                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10690                         pring = qp->pring;
10691                         if (!pring)
10692                                 continue;
10693                         if (pring == phba->sli4_hba.els_wq->pring) {
10694                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10695                                 /* Set the lpfc data pending flag */
10696                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10697                         }
10698                         prev_pring_flag = pring->flag;
10699                         spin_lock_irq(&pring->ring_lock);
10700                         list_for_each_entry_safe(iocb, next_iocb,
10701                                                  &pring->txq, list) {
10702                                 if (iocb->vport != vport)
10703                                         continue;
10704                                 list_move_tail(&iocb->list, &completions);
10705                         }
10706                         spin_unlock_irq(&pring->ring_lock);
10707                         list_for_each_entry_safe(iocb, next_iocb,
10708                                                  &pring->txcmplq, list) {
10709                                 if (iocb->vport != vport)
10710                                         continue;
10711                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10712                         }
10713                         pring->flag = prev_pring_flag;
10714                 }
10715         }
10716         spin_unlock_irqrestore(&phba->hbalock, flags);
10717
10718         /* Cancel all the IOCBs from the completions list */
10719         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10720                               IOERR_SLI_DOWN);
10721         return 1;
10722 }
10723
10724 /**
10725  * lpfc_sli_hba_down - Resource cleanup function for the HBA
10726  * @phba: Pointer to HBA context object.
10727  *
10728  * This function cleans up all iocb, buffers, mailbox commands
10729  * while shutting down the HBA. This function is called with no
10730  * lock held and always returns 1.
10731  * This function does the following to cleanup driver resources:
10732  * - Free discovery resources for each virtual port
10733  * - Cleanup any pending fabric iocbs
10734  * - Iterate through the iocb txq and free each entry
10735  *   in the list.
10736  * - Free up any buffer posted to the HBA
10737  * - Free mailbox commands in the mailbox queue.
10738  **/
10739 int
10740 lpfc_sli_hba_down(struct lpfc_hba *phba)
10741 {
10742         LIST_HEAD(completions);
10743         struct lpfc_sli *psli = &phba->sli;
10744         struct lpfc_queue *qp = NULL;
10745         struct lpfc_sli_ring *pring;
10746         struct lpfc_dmabuf *buf_ptr;
10747         unsigned long flags = 0;
10748         int i;
10749
10750         /* Shutdown the mailbox command sub-system */
10751         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10752
10753         lpfc_hba_down_prep(phba);
10754
10755         /* Disable softirqs, including timers from obtaining phba->hbalock */
10756         local_bh_disable();
10757
10758         lpfc_fabric_abort_hba(phba);
10759
10760         spin_lock_irqsave(&phba->hbalock, flags);
10761
10762         /*
10763          * Error everything on the txq since these iocbs
10764          * have not been given to the FW yet.
10765          */
10766         if (phba->sli_rev != LPFC_SLI_REV4) {
10767                 for (i = 0; i < psli->num_rings; i++) {
10768                         pring = &psli->sli3_ring[i];
10769                         /* Only slow rings */
10770                         if (pring->ringno == LPFC_ELS_RING) {
10771                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10772                                 /* Set the lpfc data pending flag */
10773                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10774                         }
10775                         list_splice_init(&pring->txq, &completions);
10776                 }
10777         } else {
10778                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10779                         pring = qp->pring;
10780                         if (!pring)
10781                                 continue;
10782                         spin_lock_irq(&pring->ring_lock);
10783                         list_splice_init(&pring->txq, &completions);
10784                         spin_unlock_irq(&pring->ring_lock);
10785                         if (pring == phba->sli4_hba.els_wq->pring) {
10786                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10787                                 /* Set the lpfc data pending flag */
10788                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10789                         }
10790                 }
10791         }
10792         spin_unlock_irqrestore(&phba->hbalock, flags);
10793
10794         /* Cancel all the IOCBs from the completions list */
10795         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10796                               IOERR_SLI_DOWN);
10797
10798         spin_lock_irqsave(&phba->hbalock, flags);
10799         list_splice_init(&phba->elsbuf, &completions);
10800         phba->elsbuf_cnt = 0;
10801         phba->elsbuf_prev_cnt = 0;
10802         spin_unlock_irqrestore(&phba->hbalock, flags);
10803
10804         while (!list_empty(&completions)) {
10805                 list_remove_head(&completions, buf_ptr,
10806                         struct lpfc_dmabuf, list);
10807                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10808                 kfree(buf_ptr);
10809         }
10810
10811         /* Enable softirqs again, done with phba->hbalock */
10812         local_bh_enable();
10813
10814         /* Return any active mbox cmds */
10815         del_timer_sync(&psli->mbox_tmo);
10816
10817         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10818         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10819         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10820
10821         return 1;
10822 }
10823
10824 /**
10825  * lpfc_sli_pcimem_bcopy - SLI memory copy function
10826  * @srcp: Source memory pointer.
10827  * @destp: Destination memory pointer.
10828  * @cnt: Number of words required to be copied.
10829  *
10830  * This function is used for copying data between driver memory
10831  * and the SLI memory. This function also changes the endianness
10832  * of each word if native endianness is different from SLI
10833  * endianness. This function can be called with or without
10834  * lock.
10835  **/
10836 void
10837 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10838 {
10839         uint32_t *src = srcp;
10840         uint32_t *dest = destp;
10841         uint32_t ldata;
10842         int i;
10843
10844         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10845                 ldata = *src;
10846                 ldata = le32_to_cpu(ldata);
10847                 *dest = ldata;
10848                 src++;
10849                 dest++;
10850         }
10851 }
10852
10853
10854 /**
10855  * lpfc_sli_bemem_bcopy - SLI memory copy function
10856  * @srcp: Source memory pointer.
10857  * @destp: Destination memory pointer.
10858  * @cnt: Number of words required to be copied.
10859  *
10860  * This function is used for copying data between a data structure
10861  * with big endian representation to local endianness.
10862  * This function can be called with or without lock.
10863  **/
10864 void
10865 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10866 {
10867         uint32_t *src = srcp;
10868         uint32_t *dest = destp;
10869         uint32_t ldata;
10870         int i;
10871
10872         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10873                 ldata = *src;
10874                 ldata = be32_to_cpu(ldata);
10875                 *dest = ldata;
10876                 src++;
10877                 dest++;
10878         }
10879 }
10880
10881 /**
10882  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10883  * @phba: Pointer to HBA context object.
10884  * @pring: Pointer to driver SLI ring object.
10885  * @mp: Pointer to driver buffer object.
10886  *
10887  * This function is called with no lock held.
10888  * It always return zero after adding the buffer to the postbufq
10889  * buffer list.
10890  **/
10891 int
10892 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10893                          struct lpfc_dmabuf *mp)
10894 {
10895         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10896            later */
10897         spin_lock_irq(&phba->hbalock);
10898         list_add_tail(&mp->list, &pring->postbufq);
10899         pring->postbufq_cnt++;
10900         spin_unlock_irq(&phba->hbalock);
10901         return 0;
10902 }
10903
10904 /**
10905  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10906  * @phba: Pointer to HBA context object.
10907  *
10908  * When HBQ is enabled, buffers are searched based on tags. This function
10909  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10910  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10911  * does not conflict with tags of buffer posted for unsolicited events.
10912  * The function returns the allocated tag. The function is called with
10913  * no locks held.
10914  **/
10915 uint32_t
10916 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10917 {
10918         spin_lock_irq(&phba->hbalock);
10919         phba->buffer_tag_count++;
10920         /*
10921          * Always set the QUE_BUFTAG_BIT to distiguish between
10922          * a tag assigned by HBQ.
10923          */
10924         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10925         spin_unlock_irq(&phba->hbalock);
10926         return phba->buffer_tag_count;
10927 }
10928
10929 /**
10930  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10931  * @phba: Pointer to HBA context object.
10932  * @pring: Pointer to driver SLI ring object.
10933  * @tag: Buffer tag.
10934  *
10935  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10936  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10937  * iocb is posted to the response ring with the tag of the buffer.
10938  * This function searches the pring->postbufq list using the tag
10939  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10940  * iocb. If the buffer is found then lpfc_dmabuf object of the
10941  * buffer is returned to the caller else NULL is returned.
10942  * This function is called with no lock held.
10943  **/
10944 struct lpfc_dmabuf *
10945 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10946                         uint32_t tag)
10947 {
10948         struct lpfc_dmabuf *mp, *next_mp;
10949         struct list_head *slp = &pring->postbufq;
10950
10951         /* Search postbufq, from the beginning, looking for a match on tag */
10952         spin_lock_irq(&phba->hbalock);
10953         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10954                 if (mp->buffer_tag == tag) {
10955                         list_del_init(&mp->list);
10956                         pring->postbufq_cnt--;
10957                         spin_unlock_irq(&phba->hbalock);
10958                         return mp;
10959                 }
10960         }
10961
10962         spin_unlock_irq(&phba->hbalock);
10963         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10964                         "0402 Cannot find virtual addr for buffer tag on "
10965                         "ring %d Data x%lx x%p x%p x%x\n",
10966                         pring->ringno, (unsigned long) tag,
10967                         slp->next, slp->prev, pring->postbufq_cnt);
10968
10969         return NULL;
10970 }
10971
10972 /**
10973  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10974  * @phba: Pointer to HBA context object.
10975  * @pring: Pointer to driver SLI ring object.
10976  * @phys: DMA address of the buffer.
10977  *
10978  * This function searches the buffer list using the dma_address
10979  * of unsolicited event to find the driver's lpfc_dmabuf object
10980  * corresponding to the dma_address. The function returns the
10981  * lpfc_dmabuf object if a buffer is found else it returns NULL.
10982  * This function is called by the ct and els unsolicited event
10983  * handlers to get the buffer associated with the unsolicited
10984  * event.
10985  *
10986  * This function is called with no lock held.
10987  **/
10988 struct lpfc_dmabuf *
10989 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10990                          dma_addr_t phys)
10991 {
10992         struct lpfc_dmabuf *mp, *next_mp;
10993         struct list_head *slp = &pring->postbufq;
10994
10995         /* Search postbufq, from the beginning, looking for a match on phys */
10996         spin_lock_irq(&phba->hbalock);
10997         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10998                 if (mp->phys == phys) {
10999                         list_del_init(&mp->list);
11000                         pring->postbufq_cnt--;
11001                         spin_unlock_irq(&phba->hbalock);
11002                         return mp;
11003                 }
11004         }
11005
11006         spin_unlock_irq(&phba->hbalock);
11007         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11008                         "0410 Cannot find virtual addr for mapped buf on "
11009                         "ring %d Data x%llx x%p x%p x%x\n",
11010                         pring->ringno, (unsigned long long)phys,
11011                         slp->next, slp->prev, pring->postbufq_cnt);
11012         return NULL;
11013 }
11014
11015 /**
11016  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
11017  * @phba: Pointer to HBA context object.
11018  * @cmdiocb: Pointer to driver command iocb object.
11019  * @rspiocb: Pointer to driver response iocb object.
11020  *
11021  * This function is the completion handler for the abort iocbs for
11022  * ELS commands. This function is called from the ELS ring event
11023  * handler with no lock held. This function frees memory resources
11024  * associated with the abort iocb.
11025  **/
11026 static void
11027 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11028                         struct lpfc_iocbq *rspiocb)
11029 {
11030         IOCB_t *irsp = &rspiocb->iocb;
11031         uint16_t abort_iotag, abort_context;
11032         struct lpfc_iocbq *abort_iocb = NULL;
11033
11034         if (irsp->ulpStatus) {
11035
11036                 /*
11037                  * Assume that the port already completed and returned, or
11038                  * will return the iocb. Just Log the message.
11039                  */
11040                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11041                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11042
11043                 spin_lock_irq(&phba->hbalock);
11044                 if (phba->sli_rev < LPFC_SLI_REV4) {
11045                         if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11046                             irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11047                             irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11048                                 spin_unlock_irq(&phba->hbalock);
11049                                 goto release_iocb;
11050                         }
11051                         if (abort_iotag != 0 &&
11052                                 abort_iotag <= phba->sli.last_iotag)
11053                                 abort_iocb =
11054                                         phba->sli.iocbq_lookup[abort_iotag];
11055                 } else
11056                         /* For sli4 the abort_tag is the XRI,
11057                          * so the abort routine puts the iotag  of the iocb
11058                          * being aborted in the context field of the abort
11059                          * IOCB.
11060                          */
11061                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
11062
11063                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11064                                 "0327 Cannot abort els iocb %p "
11065                                 "with tag %x context %x, abort status %x, "
11066                                 "abort code %x\n",
11067                                 abort_iocb, abort_iotag, abort_context,
11068                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
11069
11070                 spin_unlock_irq(&phba->hbalock);
11071         }
11072 release_iocb:
11073         lpfc_sli_release_iocbq(phba, cmdiocb);
11074         return;
11075 }
11076
11077 /**
11078  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
11079  * @phba: Pointer to HBA context object.
11080  * @cmdiocb: Pointer to driver command iocb object.
11081  * @rspiocb: Pointer to driver response iocb object.
11082  *
11083  * The function is called from SLI ring event handler with no
11084  * lock held. This function is the completion handler for ELS commands
11085  * which are aborted. The function frees memory resources used for
11086  * the aborted ELS commands.
11087  **/
11088 static void
11089 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11090                      struct lpfc_iocbq *rspiocb)
11091 {
11092         IOCB_t *irsp = &rspiocb->iocb;
11093
11094         /* ELS cmd tag <ulpIoTag> completes */
11095         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11096                         "0139 Ignoring ELS cmd tag x%x completion Data: "
11097                         "x%x x%x x%x\n",
11098                         irsp->ulpIoTag, irsp->ulpStatus,
11099                         irsp->un.ulpWord[4], irsp->ulpTimeout);
11100         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11101                 lpfc_ct_free_iocb(phba, cmdiocb);
11102         else
11103                 lpfc_els_free_iocb(phba, cmdiocb);
11104         return;
11105 }
11106
11107 /**
11108  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
11109  * @phba: Pointer to HBA context object.
11110  * @pring: Pointer to driver SLI ring object.
11111  * @cmdiocb: Pointer to driver command iocb object.
11112  *
11113  * This function issues an abort iocb for the provided command iocb down to
11114  * the port. Other than the case the outstanding command iocb is an abort
11115  * request, this function issues abort out unconditionally. This function is
11116  * called with hbalock held. The function returns 0 when it fails due to
11117  * memory allocation failure or when the command iocb is an abort request.
11118  **/
11119 static int
11120 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11121                            struct lpfc_iocbq *cmdiocb)
11122 {
11123         struct lpfc_vport *vport = cmdiocb->vport;
11124         struct lpfc_iocbq *abtsiocbp;
11125         IOCB_t *icmd = NULL;
11126         IOCB_t *iabt = NULL;
11127         int retval;
11128         unsigned long iflags;
11129         struct lpfc_nodelist *ndlp;
11130
11131         lockdep_assert_held(&phba->hbalock);
11132
11133         /*
11134          * There are certain command types we don't want to abort.  And we
11135          * don't want to abort commands that are already in the process of
11136          * being aborted.
11137          */
11138         icmd = &cmdiocb->iocb;
11139         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11140             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11141             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11142                 return 0;
11143
11144         /* issue ABTS for this IOCB based on iotag */
11145         abtsiocbp = __lpfc_sli_get_iocbq(phba);
11146         if (abtsiocbp == NULL)
11147                 return 0;
11148
11149         /* This signals the response to set the correct status
11150          * before calling the completion handler
11151          */
11152         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11153
11154         iabt = &abtsiocbp->iocb;
11155         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11156         iabt->un.acxri.abortContextTag = icmd->ulpContext;
11157         if (phba->sli_rev == LPFC_SLI_REV4) {
11158                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
11159                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
11160         } else {
11161                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
11162                 if (pring->ringno == LPFC_ELS_RING) {
11163                         ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11164                         iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11165                 }
11166         }
11167         iabt->ulpLe = 1;
11168         iabt->ulpClass = icmd->ulpClass;
11169
11170         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11171         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
11172         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11173                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
11174         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11175                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
11176
11177         if (phba->link_state >= LPFC_LINK_UP)
11178                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11179         else
11180                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11181
11182         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
11183         abtsiocbp->vport = vport;
11184
11185         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11186                          "0339 Abort xri x%x, original iotag x%x, "
11187                          "abort cmd iotag x%x\n",
11188                          iabt->un.acxri.abortIoTag,
11189                          iabt->un.acxri.abortContextTag,
11190                          abtsiocbp->iotag);
11191
11192         if (phba->sli_rev == LPFC_SLI_REV4) {
11193                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11194                 if (unlikely(pring == NULL))
11195                         return 0;
11196                 /* Note: both hbalock and ring_lock need to be set here */
11197                 spin_lock_irqsave(&pring->ring_lock, iflags);
11198                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11199                         abtsiocbp, 0);
11200                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11201         } else {
11202                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11203                         abtsiocbp, 0);
11204         }
11205
11206         if (retval)
11207                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
11208
11209         /*
11210          * Caller to this routine should check for IOCB_ERROR
11211          * and handle it properly.  This routine no longer removes
11212          * iocb off txcmplq and call compl in case of IOCB_ERROR.
11213          */
11214         return retval;
11215 }
11216
11217 /**
11218  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11219  * @phba: Pointer to HBA context object.
11220  * @pring: Pointer to driver SLI ring object.
11221  * @cmdiocb: Pointer to driver command iocb object.
11222  *
11223  * This function issues an abort iocb for the provided command iocb. In case
11224  * of unloading, the abort iocb will not be issued to commands on the ELS
11225  * ring. Instead, the callback function shall be changed to those commands
11226  * so that nothing happens when them finishes. This function is called with
11227  * hbalock held. The function returns 0 when the command iocb is an abort
11228  * request.
11229  **/
11230 int
11231 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11232                            struct lpfc_iocbq *cmdiocb)
11233 {
11234         struct lpfc_vport *vport = cmdiocb->vport;
11235         int retval = IOCB_ERROR;
11236         IOCB_t *icmd = NULL;
11237
11238         lockdep_assert_held(&phba->hbalock);
11239
11240         /*
11241          * There are certain command types we don't want to abort.  And we
11242          * don't want to abort commands that are already in the process of
11243          * being aborted.
11244          */
11245         icmd = &cmdiocb->iocb;
11246         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11247             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11248             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11249                 return 0;
11250
11251         if (!pring) {
11252                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11253                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11254                 else
11255                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11256                 goto abort_iotag_exit;
11257         }
11258
11259         /*
11260          * If we're unloading, don't abort iocb on the ELS ring, but change
11261          * the callback so that nothing happens when it finishes.
11262          */
11263         if ((vport->load_flag & FC_UNLOADING) &&
11264             (pring->ringno == LPFC_ELS_RING)) {
11265                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11266                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11267                 else
11268                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11269                 goto abort_iotag_exit;
11270         }
11271
11272         /* Now, we try to issue the abort to the cmdiocb out */
11273         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11274
11275 abort_iotag_exit:
11276         /*
11277          * Caller to this routine should check for IOCB_ERROR
11278          * and handle it properly.  This routine no longer removes
11279          * iocb off txcmplq and call compl in case of IOCB_ERROR.
11280          */
11281         return retval;
11282 }
11283
11284 /**
11285  * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11286  * @phba: Pointer to HBA context object.
11287  * @pring: Pointer to driver SLI ring object.
11288  * @cmdiocb: Pointer to driver command iocb object.
11289  *
11290  * This function issues an abort iocb for the provided command iocb down to
11291  * the port. Other than the case the outstanding command iocb is an abort
11292  * request, this function issues abort out unconditionally. This function is
11293  * called with hbalock held. The function returns 0 when it fails due to
11294  * memory allocation failure or when the command iocb is an abort request.
11295  **/
11296 static int
11297 lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11298                         struct lpfc_iocbq *cmdiocb)
11299 {
11300         struct lpfc_vport *vport = cmdiocb->vport;
11301         struct lpfc_iocbq *abtsiocbp;
11302         union lpfc_wqe128 *abts_wqe;
11303         int retval;
11304
11305         /*
11306          * There are certain command types we don't want to abort.  And we
11307          * don't want to abort commands that are already in the process of
11308          * being aborted.
11309          */
11310         if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11311             cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11312             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11313                 return 0;
11314
11315         /* issue ABTS for this io based on iotag */
11316         abtsiocbp = __lpfc_sli_get_iocbq(phba);
11317         if (abtsiocbp == NULL)
11318                 return 0;
11319
11320         /* This signals the response to set the correct status
11321          * before calling the completion handler
11322          */
11323         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11324
11325         /* Complete prepping the abort wqe and issue to the FW. */
11326         abts_wqe = &abtsiocbp->wqe;
11327
11328         /* Clear any stale WQE contents */
11329         memset(abts_wqe, 0, sizeof(union lpfc_wqe));
11330         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11331
11332         /* word 7 */
11333         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11334         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11335                cmdiocb->iocb.ulpClass);
11336
11337         /* word 8 - tell the FW to abort the IO associated with this
11338          * outstanding exchange ID.
11339          */
11340         abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11341
11342         /* word 9 - this is the iotag for the abts_wqe completion. */
11343         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11344                abtsiocbp->iotag);
11345
11346         /* word 10 */
11347         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11348         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11349
11350         /* word 11 */
11351         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11352         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11353         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11354
11355         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11356         abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11357         abtsiocbp->vport = vport;
11358         abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
11359         retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
11360         if (retval) {
11361                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11362                                  "6147 Failed abts issue_wqe with status x%x "
11363                                  "for oxid x%x\n",
11364                                  retval, cmdiocb->sli4_xritag);
11365                 lpfc_sli_release_iocbq(phba, abtsiocbp);
11366                 return retval;
11367         }
11368
11369         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11370                          "6148 Drv Abort NVME Request Issued for "
11371                          "ox_id x%x on reqtag x%x\n",
11372                          cmdiocb->sli4_xritag,
11373                          abtsiocbp->iotag);
11374
11375         return retval;
11376 }
11377
11378 /**
11379  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11380  * @phba: pointer to lpfc HBA data structure.
11381  *
11382  * This routine will abort all pending and outstanding iocbs to an HBA.
11383  **/
11384 void
11385 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11386 {
11387         struct lpfc_sli *psli = &phba->sli;
11388         struct lpfc_sli_ring *pring;
11389         struct lpfc_queue *qp = NULL;
11390         int i;
11391
11392         if (phba->sli_rev != LPFC_SLI_REV4) {
11393                 for (i = 0; i < psli->num_rings; i++) {
11394                         pring = &psli->sli3_ring[i];
11395                         lpfc_sli_abort_iocb_ring(phba, pring);
11396                 }
11397                 return;
11398         }
11399         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11400                 pring = qp->pring;
11401                 if (!pring)
11402                         continue;
11403                 lpfc_sli_abort_iocb_ring(phba, pring);
11404         }
11405 }
11406
11407 /**
11408  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
11409  * @iocbq: Pointer to driver iocb object.
11410  * @vport: Pointer to driver virtual port object.
11411  * @tgt_id: SCSI ID of the target.
11412  * @lun_id: LUN ID of the scsi device.
11413  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11414  *
11415  * This function acts as an iocb filter for functions which abort or count
11416  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11417  * 0 if the filtering criteria is met for the given iocb and will return
11418  * 1 if the filtering criteria is not met.
11419  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11420  * given iocb is for the SCSI device specified by vport, tgt_id and
11421  * lun_id parameter.
11422  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
11423  * given iocb is for the SCSI target specified by vport and tgt_id
11424  * parameters.
11425  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11426  * given iocb is for the SCSI host associated with the given vport.
11427  * This function is called with no locks held.
11428  **/
11429 static int
11430 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11431                            uint16_t tgt_id, uint64_t lun_id,
11432                            lpfc_ctx_cmd ctx_cmd)
11433 {
11434         struct lpfc_scsi_buf *lpfc_cmd;
11435         int rc = 1;
11436
11437         if (iocbq->vport != vport)
11438                 return rc;
11439
11440         if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
11441             !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
11442                 return rc;
11443
11444         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11445
11446         if (lpfc_cmd->pCmd == NULL)
11447                 return rc;
11448
11449         switch (ctx_cmd) {
11450         case LPFC_CTX_LUN:
11451                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11452                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11453                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
11454                         rc = 0;
11455                 break;
11456         case LPFC_CTX_TGT:
11457                 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
11458                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
11459                         rc = 0;
11460                 break;
11461         case LPFC_CTX_HOST:
11462                 rc = 0;
11463                 break;
11464         default:
11465                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
11466                         __func__, ctx_cmd);
11467                 break;
11468         }
11469
11470         return rc;
11471 }
11472
11473 /**
11474  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
11475  * @vport: Pointer to virtual port.
11476  * @tgt_id: SCSI ID of the target.
11477  * @lun_id: LUN ID of the scsi device.
11478  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11479  *
11480  * This function returns number of FCP commands pending for the vport.
11481  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11482  * commands pending on the vport associated with SCSI device specified
11483  * by tgt_id and lun_id parameters.
11484  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11485  * commands pending on the vport associated with SCSI target specified
11486  * by tgt_id parameter.
11487  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11488  * commands pending on the vport.
11489  * This function returns the number of iocbs which satisfy the filter.
11490  * This function is called without any lock held.
11491  **/
11492 int
11493 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11494                   lpfc_ctx_cmd ctx_cmd)
11495 {
11496         struct lpfc_hba *phba = vport->phba;
11497         struct lpfc_iocbq *iocbq;
11498         int sum, i;
11499
11500         spin_lock_irq(&phba->hbalock);
11501         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11502                 iocbq = phba->sli.iocbq_lookup[i];
11503
11504                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11505                                                 ctx_cmd) == 0)
11506                         sum++;
11507         }
11508         spin_unlock_irq(&phba->hbalock);
11509
11510         return sum;
11511 }
11512
11513 /**
11514  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11515  * @phba: Pointer to HBA context object
11516  * @cmdiocb: Pointer to command iocb object.
11517  * @rspiocb: Pointer to response iocb object.
11518  *
11519  * This function is called when an aborted FCP iocb completes. This
11520  * function is called by the ring event handler with no lock held.
11521  * This function frees the iocb.
11522  **/
11523 void
11524 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11525                         struct lpfc_iocbq *rspiocb)
11526 {
11527         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11528                         "3096 ABORT_XRI_CN completing on rpi x%x "
11529                         "original iotag x%x, abort cmd iotag x%x "
11530                         "status 0x%x, reason 0x%x\n",
11531                         cmdiocb->iocb.un.acxri.abortContextTag,
11532                         cmdiocb->iocb.un.acxri.abortIoTag,
11533                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11534                         rspiocb->iocb.un.ulpWord[4]);
11535         lpfc_sli_release_iocbq(phba, cmdiocb);
11536         return;
11537 }
11538
11539 /**
11540  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
11541  * @vport: Pointer to virtual port.
11542  * @pring: Pointer to driver SLI ring object.
11543  * @tgt_id: SCSI ID of the target.
11544  * @lun_id: LUN ID of the scsi device.
11545  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11546  *
11547  * This function sends an abort command for every SCSI command
11548  * associated with the given virtual port pending on the ring
11549  * filtered by lpfc_sli_validate_fcp_iocb function.
11550  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11551  * FCP iocbs associated with lun specified by tgt_id and lun_id
11552  * parameters
11553  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11554  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11555  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11556  * FCP iocbs associated with virtual port.
11557  * This function returns number of iocbs it failed to abort.
11558  * This function is called with no locks held.
11559  **/
11560 int
11561 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11562                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
11563 {
11564         struct lpfc_hba *phba = vport->phba;
11565         struct lpfc_iocbq *iocbq;
11566         struct lpfc_iocbq *abtsiocb;
11567         struct lpfc_sli_ring *pring_s4;
11568         IOCB_t *cmd = NULL;
11569         int errcnt = 0, ret_val = 0;
11570         int i;
11571
11572         /* all I/Os are in process of being flushed */
11573         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11574                 return errcnt;
11575
11576         for (i = 1; i <= phba->sli.last_iotag; i++) {
11577                 iocbq = phba->sli.iocbq_lookup[i];
11578
11579                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11580                                                abort_cmd) != 0)
11581                         continue;
11582
11583                 /*
11584                  * If the iocbq is already being aborted, don't take a second
11585                  * action, but do count it.
11586                  */
11587                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11588                         continue;
11589
11590                 /* issue ABTS for this IOCB based on iotag */
11591                 abtsiocb = lpfc_sli_get_iocbq(phba);
11592                 if (abtsiocb == NULL) {
11593                         errcnt++;
11594                         continue;
11595                 }
11596
11597                 /* indicate the IO is being aborted by the driver. */
11598                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11599
11600                 cmd = &iocbq->iocb;
11601                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11602                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
11603                 if (phba->sli_rev == LPFC_SLI_REV4)
11604                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11605                 else
11606                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
11607                 abtsiocb->iocb.ulpLe = 1;
11608                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
11609                 abtsiocb->vport = vport;
11610
11611                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11612                 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
11613                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11614                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
11615                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11616                         abtsiocb->iocb_flag |= LPFC_IO_FOF;
11617
11618                 if (lpfc_is_link_up(phba))
11619                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11620                 else
11621                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11622
11623                 /* Setup callback routine and issue the command. */
11624                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11625                 if (phba->sli_rev == LPFC_SLI_REV4) {
11626                         pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11627                         if (!pring_s4)
11628                                 continue;
11629                         ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11630                                                       abtsiocb, 0);
11631                 } else
11632                         ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11633                                                       abtsiocb, 0);
11634                 if (ret_val == IOCB_ERROR) {
11635                         lpfc_sli_release_iocbq(phba, abtsiocb);
11636                         errcnt++;
11637                         continue;
11638                 }
11639         }
11640
11641         return errcnt;
11642 }
11643
11644 /**
11645  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11646  * @vport: Pointer to virtual port.
11647  * @pring: Pointer to driver SLI ring object.
11648  * @tgt_id: SCSI ID of the target.
11649  * @lun_id: LUN ID of the scsi device.
11650  * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11651  *
11652  * This function sends an abort command for every SCSI command
11653  * associated with the given virtual port pending on the ring
11654  * filtered by lpfc_sli_validate_fcp_iocb function.
11655  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11656  * FCP iocbs associated with lun specified by tgt_id and lun_id
11657  * parameters
11658  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11659  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11660  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11661  * FCP iocbs associated with virtual port.
11662  * This function returns number of iocbs it aborted .
11663  * This function is called with no locks held right after a taskmgmt
11664  * command is sent.
11665  **/
11666 int
11667 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11668                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11669 {
11670         struct lpfc_hba *phba = vport->phba;
11671         struct lpfc_scsi_buf *lpfc_cmd;
11672         struct lpfc_iocbq *abtsiocbq;
11673         struct lpfc_nodelist *ndlp;
11674         struct lpfc_iocbq *iocbq;
11675         IOCB_t *icmd;
11676         int sum, i, ret_val;
11677         unsigned long iflags;
11678         struct lpfc_sli_ring *pring_s4;
11679
11680         spin_lock_irqsave(&phba->hbalock, iflags);
11681
11682         /* all I/Os are in process of being flushed */
11683         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11684                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11685                 return 0;
11686         }
11687         sum = 0;
11688
11689         for (i = 1; i <= phba->sli.last_iotag; i++) {
11690                 iocbq = phba->sli.iocbq_lookup[i];
11691
11692                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11693                                                cmd) != 0)
11694                         continue;
11695
11696                 /*
11697                  * If the iocbq is already being aborted, don't take a second
11698                  * action, but do count it.
11699                  */
11700                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11701                         continue;
11702
11703                 /* issue ABTS for this IOCB based on iotag */
11704                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11705                 if (abtsiocbq == NULL)
11706                         continue;
11707
11708                 icmd = &iocbq->iocb;
11709                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11710                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11711                 if (phba->sli_rev == LPFC_SLI_REV4)
11712                         abtsiocbq->iocb.un.acxri.abortIoTag =
11713                                                          iocbq->sli4_xritag;
11714                 else
11715                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11716                 abtsiocbq->iocb.ulpLe = 1;
11717                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11718                 abtsiocbq->vport = vport;
11719
11720                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11721                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11722                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11723                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11724                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11725                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11726
11727                 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11728                 ndlp = lpfc_cmd->rdata->pnode;
11729
11730                 if (lpfc_is_link_up(phba) &&
11731                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11732                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11733                 else
11734                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11735
11736                 /* Setup callback routine and issue the command. */
11737                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11738
11739                 /*
11740                  * Indicate the IO is being aborted by the driver and set
11741                  * the caller's flag into the aborted IO.
11742                  */
11743                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11744
11745                 if (phba->sli_rev == LPFC_SLI_REV4) {
11746                         pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11747                         if (!pring_s4)
11748                                 continue;
11749                         /* Note: both hbalock and ring_lock must be set here */
11750                         spin_lock(&pring_s4->ring_lock);
11751                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11752                                                         abtsiocbq, 0);
11753                         spin_unlock(&pring_s4->ring_lock);
11754                 } else {
11755                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11756                                                         abtsiocbq, 0);
11757                 }
11758
11759
11760                 if (ret_val == IOCB_ERROR)
11761                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
11762                 else
11763                         sum++;
11764         }
11765         spin_unlock_irqrestore(&phba->hbalock, iflags);
11766         return sum;
11767 }
11768
11769 /**
11770  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11771  * @phba: Pointer to HBA context object.
11772  * @cmdiocbq: Pointer to command iocb.
11773  * @rspiocbq: Pointer to response iocb.
11774  *
11775  * This function is the completion handler for iocbs issued using
11776  * lpfc_sli_issue_iocb_wait function. This function is called by the
11777  * ring event handler function without any lock held. This function
11778  * can be called from both worker thread context and interrupt
11779  * context. This function also can be called from other thread which
11780  * cleans up the SLI layer objects.
11781  * This function copy the contents of the response iocb to the
11782  * response iocb memory object provided by the caller of
11783  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11784  * sleeps for the iocb completion.
11785  **/
11786 static void
11787 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11788                         struct lpfc_iocbq *cmdiocbq,
11789                         struct lpfc_iocbq *rspiocbq)
11790 {
11791         wait_queue_head_t *pdone_q;
11792         unsigned long iflags;
11793         struct lpfc_scsi_buf *lpfc_cmd;
11794
11795         spin_lock_irqsave(&phba->hbalock, iflags);
11796         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11797
11798                 /*
11799                  * A time out has occurred for the iocb.  If a time out
11800                  * completion handler has been supplied, call it.  Otherwise,
11801                  * just free the iocbq.
11802                  */
11803
11804                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11805                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11806                 cmdiocbq->wait_iocb_cmpl = NULL;
11807                 if (cmdiocbq->iocb_cmpl)
11808                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11809                 else
11810                         lpfc_sli_release_iocbq(phba, cmdiocbq);
11811                 return;
11812         }
11813
11814         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11815         if (cmdiocbq->context2 && rspiocbq)
11816                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11817                        &rspiocbq->iocb, sizeof(IOCB_t));
11818
11819         /* Set the exchange busy flag for task management commands */
11820         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11821                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11822                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11823                         cur_iocbq);
11824                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11825         }
11826
11827         pdone_q = cmdiocbq->context_un.wait_queue;
11828         if (pdone_q)
11829                 wake_up(pdone_q);
11830         spin_unlock_irqrestore(&phba->hbalock, iflags);
11831         return;
11832 }
11833
11834 /**
11835  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11836  * @phba: Pointer to HBA context object..
11837  * @piocbq: Pointer to command iocb.
11838  * @flag: Flag to test.
11839  *
11840  * This routine grabs the hbalock and then test the iocb_flag to
11841  * see if the passed in flag is set.
11842  * Returns:
11843  * 1 if flag is set.
11844  * 0 if flag is not set.
11845  **/
11846 static int
11847 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11848                  struct lpfc_iocbq *piocbq, uint32_t flag)
11849 {
11850         unsigned long iflags;
11851         int ret;
11852
11853         spin_lock_irqsave(&phba->hbalock, iflags);
11854         ret = piocbq->iocb_flag & flag;
11855         spin_unlock_irqrestore(&phba->hbalock, iflags);
11856         return ret;
11857
11858 }
11859
11860 /**
11861  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11862  * @phba: Pointer to HBA context object..
11863  * @pring: Pointer to sli ring.
11864  * @piocb: Pointer to command iocb.
11865  * @prspiocbq: Pointer to response iocb.
11866  * @timeout: Timeout in number of seconds.
11867  *
11868  * This function issues the iocb to firmware and waits for the
11869  * iocb to complete. The iocb_cmpl field of the shall be used
11870  * to handle iocbs which time out. If the field is NULL, the
11871  * function shall free the iocbq structure.  If more clean up is
11872  * needed, the caller is expected to provide a completion function
11873  * that will provide the needed clean up.  If the iocb command is
11874  * not completed within timeout seconds, the function will either
11875  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11876  * completion function set in the iocb_cmpl field and then return
11877  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
11878  * resources if this function returns IOCB_TIMEDOUT.
11879  * The function waits for the iocb completion using an
11880  * non-interruptible wait.
11881  * This function will sleep while waiting for iocb completion.
11882  * So, this function should not be called from any context which
11883  * does not allow sleeping. Due to the same reason, this function
11884  * cannot be called with interrupt disabled.
11885  * This function assumes that the iocb completions occur while
11886  * this function sleep. So, this function cannot be called from
11887  * the thread which process iocb completion for this ring.
11888  * This function clears the iocb_flag of the iocb object before
11889  * issuing the iocb and the iocb completion handler sets this
11890  * flag and wakes this thread when the iocb completes.
11891  * The contents of the response iocb will be copied to prspiocbq
11892  * by the completion handler when the command completes.
11893  * This function returns IOCB_SUCCESS when success.
11894  * This function is called with no lock held.
11895  **/
11896 int
11897 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11898                          uint32_t ring_number,
11899                          struct lpfc_iocbq *piocb,
11900                          struct lpfc_iocbq *prspiocbq,
11901                          uint32_t timeout)
11902 {
11903         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11904         long timeleft, timeout_req = 0;
11905         int retval = IOCB_SUCCESS;
11906         uint32_t creg_val;
11907         struct lpfc_iocbq *iocb;
11908         int txq_cnt = 0;
11909         int txcmplq_cnt = 0;
11910         struct lpfc_sli_ring *pring;
11911         unsigned long iflags;
11912         bool iocb_completed = true;
11913
11914         if (phba->sli_rev >= LPFC_SLI_REV4)
11915                 pring = lpfc_sli4_calc_ring(phba, piocb);
11916         else
11917                 pring = &phba->sli.sli3_ring[ring_number];
11918         /*
11919          * If the caller has provided a response iocbq buffer, then context2
11920          * is NULL or its an error.
11921          */
11922         if (prspiocbq) {
11923                 if (piocb->context2)
11924                         return IOCB_ERROR;
11925                 piocb->context2 = prspiocbq;
11926         }
11927
11928         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11929         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11930         piocb->context_un.wait_queue = &done_q;
11931         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11932
11933         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11934                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11935                         return IOCB_ERROR;
11936                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11937                 writel(creg_val, phba->HCregaddr);
11938                 readl(phba->HCregaddr); /* flush */
11939         }
11940
11941         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11942                                      SLI_IOCB_RET_IOCB);
11943         if (retval == IOCB_SUCCESS) {
11944                 timeout_req = msecs_to_jiffies(timeout * 1000);
11945                 timeleft = wait_event_timeout(done_q,
11946                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11947                                 timeout_req);
11948                 spin_lock_irqsave(&phba->hbalock, iflags);
11949                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11950
11951                         /*
11952                          * IOCB timed out.  Inform the wake iocb wait
11953                          * completion function and set local status
11954                          */
11955
11956                         iocb_completed = false;
11957                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11958                 }
11959                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11960                 if (iocb_completed) {
11961                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11962                                         "0331 IOCB wake signaled\n");
11963                         /* Note: we are not indicating if the IOCB has a success
11964                          * status or not - that's for the caller to check.
11965                          * IOCB_SUCCESS means just that the command was sent and
11966                          * completed. Not that it completed successfully.
11967                          * */
11968                 } else if (timeleft == 0) {
11969                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11970                                         "0338 IOCB wait timeout error - no "
11971                                         "wake response Data x%x\n", timeout);
11972                         retval = IOCB_TIMEDOUT;
11973                 } else {
11974                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11975                                         "0330 IOCB wake NOT set, "
11976                                         "Data x%x x%lx\n",
11977                                         timeout, (timeleft / jiffies));
11978                         retval = IOCB_TIMEDOUT;
11979                 }
11980         } else if (retval == IOCB_BUSY) {
11981                 if (phba->cfg_log_verbose & LOG_SLI) {
11982                         list_for_each_entry(iocb, &pring->txq, list) {
11983                                 txq_cnt++;
11984                         }
11985                         list_for_each_entry(iocb, &pring->txcmplq, list) {
11986                                 txcmplq_cnt++;
11987                         }
11988                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11989                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11990                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11991                 }
11992                 return retval;
11993         } else {
11994                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11995                                 "0332 IOCB wait issue failed, Data x%x\n",
11996                                 retval);
11997                 retval = IOCB_ERROR;
11998         }
11999
12000         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
12001                 if (lpfc_readl(phba->HCregaddr, &creg_val))
12002                         return IOCB_ERROR;
12003                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12004                 writel(creg_val, phba->HCregaddr);
12005                 readl(phba->HCregaddr); /* flush */
12006         }
12007
12008         if (prspiocbq)
12009                 piocb->context2 = NULL;
12010
12011         piocb->context_un.wait_queue = NULL;
12012         piocb->iocb_cmpl = NULL;
12013         return retval;
12014 }
12015
12016 /**
12017  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
12018  * @phba: Pointer to HBA context object.
12019  * @pmboxq: Pointer to driver mailbox object.
12020  * @timeout: Timeout in number of seconds.
12021  *
12022  * This function issues the mailbox to firmware and waits for the
12023  * mailbox command to complete. If the mailbox command is not
12024  * completed within timeout seconds, it returns MBX_TIMEOUT.
12025  * The function waits for the mailbox completion using an
12026  * interruptible wait. If the thread is woken up due to a
12027  * signal, MBX_TIMEOUT error is returned to the caller. Caller
12028  * should not free the mailbox resources, if this function returns
12029  * MBX_TIMEOUT.
12030  * This function will sleep while waiting for mailbox completion.
12031  * So, this function should not be called from any context which
12032  * does not allow sleeping. Due to the same reason, this function
12033  * cannot be called with interrupt disabled.
12034  * This function assumes that the mailbox completion occurs while
12035  * this function sleep. So, this function cannot be called from
12036  * the worker thread which processes mailbox completion.
12037  * This function is called in the context of HBA management
12038  * applications.
12039  * This function returns MBX_SUCCESS when successful.
12040  * This function is called with no lock held.
12041  **/
12042 int
12043 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
12044                          uint32_t timeout)
12045 {
12046         struct completion mbox_done;
12047         int retval;
12048         unsigned long flag;
12049
12050         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
12051         /* setup wake call as IOCB callback */
12052         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
12053
12054         /* setup context3 field to pass wait_queue pointer to wake function  */
12055         init_completion(&mbox_done);
12056         pmboxq->context3 = &mbox_done;
12057         /* now issue the command */
12058         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
12059         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
12060                 wait_for_completion_timeout(&mbox_done,
12061                                             msecs_to_jiffies(timeout * 1000));
12062
12063                 spin_lock_irqsave(&phba->hbalock, flag);
12064                 pmboxq->context3 = NULL;
12065                 /*
12066                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
12067                  * else do not free the resources.
12068                  */
12069                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
12070                         retval = MBX_SUCCESS;
12071                 } else {
12072                         retval = MBX_TIMEOUT;
12073                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12074                 }
12075                 spin_unlock_irqrestore(&phba->hbalock, flag);
12076         }
12077         return retval;
12078 }
12079
12080 /**
12081  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
12082  * @phba: Pointer to HBA context.
12083  *
12084  * This function is called to shutdown the driver's mailbox sub-system.
12085  * It first marks the mailbox sub-system is in a block state to prevent
12086  * the asynchronous mailbox command from issued off the pending mailbox
12087  * command queue. If the mailbox command sub-system shutdown is due to
12088  * HBA error conditions such as EEH or ERATT, this routine shall invoke
12089  * the mailbox sub-system flush routine to forcefully bring down the
12090  * mailbox sub-system. Otherwise, if it is due to normal condition (such
12091  * as with offline or HBA function reset), this routine will wait for the
12092  * outstanding mailbox command to complete before invoking the mailbox
12093  * sub-system flush routine to gracefully bring down mailbox sub-system.
12094  **/
12095 void
12096 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
12097 {
12098         struct lpfc_sli *psli = &phba->sli;
12099         unsigned long timeout;
12100
12101         if (mbx_action == LPFC_MBX_NO_WAIT) {
12102                 /* delay 100ms for port state */
12103                 msleep(100);
12104                 lpfc_sli_mbox_sys_flush(phba);
12105                 return;
12106         }
12107         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
12108
12109         /* Disable softirqs, including timers from obtaining phba->hbalock */
12110         local_bh_disable();
12111
12112         spin_lock_irq(&phba->hbalock);
12113         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
12114
12115         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
12116                 /* Determine how long we might wait for the active mailbox
12117                  * command to be gracefully completed by firmware.
12118                  */
12119                 if (phba->sli.mbox_active)
12120                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12121                                                 phba->sli.mbox_active) *
12122                                                 1000) + jiffies;
12123                 spin_unlock_irq(&phba->hbalock);
12124
12125                 /* Enable softirqs again, done with phba->hbalock */
12126                 local_bh_enable();
12127
12128                 while (phba->sli.mbox_active) {
12129                         /* Check active mailbox complete status every 2ms */
12130                         msleep(2);
12131                         if (time_after(jiffies, timeout))
12132                                 /* Timeout, let the mailbox flush routine to
12133                                  * forcefully release active mailbox command
12134                                  */
12135                                 break;
12136                 }
12137         } else {
12138                 spin_unlock_irq(&phba->hbalock);
12139
12140                 /* Enable softirqs again, done with phba->hbalock */
12141                 local_bh_enable();
12142         }
12143
12144         lpfc_sli_mbox_sys_flush(phba);
12145 }
12146
12147 /**
12148  * lpfc_sli_eratt_read - read sli-3 error attention events
12149  * @phba: Pointer to HBA context.
12150  *
12151  * This function is called to read the SLI3 device error attention registers
12152  * for possible error attention events. The caller must hold the hostlock
12153  * with spin_lock_irq().
12154  *
12155  * This function returns 1 when there is Error Attention in the Host Attention
12156  * Register and returns 0 otherwise.
12157  **/
12158 static int
12159 lpfc_sli_eratt_read(struct lpfc_hba *phba)
12160 {
12161         uint32_t ha_copy;
12162
12163         /* Read chip Host Attention (HA) register */
12164         if (lpfc_readl(phba->HAregaddr, &ha_copy))
12165                 goto unplug_err;
12166
12167         if (ha_copy & HA_ERATT) {
12168                 /* Read host status register to retrieve error event */
12169                 if (lpfc_sli_read_hs(phba))
12170                         goto unplug_err;
12171
12172                 /* Check if there is a deferred error condition is active */
12173                 if ((HS_FFER1 & phba->work_hs) &&
12174                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12175                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
12176                         phba->hba_flag |= DEFER_ERATT;
12177                         /* Clear all interrupt enable conditions */
12178                         writel(0, phba->HCregaddr);
12179                         readl(phba->HCregaddr);
12180                 }
12181
12182                 /* Set the driver HA work bitmap */
12183                 phba->work_ha |= HA_ERATT;
12184                 /* Indicate polling handles this ERATT */
12185                 phba->hba_flag |= HBA_ERATT_HANDLED;
12186                 return 1;
12187         }
12188         return 0;
12189
12190 unplug_err:
12191         /* Set the driver HS work bitmap */
12192         phba->work_hs |= UNPLUG_ERR;
12193         /* Set the driver HA work bitmap */
12194         phba->work_ha |= HA_ERATT;
12195         /* Indicate polling handles this ERATT */
12196         phba->hba_flag |= HBA_ERATT_HANDLED;
12197         return 1;
12198 }
12199
12200 /**
12201  * lpfc_sli4_eratt_read - read sli-4 error attention events
12202  * @phba: Pointer to HBA context.
12203  *
12204  * This function is called to read the SLI4 device error attention registers
12205  * for possible error attention events. The caller must hold the hostlock
12206  * with spin_lock_irq().
12207  *
12208  * This function returns 1 when there is Error Attention in the Host Attention
12209  * Register and returns 0 otherwise.
12210  **/
12211 static int
12212 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12213 {
12214         uint32_t uerr_sta_hi, uerr_sta_lo;
12215         uint32_t if_type, portsmphr;
12216         struct lpfc_register portstat_reg;
12217
12218         /*
12219          * For now, use the SLI4 device internal unrecoverable error
12220          * registers for error attention. This can be changed later.
12221          */
12222         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12223         switch (if_type) {
12224         case LPFC_SLI_INTF_IF_TYPE_0:
12225                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12226                         &uerr_sta_lo) ||
12227                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12228                         &uerr_sta_hi)) {
12229                         phba->work_hs |= UNPLUG_ERR;
12230                         phba->work_ha |= HA_ERATT;
12231                         phba->hba_flag |= HBA_ERATT_HANDLED;
12232                         return 1;
12233                 }
12234                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12235                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12236                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12237                                         "1423 HBA Unrecoverable error: "
12238                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12239                                         "ue_mask_lo_reg=0x%x, "
12240                                         "ue_mask_hi_reg=0x%x\n",
12241                                         uerr_sta_lo, uerr_sta_hi,
12242                                         phba->sli4_hba.ue_mask_lo,
12243                                         phba->sli4_hba.ue_mask_hi);
12244                         phba->work_status[0] = uerr_sta_lo;
12245                         phba->work_status[1] = uerr_sta_hi;
12246                         phba->work_ha |= HA_ERATT;
12247                         phba->hba_flag |= HBA_ERATT_HANDLED;
12248                         return 1;
12249                 }
12250                 break;
12251         case LPFC_SLI_INTF_IF_TYPE_2:
12252         case LPFC_SLI_INTF_IF_TYPE_6:
12253                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12254                         &portstat_reg.word0) ||
12255                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12256                         &portsmphr)){
12257                         phba->work_hs |= UNPLUG_ERR;
12258                         phba->work_ha |= HA_ERATT;
12259                         phba->hba_flag |= HBA_ERATT_HANDLED;
12260                         return 1;
12261                 }
12262                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12263                         phba->work_status[0] =
12264                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12265                         phba->work_status[1] =
12266                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12267                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12268                                         "2885 Port Status Event: "
12269                                         "port status reg 0x%x, "
12270                                         "port smphr reg 0x%x, "
12271                                         "error 1=0x%x, error 2=0x%x\n",
12272                                         portstat_reg.word0,
12273                                         portsmphr,
12274                                         phba->work_status[0],
12275                                         phba->work_status[1]);
12276                         phba->work_ha |= HA_ERATT;
12277                         phba->hba_flag |= HBA_ERATT_HANDLED;
12278                         return 1;
12279                 }
12280                 break;
12281         case LPFC_SLI_INTF_IF_TYPE_1:
12282         default:
12283                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12284                                 "2886 HBA Error Attention on unsupported "
12285                                 "if type %d.", if_type);
12286                 return 1;
12287         }
12288
12289         return 0;
12290 }
12291
12292 /**
12293  * lpfc_sli_check_eratt - check error attention events
12294  * @phba: Pointer to HBA context.
12295  *
12296  * This function is called from timer soft interrupt context to check HBA's
12297  * error attention register bit for error attention events.
12298  *
12299  * This function returns 1 when there is Error Attention in the Host Attention
12300  * Register and returns 0 otherwise.
12301  **/
12302 int
12303 lpfc_sli_check_eratt(struct lpfc_hba *phba)
12304 {
12305         uint32_t ha_copy;
12306
12307         /* If somebody is waiting to handle an eratt, don't process it
12308          * here. The brdkill function will do this.
12309          */
12310         if (phba->link_flag & LS_IGNORE_ERATT)
12311                 return 0;
12312
12313         /* Check if interrupt handler handles this ERATT */
12314         spin_lock_irq(&phba->hbalock);
12315         if (phba->hba_flag & HBA_ERATT_HANDLED) {
12316                 /* Interrupt handler has handled ERATT */
12317                 spin_unlock_irq(&phba->hbalock);
12318                 return 0;
12319         }
12320
12321         /*
12322          * If there is deferred error attention, do not check for error
12323          * attention
12324          */
12325         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12326                 spin_unlock_irq(&phba->hbalock);
12327                 return 0;
12328         }
12329
12330         /* If PCI channel is offline, don't process it */
12331         if (unlikely(pci_channel_offline(phba->pcidev))) {
12332                 spin_unlock_irq(&phba->hbalock);
12333                 return 0;
12334         }
12335
12336         switch (phba->sli_rev) {
12337         case LPFC_SLI_REV2:
12338         case LPFC_SLI_REV3:
12339                 /* Read chip Host Attention (HA) register */
12340                 ha_copy = lpfc_sli_eratt_read(phba);
12341                 break;
12342         case LPFC_SLI_REV4:
12343                 /* Read device Uncoverable Error (UERR) registers */
12344                 ha_copy = lpfc_sli4_eratt_read(phba);
12345                 break;
12346         default:
12347                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12348                                 "0299 Invalid SLI revision (%d)\n",
12349                                 phba->sli_rev);
12350                 ha_copy = 0;
12351                 break;
12352         }
12353         spin_unlock_irq(&phba->hbalock);
12354
12355         return ha_copy;
12356 }
12357
12358 /**
12359  * lpfc_intr_state_check - Check device state for interrupt handling
12360  * @phba: Pointer to HBA context.
12361  *
12362  * This inline routine checks whether a device or its PCI slot is in a state
12363  * that the interrupt should be handled.
12364  *
12365  * This function returns 0 if the device or the PCI slot is in a state that
12366  * interrupt should be handled, otherwise -EIO.
12367  */
12368 static inline int
12369 lpfc_intr_state_check(struct lpfc_hba *phba)
12370 {
12371         /* If the pci channel is offline, ignore all the interrupts */
12372         if (unlikely(pci_channel_offline(phba->pcidev)))
12373                 return -EIO;
12374
12375         /* Update device level interrupt statistics */
12376         phba->sli.slistat.sli_intr++;
12377
12378         /* Ignore all interrupts during initialization. */
12379         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12380                 return -EIO;
12381
12382         return 0;
12383 }
12384
12385 /**
12386  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
12387  * @irq: Interrupt number.
12388  * @dev_id: The device context pointer.
12389  *
12390  * This function is directly called from the PCI layer as an interrupt
12391  * service routine when device with SLI-3 interface spec is enabled with
12392  * MSI-X multi-message interrupt mode and there are slow-path events in
12393  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12394  * interrupt mode, this function is called as part of the device-level
12395  * interrupt handler. When the PCI slot is in error recovery or the HBA
12396  * is undergoing initialization, the interrupt handler will not process
12397  * the interrupt. The link attention and ELS ring attention events are
12398  * handled by the worker thread. The interrupt handler signals the worker
12399  * thread and returns for these events. This function is called without
12400  * any lock held. It gets the hbalock to access and update SLI data
12401  * structures.
12402  *
12403  * This function returns IRQ_HANDLED when interrupt is handled else it
12404  * returns IRQ_NONE.
12405  **/
12406 irqreturn_t
12407 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
12408 {
12409         struct lpfc_hba  *phba;
12410         uint32_t ha_copy, hc_copy;
12411         uint32_t work_ha_copy;
12412         unsigned long status;
12413         unsigned long iflag;
12414         uint32_t control;
12415
12416         MAILBOX_t *mbox, *pmbox;
12417         struct lpfc_vport *vport;
12418         struct lpfc_nodelist *ndlp;
12419         struct lpfc_dmabuf *mp;
12420         LPFC_MBOXQ_t *pmb;
12421         int rc;
12422
12423         /*
12424          * Get the driver's phba structure from the dev_id and
12425          * assume the HBA is not interrupting.
12426          */
12427         phba = (struct lpfc_hba *)dev_id;
12428
12429         if (unlikely(!phba))
12430                 return IRQ_NONE;
12431
12432         /*
12433          * Stuff needs to be attented to when this function is invoked as an
12434          * individual interrupt handler in MSI-X multi-message interrupt mode
12435          */
12436         if (phba->intr_type == MSIX) {
12437                 /* Check device state for handling interrupt */
12438                 if (lpfc_intr_state_check(phba))
12439                         return IRQ_NONE;
12440                 /* Need to read HA REG for slow-path events */
12441                 spin_lock_irqsave(&phba->hbalock, iflag);
12442                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12443                         goto unplug_error;
12444                 /* If somebody is waiting to handle an eratt don't process it
12445                  * here. The brdkill function will do this.
12446                  */
12447                 if (phba->link_flag & LS_IGNORE_ERATT)
12448                         ha_copy &= ~HA_ERATT;
12449                 /* Check the need for handling ERATT in interrupt handler */
12450                 if (ha_copy & HA_ERATT) {
12451                         if (phba->hba_flag & HBA_ERATT_HANDLED)
12452                                 /* ERATT polling has handled ERATT */
12453                                 ha_copy &= ~HA_ERATT;
12454                         else
12455                                 /* Indicate interrupt handler handles ERATT */
12456                                 phba->hba_flag |= HBA_ERATT_HANDLED;
12457                 }
12458
12459                 /*
12460                  * If there is deferred error attention, do not check for any
12461                  * interrupt.
12462                  */
12463                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12464                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12465                         return IRQ_NONE;
12466                 }
12467
12468                 /* Clear up only attention source related to slow-path */
12469                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12470                         goto unplug_error;
12471
12472                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12473                         HC_LAINT_ENA | HC_ERINT_ENA),
12474                         phba->HCregaddr);
12475                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12476                         phba->HAregaddr);
12477                 writel(hc_copy, phba->HCregaddr);
12478                 readl(phba->HAregaddr); /* flush */
12479                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12480         } else
12481                 ha_copy = phba->ha_copy;
12482
12483         work_ha_copy = ha_copy & phba->work_ha_mask;
12484
12485         if (work_ha_copy) {
12486                 if (work_ha_copy & HA_LATT) {
12487                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12488                                 /*
12489                                  * Turn off Link Attention interrupts
12490                                  * until CLEAR_LA done
12491                                  */
12492                                 spin_lock_irqsave(&phba->hbalock, iflag);
12493                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
12494                                 if (lpfc_readl(phba->HCregaddr, &control))
12495                                         goto unplug_error;
12496                                 control &= ~HC_LAINT_ENA;
12497                                 writel(control, phba->HCregaddr);
12498                                 readl(phba->HCregaddr); /* flush */
12499                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12500                         }
12501                         else
12502                                 work_ha_copy &= ~HA_LATT;
12503                 }
12504
12505                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
12506                         /*
12507                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12508                          * the only slow ring.
12509                          */
12510                         status = (work_ha_copy &
12511                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
12512                         status >>= (4*LPFC_ELS_RING);
12513                         if (status & HA_RXMASK) {
12514                                 spin_lock_irqsave(&phba->hbalock, iflag);
12515                                 if (lpfc_readl(phba->HCregaddr, &control))
12516                                         goto unplug_error;
12517
12518                                 lpfc_debugfs_slow_ring_trc(phba,
12519                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
12520                                 control, status,
12521                                 (uint32_t)phba->sli.slistat.sli_intr);
12522
12523                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
12524                                         lpfc_debugfs_slow_ring_trc(phba,
12525                                                 "ISR Disable ring:"
12526                                                 "pwork:x%x hawork:x%x wait:x%x",
12527                                                 phba->work_ha, work_ha_copy,
12528                                                 (uint32_t)((unsigned long)
12529                                                 &phba->work_waitq));
12530
12531                                         control &=
12532                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
12533                                         writel(control, phba->HCregaddr);
12534                                         readl(phba->HCregaddr); /* flush */
12535                                 }
12536                                 else {
12537                                         lpfc_debugfs_slow_ring_trc(phba,
12538                                                 "ISR slow ring:   pwork:"
12539                                                 "x%x hawork:x%x wait:x%x",
12540                                                 phba->work_ha, work_ha_copy,
12541                                                 (uint32_t)((unsigned long)
12542                                                 &phba->work_waitq));
12543                                 }
12544                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12545                         }
12546                 }
12547                 spin_lock_irqsave(&phba->hbalock, iflag);
12548                 if (work_ha_copy & HA_ERATT) {
12549                         if (lpfc_sli_read_hs(phba))
12550                                 goto unplug_error;
12551                         /*
12552                          * Check if there is a deferred error condition
12553                          * is active
12554                          */
12555                         if ((HS_FFER1 & phba->work_hs) &&
12556                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12557                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
12558                                   phba->work_hs)) {
12559                                 phba->hba_flag |= DEFER_ERATT;
12560                                 /* Clear all interrupt enable conditions */
12561                                 writel(0, phba->HCregaddr);
12562                                 readl(phba->HCregaddr);
12563                         }
12564                 }
12565
12566                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
12567                         pmb = phba->sli.mbox_active;
12568                         pmbox = &pmb->u.mb;
12569                         mbox = phba->mbox;
12570                         vport = pmb->vport;
12571
12572                         /* First check out the status word */
12573                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12574                         if (pmbox->mbxOwner != OWN_HOST) {
12575                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12576                                 /*
12577                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
12578                                  * mbxStatus <status>
12579                                  */
12580                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12581                                                 LOG_SLI,
12582                                                 "(%d):0304 Stray Mailbox "
12583                                                 "Interrupt mbxCommand x%x "
12584                                                 "mbxStatus x%x\n",
12585                                                 (vport ? vport->vpi : 0),
12586                                                 pmbox->mbxCommand,
12587                                                 pmbox->mbxStatus);
12588                                 /* clear mailbox attention bit */
12589                                 work_ha_copy &= ~HA_MBATT;
12590                         } else {
12591                                 phba->sli.mbox_active = NULL;
12592                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12593                                 phba->last_completion_time = jiffies;
12594                                 del_timer(&phba->sli.mbox_tmo);
12595                                 if (pmb->mbox_cmpl) {
12596                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
12597                                                         MAILBOX_CMD_SIZE);
12598                                         if (pmb->out_ext_byte_len &&
12599                                                 pmb->ctx_buf)
12600                                                 lpfc_sli_pcimem_bcopy(
12601                                                 phba->mbox_ext,
12602                                                 pmb->ctx_buf,
12603                                                 pmb->out_ext_byte_len);
12604                                 }
12605                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12606                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12607
12608                                         lpfc_debugfs_disc_trc(vport,
12609                                                 LPFC_DISC_TRC_MBOX_VPORT,
12610                                                 "MBOX dflt rpi: : "
12611                                                 "status:x%x rpi:x%x",
12612                                                 (uint32_t)pmbox->mbxStatus,
12613                                                 pmbox->un.varWords[0], 0);
12614
12615                                         if (!pmbox->mbxStatus) {
12616                                                 mp = (struct lpfc_dmabuf *)
12617                                                         (pmb->ctx_buf);
12618                                                 ndlp = (struct lpfc_nodelist *)
12619                                                         pmb->ctx_ndlp;
12620
12621                                                 /* Reg_LOGIN of dflt RPI was
12622                                                  * successful. new lets get
12623                                                  * rid of the RPI using the
12624                                                  * same mbox buffer.
12625                                                  */
12626                                                 lpfc_unreg_login(phba,
12627                                                         vport->vpi,
12628                                                         pmbox->un.varWords[0],
12629                                                         pmb);
12630                                                 pmb->mbox_cmpl =
12631                                                         lpfc_mbx_cmpl_dflt_rpi;
12632                                                 pmb->ctx_buf = mp;
12633                                                 pmb->ctx_ndlp = ndlp;
12634                                                 pmb->vport = vport;
12635                                                 rc = lpfc_sli_issue_mbox(phba,
12636                                                                 pmb,
12637                                                                 MBX_NOWAIT);
12638                                                 if (rc != MBX_BUSY)
12639                                                         lpfc_printf_log(phba,
12640                                                         KERN_ERR,
12641                                                         LOG_MBOX | LOG_SLI,
12642                                                         "0350 rc should have"
12643                                                         "been MBX_BUSY\n");
12644                                                 if (rc != MBX_NOT_FINISHED)
12645                                                         goto send_current_mbox;
12646                                         }
12647                                 }
12648                                 spin_lock_irqsave(
12649                                                 &phba->pport->work_port_lock,
12650                                                 iflag);
12651                                 phba->pport->work_port_events &=
12652                                         ~WORKER_MBOX_TMO;
12653                                 spin_unlock_irqrestore(
12654                                                 &phba->pport->work_port_lock,
12655                                                 iflag);
12656                                 lpfc_mbox_cmpl_put(phba, pmb);
12657                         }
12658                 } else
12659                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12660
12661                 if ((work_ha_copy & HA_MBATT) &&
12662                     (phba->sli.mbox_active == NULL)) {
12663 send_current_mbox:
12664                         /* Process next mailbox command if there is one */
12665                         do {
12666                                 rc = lpfc_sli_issue_mbox(phba, NULL,
12667                                                          MBX_NOWAIT);
12668                         } while (rc == MBX_NOT_FINISHED);
12669                         if (rc != MBX_SUCCESS)
12670                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12671                                                 LOG_SLI, "0349 rc should be "
12672                                                 "MBX_SUCCESS\n");
12673                 }
12674
12675                 spin_lock_irqsave(&phba->hbalock, iflag);
12676                 phba->work_ha |= work_ha_copy;
12677                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12678                 lpfc_worker_wake_up(phba);
12679         }
12680         return IRQ_HANDLED;
12681 unplug_error:
12682         spin_unlock_irqrestore(&phba->hbalock, iflag);
12683         return IRQ_HANDLED;
12684
12685 } /* lpfc_sli_sp_intr_handler */
12686
12687 /**
12688  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
12689  * @irq: Interrupt number.
12690  * @dev_id: The device context pointer.
12691  *
12692  * This function is directly called from the PCI layer as an interrupt
12693  * service routine when device with SLI-3 interface spec is enabled with
12694  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12695  * ring event in the HBA. However, when the device is enabled with either
12696  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12697  * device-level interrupt handler. When the PCI slot is in error recovery
12698  * or the HBA is undergoing initialization, the interrupt handler will not
12699  * process the interrupt. The SCSI FCP fast-path ring event are handled in
12700  * the intrrupt context. This function is called without any lock held.
12701  * It gets the hbalock to access and update SLI data structures.
12702  *
12703  * This function returns IRQ_HANDLED when interrupt is handled else it
12704  * returns IRQ_NONE.
12705  **/
12706 irqreturn_t
12707 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
12708 {
12709         struct lpfc_hba  *phba;
12710         uint32_t ha_copy;
12711         unsigned long status;
12712         unsigned long iflag;
12713         struct lpfc_sli_ring *pring;
12714
12715         /* Get the driver's phba structure from the dev_id and
12716          * assume the HBA is not interrupting.
12717          */
12718         phba = (struct lpfc_hba *) dev_id;
12719
12720         if (unlikely(!phba))
12721                 return IRQ_NONE;
12722
12723         /*
12724          * Stuff needs to be attented to when this function is invoked as an
12725          * individual interrupt handler in MSI-X multi-message interrupt mode
12726          */
12727         if (phba->intr_type == MSIX) {
12728                 /* Check device state for handling interrupt */
12729                 if (lpfc_intr_state_check(phba))
12730                         return IRQ_NONE;
12731                 /* Need to read HA REG for FCP ring and other ring events */
12732                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12733                         return IRQ_HANDLED;
12734                 /* Clear up only attention source related to fast-path */
12735                 spin_lock_irqsave(&phba->hbalock, iflag);
12736                 /*
12737                  * If there is deferred error attention, do not check for
12738                  * any interrupt.
12739                  */
12740                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12741                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12742                         return IRQ_NONE;
12743                 }
12744                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12745                         phba->HAregaddr);
12746                 readl(phba->HAregaddr); /* flush */
12747                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12748         } else
12749                 ha_copy = phba->ha_copy;
12750
12751         /*
12752          * Process all events on FCP ring. Take the optimized path for FCP IO.
12753          */
12754         ha_copy &= ~(phba->work_ha_mask);
12755
12756         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12757         status >>= (4*LPFC_FCP_RING);
12758         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12759         if (status & HA_RXMASK)
12760                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
12761
12762         if (phba->cfg_multi_ring_support == 2) {
12763                 /*
12764                  * Process all events on extra ring. Take the optimized path
12765                  * for extra ring IO.
12766                  */
12767                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12768                 status >>= (4*LPFC_EXTRA_RING);
12769                 if (status & HA_RXMASK) {
12770                         lpfc_sli_handle_fast_ring_event(phba,
12771                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12772                                         status);
12773                 }
12774         }
12775         return IRQ_HANDLED;
12776 }  /* lpfc_sli_fp_intr_handler */
12777
12778 /**
12779  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12780  * @irq: Interrupt number.
12781  * @dev_id: The device context pointer.
12782  *
12783  * This function is the HBA device-level interrupt handler to device with
12784  * SLI-3 interface spec, called from the PCI layer when either MSI or
12785  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12786  * requires driver attention. This function invokes the slow-path interrupt
12787  * attention handling function and fast-path interrupt attention handling
12788  * function in turn to process the relevant HBA attention events. This
12789  * function is called without any lock held. It gets the hbalock to access
12790  * and update SLI data structures.
12791  *
12792  * This function returns IRQ_HANDLED when interrupt is handled, else it
12793  * returns IRQ_NONE.
12794  **/
12795 irqreturn_t
12796 lpfc_sli_intr_handler(int irq, void *dev_id)
12797 {
12798         struct lpfc_hba  *phba;
12799         irqreturn_t sp_irq_rc, fp_irq_rc;
12800         unsigned long status1, status2;
12801         uint32_t hc_copy;
12802
12803         /*
12804          * Get the driver's phba structure from the dev_id and
12805          * assume the HBA is not interrupting.
12806          */
12807         phba = (struct lpfc_hba *) dev_id;
12808
12809         if (unlikely(!phba))
12810                 return IRQ_NONE;
12811
12812         /* Check device state for handling interrupt */
12813         if (lpfc_intr_state_check(phba))
12814                 return IRQ_NONE;
12815
12816         spin_lock(&phba->hbalock);
12817         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12818                 spin_unlock(&phba->hbalock);
12819                 return IRQ_HANDLED;
12820         }
12821
12822         if (unlikely(!phba->ha_copy)) {
12823                 spin_unlock(&phba->hbalock);
12824                 return IRQ_NONE;
12825         } else if (phba->ha_copy & HA_ERATT) {
12826                 if (phba->hba_flag & HBA_ERATT_HANDLED)
12827                         /* ERATT polling has handled ERATT */
12828                         phba->ha_copy &= ~HA_ERATT;
12829                 else
12830                         /* Indicate interrupt handler handles ERATT */
12831                         phba->hba_flag |= HBA_ERATT_HANDLED;
12832         }
12833
12834         /*
12835          * If there is deferred error attention, do not check for any interrupt.
12836          */
12837         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12838                 spin_unlock(&phba->hbalock);
12839                 return IRQ_NONE;
12840         }
12841
12842         /* Clear attention sources except link and error attentions */
12843         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12844                 spin_unlock(&phba->hbalock);
12845                 return IRQ_HANDLED;
12846         }
12847         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12848                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12849                 phba->HCregaddr);
12850         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12851         writel(hc_copy, phba->HCregaddr);
12852         readl(phba->HAregaddr); /* flush */
12853         spin_unlock(&phba->hbalock);
12854
12855         /*
12856          * Invokes slow-path host attention interrupt handling as appropriate.
12857          */
12858
12859         /* status of events with mailbox and link attention */
12860         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12861
12862         /* status of events with ELS ring */
12863         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
12864         status2 >>= (4*LPFC_ELS_RING);
12865
12866         if (status1 || (status2 & HA_RXMASK))
12867                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12868         else
12869                 sp_irq_rc = IRQ_NONE;
12870
12871         /*
12872          * Invoke fast-path host attention interrupt handling as appropriate.
12873          */
12874
12875         /* status of events with FCP ring */
12876         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12877         status1 >>= (4*LPFC_FCP_RING);
12878
12879         /* status of events with extra ring */
12880         if (phba->cfg_multi_ring_support == 2) {
12881                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12882                 status2 >>= (4*LPFC_EXTRA_RING);
12883         } else
12884                 status2 = 0;
12885
12886         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12887                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12888         else
12889                 fp_irq_rc = IRQ_NONE;
12890
12891         /* Return device-level interrupt handling status */
12892         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12893 }  /* lpfc_sli_intr_handler */
12894
12895 /**
12896  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12897  * @phba: pointer to lpfc hba data structure.
12898  *
12899  * This routine is invoked by the worker thread to process all the pending
12900  * SLI4 els abort xri events.
12901  **/
12902 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12903 {
12904         struct lpfc_cq_event *cq_event;
12905
12906         /* First, declare the els xri abort event has been handled */
12907         spin_lock_irq(&phba->hbalock);
12908         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12909         spin_unlock_irq(&phba->hbalock);
12910         /* Now, handle all the els xri abort events */
12911         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12912                 /* Get the first event from the head of the event queue */
12913                 spin_lock_irq(&phba->hbalock);
12914                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12915                                  cq_event, struct lpfc_cq_event, list);
12916                 spin_unlock_irq(&phba->hbalock);
12917                 /* Notify aborted XRI for ELS work queue */
12918                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12919                 /* Free the event processed back to the free pool */
12920                 lpfc_sli4_cq_event_release(phba, cq_event);
12921         }
12922 }
12923
12924 /**
12925  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12926  * @phba: pointer to lpfc hba data structure
12927  * @pIocbIn: pointer to the rspiocbq
12928  * @pIocbOut: pointer to the cmdiocbq
12929  * @wcqe: pointer to the complete wcqe
12930  *
12931  * This routine transfers the fields of a command iocbq to a response iocbq
12932  * by copying all the IOCB fields from command iocbq and transferring the
12933  * completion status information from the complete wcqe.
12934  **/
12935 static void
12936 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12937                               struct lpfc_iocbq *pIocbIn,
12938                               struct lpfc_iocbq *pIocbOut,
12939                               struct lpfc_wcqe_complete *wcqe)
12940 {
12941         int numBdes, i;
12942         unsigned long iflags;
12943         uint32_t status, max_response;
12944         struct lpfc_dmabuf *dmabuf;
12945         struct ulp_bde64 *bpl, bde;
12946         size_t offset = offsetof(struct lpfc_iocbq, iocb);
12947
12948         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12949                sizeof(struct lpfc_iocbq) - offset);
12950         /* Map WCQE parameters into irspiocb parameters */
12951         status = bf_get(lpfc_wcqe_c_status, wcqe);
12952         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12953         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12954                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12955                         pIocbIn->iocb.un.fcpi.fcpi_parm =
12956                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
12957                                         wcqe->total_data_placed;
12958                 else
12959                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12960         else {
12961                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12962                 switch (pIocbOut->iocb.ulpCommand) {
12963                 case CMD_ELS_REQUEST64_CR:
12964                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12965                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
12966                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12967                         max_response = bde.tus.f.bdeSize;
12968                         break;
12969                 case CMD_GEN_REQUEST64_CR:
12970                         max_response = 0;
12971                         if (!pIocbOut->context3)
12972                                 break;
12973                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12974                                         sizeof(struct ulp_bde64);
12975                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12976                         bpl = (struct ulp_bde64 *)dmabuf->virt;
12977                         for (i = 0; i < numBdes; i++) {
12978                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12979                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12980                                         max_response += bde.tus.f.bdeSize;
12981                         }
12982                         break;
12983                 default:
12984                         max_response = wcqe->total_data_placed;
12985                         break;
12986                 }
12987                 if (max_response < wcqe->total_data_placed)
12988                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12989                 else
12990                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12991                                 wcqe->total_data_placed;
12992         }
12993
12994         /* Convert BG errors for completion status */
12995         if (status == CQE_STATUS_DI_ERROR) {
12996                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12997
12998                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12999                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13000                 else
13001                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13002
13003                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13004                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13005                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13006                                 BGS_GUARD_ERR_MASK;
13007                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13008                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13009                                 BGS_APPTAG_ERR_MASK;
13010                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13011                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13012                                 BGS_REFTAG_ERR_MASK;
13013
13014                 /* Check to see if there was any good data before the error */
13015                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13016                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13017                                 BGS_HI_WATER_MARK_PRESENT_MASK;
13018                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
13019                                 wcqe->total_data_placed;
13020                 }
13021
13022                 /*
13023                 * Set ALL the error bits to indicate we don't know what
13024                 * type of error it is.
13025                 */
13026                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13027                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13028                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13029                                 BGS_GUARD_ERR_MASK);
13030         }
13031
13032         /* Pick up HBA exchange busy condition */
13033         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13034                 spin_lock_irqsave(&phba->hbalock, iflags);
13035                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13036                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13037         }
13038 }
13039
13040 /**
13041  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13042  * @phba: Pointer to HBA context object.
13043  * @wcqe: Pointer to work-queue completion queue entry.
13044  *
13045  * This routine handles an ELS work-queue completion event and construct
13046  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13047  * discovery engine to handle.
13048  *
13049  * Return: Pointer to the receive IOCBQ, NULL otherwise.
13050  **/
13051 static struct lpfc_iocbq *
13052 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13053                                struct lpfc_iocbq *irspiocbq)
13054 {
13055         struct lpfc_sli_ring *pring;
13056         struct lpfc_iocbq *cmdiocbq;
13057         struct lpfc_wcqe_complete *wcqe;
13058         unsigned long iflags;
13059
13060         pring = lpfc_phba_elsring(phba);
13061         if (unlikely(!pring))
13062                 return NULL;
13063
13064         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
13065         spin_lock_irqsave(&pring->ring_lock, iflags);
13066         pring->stats.iocb_event++;
13067         /* Look up the ELS command IOCB and create pseudo response IOCB */
13068         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13069                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13070         if (unlikely(!cmdiocbq)) {
13071                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13072                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13073                                 "0386 ELS complete with no corresponding "
13074                                 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13075                                 wcqe->word0, wcqe->total_data_placed,
13076                                 wcqe->parameter, wcqe->word3);
13077                 lpfc_sli_release_iocbq(phba, irspiocbq);
13078                 return NULL;
13079         }
13080
13081         /* Put the iocb back on the txcmplq */
13082         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13083         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13084
13085         /* Fake the irspiocbq and copy necessary response information */
13086         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
13087
13088         return irspiocbq;
13089 }
13090
13091 inline struct lpfc_cq_event *
13092 lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13093 {
13094         struct lpfc_cq_event *cq_event;
13095
13096         /* Allocate a new internal CQ_EVENT entry */
13097         cq_event = lpfc_sli4_cq_event_alloc(phba);
13098         if (!cq_event) {
13099                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13100                                 "0602 Failed to alloc CQ_EVENT entry\n");
13101                 return NULL;
13102         }
13103
13104         /* Move the CQE into the event */
13105         memcpy(&cq_event->cqe, entry, size);
13106         return cq_event;
13107 }
13108
13109 /**
13110  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13111  * @phba: Pointer to HBA context object.
13112  * @cqe: Pointer to mailbox completion queue entry.
13113  *
13114  * This routine process a mailbox completion queue entry with asynchrous
13115  * event.
13116  *
13117  * Return: true if work posted to worker thread, otherwise false.
13118  **/
13119 static bool
13120 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13121 {
13122         struct lpfc_cq_event *cq_event;
13123         unsigned long iflags;
13124
13125         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13126                         "0392 Async Event: word0:x%x, word1:x%x, "
13127                         "word2:x%x, word3:x%x\n", mcqe->word0,
13128                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13129
13130         cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13131         if (!cq_event)
13132                 return false;
13133         spin_lock_irqsave(&phba->hbalock, iflags);
13134         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13135         /* Set the async event flag */
13136         phba->hba_flag |= ASYNC_EVENT;
13137         spin_unlock_irqrestore(&phba->hbalock, iflags);
13138
13139         return true;
13140 }
13141
13142 /**
13143  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13144  * @phba: Pointer to HBA context object.
13145  * @cqe: Pointer to mailbox completion queue entry.
13146  *
13147  * This routine process a mailbox completion queue entry with mailbox
13148  * completion event.
13149  *
13150  * Return: true if work posted to worker thread, otherwise false.
13151  **/
13152 static bool
13153 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13154 {
13155         uint32_t mcqe_status;
13156         MAILBOX_t *mbox, *pmbox;
13157         struct lpfc_mqe *mqe;
13158         struct lpfc_vport *vport;
13159         struct lpfc_nodelist *ndlp;
13160         struct lpfc_dmabuf *mp;
13161         unsigned long iflags;
13162         LPFC_MBOXQ_t *pmb;
13163         bool workposted = false;
13164         int rc;
13165
13166         /* If not a mailbox complete MCQE, out by checking mailbox consume */
13167         if (!bf_get(lpfc_trailer_completed, mcqe))
13168                 goto out_no_mqe_complete;
13169
13170         /* Get the reference to the active mbox command */
13171         spin_lock_irqsave(&phba->hbalock, iflags);
13172         pmb = phba->sli.mbox_active;
13173         if (unlikely(!pmb)) {
13174                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13175                                 "1832 No pending MBOX command to handle\n");
13176                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13177                 goto out_no_mqe_complete;
13178         }
13179         spin_unlock_irqrestore(&phba->hbalock, iflags);
13180         mqe = &pmb->u.mqe;
13181         pmbox = (MAILBOX_t *)&pmb->u.mqe;
13182         mbox = phba->mbox;
13183         vport = pmb->vport;
13184
13185         /* Reset heartbeat timer */
13186         phba->last_completion_time = jiffies;
13187         del_timer(&phba->sli.mbox_tmo);
13188
13189         /* Move mbox data to caller's mailbox region, do endian swapping */
13190         if (pmb->mbox_cmpl && mbox)
13191                 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
13192
13193         /*
13194          * For mcqe errors, conditionally move a modified error code to
13195          * the mbox so that the error will not be missed.
13196          */
13197         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13198         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13199                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13200                         bf_set(lpfc_mqe_status, mqe,
13201                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
13202         }
13203         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13204                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13205                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13206                                       "MBOX dflt rpi: status:x%x rpi:x%x",
13207                                       mcqe_status,
13208                                       pmbox->un.varWords[0], 0);
13209                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
13210                         mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13211                         ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
13212                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
13213                          * RID of the PPI using the same mbox buffer.
13214                          */
13215                         lpfc_unreg_login(phba, vport->vpi,
13216                                          pmbox->un.varWords[0], pmb);
13217                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
13218                         pmb->ctx_buf = mp;
13219                         pmb->ctx_ndlp = ndlp;
13220                         pmb->vport = vport;
13221                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13222                         if (rc != MBX_BUSY)
13223                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13224                                                 LOG_SLI, "0385 rc should "
13225                                                 "have been MBX_BUSY\n");
13226                         if (rc != MBX_NOT_FINISHED)
13227                                 goto send_current_mbox;
13228                 }
13229         }
13230         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13231         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13232         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13233
13234         /* There is mailbox completion work to do */
13235         spin_lock_irqsave(&phba->hbalock, iflags);
13236         __lpfc_mbox_cmpl_put(phba, pmb);
13237         phba->work_ha |= HA_MBATT;
13238         spin_unlock_irqrestore(&phba->hbalock, iflags);
13239         workposted = true;
13240
13241 send_current_mbox:
13242         spin_lock_irqsave(&phba->hbalock, iflags);
13243         /* Release the mailbox command posting token */
13244         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13245         /* Setting active mailbox pointer need to be in sync to flag clear */
13246         phba->sli.mbox_active = NULL;
13247         spin_unlock_irqrestore(&phba->hbalock, iflags);
13248         /* Wake up worker thread to post the next pending mailbox command */
13249         lpfc_worker_wake_up(phba);
13250 out_no_mqe_complete:
13251         if (bf_get(lpfc_trailer_consumed, mcqe))
13252                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13253         return workposted;
13254 }
13255
13256 /**
13257  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13258  * @phba: Pointer to HBA context object.
13259  * @cqe: Pointer to mailbox completion queue entry.
13260  *
13261  * This routine process a mailbox completion queue entry, it invokes the
13262  * proper mailbox complete handling or asynchrous event handling routine
13263  * according to the MCQE's async bit.
13264  *
13265  * Return: true if work posted to worker thread, otherwise false.
13266  **/
13267 static bool
13268 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
13269 {
13270         struct lpfc_mcqe mcqe;
13271         bool workposted;
13272
13273         /* Copy the mailbox MCQE and convert endian order as needed */
13274         lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
13275
13276         /* Invoke the proper event handling routine */
13277         if (!bf_get(lpfc_trailer_async, &mcqe))
13278                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13279         else
13280                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13281         return workposted;
13282 }
13283
13284 /**
13285  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13286  * @phba: Pointer to HBA context object.
13287  * @cq: Pointer to associated CQ
13288  * @wcqe: Pointer to work-queue completion queue entry.
13289  *
13290  * This routine handles an ELS work-queue completion event.
13291  *
13292  * Return: true if work posted to worker thread, otherwise false.
13293  **/
13294 static bool
13295 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13296                              struct lpfc_wcqe_complete *wcqe)
13297 {
13298         struct lpfc_iocbq *irspiocbq;
13299         unsigned long iflags;
13300         struct lpfc_sli_ring *pring = cq->pring;
13301         int txq_cnt = 0;
13302         int txcmplq_cnt = 0;
13303         int fcp_txcmplq_cnt = 0;
13304
13305         /* Check for response status */
13306         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13307                 /* Log the error status */
13308                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13309                                 "0357 ELS CQE error: status=x%x: "
13310                                 "CQE: %08x %08x %08x %08x\n",
13311                                 bf_get(lpfc_wcqe_c_status, wcqe),
13312                                 wcqe->word0, wcqe->total_data_placed,
13313                                 wcqe->parameter, wcqe->word3);
13314         }
13315
13316         /* Get an irspiocbq for later ELS response processing use */
13317         irspiocbq = lpfc_sli_get_iocbq(phba);
13318         if (!irspiocbq) {
13319                 if (!list_empty(&pring->txq))
13320                         txq_cnt++;
13321                 if (!list_empty(&pring->txcmplq))
13322                         txcmplq_cnt++;
13323                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13324                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13325                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
13326                         txq_cnt, phba->iocb_cnt,
13327                         fcp_txcmplq_cnt,
13328                         txcmplq_cnt);
13329                 return false;
13330         }
13331
13332         /* Save off the slow-path queue event for work thread to process */
13333         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
13334         spin_lock_irqsave(&phba->hbalock, iflags);
13335         list_add_tail(&irspiocbq->cq_event.list,
13336                       &phba->sli4_hba.sp_queue_event);
13337         phba->hba_flag |= HBA_SP_QUEUE_EVT;
13338         spin_unlock_irqrestore(&phba->hbalock, iflags);
13339
13340         return true;
13341 }
13342
13343 /**
13344  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13345  * @phba: Pointer to HBA context object.
13346  * @wcqe: Pointer to work-queue completion queue entry.
13347  *
13348  * This routine handles slow-path WQ entry consumed event by invoking the
13349  * proper WQ release routine to the slow-path WQ.
13350  **/
13351 static void
13352 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13353                              struct lpfc_wcqe_release *wcqe)
13354 {
13355         /* sanity check on queue memory */
13356         if (unlikely(!phba->sli4_hba.els_wq))
13357                 return;
13358         /* Check for the slow-path ELS work queue */
13359         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13360                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13361                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13362         else
13363                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13364                                 "2579 Slow-path wqe consume event carries "
13365                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13366                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13367                                 phba->sli4_hba.els_wq->queue_id);
13368 }
13369
13370 /**
13371  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13372  * @phba: Pointer to HBA context object.
13373  * @cq: Pointer to a WQ completion queue.
13374  * @wcqe: Pointer to work-queue completion queue entry.
13375  *
13376  * This routine handles an XRI abort event.
13377  *
13378  * Return: true if work posted to worker thread, otherwise false.
13379  **/
13380 static bool
13381 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13382                                    struct lpfc_queue *cq,
13383                                    struct sli4_wcqe_xri_aborted *wcqe)
13384 {
13385         bool workposted = false;
13386         struct lpfc_cq_event *cq_event;
13387         unsigned long iflags;
13388
13389         switch (cq->subtype) {
13390         case LPFC_FCP:
13391                 lpfc_sli4_fcp_xri_aborted(phba, wcqe, cq->hdwq);
13392                 workposted = false;
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                 cq_event->hdwq = cq->hdwq;
13401                 spin_lock_irqsave(&phba->hbalock, iflags);
13402                 list_add_tail(&cq_event->list,
13403                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13404                 /* Set the els xri abort event flag */
13405                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13406                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13407                 workposted = true;
13408                 break;
13409         case LPFC_NVME:
13410                 /* Notify aborted XRI for NVME work queue */
13411                 if (phba->nvmet_support)
13412                         lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13413                 else
13414                         lpfc_sli4_nvme_xri_aborted(phba, wcqe, cq->hdwq);
13415
13416                 workposted = false;
13417                 break;
13418         default:
13419                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13420                                 "0603 Invalid CQ subtype %d: "
13421                                 "%08x %08x %08x %08x\n",
13422                                 cq->subtype, wcqe->word0, wcqe->parameter,
13423                                 wcqe->word2, wcqe->word3);
13424                 workposted = false;
13425                 break;
13426         }
13427         return workposted;
13428 }
13429
13430 #define FC_RCTL_MDS_DIAGS       0xF4
13431
13432 /**
13433  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13434  * @phba: Pointer to HBA context object.
13435  * @rcqe: Pointer to receive-queue completion queue entry.
13436  *
13437  * This routine process a receive-queue completion queue entry.
13438  *
13439  * Return: true if work posted to worker thread, otherwise false.
13440  **/
13441 static bool
13442 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13443 {
13444         bool workposted = false;
13445         struct fc_frame_header *fc_hdr;
13446         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13447         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
13448         struct lpfc_nvmet_tgtport *tgtp;
13449         struct hbq_dmabuf *dma_buf;
13450         uint32_t status, rq_id;
13451         unsigned long iflags;
13452
13453         /* sanity check on queue memory */
13454         if (unlikely(!hrq) || unlikely(!drq))
13455                 return workposted;
13456
13457         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13458                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13459         else
13460                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13461         if (rq_id != hrq->queue_id)
13462                 goto out;
13463
13464         status = bf_get(lpfc_rcqe_status, rcqe);
13465         switch (status) {
13466         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13467                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13468                                 "2537 Receive Frame Truncated!!\n");
13469         case FC_STATUS_RQ_SUCCESS:
13470                 spin_lock_irqsave(&phba->hbalock, iflags);
13471                 lpfc_sli4_rq_release(hrq, drq);
13472                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13473                 if (!dma_buf) {
13474                         hrq->RQ_no_buf_found++;
13475                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13476                         goto out;
13477                 }
13478                 hrq->RQ_rcv_buf++;
13479                 hrq->RQ_buf_posted--;
13480                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
13481
13482                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13483
13484                 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13485                     fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13486                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13487                         /* Handle MDS Loopback frames */
13488                         lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13489                         break;
13490                 }
13491
13492                 /* save off the frame for the work thread to process */
13493                 list_add_tail(&dma_buf->cq_event.list,
13494                               &phba->sli4_hba.sp_queue_event);
13495                 /* Frame received */
13496                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
13497                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13498                 workposted = true;
13499                 break;
13500         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13501                 if (phba->nvmet_support) {
13502                         tgtp = phba->targetport->private;
13503                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13504                                         "6402 RQE Error x%x, posted %d err_cnt "
13505                                         "%d: %x %x %x\n",
13506                                         status, hrq->RQ_buf_posted,
13507                                         hrq->RQ_no_posted_buf,
13508                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13509                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13510                                         atomic_read(&tgtp->xmt_fcp_release));
13511                 }
13512                 /* fallthrough */
13513
13514         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13515                 hrq->RQ_no_posted_buf++;
13516                 /* Post more buffers if possible */
13517                 spin_lock_irqsave(&phba->hbalock, iflags);
13518                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13519                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13520                 workposted = true;
13521                 break;
13522         }
13523 out:
13524         return workposted;
13525 }
13526
13527 /**
13528  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13529  * @phba: Pointer to HBA context object.
13530  * @cq: Pointer to the completion queue.
13531  * @wcqe: Pointer to a completion queue entry.
13532  *
13533  * This routine process a slow-path work-queue or receive queue completion queue
13534  * entry.
13535  *
13536  * Return: true if work posted to worker thread, otherwise false.
13537  **/
13538 static bool
13539 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13540                          struct lpfc_cqe *cqe)
13541 {
13542         struct lpfc_cqe cqevt;
13543         bool workposted = false;
13544
13545         /* Copy the work queue CQE and convert endian order if needed */
13546         lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
13547
13548         /* Check and process for different type of WCQE and dispatch */
13549         switch (bf_get(lpfc_cqe_code, &cqevt)) {
13550         case CQE_CODE_COMPL_WQE:
13551                 /* Process the WQ/RQ complete event */
13552                 phba->last_completion_time = jiffies;
13553                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
13554                                 (struct lpfc_wcqe_complete *)&cqevt);
13555                 break;
13556         case CQE_CODE_RELEASE_WQE:
13557                 /* Process the WQ release event */
13558                 lpfc_sli4_sp_handle_rel_wcqe(phba,
13559                                 (struct lpfc_wcqe_release *)&cqevt);
13560                 break;
13561         case CQE_CODE_XRI_ABORTED:
13562                 /* Process the WQ XRI abort event */
13563                 phba->last_completion_time = jiffies;
13564                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13565                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
13566                 break;
13567         case CQE_CODE_RECEIVE:
13568         case CQE_CODE_RECEIVE_V1:
13569                 /* Process the RQ event */
13570                 phba->last_completion_time = jiffies;
13571                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
13572                                 (struct lpfc_rcqe *)&cqevt);
13573                 break;
13574         default:
13575                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13576                                 "0388 Not a valid WCQE code: x%x\n",
13577                                 bf_get(lpfc_cqe_code, &cqevt));
13578                 break;
13579         }
13580         return workposted;
13581 }
13582
13583 /**
13584  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13585  * @phba: Pointer to HBA context object.
13586  * @eqe: Pointer to fast-path event queue entry.
13587  *
13588  * This routine process a event queue entry from the slow-path event queue.
13589  * It will check the MajorCode and MinorCode to determine this is for a
13590  * completion event on a completion queue, if not, an error shall be logged
13591  * and just return. Otherwise, it will get to the corresponding completion
13592  * queue and process all the entries on that completion queue, rearm the
13593  * completion queue, and then return.
13594  *
13595  **/
13596 static void
13597 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13598         struct lpfc_queue *speq)
13599 {
13600         struct lpfc_queue *cq = NULL, *childq;
13601         uint16_t cqid;
13602
13603         /* Get the reference to the corresponding CQ */
13604         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13605
13606         list_for_each_entry(childq, &speq->child_list, list) {
13607                 if (childq->queue_id == cqid) {
13608                         cq = childq;
13609                         break;
13610                 }
13611         }
13612         if (unlikely(!cq)) {
13613                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13614                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13615                                         "0365 Slow-path CQ identifier "
13616                                         "(%d) does not exist\n", cqid);
13617                 return;
13618         }
13619
13620         /* Save EQ associated with this CQ */
13621         cq->assoc_qp = speq;
13622
13623         if (!queue_work(phba->wq, &cq->spwork))
13624                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13625                                 "0390 Cannot schedule soft IRQ "
13626                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13627                                 cqid, cq->queue_id, smp_processor_id());
13628 }
13629
13630 /**
13631  * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13632  * @phba: Pointer to HBA context object.
13633  *
13634  * This routine process a event queue entry from the slow-path event queue.
13635  * It will check the MajorCode and MinorCode to determine this is for a
13636  * completion event on a completion queue, if not, an error shall be logged
13637  * and just return. Otherwise, it will get to the corresponding completion
13638  * queue and process all the entries on that completion queue, rearm the
13639  * completion queue, and then return.
13640  *
13641  **/
13642 static void
13643 lpfc_sli4_sp_process_cq(struct work_struct *work)
13644 {
13645         struct lpfc_queue *cq =
13646                 container_of(work, struct lpfc_queue, spwork);
13647         struct lpfc_hba *phba = cq->phba;
13648         struct lpfc_cqe *cqe;
13649         bool workposted = false;
13650         int ccount = 0;
13651
13652         /* Process all the entries to the CQ */
13653         switch (cq->type) {
13654         case LPFC_MCQ:
13655                 while ((cqe = lpfc_sli4_cq_get(cq))) {
13656                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
13657                         if (!(++ccount % cq->entry_repost))
13658                                 break;
13659                         cq->CQ_mbox++;
13660                 }
13661                 break;
13662         case LPFC_WCQ:
13663                 while ((cqe = lpfc_sli4_cq_get(cq))) {
13664                         if (cq->subtype == LPFC_FCP ||
13665                             cq->subtype == LPFC_NVME) {
13666 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13667                                 if (phba->ktime_on)
13668                                         cq->isr_timestamp = ktime_get_ns();
13669                                 else
13670                                         cq->isr_timestamp = 0;
13671 #endif
13672                                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
13673                                                                        cqe);
13674                         } else {
13675                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13676                                                                       cqe);
13677                         }
13678                         if (!(++ccount % cq->entry_repost))
13679                                 break;
13680                 }
13681
13682                 /* Track the max number of CQEs processed in 1 EQ */
13683                 if (ccount > cq->CQ_max_cqe)
13684                         cq->CQ_max_cqe = ccount;
13685                 break;
13686         default:
13687                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13688                                 "0370 Invalid completion queue type (%d)\n",
13689                                 cq->type);
13690                 return;
13691         }
13692
13693         /* Catch the no cq entry condition, log an error */
13694         if (unlikely(ccount == 0))
13695                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13696                                 "0371 No entry from the CQ: identifier "
13697                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13698
13699         /* In any case, flash and re-arm the RCQ */
13700         phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
13701
13702         /* wake up worker thread if there are works to be done */
13703         if (workposted)
13704                 lpfc_worker_wake_up(phba);
13705 }
13706
13707 /**
13708  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
13709  * @phba: Pointer to HBA context object.
13710  * @cq: Pointer to associated CQ
13711  * @wcqe: Pointer to work-queue completion queue entry.
13712  *
13713  * This routine process a fast-path work queue completion entry from fast-path
13714  * event queue for FCP command response completion.
13715  **/
13716 static void
13717 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13718                              struct lpfc_wcqe_complete *wcqe)
13719 {
13720         struct lpfc_sli_ring *pring = cq->pring;
13721         struct lpfc_iocbq *cmdiocbq;
13722         struct lpfc_iocbq irspiocbq;
13723         unsigned long iflags;
13724
13725         /* Check for response status */
13726         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13727                 /* If resource errors reported from HBA, reduce queue
13728                  * depth of the SCSI device.
13729                  */
13730                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13731                      IOSTAT_LOCAL_REJECT)) &&
13732                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
13733                      IOERR_NO_RESOURCES))
13734                         phba->lpfc_rampdown_queue_depth(phba);
13735
13736                 /* Log the error status */
13737                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13738                                 "0373 FCP CQE error: status=x%x: "
13739                                 "CQE: %08x %08x %08x %08x\n",
13740                                 bf_get(lpfc_wcqe_c_status, wcqe),
13741                                 wcqe->word0, wcqe->total_data_placed,
13742                                 wcqe->parameter, wcqe->word3);
13743         }
13744
13745         /* Look up the FCP command IOCB and create pseudo response IOCB */
13746         spin_lock_irqsave(&pring->ring_lock, iflags);
13747         pring->stats.iocb_event++;
13748         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13749                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13750         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13751         if (unlikely(!cmdiocbq)) {
13752                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13753                                 "0374 FCP complete with no corresponding "
13754                                 "cmdiocb: iotag (%d)\n",
13755                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13756                 return;
13757         }
13758 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13759         cmdiocbq->isr_timestamp = cq->isr_timestamp;
13760 #endif
13761         if (cmdiocbq->iocb_cmpl == NULL) {
13762                 if (cmdiocbq->wqe_cmpl) {
13763                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13764                                 spin_lock_irqsave(&phba->hbalock, iflags);
13765                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13766                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13767                         }
13768
13769                         /* Pass the cmd_iocb and the wcqe to the upper layer */
13770                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13771                         return;
13772                 }
13773                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13774                                 "0375 FCP cmdiocb not callback function "
13775                                 "iotag: (%d)\n",
13776                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13777                 return;
13778         }
13779
13780         /* Fake the irspiocb and copy necessary response information */
13781         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13782
13783         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13784                 spin_lock_irqsave(&phba->hbalock, iflags);
13785                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13786                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13787         }
13788
13789         /* Pass the cmd_iocb and the rsp state to the upper layer */
13790         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13791 }
13792
13793 /**
13794  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13795  * @phba: Pointer to HBA context object.
13796  * @cq: Pointer to completion queue.
13797  * @wcqe: Pointer to work-queue completion queue entry.
13798  *
13799  * This routine handles an fast-path WQ entry consumed event by invoking the
13800  * proper WQ release routine to the slow-path WQ.
13801  **/
13802 static void
13803 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13804                              struct lpfc_wcqe_release *wcqe)
13805 {
13806         struct lpfc_queue *childwq;
13807         bool wqid_matched = false;
13808         uint16_t hba_wqid;
13809
13810         /* Check for fast-path FCP work queue release */
13811         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13812         list_for_each_entry(childwq, &cq->child_list, list) {
13813                 if (childwq->queue_id == hba_wqid) {
13814                         lpfc_sli4_wq_release(childwq,
13815                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13816                         if (childwq->q_flag & HBA_NVMET_WQFULL)
13817                                 lpfc_nvmet_wqfull_process(phba, childwq);
13818                         wqid_matched = true;
13819                         break;
13820                 }
13821         }
13822         /* Report warning log message if no match found */
13823         if (wqid_matched != true)
13824                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13825                                 "2580 Fast-path wqe consume event carries "
13826                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13827 }
13828
13829 /**
13830  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13831  * @phba: Pointer to HBA context object.
13832  * @rcqe: Pointer to receive-queue completion queue entry.
13833  *
13834  * This routine process a receive-queue completion queue entry.
13835  *
13836  * Return: true if work posted to worker thread, otherwise false.
13837  **/
13838 static bool
13839 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13840                             struct lpfc_rcqe *rcqe)
13841 {
13842         bool workposted = false;
13843         struct lpfc_queue *hrq;
13844         struct lpfc_queue *drq;
13845         struct rqb_dmabuf *dma_buf;
13846         struct fc_frame_header *fc_hdr;
13847         struct lpfc_nvmet_tgtport *tgtp;
13848         uint32_t status, rq_id;
13849         unsigned long iflags;
13850         uint32_t fctl, idx;
13851
13852         if ((phba->nvmet_support == 0) ||
13853             (phba->sli4_hba.nvmet_cqset == NULL))
13854                 return workposted;
13855
13856         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13857         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13858         drq = phba->sli4_hba.nvmet_mrq_data[idx];
13859
13860         /* sanity check on queue memory */
13861         if (unlikely(!hrq) || unlikely(!drq))
13862                 return workposted;
13863
13864         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13865                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13866         else
13867                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13868
13869         if ((phba->nvmet_support == 0) ||
13870             (rq_id != hrq->queue_id))
13871                 return workposted;
13872
13873         status = bf_get(lpfc_rcqe_status, rcqe);
13874         switch (status) {
13875         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13876                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13877                                 "6126 Receive Frame Truncated!!\n");
13878                 /* Drop thru */
13879         case FC_STATUS_RQ_SUCCESS:
13880                 spin_lock_irqsave(&phba->hbalock, iflags);
13881                 lpfc_sli4_rq_release(hrq, drq);
13882                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13883                 if (!dma_buf) {
13884                         hrq->RQ_no_buf_found++;
13885                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13886                         goto out;
13887                 }
13888                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13889                 hrq->RQ_rcv_buf++;
13890                 hrq->RQ_buf_posted--;
13891                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13892
13893                 /* Just some basic sanity checks on FCP Command frame */
13894                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13895                 fc_hdr->fh_f_ctl[1] << 8 |
13896                 fc_hdr->fh_f_ctl[2]);
13897                 if (((fctl &
13898                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13899                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13900                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13901                         goto drop;
13902
13903                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13904                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length,  rcqe);
13905                         lpfc_nvmet_unsol_fcp_event(
13906                                 phba, idx, dma_buf,
13907                                 cq->isr_timestamp);
13908                         return false;
13909                 }
13910 drop:
13911                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13912                 break;
13913         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13914                 if (phba->nvmet_support) {
13915                         tgtp = phba->targetport->private;
13916                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13917                                         "6401 RQE Error x%x, posted %d err_cnt "
13918                                         "%d: %x %x %x\n",
13919                                         status, hrq->RQ_buf_posted,
13920                                         hrq->RQ_no_posted_buf,
13921                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
13922                                         atomic_read(&tgtp->rcv_fcp_cmd_out),
13923                                         atomic_read(&tgtp->xmt_fcp_release));
13924                 }
13925                 /* fallthrough */
13926
13927         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13928                 hrq->RQ_no_posted_buf++;
13929                 /* Post more buffers if possible */
13930                 break;
13931         }
13932 out:
13933         return workposted;
13934 }
13935
13936 /**
13937  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13938  * @cq: Pointer to the completion queue.
13939  * @eqe: Pointer to fast-path completion queue entry.
13940  *
13941  * This routine process a fast-path work queue completion entry from fast-path
13942  * event queue for FCP command response completion.
13943  **/
13944 static int
13945 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13946                          struct lpfc_cqe *cqe)
13947 {
13948         struct lpfc_wcqe_release wcqe;
13949         bool workposted = false;
13950
13951         /* Copy the work queue CQE and convert endian order if needed */
13952         lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13953
13954         /* Check and process for different type of WCQE and dispatch */
13955         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13956         case CQE_CODE_COMPL_WQE:
13957         case CQE_CODE_NVME_ERSP:
13958                 cq->CQ_wq++;
13959                 /* Process the WQ complete event */
13960                 phba->last_completion_time = jiffies;
13961                 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13962                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13963                                 (struct lpfc_wcqe_complete *)&wcqe);
13964                 if (cq->subtype == LPFC_NVME_LS)
13965                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13966                                 (struct lpfc_wcqe_complete *)&wcqe);
13967                 break;
13968         case CQE_CODE_RELEASE_WQE:
13969                 cq->CQ_release_wqe++;
13970                 /* Process the WQ release event */
13971                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13972                                 (struct lpfc_wcqe_release *)&wcqe);
13973                 break;
13974         case CQE_CODE_XRI_ABORTED:
13975                 cq->CQ_xri_aborted++;
13976                 /* Process the WQ XRI abort event */
13977                 phba->last_completion_time = jiffies;
13978                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13979                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
13980                 break;
13981         case CQE_CODE_RECEIVE_V1:
13982         case CQE_CODE_RECEIVE:
13983                 phba->last_completion_time = jiffies;
13984                 if (cq->subtype == LPFC_NVMET) {
13985                         workposted = lpfc_sli4_nvmet_handle_rcqe(
13986                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
13987                 }
13988                 break;
13989         default:
13990                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13991                                 "0144 Not a valid CQE code: x%x\n",
13992                                 bf_get(lpfc_wcqe_c_code, &wcqe));
13993                 break;
13994         }
13995         return workposted;
13996 }
13997
13998 /**
13999  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
14000  * @phba: Pointer to HBA context object.
14001  * @eqe: Pointer to fast-path event queue entry.
14002  *
14003  * This routine process a event queue entry from the fast-path event queue.
14004  * It will check the MajorCode and MinorCode to determine this is for a
14005  * completion event on a completion queue, if not, an error shall be logged
14006  * and just return. Otherwise, it will get to the corresponding completion
14007  * queue and process all the entries on the completion queue, rearm the
14008  * completion queue, and then return.
14009  **/
14010 static void
14011 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14012                         uint32_t qidx)
14013 {
14014         struct lpfc_queue *cq = NULL;
14015         uint16_t cqid, id;
14016
14017         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14018                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14019                                 "0366 Not a valid completion "
14020                                 "event: majorcode=x%x, minorcode=x%x\n",
14021                                 bf_get_le32(lpfc_eqe_major_code, eqe),
14022                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
14023                 return;
14024         }
14025
14026         /* Get the reference to the corresponding CQ */
14027         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14028
14029         /* First check for NVME/SCSI completion */
14030         if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
14031             (cqid == phba->sli4_hba.hdwq[qidx].nvme_cq_map)) {
14032                 /* Process NVME / NVMET command completion */
14033                 cq = phba->sli4_hba.hdwq[qidx].nvme_cq;
14034                 goto  process_cq;
14035         }
14036
14037         if (cqid == phba->sli4_hba.hdwq[qidx].fcp_cq_map) {
14038                 /* Process FCP command completion */
14039                 cq = phba->sli4_hba.hdwq[qidx].fcp_cq;
14040                 goto  process_cq;
14041         }
14042
14043         /* Next check for NVMET completion */
14044         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14045                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14046                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14047                         /* Process NVMET unsol rcv */
14048                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14049                         goto  process_cq;
14050                 }
14051         }
14052
14053         if (phba->sli4_hba.nvmels_cq &&
14054             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14055                 /* Process NVME unsol rcv */
14056                 cq = phba->sli4_hba.nvmels_cq;
14057         }
14058
14059         /* Otherwise this is a Slow path event */
14060         if (cq == NULL) {
14061                 lpfc_sli4_sp_handle_eqe(phba, eqe,
14062                                         phba->sli4_hba.hdwq[qidx].hba_eq);
14063                 return;
14064         }
14065
14066 process_cq:
14067         if (unlikely(cqid != cq->queue_id)) {
14068                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14069                                 "0368 Miss-matched fast-path completion "
14070                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14071                                 cqid, cq->queue_id);
14072                 return;
14073         }
14074
14075         /* Save EQ associated with this CQ */
14076         cq->assoc_qp = phba->sli4_hba.hdwq[qidx].hba_eq;
14077
14078         if (!queue_work(phba->wq, &cq->irqwork))
14079                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14080                                 "0363 Cannot schedule soft IRQ "
14081                                 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14082                                 cqid, cq->queue_id, smp_processor_id());
14083 }
14084
14085 /**
14086  * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14087  * @phba: Pointer to HBA context object.
14088  * @eqe: Pointer to fast-path event queue entry.
14089  *
14090  * This routine process a event queue entry from the fast-path event queue.
14091  * It will check the MajorCode and MinorCode to determine this is for a
14092  * completion event on a completion queue, if not, an error shall be logged
14093  * and just return. Otherwise, it will get to the corresponding completion
14094  * queue and process all the entries on the completion queue, rearm the
14095  * completion queue, and then return.
14096  **/
14097 static void
14098 lpfc_sli4_hba_process_cq(struct work_struct *work)
14099 {
14100         struct lpfc_queue *cq =
14101                 container_of(work, struct lpfc_queue, irqwork);
14102         struct lpfc_hba *phba = cq->phba;
14103         struct lpfc_cqe *cqe;
14104         bool workposted = false;
14105         int ccount = 0;
14106
14107         /* Process all the entries to the CQ */
14108         while ((cqe = lpfc_sli4_cq_get(cq))) {
14109 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14110                 if (phba->ktime_on)
14111                         cq->isr_timestamp = ktime_get_ns();
14112                 else
14113                         cq->isr_timestamp = 0;
14114 #endif
14115                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
14116                 if (!(++ccount % cq->entry_repost))
14117                         break;
14118         }
14119
14120         /* Track the max number of CQEs processed in 1 EQ */
14121         if (ccount > cq->CQ_max_cqe)
14122                 cq->CQ_max_cqe = ccount;
14123         cq->assoc_qp->EQ_cqe_cnt += ccount;
14124
14125         /* Catch the no cq entry condition */
14126         if (unlikely(ccount == 0))
14127                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14128                                 "0369 No entry from fast-path completion "
14129                                 "queue fcpcqid=%d\n", cq->queue_id);
14130
14131         /* In any case, flash and re-arm the CQ */
14132         phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
14133
14134         /* wake up worker thread if there are works to be done */
14135         if (workposted)
14136                 lpfc_worker_wake_up(phba);
14137 }
14138
14139 static void
14140 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
14141 {
14142         struct lpfc_eqe *eqe;
14143
14144         /* walk all the EQ entries and drop on the floor */
14145         while ((eqe = lpfc_sli4_eq_get(eq)))
14146                 ;
14147
14148         /* Clear and re-arm the EQ */
14149         phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
14150 }
14151
14152
14153 /**
14154  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
14155  * @irq: Interrupt number.
14156  * @dev_id: The device context pointer.
14157  *
14158  * This function is directly called from the PCI layer as an interrupt
14159  * service routine when device with SLI-4 interface spec is enabled with
14160  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14161  * ring event in the HBA. However, when the device is enabled with either
14162  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14163  * device-level interrupt handler. When the PCI slot is in error recovery
14164  * or the HBA is undergoing initialization, the interrupt handler will not
14165  * process the interrupt. The SCSI FCP fast-path ring event are handled in
14166  * the intrrupt context. This function is called without any lock held.
14167  * It gets the hbalock to access and update SLI data structures. Note that,
14168  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14169  * equal to that of FCP CQ index.
14170  *
14171  * The link attention and ELS ring attention events are handled
14172  * by the worker thread. The interrupt handler signals the worker thread
14173  * and returns for these events. This function is called without any lock
14174  * held. It gets the hbalock to access and update SLI data structures.
14175  *
14176  * This function returns IRQ_HANDLED when interrupt is handled else it
14177  * returns IRQ_NONE.
14178  **/
14179 irqreturn_t
14180 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
14181 {
14182         struct lpfc_hba *phba;
14183         struct lpfc_hba_eq_hdl *hba_eq_hdl;
14184         struct lpfc_queue *fpeq;
14185         struct lpfc_eqe *eqe;
14186         unsigned long iflag;
14187         int ecount = 0;
14188         int hba_eqidx;
14189
14190         /* Get the driver's phba structure from the dev_id */
14191         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14192         phba = hba_eq_hdl->phba;
14193         hba_eqidx = hba_eq_hdl->idx;
14194
14195         if (unlikely(!phba))
14196                 return IRQ_NONE;
14197         if (unlikely(!phba->sli4_hba.hdwq))
14198                 return IRQ_NONE;
14199
14200         /* Get to the EQ struct associated with this vector */
14201         fpeq = phba->sli4_hba.hdwq[hba_eqidx].hba_eq;
14202         if (unlikely(!fpeq))
14203                 return IRQ_NONE;
14204
14205         if (lpfc_fcp_look_ahead) {
14206                 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
14207                         phba->sli4_hba.sli4_eq_clr_intr(fpeq);
14208                 else {
14209                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14210                         return IRQ_NONE;
14211                 }
14212         }
14213
14214         /* Check device state for handling interrupt */
14215         if (unlikely(lpfc_intr_state_check(phba))) {
14216                 /* Check again for link_state with lock held */
14217                 spin_lock_irqsave(&phba->hbalock, iflag);
14218                 if (phba->link_state < LPFC_LINK_DOWN)
14219                         /* Flush, clear interrupt, and rearm the EQ */
14220                         lpfc_sli4_eq_flush(phba, fpeq);
14221                 spin_unlock_irqrestore(&phba->hbalock, iflag);
14222                 if (lpfc_fcp_look_ahead)
14223                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14224                 return IRQ_NONE;
14225         }
14226
14227         /*
14228          * Process all the event on FCP fast-path EQ
14229          */
14230         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
14231                 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14232                 if (!(++ecount % fpeq->entry_repost))
14233                         break;
14234                 fpeq->EQ_processed++;
14235         }
14236
14237         /* Track the max number of EQEs processed in 1 intr */
14238         if (ecount > fpeq->EQ_max_eqe)
14239                 fpeq->EQ_max_eqe = ecount;
14240
14241         /* Always clear and re-arm the fast-path EQ */
14242         phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
14243
14244         if (unlikely(ecount == 0)) {
14245                 fpeq->EQ_no_entry++;
14246
14247                 if (lpfc_fcp_look_ahead) {
14248                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14249                         return IRQ_NONE;
14250                 }
14251
14252                 if (phba->intr_type == MSIX)
14253                         /* MSI-X treated interrupt served as no EQ share INT */
14254                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14255                                         "0358 MSI-X interrupt with no EQE\n");
14256                 else
14257                         /* Non MSI-X treated on interrupt as EQ share INT */
14258                         return IRQ_NONE;
14259         }
14260
14261         if (lpfc_fcp_look_ahead)
14262                 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14263
14264         return IRQ_HANDLED;
14265 } /* lpfc_sli4_fp_intr_handler */
14266
14267 /**
14268  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14269  * @irq: Interrupt number.
14270  * @dev_id: The device context pointer.
14271  *
14272  * This function is the device-level interrupt handler to device with SLI-4
14273  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14274  * interrupt mode is enabled and there is an event in the HBA which requires
14275  * driver attention. This function invokes the slow-path interrupt attention
14276  * handling function and fast-path interrupt attention handling function in
14277  * turn to process the relevant HBA attention events. This function is called
14278  * without any lock held. It gets the hbalock to access and update SLI data
14279  * structures.
14280  *
14281  * This function returns IRQ_HANDLED when interrupt is handled, else it
14282  * returns IRQ_NONE.
14283  **/
14284 irqreturn_t
14285 lpfc_sli4_intr_handler(int irq, void *dev_id)
14286 {
14287         struct lpfc_hba  *phba;
14288         irqreturn_t hba_irq_rc;
14289         bool hba_handled = false;
14290         int qidx;
14291
14292         /* Get the driver's phba structure from the dev_id */
14293         phba = (struct lpfc_hba *)dev_id;
14294
14295         if (unlikely(!phba))
14296                 return IRQ_NONE;
14297
14298         /*
14299          * Invoke fast-path host attention interrupt handling as appropriate.
14300          */
14301         for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
14302                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
14303                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
14304                 if (hba_irq_rc == IRQ_HANDLED)
14305                         hba_handled |= true;
14306         }
14307
14308         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
14309 } /* lpfc_sli4_intr_handler */
14310
14311 /**
14312  * lpfc_sli4_queue_free - free a queue structure and associated memory
14313  * @queue: The queue structure to free.
14314  *
14315  * This function frees a queue structure and the DMAable memory used for
14316  * the host resident queue. This function must be called after destroying the
14317  * queue on the HBA.
14318  **/
14319 void
14320 lpfc_sli4_queue_free(struct lpfc_queue *queue)
14321 {
14322         struct lpfc_dmabuf *dmabuf;
14323
14324         if (!queue)
14325                 return;
14326
14327         while (!list_empty(&queue->page_list)) {
14328                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14329                                  list);
14330                 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
14331                                   dmabuf->virt, dmabuf->phys);
14332                 kfree(dmabuf);
14333         }
14334         if (queue->rqbp) {
14335                 lpfc_free_rq_buffer(queue->phba, queue);
14336                 kfree(queue->rqbp);
14337         }
14338
14339         if (!list_empty(&queue->wq_list))
14340                 list_del(&queue->wq_list);
14341
14342         kfree(queue);
14343         return;
14344 }
14345
14346 /**
14347  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14348  * @phba: The HBA that this queue is being created on.
14349  * @page_size: The size of a queue page
14350  * @entry_size: The size of each queue entry for this queue.
14351  * @entry count: The number of entries that this queue will handle.
14352  *
14353  * This function allocates a queue structure and the DMAable memory used for
14354  * the host resident queue. This function must be called before creating the
14355  * queue on the HBA.
14356  **/
14357 struct lpfc_queue *
14358 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14359                       uint32_t entry_size, uint32_t entry_count)
14360 {
14361         struct lpfc_queue *queue;
14362         struct lpfc_dmabuf *dmabuf;
14363         int x, total_qe_count;
14364         void *dma_pointer;
14365         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14366
14367         if (!phba->sli4_hba.pc_sli4_params.supported)
14368                 hw_page_size = page_size;
14369
14370         queue = kzalloc(sizeof(struct lpfc_queue) +
14371                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14372         if (!queue)
14373                 return NULL;
14374         queue->page_count = (ALIGN(entry_size * entry_count,
14375                         hw_page_size))/hw_page_size;
14376
14377         /* If needed, Adjust page count to match the max the adapter supports */
14378         if (phba->sli4_hba.pc_sli4_params.wqpcnt &&
14379             (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt))
14380                 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14381
14382         INIT_LIST_HEAD(&queue->list);
14383         INIT_LIST_HEAD(&queue->wq_list);
14384         INIT_LIST_HEAD(&queue->wqfull_list);
14385         INIT_LIST_HEAD(&queue->page_list);
14386         INIT_LIST_HEAD(&queue->child_list);
14387
14388         /* Set queue parameters now.  If the system cannot provide memory
14389          * resources, the free routine needs to know what was allocated.
14390          */
14391         queue->entry_size = entry_size;
14392         queue->entry_count = entry_count;
14393         queue->page_size = hw_page_size;
14394         queue->phba = phba;
14395
14396         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14397                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14398                 if (!dmabuf)
14399                         goto out_fail;
14400                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14401                                                    hw_page_size, &dmabuf->phys,
14402                                                    GFP_KERNEL);
14403                 if (!dmabuf->virt) {
14404                         kfree(dmabuf);
14405                         goto out_fail;
14406                 }
14407                 dmabuf->buffer_tag = x;
14408                 list_add_tail(&dmabuf->list, &queue->page_list);
14409                 /* initialize queue's entry array */
14410                 dma_pointer = dmabuf->virt;
14411                 for (; total_qe_count < entry_count &&
14412                      dma_pointer < (hw_page_size + dmabuf->virt);
14413                      total_qe_count++, dma_pointer += entry_size) {
14414                         queue->qe[total_qe_count].address = dma_pointer;
14415                 }
14416         }
14417         INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14418         INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
14419
14420         /* entry_repost will be set during q creation */
14421
14422         return queue;
14423 out_fail:
14424         lpfc_sli4_queue_free(queue);
14425         return NULL;
14426 }
14427
14428 /**
14429  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14430  * @phba: HBA structure that indicates port to create a queue on.
14431  * @pci_barset: PCI BAR set flag.
14432  *
14433  * This function shall perform iomap of the specified PCI BAR address to host
14434  * memory address if not already done so and return it. The returned host
14435  * memory address can be NULL.
14436  */
14437 static void __iomem *
14438 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14439 {
14440         if (!phba->pcidev)
14441                 return NULL;
14442
14443         switch (pci_barset) {
14444         case WQ_PCI_BAR_0_AND_1:
14445                 return phba->pci_bar0_memmap_p;
14446         case WQ_PCI_BAR_2_AND_3:
14447                 return phba->pci_bar2_memmap_p;
14448         case WQ_PCI_BAR_4_AND_5:
14449                 return phba->pci_bar4_memmap_p;
14450         default:
14451                 break;
14452         }
14453         return NULL;
14454 }
14455
14456 /**
14457  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
14458  * @phba: HBA structure that indicates port to create a queue on.
14459  * @startq: The starting FCP EQ to modify
14460  *
14461  * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
14462  * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14463  * updated in one mailbox command.
14464  *
14465  * The @phba struct is used to send mailbox command to HBA. The @startq
14466  * is used to get the starting FCP EQ to change.
14467  * This function is asynchronous and will wait for the mailbox
14468  * command to finish before continuing.
14469  *
14470  * On success this function will return a zero. If unable to allocate enough
14471  * memory this function will return -ENOMEM. If the queue create mailbox command
14472  * fails this function will return -ENXIO.
14473  **/
14474 int
14475 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14476                          uint32_t numq, uint32_t imax)
14477 {
14478         struct lpfc_mbx_modify_eq_delay *eq_delay;
14479         LPFC_MBOXQ_t *mbox;
14480         struct lpfc_queue *eq;
14481         int cnt, rc, length, status = 0;
14482         uint32_t shdr_status, shdr_add_status;
14483         uint32_t result, val;
14484         int qidx;
14485         union lpfc_sli4_cfg_shdr *shdr;
14486         uint16_t dmult;
14487
14488         if (startq >= phba->cfg_hdw_queue)
14489                 return 0;
14490
14491         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14492         if (!mbox)
14493                 return -ENOMEM;
14494         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14495                   sizeof(struct lpfc_sli4_cfg_mhdr));
14496         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14497                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14498                          length, LPFC_SLI4_MBX_EMBED);
14499         eq_delay = &mbox->u.mqe.un.eq_delay;
14500
14501         /* Calculate delay multiper from maximum interrupt per second */
14502         result = imax / phba->cfg_hdw_queue;
14503         if (result > LPFC_DMULT_CONST || result == 0)
14504                 dmult = 0;
14505         else
14506                 dmult = LPFC_DMULT_CONST/result - 1;
14507         if (dmult > LPFC_DMULT_MAX)
14508                 dmult = LPFC_DMULT_MAX;
14509
14510         cnt = 0;
14511         for (qidx = startq; qidx < phba->cfg_hdw_queue; qidx++) {
14512                 eq = phba->sli4_hba.hdwq[qidx].hba_eq;
14513                 if (!eq)
14514                         continue;
14515                 eq->q_mode = imax;
14516                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14517                 eq_delay->u.request.eq[cnt].phase = 0;
14518                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14519                 cnt++;
14520
14521                 /* q_mode is only used for auto_imax */
14522                 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14523                         /* Use EQ Delay Register method for q_mode */
14524
14525                         /* Convert for EQ Delay register */
14526                         val =  phba->cfg_fcp_imax;
14527                         if (val) {
14528                                 /* First, interrupts per sec per EQ */
14529                                 val = phba->cfg_fcp_imax / phba->cfg_hdw_queue;
14530
14531                                 /* us delay between each interrupt */
14532                                 val = LPFC_SEC_TO_USEC / val;
14533                         }
14534                         eq->q_mode = val;
14535                 } else {
14536                         eq->q_mode = imax;
14537                 }
14538
14539                 if (cnt >= numq)
14540                         break;
14541         }
14542         eq_delay->u.request.num_eq = cnt;
14543
14544         mbox->vport = phba->pport;
14545         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14546         mbox->ctx_buf = NULL;
14547         mbox->ctx_ndlp = NULL;
14548         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14549         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14550         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14551         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14552         if (shdr_status || shdr_add_status || rc) {
14553                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14554                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
14555                                 "status x%x add_status x%x, mbx status x%x\n",
14556                                 shdr_status, shdr_add_status, rc);
14557                 status = -ENXIO;
14558         }
14559         mempool_free(mbox, phba->mbox_mem_pool);
14560         return status;
14561 }
14562
14563 /**
14564  * lpfc_eq_create - Create an Event Queue on the HBA
14565  * @phba: HBA structure that indicates port to create a queue on.
14566  * @eq: The queue structure to use to create the event queue.
14567  * @imax: The maximum interrupt per second limit.
14568  *
14569  * This function creates an event queue, as detailed in @eq, on a port,
14570  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14571  *
14572  * The @phba struct is used to send mailbox command to HBA. The @eq struct
14573  * is used to get the entry count and entry size that are necessary to
14574  * determine the number of pages to allocate and use for this queue. This
14575  * function will send the EQ_CREATE mailbox command to the HBA to setup the
14576  * event queue. This function is asynchronous and will wait for the mailbox
14577  * command to finish before continuing.
14578  *
14579  * On success this function will return a zero. If unable to allocate enough
14580  * memory this function will return -ENOMEM. If the queue create mailbox command
14581  * fails this function will return -ENXIO.
14582  **/
14583 int
14584 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
14585 {
14586         struct lpfc_mbx_eq_create *eq_create;
14587         LPFC_MBOXQ_t *mbox;
14588         int rc, length, status = 0;
14589         struct lpfc_dmabuf *dmabuf;
14590         uint32_t shdr_status, shdr_add_status;
14591         union lpfc_sli4_cfg_shdr *shdr;
14592         uint16_t dmult;
14593         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14594
14595         /* sanity check on queue memory */
14596         if (!eq)
14597                 return -ENODEV;
14598         if (!phba->sli4_hba.pc_sli4_params.supported)
14599                 hw_page_size = SLI4_PAGE_SIZE;
14600
14601         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14602         if (!mbox)
14603                 return -ENOMEM;
14604         length = (sizeof(struct lpfc_mbx_eq_create) -
14605                   sizeof(struct lpfc_sli4_cfg_mhdr));
14606         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14607                          LPFC_MBOX_OPCODE_EQ_CREATE,
14608                          length, LPFC_SLI4_MBX_EMBED);
14609         eq_create = &mbox->u.mqe.un.eq_create;
14610         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14611         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14612                eq->page_count);
14613         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14614                LPFC_EQE_SIZE);
14615         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
14616
14617         /* Use version 2 of CREATE_EQ if eqav is set */
14618         if (phba->sli4_hba.pc_sli4_params.eqav) {
14619                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14620                        LPFC_Q_CREATE_VERSION_2);
14621                 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14622                        phba->sli4_hba.pc_sli4_params.eqav);
14623         }
14624
14625         /* don't setup delay multiplier using EQ_CREATE */
14626         dmult = 0;
14627         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14628                dmult);
14629         switch (eq->entry_count) {
14630         default:
14631                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14632                                 "0360 Unsupported EQ count. (%d)\n",
14633                                 eq->entry_count);
14634                 if (eq->entry_count < 256)
14635                         return -EINVAL;
14636                 /* otherwise default to smallest count (drop through) */
14637         case 256:
14638                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14639                        LPFC_EQ_CNT_256);
14640                 break;
14641         case 512:
14642                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14643                        LPFC_EQ_CNT_512);
14644                 break;
14645         case 1024:
14646                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14647                        LPFC_EQ_CNT_1024);
14648                 break;
14649         case 2048:
14650                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14651                        LPFC_EQ_CNT_2048);
14652                 break;
14653         case 4096:
14654                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14655                        LPFC_EQ_CNT_4096);
14656                 break;
14657         }
14658         list_for_each_entry(dmabuf, &eq->page_list, list) {
14659                 memset(dmabuf->virt, 0, hw_page_size);
14660                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14661                                         putPaddrLow(dmabuf->phys);
14662                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14663                                         putPaddrHigh(dmabuf->phys);
14664         }
14665         mbox->vport = phba->pport;
14666         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14667         mbox->ctx_buf = NULL;
14668         mbox->ctx_ndlp = NULL;
14669         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14670         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14671         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14672         if (shdr_status || shdr_add_status || rc) {
14673                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14674                                 "2500 EQ_CREATE mailbox failed with "
14675                                 "status x%x add_status x%x, mbx status x%x\n",
14676                                 shdr_status, shdr_add_status, rc);
14677                 status = -ENXIO;
14678         }
14679         eq->type = LPFC_EQ;
14680         eq->subtype = LPFC_NONE;
14681         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14682         if (eq->queue_id == 0xFFFF)
14683                 status = -ENXIO;
14684         eq->host_index = 0;
14685         eq->hba_index = 0;
14686         eq->entry_repost = LPFC_EQ_REPOST;
14687
14688         mempool_free(mbox, phba->mbox_mem_pool);
14689         return status;
14690 }
14691
14692 /**
14693  * lpfc_cq_create - Create a Completion Queue on the HBA
14694  * @phba: HBA structure that indicates port to create a queue on.
14695  * @cq: The queue structure to use to create the completion queue.
14696  * @eq: The event queue to bind this completion queue to.
14697  *
14698  * This function creates a completion queue, as detailed in @wq, on a port,
14699  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14700  *
14701  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14702  * is used to get the entry count and entry size that are necessary to
14703  * determine the number of pages to allocate and use for this queue. The @eq
14704  * is used to indicate which event queue to bind this completion queue to. This
14705  * function will send the CQ_CREATE mailbox command to the HBA to setup the
14706  * completion queue. This function is asynchronous and will wait for the mailbox
14707  * command to finish before continuing.
14708  *
14709  * On success this function will return a zero. If unable to allocate enough
14710  * memory this function will return -ENOMEM. If the queue create mailbox command
14711  * fails this function will return -ENXIO.
14712  **/
14713 int
14714 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14715                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14716 {
14717         struct lpfc_mbx_cq_create *cq_create;
14718         struct lpfc_dmabuf *dmabuf;
14719         LPFC_MBOXQ_t *mbox;
14720         int rc, length, status = 0;
14721         uint32_t shdr_status, shdr_add_status;
14722         union lpfc_sli4_cfg_shdr *shdr;
14723
14724         /* sanity check on queue memory */
14725         if (!cq || !eq)
14726                 return -ENODEV;
14727
14728         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14729         if (!mbox)
14730                 return -ENOMEM;
14731         length = (sizeof(struct lpfc_mbx_cq_create) -
14732                   sizeof(struct lpfc_sli4_cfg_mhdr));
14733         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14734                          LPFC_MBOX_OPCODE_CQ_CREATE,
14735                          length, LPFC_SLI4_MBX_EMBED);
14736         cq_create = &mbox->u.mqe.un.cq_create;
14737         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14738         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14739                     cq->page_count);
14740         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14741         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14742         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14743                phba->sli4_hba.pc_sli4_params.cqv);
14744         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14745                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14746                        (cq->page_size / SLI4_PAGE_SIZE));
14747                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14748                        eq->queue_id);
14749                 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14750                        phba->sli4_hba.pc_sli4_params.cqav);
14751         } else {
14752                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14753                        eq->queue_id);
14754         }
14755         switch (cq->entry_count) {
14756         case 2048:
14757         case 4096:
14758                 if (phba->sli4_hba.pc_sli4_params.cqv ==
14759                     LPFC_Q_CREATE_VERSION_2) {
14760                         cq_create->u.request.context.lpfc_cq_context_count =
14761                                 cq->entry_count;
14762                         bf_set(lpfc_cq_context_count,
14763                                &cq_create->u.request.context,
14764                                LPFC_CQ_CNT_WORD7);
14765                         break;
14766                 }
14767                 /* Fall Thru */
14768         default:
14769                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14770                                 "0361 Unsupported CQ count: "
14771                                 "entry cnt %d sz %d pg cnt %d\n",
14772                                 cq->entry_count, cq->entry_size,
14773                                 cq->page_count);
14774                 if (cq->entry_count < 256) {
14775                         status = -EINVAL;
14776                         goto out;
14777                 }
14778                 /* otherwise default to smallest count (drop through) */
14779         case 256:
14780                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14781                        LPFC_CQ_CNT_256);
14782                 break;
14783         case 512:
14784                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14785                        LPFC_CQ_CNT_512);
14786                 break;
14787         case 1024:
14788                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14789                        LPFC_CQ_CNT_1024);
14790                 break;
14791         }
14792         list_for_each_entry(dmabuf, &cq->page_list, list) {
14793                 memset(dmabuf->virt, 0, cq->page_size);
14794                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14795                                         putPaddrLow(dmabuf->phys);
14796                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14797                                         putPaddrHigh(dmabuf->phys);
14798         }
14799         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14800
14801         /* The IOCTL status is embedded in the mailbox subheader. */
14802         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14803         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14804         if (shdr_status || shdr_add_status || rc) {
14805                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14806                                 "2501 CQ_CREATE mailbox failed with "
14807                                 "status x%x add_status x%x, mbx status x%x\n",
14808                                 shdr_status, shdr_add_status, rc);
14809                 status = -ENXIO;
14810                 goto out;
14811         }
14812         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14813         if (cq->queue_id == 0xFFFF) {
14814                 status = -ENXIO;
14815                 goto out;
14816         }
14817         /* link the cq onto the parent eq child list */
14818         list_add_tail(&cq->list, &eq->child_list);
14819         /* Set up completion queue's type and subtype */
14820         cq->type = type;
14821         cq->subtype = subtype;
14822         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14823         cq->assoc_qid = eq->queue_id;
14824         cq->host_index = 0;
14825         cq->hba_index = 0;
14826         cq->entry_repost = LPFC_CQ_REPOST;
14827
14828 out:
14829         mempool_free(mbox, phba->mbox_mem_pool);
14830         return status;
14831 }
14832
14833 /**
14834  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14835  * @phba: HBA structure that indicates port to create a queue on.
14836  * @cqp: The queue structure array to use to create the completion queues.
14837  * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
14838  *
14839  * This function creates a set of  completion queue, s to support MRQ
14840  * as detailed in @cqp, on a port,
14841  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14842  *
14843  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14844  * is used to get the entry count and entry size that are necessary to
14845  * determine the number of pages to allocate and use for this queue. The @eq
14846  * is used to indicate which event queue to bind this completion queue to. This
14847  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14848  * completion queue. This function is asynchronous and will wait for the mailbox
14849  * command to finish before continuing.
14850  *
14851  * On success this function will return a zero. If unable to allocate enough
14852  * memory this function will return -ENOMEM. If the queue create mailbox command
14853  * fails this function will return -ENXIO.
14854  **/
14855 int
14856 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14857                    struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
14858                    uint32_t subtype)
14859 {
14860         struct lpfc_queue *cq;
14861         struct lpfc_queue *eq;
14862         struct lpfc_mbx_cq_create_set *cq_set;
14863         struct lpfc_dmabuf *dmabuf;
14864         LPFC_MBOXQ_t *mbox;
14865         int rc, length, alloclen, status = 0;
14866         int cnt, idx, numcq, page_idx = 0;
14867         uint32_t shdr_status, shdr_add_status;
14868         union lpfc_sli4_cfg_shdr *shdr;
14869         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14870
14871         /* sanity check on queue memory */
14872         numcq = phba->cfg_nvmet_mrq;
14873         if (!cqp || !hdwq || !numcq)
14874                 return -ENODEV;
14875
14876         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14877         if (!mbox)
14878                 return -ENOMEM;
14879
14880         length = sizeof(struct lpfc_mbx_cq_create_set);
14881         length += ((numcq * cqp[0]->page_count) *
14882                    sizeof(struct dma_address));
14883         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14884                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14885                         LPFC_SLI4_MBX_NEMBED);
14886         if (alloclen < length) {
14887                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14888                                 "3098 Allocated DMA memory size (%d) is "
14889                                 "less than the requested DMA memory size "
14890                                 "(%d)\n", alloclen, length);
14891                 status = -ENOMEM;
14892                 goto out;
14893         }
14894         cq_set = mbox->sge_array->addr[0];
14895         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14896         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14897
14898         for (idx = 0; idx < numcq; idx++) {
14899                 cq = cqp[idx];
14900                 eq = hdwq[idx].hba_eq;
14901                 if (!cq || !eq) {
14902                         status = -ENOMEM;
14903                         goto out;
14904                 }
14905                 if (!phba->sli4_hba.pc_sli4_params.supported)
14906                         hw_page_size = cq->page_size;
14907
14908                 switch (idx) {
14909                 case 0:
14910                         bf_set(lpfc_mbx_cq_create_set_page_size,
14911                                &cq_set->u.request,
14912                                (hw_page_size / SLI4_PAGE_SIZE));
14913                         bf_set(lpfc_mbx_cq_create_set_num_pages,
14914                                &cq_set->u.request, cq->page_count);
14915                         bf_set(lpfc_mbx_cq_create_set_evt,
14916                                &cq_set->u.request, 1);
14917                         bf_set(lpfc_mbx_cq_create_set_valid,
14918                                &cq_set->u.request, 1);
14919                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
14920                                &cq_set->u.request, 0);
14921                         bf_set(lpfc_mbx_cq_create_set_num_cq,
14922                                &cq_set->u.request, numcq);
14923                         bf_set(lpfc_mbx_cq_create_set_autovalid,
14924                                &cq_set->u.request,
14925                                phba->sli4_hba.pc_sli4_params.cqav);
14926                         switch (cq->entry_count) {
14927                         case 2048:
14928                         case 4096:
14929                                 if (phba->sli4_hba.pc_sli4_params.cqv ==
14930                                     LPFC_Q_CREATE_VERSION_2) {
14931                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14932                                                &cq_set->u.request,
14933                                                 cq->entry_count);
14934                                         bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14935                                                &cq_set->u.request,
14936                                                LPFC_CQ_CNT_WORD7);
14937                                         break;
14938                                 }
14939                                 /* Fall Thru */
14940                         default:
14941                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14942                                                 "3118 Bad CQ count. (%d)\n",
14943                                                 cq->entry_count);
14944                                 if (cq->entry_count < 256) {
14945                                         status = -EINVAL;
14946                                         goto out;
14947                                 }
14948                                 /* otherwise default to smallest (drop thru) */
14949                         case 256:
14950                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14951                                        &cq_set->u.request, LPFC_CQ_CNT_256);
14952                                 break;
14953                         case 512:
14954                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14955                                        &cq_set->u.request, LPFC_CQ_CNT_512);
14956                                 break;
14957                         case 1024:
14958                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14959                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
14960                                 break;
14961                         }
14962                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
14963                                &cq_set->u.request, eq->queue_id);
14964                         break;
14965                 case 1:
14966                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
14967                                &cq_set->u.request, eq->queue_id);
14968                         break;
14969                 case 2:
14970                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
14971                                &cq_set->u.request, eq->queue_id);
14972                         break;
14973                 case 3:
14974                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
14975                                &cq_set->u.request, eq->queue_id);
14976                         break;
14977                 case 4:
14978                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
14979                                &cq_set->u.request, eq->queue_id);
14980                         break;
14981                 case 5:
14982                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
14983                                &cq_set->u.request, eq->queue_id);
14984                         break;
14985                 case 6:
14986                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
14987                                &cq_set->u.request, eq->queue_id);
14988                         break;
14989                 case 7:
14990                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
14991                                &cq_set->u.request, eq->queue_id);
14992                         break;
14993                 case 8:
14994                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
14995                                &cq_set->u.request, eq->queue_id);
14996                         break;
14997                 case 9:
14998                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
14999                                &cq_set->u.request, eq->queue_id);
15000                         break;
15001                 case 10:
15002                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
15003                                &cq_set->u.request, eq->queue_id);
15004                         break;
15005                 case 11:
15006                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
15007                                &cq_set->u.request, eq->queue_id);
15008                         break;
15009                 case 12:
15010                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
15011                                &cq_set->u.request, eq->queue_id);
15012                         break;
15013                 case 13:
15014                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
15015                                &cq_set->u.request, eq->queue_id);
15016                         break;
15017                 case 14:
15018                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
15019                                &cq_set->u.request, eq->queue_id);
15020                         break;
15021                 case 15:
15022                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
15023                                &cq_set->u.request, eq->queue_id);
15024                         break;
15025                 }
15026
15027                 /* link the cq onto the parent eq child list */
15028                 list_add_tail(&cq->list, &eq->child_list);
15029                 /* Set up completion queue's type and subtype */
15030                 cq->type = type;
15031                 cq->subtype = subtype;
15032                 cq->assoc_qid = eq->queue_id;
15033                 cq->host_index = 0;
15034                 cq->hba_index = 0;
15035                 cq->entry_repost = LPFC_CQ_REPOST;
15036                 cq->chann = idx;
15037
15038                 rc = 0;
15039                 list_for_each_entry(dmabuf, &cq->page_list, list) {
15040                         memset(dmabuf->virt, 0, hw_page_size);
15041                         cnt = page_idx + dmabuf->buffer_tag;
15042                         cq_set->u.request.page[cnt].addr_lo =
15043                                         putPaddrLow(dmabuf->phys);
15044                         cq_set->u.request.page[cnt].addr_hi =
15045                                         putPaddrHigh(dmabuf->phys);
15046                         rc++;
15047                 }
15048                 page_idx += rc;
15049         }
15050
15051         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15052
15053         /* The IOCTL status is embedded in the mailbox subheader. */
15054         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15055         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15056         if (shdr_status || shdr_add_status || rc) {
15057                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15058                                 "3119 CQ_CREATE_SET mailbox failed with "
15059                                 "status x%x add_status x%x, mbx status x%x\n",
15060                                 shdr_status, shdr_add_status, rc);
15061                 status = -ENXIO;
15062                 goto out;
15063         }
15064         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15065         if (rc == 0xFFFF) {
15066                 status = -ENXIO;
15067                 goto out;
15068         }
15069
15070         for (idx = 0; idx < numcq; idx++) {
15071                 cq = cqp[idx];
15072                 cq->queue_id = rc + idx;
15073         }
15074
15075 out:
15076         lpfc_sli4_mbox_cmd_free(phba, mbox);
15077         return status;
15078 }
15079
15080 /**
15081  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
15082  * @phba: HBA structure that indicates port to create a queue on.
15083  * @mq: The queue structure to use to create the mailbox queue.
15084  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15085  * @cq: The completion queue to associate with this cq.
15086  *
15087  * This function provides failback (fb) functionality when the
15088  * mq_create_ext fails on older FW generations.  It's purpose is identical
15089  * to mq_create_ext otherwise.
15090  *
15091  * This routine cannot fail as all attributes were previously accessed and
15092  * initialized in mq_create_ext.
15093  **/
15094 static void
15095 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15096                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
15097 {
15098         struct lpfc_mbx_mq_create *mq_create;
15099         struct lpfc_dmabuf *dmabuf;
15100         int length;
15101
15102         length = (sizeof(struct lpfc_mbx_mq_create) -
15103                   sizeof(struct lpfc_sli4_cfg_mhdr));
15104         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15105                          LPFC_MBOX_OPCODE_MQ_CREATE,
15106                          length, LPFC_SLI4_MBX_EMBED);
15107         mq_create = &mbox->u.mqe.un.mq_create;
15108         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
15109                mq->page_count);
15110         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
15111                cq->queue_id);
15112         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15113         switch (mq->entry_count) {
15114         case 16:
15115                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15116                        LPFC_MQ_RING_SIZE_16);
15117                 break;
15118         case 32:
15119                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15120                        LPFC_MQ_RING_SIZE_32);
15121                 break;
15122         case 64:
15123                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15124                        LPFC_MQ_RING_SIZE_64);
15125                 break;
15126         case 128:
15127                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15128                        LPFC_MQ_RING_SIZE_128);
15129                 break;
15130         }
15131         list_for_each_entry(dmabuf, &mq->page_list, list) {
15132                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15133                         putPaddrLow(dmabuf->phys);
15134                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15135                         putPaddrHigh(dmabuf->phys);
15136         }
15137 }
15138
15139 /**
15140  * lpfc_mq_create - Create a mailbox Queue on the HBA
15141  * @phba: HBA structure that indicates port to create a queue on.
15142  * @mq: The queue structure to use to create the mailbox queue.
15143  * @cq: The completion queue to associate with this cq.
15144  * @subtype: The queue's subtype.
15145  *
15146  * This function creates a mailbox queue, as detailed in @mq, on a port,
15147  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15148  *
15149  * The @phba struct is used to send mailbox command to HBA. The @cq struct
15150  * is used to get the entry count and entry size that are necessary to
15151  * determine the number of pages to allocate and use for this queue. This
15152  * function will send the MQ_CREATE mailbox command to the HBA to setup the
15153  * mailbox queue. This function is asynchronous and will wait for the mailbox
15154  * command to finish before continuing.
15155  *
15156  * On success this function will return a zero. If unable to allocate enough
15157  * memory this function will return -ENOMEM. If the queue create mailbox command
15158  * fails this function will return -ENXIO.
15159  **/
15160 int32_t
15161 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15162                struct lpfc_queue *cq, uint32_t subtype)
15163 {
15164         struct lpfc_mbx_mq_create *mq_create;
15165         struct lpfc_mbx_mq_create_ext *mq_create_ext;
15166         struct lpfc_dmabuf *dmabuf;
15167         LPFC_MBOXQ_t *mbox;
15168         int rc, length, status = 0;
15169         uint32_t shdr_status, shdr_add_status;
15170         union lpfc_sli4_cfg_shdr *shdr;
15171         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15172
15173         /* sanity check on queue memory */
15174         if (!mq || !cq)
15175                 return -ENODEV;
15176         if (!phba->sli4_hba.pc_sli4_params.supported)
15177                 hw_page_size = SLI4_PAGE_SIZE;
15178
15179         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15180         if (!mbox)
15181                 return -ENOMEM;
15182         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15183                   sizeof(struct lpfc_sli4_cfg_mhdr));
15184         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15185                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15186                          length, LPFC_SLI4_MBX_EMBED);
15187
15188         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
15189         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
15190         bf_set(lpfc_mbx_mq_create_ext_num_pages,
15191                &mq_create_ext->u.request, mq->page_count);
15192         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15193                &mq_create_ext->u.request, 1);
15194         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
15195                &mq_create_ext->u.request, 1);
15196         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15197                &mq_create_ext->u.request, 1);
15198         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15199                &mq_create_ext->u.request, 1);
15200         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15201                &mq_create_ext->u.request, 1);
15202         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
15203         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15204                phba->sli4_hba.pc_sli4_params.mqv);
15205         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15206                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15207                        cq->queue_id);
15208         else
15209                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15210                        cq->queue_id);
15211         switch (mq->entry_count) {
15212         default:
15213                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15214                                 "0362 Unsupported MQ count. (%d)\n",
15215                                 mq->entry_count);
15216                 if (mq->entry_count < 16) {
15217                         status = -EINVAL;
15218                         goto out;
15219                 }
15220                 /* otherwise default to smallest count (drop through) */
15221         case 16:
15222                 bf_set(lpfc_mq_context_ring_size,
15223                        &mq_create_ext->u.request.context,
15224                        LPFC_MQ_RING_SIZE_16);
15225                 break;
15226         case 32:
15227                 bf_set(lpfc_mq_context_ring_size,
15228                        &mq_create_ext->u.request.context,
15229                        LPFC_MQ_RING_SIZE_32);
15230                 break;
15231         case 64:
15232                 bf_set(lpfc_mq_context_ring_size,
15233                        &mq_create_ext->u.request.context,
15234                        LPFC_MQ_RING_SIZE_64);
15235                 break;
15236         case 128:
15237                 bf_set(lpfc_mq_context_ring_size,
15238                        &mq_create_ext->u.request.context,
15239                        LPFC_MQ_RING_SIZE_128);
15240                 break;
15241         }
15242         list_for_each_entry(dmabuf, &mq->page_list, list) {
15243                 memset(dmabuf->virt, 0, hw_page_size);
15244                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15245                                         putPaddrLow(dmabuf->phys);
15246                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
15247                                         putPaddrHigh(dmabuf->phys);
15248         }
15249         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15250         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15251                               &mq_create_ext->u.response);
15252         if (rc != MBX_SUCCESS) {
15253                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15254                                 "2795 MQ_CREATE_EXT failed with "
15255                                 "status x%x. Failback to MQ_CREATE.\n",
15256                                 rc);
15257                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15258                 mq_create = &mbox->u.mqe.un.mq_create;
15259                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15260                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15261                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15262                                       &mq_create->u.response);
15263         }
15264
15265         /* The IOCTL status is embedded in the mailbox subheader. */
15266         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15267         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15268         if (shdr_status || shdr_add_status || rc) {
15269                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15270                                 "2502 MQ_CREATE mailbox failed with "
15271                                 "status x%x add_status x%x, mbx status x%x\n",
15272                                 shdr_status, shdr_add_status, rc);
15273                 status = -ENXIO;
15274                 goto out;
15275         }
15276         if (mq->queue_id == 0xFFFF) {
15277                 status = -ENXIO;
15278                 goto out;
15279         }
15280         mq->type = LPFC_MQ;
15281         mq->assoc_qid = cq->queue_id;
15282         mq->subtype = subtype;
15283         mq->host_index = 0;
15284         mq->hba_index = 0;
15285         mq->entry_repost = LPFC_MQ_REPOST;
15286
15287         /* link the mq onto the parent cq child list */
15288         list_add_tail(&mq->list, &cq->child_list);
15289 out:
15290         mempool_free(mbox, phba->mbox_mem_pool);
15291         return status;
15292 }
15293
15294 /**
15295  * lpfc_wq_create - Create a Work Queue on the HBA
15296  * @phba: HBA structure that indicates port to create a queue on.
15297  * @wq: The queue structure to use to create the work queue.
15298  * @cq: The completion queue to bind this work queue to.
15299  * @subtype: The subtype of the work queue indicating its functionality.
15300  *
15301  * This function creates a work queue, as detailed in @wq, on a port, described
15302  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15303  *
15304  * The @phba struct is used to send mailbox command to HBA. The @wq struct
15305  * is used to get the entry count and entry size that are necessary to
15306  * determine the number of pages to allocate and use for this queue. The @cq
15307  * is used to indicate which completion queue to bind this work queue to. This
15308  * function will send the WQ_CREATE mailbox command to the HBA to setup the
15309  * work queue. This function is asynchronous and will wait for the mailbox
15310  * command to finish before continuing.
15311  *
15312  * On success this function will return a zero. If unable to allocate enough
15313  * memory this function will return -ENOMEM. If the queue create mailbox command
15314  * fails this function will return -ENXIO.
15315  **/
15316 int
15317 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15318                struct lpfc_queue *cq, uint32_t subtype)
15319 {
15320         struct lpfc_mbx_wq_create *wq_create;
15321         struct lpfc_dmabuf *dmabuf;
15322         LPFC_MBOXQ_t *mbox;
15323         int rc, length, status = 0;
15324         uint32_t shdr_status, shdr_add_status;
15325         union lpfc_sli4_cfg_shdr *shdr;
15326         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15327         struct dma_address *page;
15328         void __iomem *bar_memmap_p;
15329         uint32_t db_offset;
15330         uint16_t pci_barset;
15331         uint8_t dpp_barset;
15332         uint32_t dpp_offset;
15333         unsigned long pg_addr;
15334         uint8_t wq_create_version;
15335
15336         /* sanity check on queue memory */
15337         if (!wq || !cq)
15338                 return -ENODEV;
15339         if (!phba->sli4_hba.pc_sli4_params.supported)
15340                 hw_page_size = wq->page_size;
15341
15342         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15343         if (!mbox)
15344                 return -ENOMEM;
15345         length = (sizeof(struct lpfc_mbx_wq_create) -
15346                   sizeof(struct lpfc_sli4_cfg_mhdr));
15347         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15348                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15349                          length, LPFC_SLI4_MBX_EMBED);
15350         wq_create = &mbox->u.mqe.un.wq_create;
15351         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
15352         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15353                     wq->page_count);
15354         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15355                     cq->queue_id);
15356
15357         /* wqv is the earliest version supported, NOT the latest */
15358         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15359                phba->sli4_hba.pc_sli4_params.wqv);
15360
15361         if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15362             (wq->page_size > SLI4_PAGE_SIZE))
15363                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15364         else
15365                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15366
15367
15368         if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15369                 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15370         else
15371                 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15372
15373         switch (wq_create_version) {
15374         case LPFC_Q_CREATE_VERSION_1:
15375                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15376                        wq->entry_count);
15377                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15378                        LPFC_Q_CREATE_VERSION_1);
15379
15380                 switch (wq->entry_size) {
15381                 default:
15382                 case 64:
15383                         bf_set(lpfc_mbx_wq_create_wqe_size,
15384                                &wq_create->u.request_1,
15385                                LPFC_WQ_WQE_SIZE_64);
15386                         break;
15387                 case 128:
15388                         bf_set(lpfc_mbx_wq_create_wqe_size,
15389                                &wq_create->u.request_1,
15390                                LPFC_WQ_WQE_SIZE_128);
15391                         break;
15392                 }
15393                 /* Request DPP by default */
15394                 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
15395                 bf_set(lpfc_mbx_wq_create_page_size,
15396                        &wq_create->u.request_1,
15397                        (wq->page_size / SLI4_PAGE_SIZE));
15398                 page = wq_create->u.request_1.page;
15399                 break;
15400         default:
15401                 page = wq_create->u.request.page;
15402                 break;
15403         }
15404
15405         list_for_each_entry(dmabuf, &wq->page_list, list) {
15406                 memset(dmabuf->virt, 0, hw_page_size);
15407                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15408                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
15409         }
15410
15411         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15412                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15413
15414         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15415         /* The IOCTL status is embedded in the mailbox subheader. */
15416         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15417         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15418         if (shdr_status || shdr_add_status || rc) {
15419                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15420                                 "2503 WQ_CREATE mailbox failed with "
15421                                 "status x%x add_status x%x, mbx status x%x\n",
15422                                 shdr_status, shdr_add_status, rc);
15423                 status = -ENXIO;
15424                 goto out;
15425         }
15426
15427         if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15428                 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15429                                         &wq_create->u.response);
15430         else
15431                 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15432                                         &wq_create->u.response_1);
15433
15434         if (wq->queue_id == 0xFFFF) {
15435                 status = -ENXIO;
15436                 goto out;
15437         }
15438
15439         wq->db_format = LPFC_DB_LIST_FORMAT;
15440         if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15441                 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15442                         wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15443                                                &wq_create->u.response);
15444                         if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15445                             (wq->db_format != LPFC_DB_RING_FORMAT)) {
15446                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15447                                                 "3265 WQ[%d] doorbell format "
15448                                                 "not supported: x%x\n",
15449                                                 wq->queue_id, wq->db_format);
15450                                 status = -EINVAL;
15451                                 goto out;
15452                         }
15453                         pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15454                                             &wq_create->u.response);
15455                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15456                                                                    pci_barset);
15457                         if (!bar_memmap_p) {
15458                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15459                                                 "3263 WQ[%d] failed to memmap "
15460                                                 "pci barset:x%x\n",
15461                                                 wq->queue_id, pci_barset);
15462                                 status = -ENOMEM;
15463                                 goto out;
15464                         }
15465                         db_offset = wq_create->u.response.doorbell_offset;
15466                         if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15467                             (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15468                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15469                                                 "3252 WQ[%d] doorbell offset "
15470                                                 "not supported: x%x\n",
15471                                                 wq->queue_id, db_offset);
15472                                 status = -EINVAL;
15473                                 goto out;
15474                         }
15475                         wq->db_regaddr = bar_memmap_p + db_offset;
15476                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15477                                         "3264 WQ[%d]: barset:x%x, offset:x%x, "
15478                                         "format:x%x\n", wq->queue_id,
15479                                         pci_barset, db_offset, wq->db_format);
15480                 } else
15481                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15482         } else {
15483                 /* Check if DPP was honored by the firmware */
15484                 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15485                                     &wq_create->u.response_1);
15486                 if (wq->dpp_enable) {
15487                         pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15488                                             &wq_create->u.response_1);
15489                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15490                                                                    pci_barset);
15491                         if (!bar_memmap_p) {
15492                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15493                                                 "3267 WQ[%d] failed to memmap "
15494                                                 "pci barset:x%x\n",
15495                                                 wq->queue_id, pci_barset);
15496                                 status = -ENOMEM;
15497                                 goto out;
15498                         }
15499                         db_offset = wq_create->u.response_1.doorbell_offset;
15500                         wq->db_regaddr = bar_memmap_p + db_offset;
15501                         wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15502                                             &wq_create->u.response_1);
15503                         dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15504                                             &wq_create->u.response_1);
15505                         bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15506                                                                    dpp_barset);
15507                         if (!bar_memmap_p) {
15508                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15509                                                 "3268 WQ[%d] failed to memmap "
15510                                                 "pci barset:x%x\n",
15511                                                 wq->queue_id, dpp_barset);
15512                                 status = -ENOMEM;
15513                                 goto out;
15514                         }
15515                         dpp_offset = wq_create->u.response_1.dpp_offset;
15516                         wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15517                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15518                                         "3271 WQ[%d]: barset:x%x, offset:x%x, "
15519                                         "dpp_id:x%x dpp_barset:x%x "
15520                                         "dpp_offset:x%x\n",
15521                                         wq->queue_id, pci_barset, db_offset,
15522                                         wq->dpp_id, dpp_barset, dpp_offset);
15523
15524                         /* Enable combined writes for DPP aperture */
15525                         pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15526 #ifdef CONFIG_X86
15527                         rc = set_memory_wc(pg_addr, 1);
15528                         if (rc) {
15529                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15530                                         "3272 Cannot setup Combined "
15531                                         "Write on WQ[%d] - disable DPP\n",
15532                                         wq->queue_id);
15533                                 phba->cfg_enable_dpp = 0;
15534                         }
15535 #else
15536                         phba->cfg_enable_dpp = 0;
15537 #endif
15538                 } else
15539                         wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15540         }
15541         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15542         if (wq->pring == NULL) {
15543                 status = -ENOMEM;
15544                 goto out;
15545         }
15546         wq->type = LPFC_WQ;
15547         wq->assoc_qid = cq->queue_id;
15548         wq->subtype = subtype;
15549         wq->host_index = 0;
15550         wq->hba_index = 0;
15551         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
15552
15553         /* link the wq onto the parent cq child list */
15554         list_add_tail(&wq->list, &cq->child_list);
15555 out:
15556         mempool_free(mbox, phba->mbox_mem_pool);
15557         return status;
15558 }
15559
15560 /**
15561  * lpfc_rq_create - Create a Receive Queue on the HBA
15562  * @phba: HBA structure that indicates port to create a queue on.
15563  * @hrq: The queue structure to use to create the header receive queue.
15564  * @drq: The queue structure to use to create the data receive queue.
15565  * @cq: The completion queue to bind this work queue to.
15566  *
15567  * This function creates a receive buffer queue pair , as detailed in @hrq and
15568  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15569  * to the HBA.
15570  *
15571  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15572  * struct is used to get the entry count that is necessary to determine the
15573  * number of pages to use for this queue. The @cq is used to indicate which
15574  * completion queue to bind received buffers that are posted to these queues to.
15575  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15576  * receive queue pair. This function is asynchronous and will wait for the
15577  * mailbox command to finish before continuing.
15578  *
15579  * On success this function will return a zero. If unable to allocate enough
15580  * memory this function will return -ENOMEM. If the queue create mailbox command
15581  * fails this function will return -ENXIO.
15582  **/
15583 int
15584 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15585                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15586 {
15587         struct lpfc_mbx_rq_create *rq_create;
15588         struct lpfc_dmabuf *dmabuf;
15589         LPFC_MBOXQ_t *mbox;
15590         int rc, length, status = 0;
15591         uint32_t shdr_status, shdr_add_status;
15592         union lpfc_sli4_cfg_shdr *shdr;
15593         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15594         void __iomem *bar_memmap_p;
15595         uint32_t db_offset;
15596         uint16_t pci_barset;
15597
15598         /* sanity check on queue memory */
15599         if (!hrq || !drq || !cq)
15600                 return -ENODEV;
15601         if (!phba->sli4_hba.pc_sli4_params.supported)
15602                 hw_page_size = SLI4_PAGE_SIZE;
15603
15604         if (hrq->entry_count != drq->entry_count)
15605                 return -EINVAL;
15606         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15607         if (!mbox)
15608                 return -ENOMEM;
15609         length = (sizeof(struct lpfc_mbx_rq_create) -
15610                   sizeof(struct lpfc_sli4_cfg_mhdr));
15611         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15612                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15613                          length, LPFC_SLI4_MBX_EMBED);
15614         rq_create = &mbox->u.mqe.un.rq_create;
15615         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15616         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15617                phba->sli4_hba.pc_sli4_params.rqv);
15618         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15619                 bf_set(lpfc_rq_context_rqe_count_1,
15620                        &rq_create->u.request.context,
15621                        hrq->entry_count);
15622                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
15623                 bf_set(lpfc_rq_context_rqe_size,
15624                        &rq_create->u.request.context,
15625                        LPFC_RQE_SIZE_8);
15626                 bf_set(lpfc_rq_context_page_size,
15627                        &rq_create->u.request.context,
15628                        LPFC_RQ_PAGE_SIZE_4096);
15629         } else {
15630                 switch (hrq->entry_count) {
15631                 default:
15632                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15633                                         "2535 Unsupported RQ count. (%d)\n",
15634                                         hrq->entry_count);
15635                         if (hrq->entry_count < 512) {
15636                                 status = -EINVAL;
15637                                 goto out;
15638                         }
15639                         /* otherwise default to smallest count (drop through) */
15640                 case 512:
15641                         bf_set(lpfc_rq_context_rqe_count,
15642                                &rq_create->u.request.context,
15643                                LPFC_RQ_RING_SIZE_512);
15644                         break;
15645                 case 1024:
15646                         bf_set(lpfc_rq_context_rqe_count,
15647                                &rq_create->u.request.context,
15648                                LPFC_RQ_RING_SIZE_1024);
15649                         break;
15650                 case 2048:
15651                         bf_set(lpfc_rq_context_rqe_count,
15652                                &rq_create->u.request.context,
15653                                LPFC_RQ_RING_SIZE_2048);
15654                         break;
15655                 case 4096:
15656                         bf_set(lpfc_rq_context_rqe_count,
15657                                &rq_create->u.request.context,
15658                                LPFC_RQ_RING_SIZE_4096);
15659                         break;
15660                 }
15661                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15662                        LPFC_HDR_BUF_SIZE);
15663         }
15664         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15665                cq->queue_id);
15666         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15667                hrq->page_count);
15668         list_for_each_entry(dmabuf, &hrq->page_list, list) {
15669                 memset(dmabuf->virt, 0, hw_page_size);
15670                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15671                                         putPaddrLow(dmabuf->phys);
15672                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15673                                         putPaddrHigh(dmabuf->phys);
15674         }
15675         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15676                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15677
15678         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15679         /* The IOCTL status is embedded in the mailbox subheader. */
15680         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15681         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15682         if (shdr_status || shdr_add_status || rc) {
15683                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15684                                 "2504 RQ_CREATE mailbox failed with "
15685                                 "status x%x add_status x%x, mbx status x%x\n",
15686                                 shdr_status, shdr_add_status, rc);
15687                 status = -ENXIO;
15688                 goto out;
15689         }
15690         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15691         if (hrq->queue_id == 0xFFFF) {
15692                 status = -ENXIO;
15693                 goto out;
15694         }
15695
15696         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15697                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15698                                         &rq_create->u.response);
15699                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15700                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15701                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15702                                         "3262 RQ [%d] doorbell format not "
15703                                         "supported: x%x\n", hrq->queue_id,
15704                                         hrq->db_format);
15705                         status = -EINVAL;
15706                         goto out;
15707                 }
15708
15709                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15710                                     &rq_create->u.response);
15711                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15712                 if (!bar_memmap_p) {
15713                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15714                                         "3269 RQ[%d] failed to memmap pci "
15715                                         "barset:x%x\n", hrq->queue_id,
15716                                         pci_barset);
15717                         status = -ENOMEM;
15718                         goto out;
15719                 }
15720
15721                 db_offset = rq_create->u.response.doorbell_offset;
15722                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15723                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15724                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15725                                         "3270 RQ[%d] doorbell offset not "
15726                                         "supported: x%x\n", hrq->queue_id,
15727                                         db_offset);
15728                         status = -EINVAL;
15729                         goto out;
15730                 }
15731                 hrq->db_regaddr = bar_memmap_p + db_offset;
15732                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15733                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15734                                 "format:x%x\n", hrq->queue_id, pci_barset,
15735                                 db_offset, hrq->db_format);
15736         } else {
15737                 hrq->db_format = LPFC_DB_RING_FORMAT;
15738                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15739         }
15740         hrq->type = LPFC_HRQ;
15741         hrq->assoc_qid = cq->queue_id;
15742         hrq->subtype = subtype;
15743         hrq->host_index = 0;
15744         hrq->hba_index = 0;
15745         hrq->entry_repost = LPFC_RQ_REPOST;
15746
15747         /* now create the data queue */
15748         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15749                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15750                          length, LPFC_SLI4_MBX_EMBED);
15751         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15752                phba->sli4_hba.pc_sli4_params.rqv);
15753         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15754                 bf_set(lpfc_rq_context_rqe_count_1,
15755                        &rq_create->u.request.context, hrq->entry_count);
15756                 if (subtype == LPFC_NVMET)
15757                         rq_create->u.request.context.buffer_size =
15758                                 LPFC_NVMET_DATA_BUF_SIZE;
15759                 else
15760                         rq_create->u.request.context.buffer_size =
15761                                 LPFC_DATA_BUF_SIZE;
15762                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15763                        LPFC_RQE_SIZE_8);
15764                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15765                        (PAGE_SIZE/SLI4_PAGE_SIZE));
15766         } else {
15767                 switch (drq->entry_count) {
15768                 default:
15769                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15770                                         "2536 Unsupported RQ count. (%d)\n",
15771                                         drq->entry_count);
15772                         if (drq->entry_count < 512) {
15773                                 status = -EINVAL;
15774                                 goto out;
15775                         }
15776                         /* otherwise default to smallest count (drop through) */
15777                 case 512:
15778                         bf_set(lpfc_rq_context_rqe_count,
15779                                &rq_create->u.request.context,
15780                                LPFC_RQ_RING_SIZE_512);
15781                         break;
15782                 case 1024:
15783                         bf_set(lpfc_rq_context_rqe_count,
15784                                &rq_create->u.request.context,
15785                                LPFC_RQ_RING_SIZE_1024);
15786                         break;
15787                 case 2048:
15788                         bf_set(lpfc_rq_context_rqe_count,
15789                                &rq_create->u.request.context,
15790                                LPFC_RQ_RING_SIZE_2048);
15791                         break;
15792                 case 4096:
15793                         bf_set(lpfc_rq_context_rqe_count,
15794                                &rq_create->u.request.context,
15795                                LPFC_RQ_RING_SIZE_4096);
15796                         break;
15797                 }
15798                 if (subtype == LPFC_NVMET)
15799                         bf_set(lpfc_rq_context_buf_size,
15800                                &rq_create->u.request.context,
15801                                LPFC_NVMET_DATA_BUF_SIZE);
15802                 else
15803                         bf_set(lpfc_rq_context_buf_size,
15804                                &rq_create->u.request.context,
15805                                LPFC_DATA_BUF_SIZE);
15806         }
15807         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15808                cq->queue_id);
15809         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15810                drq->page_count);
15811         list_for_each_entry(dmabuf, &drq->page_list, list) {
15812                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15813                                         putPaddrLow(dmabuf->phys);
15814                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15815                                         putPaddrHigh(dmabuf->phys);
15816         }
15817         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15818                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15819         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15820         /* The IOCTL status is embedded in the mailbox subheader. */
15821         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15822         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15823         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15824         if (shdr_status || shdr_add_status || rc) {
15825                 status = -ENXIO;
15826                 goto out;
15827         }
15828         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15829         if (drq->queue_id == 0xFFFF) {
15830                 status = -ENXIO;
15831                 goto out;
15832         }
15833         drq->type = LPFC_DRQ;
15834         drq->assoc_qid = cq->queue_id;
15835         drq->subtype = subtype;
15836         drq->host_index = 0;
15837         drq->hba_index = 0;
15838         drq->entry_repost = LPFC_RQ_REPOST;
15839
15840         /* link the header and data RQs onto the parent cq child list */
15841         list_add_tail(&hrq->list, &cq->child_list);
15842         list_add_tail(&drq->list, &cq->child_list);
15843
15844 out:
15845         mempool_free(mbox, phba->mbox_mem_pool);
15846         return status;
15847 }
15848
15849 /**
15850  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15851  * @phba: HBA structure that indicates port to create a queue on.
15852  * @hrqp: The queue structure array to use to create the header receive queues.
15853  * @drqp: The queue structure array to use to create the data receive queues.
15854  * @cqp: The completion queue array to bind these receive queues to.
15855  *
15856  * This function creates a receive buffer queue pair , as detailed in @hrq and
15857  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15858  * to the HBA.
15859  *
15860  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15861  * struct is used to get the entry count that is necessary to determine the
15862  * number of pages to use for this queue. The @cq is used to indicate which
15863  * completion queue to bind received buffers that are posted to these queues to.
15864  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15865  * receive queue pair. This function is asynchronous and will wait for the
15866  * mailbox command to finish before continuing.
15867  *
15868  * On success this function will return a zero. If unable to allocate enough
15869  * memory this function will return -ENOMEM. If the queue create mailbox command
15870  * fails this function will return -ENXIO.
15871  **/
15872 int
15873 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15874                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15875                 uint32_t subtype)
15876 {
15877         struct lpfc_queue *hrq, *drq, *cq;
15878         struct lpfc_mbx_rq_create_v2 *rq_create;
15879         struct lpfc_dmabuf *dmabuf;
15880         LPFC_MBOXQ_t *mbox;
15881         int rc, length, alloclen, status = 0;
15882         int cnt, idx, numrq, page_idx = 0;
15883         uint32_t shdr_status, shdr_add_status;
15884         union lpfc_sli4_cfg_shdr *shdr;
15885         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15886
15887         numrq = phba->cfg_nvmet_mrq;
15888         /* sanity check on array memory */
15889         if (!hrqp || !drqp || !cqp || !numrq)
15890                 return -ENODEV;
15891         if (!phba->sli4_hba.pc_sli4_params.supported)
15892                 hw_page_size = SLI4_PAGE_SIZE;
15893
15894         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15895         if (!mbox)
15896                 return -ENOMEM;
15897
15898         length = sizeof(struct lpfc_mbx_rq_create_v2);
15899         length += ((2 * numrq * hrqp[0]->page_count) *
15900                    sizeof(struct dma_address));
15901
15902         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15903                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15904                                     LPFC_SLI4_MBX_NEMBED);
15905         if (alloclen < length) {
15906                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15907                                 "3099 Allocated DMA memory size (%d) is "
15908                                 "less than the requested DMA memory size "
15909                                 "(%d)\n", alloclen, length);
15910                 status = -ENOMEM;
15911                 goto out;
15912         }
15913
15914
15915
15916         rq_create = mbox->sge_array->addr[0];
15917         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15918
15919         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15920         cnt = 0;
15921
15922         for (idx = 0; idx < numrq; idx++) {
15923                 hrq = hrqp[idx];
15924                 drq = drqp[idx];
15925                 cq  = cqp[idx];
15926
15927                 /* sanity check on queue memory */
15928                 if (!hrq || !drq || !cq) {
15929                         status = -ENODEV;
15930                         goto out;
15931                 }
15932
15933                 if (hrq->entry_count != drq->entry_count) {
15934                         status = -EINVAL;
15935                         goto out;
15936                 }
15937
15938                 if (idx == 0) {
15939                         bf_set(lpfc_mbx_rq_create_num_pages,
15940                                &rq_create->u.request,
15941                                hrq->page_count);
15942                         bf_set(lpfc_mbx_rq_create_rq_cnt,
15943                                &rq_create->u.request, (numrq * 2));
15944                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15945                                1);
15946                         bf_set(lpfc_rq_context_base_cq,
15947                                &rq_create->u.request.context,
15948                                cq->queue_id);
15949                         bf_set(lpfc_rq_context_data_size,
15950                                &rq_create->u.request.context,
15951                                LPFC_NVMET_DATA_BUF_SIZE);
15952                         bf_set(lpfc_rq_context_hdr_size,
15953                                &rq_create->u.request.context,
15954                                LPFC_HDR_BUF_SIZE);
15955                         bf_set(lpfc_rq_context_rqe_count_1,
15956                                &rq_create->u.request.context,
15957                                hrq->entry_count);
15958                         bf_set(lpfc_rq_context_rqe_size,
15959                                &rq_create->u.request.context,
15960                                LPFC_RQE_SIZE_8);
15961                         bf_set(lpfc_rq_context_page_size,
15962                                &rq_create->u.request.context,
15963                                (PAGE_SIZE/SLI4_PAGE_SIZE));
15964                 }
15965                 rc = 0;
15966                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15967                         memset(dmabuf->virt, 0, hw_page_size);
15968                         cnt = page_idx + dmabuf->buffer_tag;
15969                         rq_create->u.request.page[cnt].addr_lo =
15970                                         putPaddrLow(dmabuf->phys);
15971                         rq_create->u.request.page[cnt].addr_hi =
15972                                         putPaddrHigh(dmabuf->phys);
15973                         rc++;
15974                 }
15975                 page_idx += rc;
15976
15977                 rc = 0;
15978                 list_for_each_entry(dmabuf, &drq->page_list, list) {
15979                         memset(dmabuf->virt, 0, hw_page_size);
15980                         cnt = page_idx + dmabuf->buffer_tag;
15981                         rq_create->u.request.page[cnt].addr_lo =
15982                                         putPaddrLow(dmabuf->phys);
15983                         rq_create->u.request.page[cnt].addr_hi =
15984                                         putPaddrHigh(dmabuf->phys);
15985                         rc++;
15986                 }
15987                 page_idx += rc;
15988
15989                 hrq->db_format = LPFC_DB_RING_FORMAT;
15990                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15991                 hrq->type = LPFC_HRQ;
15992                 hrq->assoc_qid = cq->queue_id;
15993                 hrq->subtype = subtype;
15994                 hrq->host_index = 0;
15995                 hrq->hba_index = 0;
15996                 hrq->entry_repost = LPFC_RQ_REPOST;
15997
15998                 drq->db_format = LPFC_DB_RING_FORMAT;
15999                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16000                 drq->type = LPFC_DRQ;
16001                 drq->assoc_qid = cq->queue_id;
16002                 drq->subtype = subtype;
16003                 drq->host_index = 0;
16004                 drq->hba_index = 0;
16005                 drq->entry_repost = LPFC_RQ_REPOST;
16006
16007                 list_add_tail(&hrq->list, &cq->child_list);
16008                 list_add_tail(&drq->list, &cq->child_list);
16009         }
16010
16011         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16012         /* The IOCTL status is embedded in the mailbox subheader. */
16013         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16014         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16015         if (shdr_status || shdr_add_status || rc) {
16016                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16017                                 "3120 RQ_CREATE mailbox failed with "
16018                                 "status x%x add_status x%x, mbx status x%x\n",
16019                                 shdr_status, shdr_add_status, rc);
16020                 status = -ENXIO;
16021                 goto out;
16022         }
16023         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16024         if (rc == 0xFFFF) {
16025                 status = -ENXIO;
16026                 goto out;
16027         }
16028
16029         /* Initialize all RQs with associated queue id */
16030         for (idx = 0; idx < numrq; idx++) {
16031                 hrq = hrqp[idx];
16032                 hrq->queue_id = rc + (2 * idx);
16033                 drq = drqp[idx];
16034                 drq->queue_id = rc + (2 * idx) + 1;
16035         }
16036
16037 out:
16038         lpfc_sli4_mbox_cmd_free(phba, mbox);
16039         return status;
16040 }
16041
16042 /**
16043  * lpfc_eq_destroy - Destroy an event Queue on the HBA
16044  * @eq: The queue structure associated with the queue to destroy.
16045  *
16046  * This function destroys a queue, as detailed in @eq by sending an mailbox
16047  * command, specific to the type of queue, to the HBA.
16048  *
16049  * The @eq struct is used to get the queue ID of the queue to destroy.
16050  *
16051  * On success this function will return a zero. If the queue destroy mailbox
16052  * command fails this function will return -ENXIO.
16053  **/
16054 int
16055 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16056 {
16057         LPFC_MBOXQ_t *mbox;
16058         int rc, length, status = 0;
16059         uint32_t shdr_status, shdr_add_status;
16060         union lpfc_sli4_cfg_shdr *shdr;
16061
16062         /* sanity check on queue memory */
16063         if (!eq)
16064                 return -ENODEV;
16065         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16066         if (!mbox)
16067                 return -ENOMEM;
16068         length = (sizeof(struct lpfc_mbx_eq_destroy) -
16069                   sizeof(struct lpfc_sli4_cfg_mhdr));
16070         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16071                          LPFC_MBOX_OPCODE_EQ_DESTROY,
16072                          length, LPFC_SLI4_MBX_EMBED);
16073         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16074                eq->queue_id);
16075         mbox->vport = eq->phba->pport;
16076         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16077
16078         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16079         /* The IOCTL status is embedded in the mailbox subheader. */
16080         shdr = (union lpfc_sli4_cfg_shdr *)
16081                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16082         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16083         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16084         if (shdr_status || shdr_add_status || rc) {
16085                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16086                                 "2505 EQ_DESTROY mailbox failed with "
16087                                 "status x%x add_status x%x, mbx status x%x\n",
16088                                 shdr_status, shdr_add_status, rc);
16089                 status = -ENXIO;
16090         }
16091
16092         /* Remove eq from any list */
16093         list_del_init(&eq->list);
16094         mempool_free(mbox, eq->phba->mbox_mem_pool);
16095         return status;
16096 }
16097
16098 /**
16099  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16100  * @cq: The queue structure associated with the queue to destroy.
16101  *
16102  * This function destroys a queue, as detailed in @cq by sending an mailbox
16103  * command, specific to the type of queue, to the HBA.
16104  *
16105  * The @cq struct is used to get the queue ID of the queue to destroy.
16106  *
16107  * On success this function will return a zero. If the queue destroy mailbox
16108  * command fails this function will return -ENXIO.
16109  **/
16110 int
16111 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16112 {
16113         LPFC_MBOXQ_t *mbox;
16114         int rc, length, status = 0;
16115         uint32_t shdr_status, shdr_add_status;
16116         union lpfc_sli4_cfg_shdr *shdr;
16117
16118         /* sanity check on queue memory */
16119         if (!cq)
16120                 return -ENODEV;
16121         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16122         if (!mbox)
16123                 return -ENOMEM;
16124         length = (sizeof(struct lpfc_mbx_cq_destroy) -
16125                   sizeof(struct lpfc_sli4_cfg_mhdr));
16126         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16127                          LPFC_MBOX_OPCODE_CQ_DESTROY,
16128                          length, LPFC_SLI4_MBX_EMBED);
16129         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16130                cq->queue_id);
16131         mbox->vport = cq->phba->pport;
16132         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16133         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16134         /* The IOCTL status is embedded in the mailbox subheader. */
16135         shdr = (union lpfc_sli4_cfg_shdr *)
16136                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16137         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16138         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16139         if (shdr_status || shdr_add_status || rc) {
16140                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16141                                 "2506 CQ_DESTROY mailbox failed with "
16142                                 "status x%x add_status x%x, mbx status x%x\n",
16143                                 shdr_status, shdr_add_status, rc);
16144                 status = -ENXIO;
16145         }
16146         /* Remove cq from any list */
16147         list_del_init(&cq->list);
16148         mempool_free(mbox, cq->phba->mbox_mem_pool);
16149         return status;
16150 }
16151
16152 /**
16153  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16154  * @qm: The queue structure associated with the queue to destroy.
16155  *
16156  * This function destroys a queue, as detailed in @mq by sending an mailbox
16157  * command, specific to the type of queue, to the HBA.
16158  *
16159  * The @mq struct is used to get the queue ID of the queue to destroy.
16160  *
16161  * On success this function will return a zero. If the queue destroy mailbox
16162  * command fails this function will return -ENXIO.
16163  **/
16164 int
16165 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16166 {
16167         LPFC_MBOXQ_t *mbox;
16168         int rc, length, status = 0;
16169         uint32_t shdr_status, shdr_add_status;
16170         union lpfc_sli4_cfg_shdr *shdr;
16171
16172         /* sanity check on queue memory */
16173         if (!mq)
16174                 return -ENODEV;
16175         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16176         if (!mbox)
16177                 return -ENOMEM;
16178         length = (sizeof(struct lpfc_mbx_mq_destroy) -
16179                   sizeof(struct lpfc_sli4_cfg_mhdr));
16180         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16181                          LPFC_MBOX_OPCODE_MQ_DESTROY,
16182                          length, LPFC_SLI4_MBX_EMBED);
16183         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16184                mq->queue_id);
16185         mbox->vport = mq->phba->pport;
16186         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16187         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16188         /* The IOCTL status is embedded in the mailbox subheader. */
16189         shdr = (union lpfc_sli4_cfg_shdr *)
16190                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16191         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16192         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16193         if (shdr_status || shdr_add_status || rc) {
16194                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16195                                 "2507 MQ_DESTROY mailbox failed with "
16196                                 "status x%x add_status x%x, mbx status x%x\n",
16197                                 shdr_status, shdr_add_status, rc);
16198                 status = -ENXIO;
16199         }
16200         /* Remove mq from any list */
16201         list_del_init(&mq->list);
16202         mempool_free(mbox, mq->phba->mbox_mem_pool);
16203         return status;
16204 }
16205
16206 /**
16207  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16208  * @wq: The queue structure associated with the queue to destroy.
16209  *
16210  * This function destroys a queue, as detailed in @wq by sending an mailbox
16211  * command, specific to the type of queue, to the HBA.
16212  *
16213  * The @wq struct is used to get the queue ID of the queue to destroy.
16214  *
16215  * On success this function will return a zero. If the queue destroy mailbox
16216  * command fails this function will return -ENXIO.
16217  **/
16218 int
16219 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16220 {
16221         LPFC_MBOXQ_t *mbox;
16222         int rc, length, status = 0;
16223         uint32_t shdr_status, shdr_add_status;
16224         union lpfc_sli4_cfg_shdr *shdr;
16225
16226         /* sanity check on queue memory */
16227         if (!wq)
16228                 return -ENODEV;
16229         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16230         if (!mbox)
16231                 return -ENOMEM;
16232         length = (sizeof(struct lpfc_mbx_wq_destroy) -
16233                   sizeof(struct lpfc_sli4_cfg_mhdr));
16234         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16235                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16236                          length, LPFC_SLI4_MBX_EMBED);
16237         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16238                wq->queue_id);
16239         mbox->vport = wq->phba->pport;
16240         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16241         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16242         shdr = (union lpfc_sli4_cfg_shdr *)
16243                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16244         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16245         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16246         if (shdr_status || shdr_add_status || rc) {
16247                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16248                                 "2508 WQ_DESTROY mailbox failed with "
16249                                 "status x%x add_status x%x, mbx status x%x\n",
16250                                 shdr_status, shdr_add_status, rc);
16251                 status = -ENXIO;
16252         }
16253         /* Remove wq from any list */
16254         list_del_init(&wq->list);
16255         kfree(wq->pring);
16256         wq->pring = NULL;
16257         mempool_free(mbox, wq->phba->mbox_mem_pool);
16258         return status;
16259 }
16260
16261 /**
16262  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16263  * @rq: The queue structure associated with the queue to destroy.
16264  *
16265  * This function destroys a queue, as detailed in @rq by sending an mailbox
16266  * command, specific to the type of queue, to the HBA.
16267  *
16268  * The @rq struct is used to get the queue ID of the queue to destroy.
16269  *
16270  * On success this function will return a zero. If the queue destroy mailbox
16271  * command fails this function will return -ENXIO.
16272  **/
16273 int
16274 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16275                 struct lpfc_queue *drq)
16276 {
16277         LPFC_MBOXQ_t *mbox;
16278         int rc, length, status = 0;
16279         uint32_t shdr_status, shdr_add_status;
16280         union lpfc_sli4_cfg_shdr *shdr;
16281
16282         /* sanity check on queue memory */
16283         if (!hrq || !drq)
16284                 return -ENODEV;
16285         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16286         if (!mbox)
16287                 return -ENOMEM;
16288         length = (sizeof(struct lpfc_mbx_rq_destroy) -
16289                   sizeof(struct lpfc_sli4_cfg_mhdr));
16290         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16291                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16292                          length, LPFC_SLI4_MBX_EMBED);
16293         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16294                hrq->queue_id);
16295         mbox->vport = hrq->phba->pport;
16296         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16297         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16298         /* The IOCTL status is embedded in the mailbox subheader. */
16299         shdr = (union lpfc_sli4_cfg_shdr *)
16300                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16301         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16302         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16303         if (shdr_status || shdr_add_status || rc) {
16304                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16305                                 "2509 RQ_DESTROY mailbox failed with "
16306                                 "status x%x add_status x%x, mbx status x%x\n",
16307                                 shdr_status, shdr_add_status, rc);
16308                 if (rc != MBX_TIMEOUT)
16309                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
16310                 return -ENXIO;
16311         }
16312         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16313                drq->queue_id);
16314         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16315         shdr = (union lpfc_sli4_cfg_shdr *)
16316                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16317         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16318         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16319         if (shdr_status || shdr_add_status || rc) {
16320                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16321                                 "2510 RQ_DESTROY mailbox failed with "
16322                                 "status x%x add_status x%x, mbx status x%x\n",
16323                                 shdr_status, shdr_add_status, rc);
16324                 status = -ENXIO;
16325         }
16326         list_del_init(&hrq->list);
16327         list_del_init(&drq->list);
16328         mempool_free(mbox, hrq->phba->mbox_mem_pool);
16329         return status;
16330 }
16331
16332 /**
16333  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16334  * @phba: The virtual port for which this call being executed.
16335  * @pdma_phys_addr0: Physical address of the 1st SGL page.
16336  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16337  * @xritag: the xritag that ties this io to the SGL pages.
16338  *
16339  * This routine will post the sgl pages for the IO that has the xritag
16340  * that is in the iocbq structure. The xritag is assigned during iocbq
16341  * creation and persists for as long as the driver is loaded.
16342  * if the caller has fewer than 256 scatter gather segments to map then
16343  * pdma_phys_addr1 should be 0.
16344  * If the caller needs to map more than 256 scatter gather segment then
16345  * pdma_phys_addr1 should be a valid physical address.
16346  * physical address for SGLs must be 64 byte aligned.
16347  * If you are going to map 2 SGL's then the first one must have 256 entries
16348  * the second sgl can have between 1 and 256 entries.
16349  *
16350  * Return codes:
16351  *      0 - Success
16352  *      -ENXIO, -ENOMEM - Failure
16353  **/
16354 int
16355 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16356                 dma_addr_t pdma_phys_addr0,
16357                 dma_addr_t pdma_phys_addr1,
16358                 uint16_t xritag)
16359 {
16360         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16361         LPFC_MBOXQ_t *mbox;
16362         int rc;
16363         uint32_t shdr_status, shdr_add_status;
16364         uint32_t mbox_tmo;
16365         union lpfc_sli4_cfg_shdr *shdr;
16366
16367         if (xritag == NO_XRI) {
16368                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16369                                 "0364 Invalid param:\n");
16370                 return -EINVAL;
16371         }
16372
16373         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16374         if (!mbox)
16375                 return -ENOMEM;
16376
16377         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16378                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16379                         sizeof(struct lpfc_mbx_post_sgl_pages) -
16380                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16381
16382         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16383                                 &mbox->u.mqe.un.post_sgl_pages;
16384         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16385         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16386
16387         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16388                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16389         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16390                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16391
16392         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16393                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16394         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16395                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16396         if (!phba->sli4_hba.intr_enable)
16397                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16398         else {
16399                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16400                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16401         }
16402         /* The IOCTL status is embedded in the mailbox subheader. */
16403         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16404         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16405         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16406         if (rc != MBX_TIMEOUT)
16407                 mempool_free(mbox, phba->mbox_mem_pool);
16408         if (shdr_status || shdr_add_status || rc) {
16409                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16410                                 "2511 POST_SGL mailbox failed with "
16411                                 "status x%x add_status x%x, mbx status x%x\n",
16412                                 shdr_status, shdr_add_status, rc);
16413         }
16414         return 0;
16415 }
16416
16417 /**
16418  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
16419  * @phba: pointer to lpfc hba data structure.
16420  *
16421  * This routine is invoked to post rpi header templates to the
16422  * HBA consistent with the SLI-4 interface spec.  This routine
16423  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16424  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
16425  *
16426  * Returns
16427  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16428  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
16429  **/
16430 static uint16_t
16431 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16432 {
16433         unsigned long xri;
16434
16435         /*
16436          * Fetch the next logical xri.  Because this index is logical,
16437          * the driver starts at 0 each time.
16438          */
16439         spin_lock_irq(&phba->hbalock);
16440         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16441                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
16442         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16443                 spin_unlock_irq(&phba->hbalock);
16444                 return NO_XRI;
16445         } else {
16446                 set_bit(xri, phba->sli4_hba.xri_bmask);
16447                 phba->sli4_hba.max_cfg_param.xri_used++;
16448         }
16449         spin_unlock_irq(&phba->hbalock);
16450         return xri;
16451 }
16452
16453 /**
16454  * lpfc_sli4_free_xri - Release an xri for reuse.
16455  * @phba: pointer to lpfc hba data structure.
16456  *
16457  * This routine is invoked to release an xri to the pool of
16458  * available rpis maintained by the driver.
16459  **/
16460 static void
16461 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16462 {
16463         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
16464                 phba->sli4_hba.max_cfg_param.xri_used--;
16465         }
16466 }
16467
16468 /**
16469  * lpfc_sli4_free_xri - Release an xri for reuse.
16470  * @phba: pointer to lpfc hba data structure.
16471  *
16472  * This routine is invoked to release an xri to the pool of
16473  * available rpis maintained by the driver.
16474  **/
16475 void
16476 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16477 {
16478         spin_lock_irq(&phba->hbalock);
16479         __lpfc_sli4_free_xri(phba, xri);
16480         spin_unlock_irq(&phba->hbalock);
16481 }
16482
16483 /**
16484  * lpfc_sli4_next_xritag - Get an xritag for the io
16485  * @phba: Pointer to HBA context object.
16486  *
16487  * This function gets an xritag for the iocb. If there is no unused xritag
16488  * it will return 0xffff.
16489  * The function returns the allocated xritag if successful, else returns zero.
16490  * Zero is not a valid xritag.
16491  * The caller is not required to hold any lock.
16492  **/
16493 uint16_t
16494 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16495 {
16496         uint16_t xri_index;
16497
16498         xri_index = lpfc_sli4_alloc_xri(phba);
16499         if (xri_index == NO_XRI)
16500                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16501                                 "2004 Failed to allocate XRI.last XRITAG is %d"
16502                                 " Max XRI is %d, Used XRI is %d\n",
16503                                 xri_index,
16504                                 phba->sli4_hba.max_cfg_param.max_xri,
16505                                 phba->sli4_hba.max_cfg_param.xri_used);
16506         return xri_index;
16507 }
16508
16509 /**
16510  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
16511  * @phba: pointer to lpfc hba data structure.
16512  * @post_sgl_list: pointer to els sgl entry list.
16513  * @count: number of els sgl entries on the list.
16514  *
16515  * This routine is invoked to post a block of driver's sgl pages to the
16516  * HBA using non-embedded mailbox command. No Lock is held. This routine
16517  * is only called when the driver is loading and after all IO has been
16518  * stopped.
16519  **/
16520 static int
16521 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
16522                             struct list_head *post_sgl_list,
16523                             int post_cnt)
16524 {
16525         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
16526         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16527         struct sgl_page_pairs *sgl_pg_pairs;
16528         void *viraddr;
16529         LPFC_MBOXQ_t *mbox;
16530         uint32_t reqlen, alloclen, pg_pairs;
16531         uint32_t mbox_tmo;
16532         uint16_t xritag_start = 0;
16533         int rc = 0;
16534         uint32_t shdr_status, shdr_add_status;
16535         union lpfc_sli4_cfg_shdr *shdr;
16536
16537         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
16538                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16539         if (reqlen > SLI4_PAGE_SIZE) {
16540                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16541                                 "2559 Block sgl registration required DMA "
16542                                 "size (%d) great than a page\n", reqlen);
16543                 return -ENOMEM;
16544         }
16545
16546         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16547         if (!mbox)
16548                 return -ENOMEM;
16549
16550         /* Allocate DMA memory and set up the non-embedded mailbox command */
16551         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16552                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16553                          LPFC_SLI4_MBX_NEMBED);
16554
16555         if (alloclen < reqlen) {
16556                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16557                                 "0285 Allocated DMA memory size (%d) is "
16558                                 "less than the requested DMA memory "
16559                                 "size (%d)\n", alloclen, reqlen);
16560                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16561                 return -ENOMEM;
16562         }
16563         /* Set up the SGL pages in the non-embedded DMA pages */
16564         viraddr = mbox->sge_array->addr[0];
16565         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16566         sgl_pg_pairs = &sgl->sgl_pg_pairs;
16567
16568         pg_pairs = 0;
16569         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
16570                 /* Set up the sge entry */
16571                 sgl_pg_pairs->sgl_pg0_addr_lo =
16572                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16573                 sgl_pg_pairs->sgl_pg0_addr_hi =
16574                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16575                 sgl_pg_pairs->sgl_pg1_addr_lo =
16576                                 cpu_to_le32(putPaddrLow(0));
16577                 sgl_pg_pairs->sgl_pg1_addr_hi =
16578                                 cpu_to_le32(putPaddrHigh(0));
16579
16580                 /* Keep the first xritag on the list */
16581                 if (pg_pairs == 0)
16582                         xritag_start = sglq_entry->sli4_xritag;
16583                 sgl_pg_pairs++;
16584                 pg_pairs++;
16585         }
16586
16587         /* Complete initialization and perform endian conversion. */
16588         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16589         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
16590         sgl->word0 = cpu_to_le32(sgl->word0);
16591
16592         if (!phba->sli4_hba.intr_enable)
16593                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16594         else {
16595                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16596                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16597         }
16598         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16599         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16600         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16601         if (rc != MBX_TIMEOUT)
16602                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16603         if (shdr_status || shdr_add_status || rc) {
16604                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16605                                 "2513 POST_SGL_BLOCK mailbox command failed "
16606                                 "status x%x add_status x%x mbx status x%x\n",
16607                                 shdr_status, shdr_add_status, rc);
16608                 rc = -ENXIO;
16609         }
16610         return rc;
16611 }
16612
16613 /**
16614  * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
16615  * @phba: pointer to lpfc hba data structure.
16616  * @nblist: pointer to nvme buffer list.
16617  * @count: number of scsi buffers on the list.
16618  *
16619  * This routine is invoked to post a block of @count scsi sgl pages from a
16620  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
16621  * No Lock is held.
16622  *
16623  **/
16624 static int
16625 lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
16626                             int count)
16627 {
16628         struct lpfc_nvme_buf *lpfc_ncmd;
16629         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16630         struct sgl_page_pairs *sgl_pg_pairs;
16631         void *viraddr;
16632         LPFC_MBOXQ_t *mbox;
16633         uint32_t reqlen, alloclen, pg_pairs;
16634         uint32_t mbox_tmo;
16635         uint16_t xritag_start = 0;
16636         int rc = 0;
16637         uint32_t shdr_status, shdr_add_status;
16638         dma_addr_t pdma_phys_bpl1;
16639         union lpfc_sli4_cfg_shdr *shdr;
16640
16641         /* Calculate the requested length of the dma memory */
16642         reqlen = count * sizeof(struct sgl_page_pairs) +
16643                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16644         if (reqlen > SLI4_PAGE_SIZE) {
16645                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16646                                 "6118 Block sgl registration required DMA "
16647                                 "size (%d) great than a page\n", reqlen);
16648                 return -ENOMEM;
16649         }
16650         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16651         if (!mbox) {
16652                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16653                                 "6119 Failed to allocate mbox cmd memory\n");
16654                 return -ENOMEM;
16655         }
16656
16657         /* Allocate DMA memory and set up the non-embedded mailbox command */
16658         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16659                                     LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16660                                     reqlen, LPFC_SLI4_MBX_NEMBED);
16661
16662         if (alloclen < reqlen) {
16663                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16664                                 "6120 Allocated DMA memory size (%d) is "
16665                                 "less than the requested DMA memory "
16666                                 "size (%d)\n", alloclen, reqlen);
16667                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16668                 return -ENOMEM;
16669         }
16670
16671         /* Get the first SGE entry from the non-embedded DMA memory */
16672         viraddr = mbox->sge_array->addr[0];
16673
16674         /* Set up the SGL pages in the non-embedded DMA pages */
16675         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16676         sgl_pg_pairs = &sgl->sgl_pg_pairs;
16677
16678         pg_pairs = 0;
16679         list_for_each_entry(lpfc_ncmd, nblist, list) {
16680                 /* Set up the sge entry */
16681                 sgl_pg_pairs->sgl_pg0_addr_lo =
16682                         cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
16683                 sgl_pg_pairs->sgl_pg0_addr_hi =
16684                         cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
16685                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16686                         pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
16687                                                 SGL_PAGE_SIZE;
16688                 else
16689                         pdma_phys_bpl1 = 0;
16690                 sgl_pg_pairs->sgl_pg1_addr_lo =
16691                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16692                 sgl_pg_pairs->sgl_pg1_addr_hi =
16693                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16694                 /* Keep the first xritag on the list */
16695                 if (pg_pairs == 0)
16696                         xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
16697                 sgl_pg_pairs++;
16698                 pg_pairs++;
16699         }
16700         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16701         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16702         /* Perform endian conversion if necessary */
16703         sgl->word0 = cpu_to_le32(sgl->word0);
16704
16705         if (!phba->sli4_hba.intr_enable) {
16706                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16707         } else {
16708                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16709                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16710         }
16711         shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
16712         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16713         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16714         if (rc != MBX_TIMEOUT)
16715                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16716         if (shdr_status || shdr_add_status || rc) {
16717                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16718                                 "6125 POST_SGL_BLOCK mailbox command failed "
16719                                 "status x%x add_status x%x mbx status x%x\n",
16720                                 shdr_status, shdr_add_status, rc);
16721                 rc = -ENXIO;
16722         }
16723         return rc;
16724 }
16725
16726 /**
16727  * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
16728  * @phba: pointer to lpfc hba data structure.
16729  * @post_nblist: pointer to the nvme buffer list.
16730  *
16731  * This routine walks a list of nvme buffers that was passed in. It attempts
16732  * to construct blocks of nvme buffer sgls which contains contiguous xris and
16733  * uses the non-embedded SGL block post mailbox commands to post to the port.
16734  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
16735  * embedded SGL post mailbox command for posting. The @post_nblist passed in
16736  * must be local list, thus no lock is needed when manipulate the list.
16737  *
16738  * Returns: 0 = failure, non-zero number of successfully posted buffers.
16739  **/
16740 int
16741 lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
16742                            struct list_head *post_nblist, int sb_count)
16743 {
16744         struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
16745         int status, sgl_size;
16746         int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
16747         dma_addr_t pdma_phys_sgl1;
16748         int last_xritag = NO_XRI;
16749         int cur_xritag;
16750         LIST_HEAD(prep_nblist);
16751         LIST_HEAD(blck_nblist);
16752         LIST_HEAD(nvme_nblist);
16753
16754         /* sanity check */
16755         if (sb_count <= 0)
16756                 return -EINVAL;
16757
16758         sgl_size = phba->cfg_sg_dma_buf_size;
16759         list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
16760                 list_del_init(&lpfc_ncmd->list);
16761                 block_cnt++;
16762                 if ((last_xritag != NO_XRI) &&
16763                     (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
16764                         /* a hole in xri block, form a sgl posting block */
16765                         list_splice_init(&prep_nblist, &blck_nblist);
16766                         post_cnt = block_cnt - 1;
16767                         /* prepare list for next posting block */
16768                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16769                         block_cnt = 1;
16770                 } else {
16771                         /* prepare list for next posting block */
16772                         list_add_tail(&lpfc_ncmd->list, &prep_nblist);
16773                         /* enough sgls for non-embed sgl mbox command */
16774                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
16775                                 list_splice_init(&prep_nblist, &blck_nblist);
16776                                 post_cnt = block_cnt;
16777                                 block_cnt = 0;
16778                         }
16779                 }
16780                 num_posting++;
16781                 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16782
16783                 /* end of repost sgl list condition for NVME buffers */
16784                 if (num_posting == sb_count) {
16785                         if (post_cnt == 0) {
16786                                 /* last sgl posting block */
16787                                 list_splice_init(&prep_nblist, &blck_nblist);
16788                                 post_cnt = block_cnt;
16789                         } else if (block_cnt == 1) {
16790                                 /* last single sgl with non-contiguous xri */
16791                                 if (sgl_size > SGL_PAGE_SIZE)
16792                                         pdma_phys_sgl1 =
16793                                                 lpfc_ncmd->dma_phys_sgl +
16794                                                 SGL_PAGE_SIZE;
16795                                 else
16796                                         pdma_phys_sgl1 = 0;
16797                                 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
16798                                 status = lpfc_sli4_post_sgl(
16799                                                 phba, lpfc_ncmd->dma_phys_sgl,
16800                                                 pdma_phys_sgl1, cur_xritag);
16801                                 if (status) {
16802                                         /* failure, put on abort nvme list */
16803                                         lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
16804                                 } else {
16805                                         /* success, put on NVME buffer list */
16806                                         lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
16807                                         lpfc_ncmd->status = IOSTAT_SUCCESS;
16808                                         num_posted++;
16809                                 }
16810                                 /* success, put on NVME buffer sgl list */
16811                                 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16812                         }
16813                 }
16814
16815                 /* continue until a nembed page worth of sgls */
16816                 if (post_cnt == 0)
16817                         continue;
16818
16819                 /* post block of NVME buffer list sgls */
16820                 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
16821                                                      post_cnt);
16822
16823                 /* don't reset xirtag due to hole in xri block */
16824                 if (block_cnt == 0)
16825                         last_xritag = NO_XRI;
16826
16827                 /* reset NVME buffer post count for next round of posting */
16828                 post_cnt = 0;
16829
16830                 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
16831                 while (!list_empty(&blck_nblist)) {
16832                         list_remove_head(&blck_nblist, lpfc_ncmd,
16833                                          struct lpfc_nvme_buf, list);
16834                         if (status) {
16835                                 /* failure, put on abort nvme list */
16836                                 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
16837                         } else {
16838                                 /* success, put on NVME buffer list */
16839                                 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
16840                                 lpfc_ncmd->status = IOSTAT_SUCCESS;
16841                                 num_posted++;
16842                         }
16843                         list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
16844                 }
16845         }
16846         /* Push NVME buffers with sgl posted to the available list */
16847         lpfc_io_buf_replenish(phba, &nvme_nblist);
16848
16849         return num_posted;
16850 }
16851
16852 /**
16853  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16854  * @phba: pointer to lpfc_hba struct that the frame was received on
16855  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16856  *
16857  * This function checks the fields in the @fc_hdr to see if the FC frame is a
16858  * valid type of frame that the LPFC driver will handle. This function will
16859  * return a zero if the frame is a valid frame or a non zero value when the
16860  * frame does not pass the check.
16861  **/
16862 static int
16863 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16864 {
16865         /*  make rctl_names static to save stack space */
16866         struct fc_vft_header *fc_vft_hdr;
16867         uint32_t *header = (uint32_t *) fc_hdr;
16868
16869         switch (fc_hdr->fh_r_ctl) {
16870         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
16871         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
16872         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
16873         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
16874         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
16875         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
16876         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
16877         case FC_RCTL_DD_CMD_STATUS:     /* command status */
16878         case FC_RCTL_ELS_REQ:   /* extended link services request */
16879         case FC_RCTL_ELS_REP:   /* extended link services reply */
16880         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
16881         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
16882         case FC_RCTL_BA_NOP:    /* basic link service NOP */
16883         case FC_RCTL_BA_ABTS:   /* basic link service abort */
16884         case FC_RCTL_BA_RMC:    /* remove connection */
16885         case FC_RCTL_BA_ACC:    /* basic accept */
16886         case FC_RCTL_BA_RJT:    /* basic reject */
16887         case FC_RCTL_BA_PRMT:
16888         case FC_RCTL_ACK_1:     /* acknowledge_1 */
16889         case FC_RCTL_ACK_0:     /* acknowledge_0 */
16890         case FC_RCTL_P_RJT:     /* port reject */
16891         case FC_RCTL_F_RJT:     /* fabric reject */
16892         case FC_RCTL_P_BSY:     /* port busy */
16893         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
16894         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
16895         case FC_RCTL_LCR:       /* link credit reset */
16896         case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
16897         case FC_RCTL_END:       /* end */
16898                 break;
16899         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
16900                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16901                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16902                 return lpfc_fc_frame_check(phba, fc_hdr);
16903         default:
16904                 goto drop;
16905         }
16906
16907         switch (fc_hdr->fh_type) {
16908         case FC_TYPE_BLS:
16909         case FC_TYPE_ELS:
16910         case FC_TYPE_FCP:
16911         case FC_TYPE_CT:
16912         case FC_TYPE_NVME:
16913                 break;
16914         case FC_TYPE_IP:
16915         case FC_TYPE_ILS:
16916         default:
16917                 goto drop;
16918         }
16919
16920         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16921                         "2538 Received frame rctl:x%x, type:x%x, "
16922                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16923                         fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16924                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16925                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16926                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16927                         be32_to_cpu(header[6]));
16928         return 0;
16929 drop:
16930         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16931                         "2539 Dropped frame rctl:x%x type:x%x\n",
16932                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16933         return 1;
16934 }
16935
16936 /**
16937  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16938  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16939  *
16940  * This function processes the FC header to retrieve the VFI from the VF
16941  * header, if one exists. This function will return the VFI if one exists
16942  * or 0 if no VSAN Header exists.
16943  **/
16944 static uint32_t
16945 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16946 {
16947         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16948
16949         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16950                 return 0;
16951         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16952 }
16953
16954 /**
16955  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16956  * @phba: Pointer to the HBA structure to search for the vport on
16957  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16958  * @fcfi: The FC Fabric ID that the frame came from
16959  *
16960  * This function searches the @phba for a vport that matches the content of the
16961  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16962  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16963  * returns the matching vport pointer or NULL if unable to match frame to a
16964  * vport.
16965  **/
16966 static struct lpfc_vport *
16967 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16968                        uint16_t fcfi, uint32_t did)
16969 {
16970         struct lpfc_vport **vports;
16971         struct lpfc_vport *vport = NULL;
16972         int i;
16973
16974         if (did == Fabric_DID)
16975                 return phba->pport;
16976         if ((phba->pport->fc_flag & FC_PT2PT) &&
16977                 !(phba->link_state == LPFC_HBA_READY))
16978                 return phba->pport;
16979
16980         vports = lpfc_create_vport_work_array(phba);
16981         if (vports != NULL) {
16982                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16983                         if (phba->fcf.fcfi == fcfi &&
16984                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16985                             vports[i]->fc_myDID == did) {
16986                                 vport = vports[i];
16987                                 break;
16988                         }
16989                 }
16990         }
16991         lpfc_destroy_vport_work_array(phba, vports);
16992         return vport;
16993 }
16994
16995 /**
16996  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16997  * @vport: The vport to work on.
16998  *
16999  * This function updates the receive sequence time stamp for this vport. The
17000  * receive sequence time stamp indicates the time that the last frame of the
17001  * the sequence that has been idle for the longest amount of time was received.
17002  * the driver uses this time stamp to indicate if any received sequences have
17003  * timed out.
17004  **/
17005 static void
17006 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17007 {
17008         struct lpfc_dmabuf *h_buf;
17009         struct hbq_dmabuf *dmabuf = NULL;
17010
17011         /* get the oldest sequence on the rcv list */
17012         h_buf = list_get_first(&vport->rcv_buffer_list,
17013                                struct lpfc_dmabuf, list);
17014         if (!h_buf)
17015                 return;
17016         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17017         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17018 }
17019
17020 /**
17021  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17022  * @vport: The vport that the received sequences were sent to.
17023  *
17024  * This function cleans up all outstanding received sequences. This is called
17025  * by the driver when a link event or user action invalidates all the received
17026  * sequences.
17027  **/
17028 void
17029 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17030 {
17031         struct lpfc_dmabuf *h_buf, *hnext;
17032         struct lpfc_dmabuf *d_buf, *dnext;
17033         struct hbq_dmabuf *dmabuf = NULL;
17034
17035         /* start with the oldest sequence on the rcv list */
17036         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17037                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17038                 list_del_init(&dmabuf->hbuf.list);
17039                 list_for_each_entry_safe(d_buf, dnext,
17040                                          &dmabuf->dbuf.list, list) {
17041                         list_del_init(&d_buf->list);
17042                         lpfc_in_buf_free(vport->phba, d_buf);
17043                 }
17044                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17045         }
17046 }
17047
17048 /**
17049  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17050  * @vport: The vport that the received sequences were sent to.
17051  *
17052  * This function determines whether any received sequences have timed out by
17053  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17054  * indicates that there is at least one timed out sequence this routine will
17055  * go through the received sequences one at a time from most inactive to most
17056  * active to determine which ones need to be cleaned up. Once it has determined
17057  * that a sequence needs to be cleaned up it will simply free up the resources
17058  * without sending an abort.
17059  **/
17060 void
17061 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17062 {
17063         struct lpfc_dmabuf *h_buf, *hnext;
17064         struct lpfc_dmabuf *d_buf, *dnext;
17065         struct hbq_dmabuf *dmabuf = NULL;
17066         unsigned long timeout;
17067         int abort_count = 0;
17068
17069         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17070                    vport->rcv_buffer_time_stamp);
17071         if (list_empty(&vport->rcv_buffer_list) ||
17072             time_before(jiffies, timeout))
17073                 return;
17074         /* start with the oldest sequence on the rcv list */
17075         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17076                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17077                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17078                            dmabuf->time_stamp);
17079                 if (time_before(jiffies, timeout))
17080                         break;
17081                 abort_count++;
17082                 list_del_init(&dmabuf->hbuf.list);
17083                 list_for_each_entry_safe(d_buf, dnext,
17084                                          &dmabuf->dbuf.list, list) {
17085                         list_del_init(&d_buf->list);
17086                         lpfc_in_buf_free(vport->phba, d_buf);
17087                 }
17088                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17089         }
17090         if (abort_count)
17091                 lpfc_update_rcv_time_stamp(vport);
17092 }
17093
17094 /**
17095  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17096  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17097  *
17098  * This function searches through the existing incomplete sequences that have
17099  * been sent to this @vport. If the frame matches one of the incomplete
17100  * sequences then the dbuf in the @dmabuf is added to the list of frames that
17101  * make up that sequence. If no sequence is found that matches this frame then
17102  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17103  * This function returns a pointer to the first dmabuf in the sequence list that
17104  * the frame was linked to.
17105  **/
17106 static struct hbq_dmabuf *
17107 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17108 {
17109         struct fc_frame_header *new_hdr;
17110         struct fc_frame_header *temp_hdr;
17111         struct lpfc_dmabuf *d_buf;
17112         struct lpfc_dmabuf *h_buf;
17113         struct hbq_dmabuf *seq_dmabuf = NULL;
17114         struct hbq_dmabuf *temp_dmabuf = NULL;
17115         uint8_t found = 0;
17116
17117         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17118         dmabuf->time_stamp = jiffies;
17119         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17120
17121         /* Use the hdr_buf to find the sequence that this frame belongs to */
17122         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17123                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17124                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17125                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17126                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17127                         continue;
17128                 /* found a pending sequence that matches this frame */
17129                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17130                 break;
17131         }
17132         if (!seq_dmabuf) {
17133                 /*
17134                  * This indicates first frame received for this sequence.
17135                  * Queue the buffer on the vport's rcv_buffer_list.
17136                  */
17137                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17138                 lpfc_update_rcv_time_stamp(vport);
17139                 return dmabuf;
17140         }
17141         temp_hdr = seq_dmabuf->hbuf.virt;
17142         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17143                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17144                 list_del_init(&seq_dmabuf->hbuf.list);
17145                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17146                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17147                 lpfc_update_rcv_time_stamp(vport);
17148                 return dmabuf;
17149         }
17150         /* move this sequence to the tail to indicate a young sequence */
17151         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17152         seq_dmabuf->time_stamp = jiffies;
17153         lpfc_update_rcv_time_stamp(vport);
17154         if (list_empty(&seq_dmabuf->dbuf.list)) {
17155                 temp_hdr = dmabuf->hbuf.virt;
17156                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17157                 return seq_dmabuf;
17158         }
17159         /* find the correct place in the sequence to insert this frame */
17160         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17161         while (!found) {
17162                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17163                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17164                 /*
17165                  * If the frame's sequence count is greater than the frame on
17166                  * the list then insert the frame right after this frame
17167                  */
17168                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17169                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
17170                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
17171                         found = 1;
17172                         break;
17173                 }
17174
17175                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17176                         break;
17177                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
17178         }
17179
17180         if (found)
17181                 return seq_dmabuf;
17182         return NULL;
17183 }
17184
17185 /**
17186  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17187  * @vport: pointer to a vitural port
17188  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17189  *
17190  * This function tries to abort from the partially assembed sequence, described
17191  * by the information from basic abbort @dmabuf. It checks to see whether such
17192  * partially assembled sequence held by the driver. If so, it shall free up all
17193  * the frames from the partially assembled sequence.
17194  *
17195  * Return
17196  * true  -- if there is matching partially assembled sequence present and all
17197  *          the frames freed with the sequence;
17198  * false -- if there is no matching partially assembled sequence present so
17199  *          nothing got aborted in the lower layer driver
17200  **/
17201 static bool
17202 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17203                             struct hbq_dmabuf *dmabuf)
17204 {
17205         struct fc_frame_header *new_hdr;
17206         struct fc_frame_header *temp_hdr;
17207         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17208         struct hbq_dmabuf *seq_dmabuf = NULL;
17209
17210         /* Use the hdr_buf to find the sequence that matches this frame */
17211         INIT_LIST_HEAD(&dmabuf->dbuf.list);
17212         INIT_LIST_HEAD(&dmabuf->hbuf.list);
17213         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17214         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17215                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17216                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17217                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17218                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17219                         continue;
17220                 /* found a pending sequence that matches this frame */
17221                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17222                 break;
17223         }
17224
17225         /* Free up all the frames from the partially assembled sequence */
17226         if (seq_dmabuf) {
17227                 list_for_each_entry_safe(d_buf, n_buf,
17228                                          &seq_dmabuf->dbuf.list, list) {
17229                         list_del_init(&d_buf->list);
17230                         lpfc_in_buf_free(vport->phba, d_buf);
17231                 }
17232                 return true;
17233         }
17234         return false;
17235 }
17236
17237 /**
17238  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17239  * @vport: pointer to a vitural port
17240  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17241  *
17242  * This function tries to abort from the assembed sequence from upper level
17243  * protocol, described by the information from basic abbort @dmabuf. It
17244  * checks to see whether such pending context exists at upper level protocol.
17245  * If so, it shall clean up the pending context.
17246  *
17247  * Return
17248  * true  -- if there is matching pending context of the sequence cleaned
17249  *          at ulp;
17250  * false -- if there is no matching pending context of the sequence present
17251  *          at ulp.
17252  **/
17253 static bool
17254 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17255 {
17256         struct lpfc_hba *phba = vport->phba;
17257         int handled;
17258
17259         /* Accepting abort at ulp with SLI4 only */
17260         if (phba->sli_rev < LPFC_SLI_REV4)
17261                 return false;
17262
17263         /* Register all caring upper level protocols to attend abort */
17264         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17265         if (handled)
17266                 return true;
17267
17268         return false;
17269 }
17270
17271 /**
17272  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
17273  * @phba: Pointer to HBA context object.
17274  * @cmd_iocbq: pointer to the command iocbq structure.
17275  * @rsp_iocbq: pointer to the response iocbq structure.
17276  *
17277  * This function handles the sequence abort response iocb command complete
17278  * event. It properly releases the memory allocated to the sequence abort
17279  * accept iocb.
17280  **/
17281 static void
17282 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
17283                              struct lpfc_iocbq *cmd_iocbq,
17284                              struct lpfc_iocbq *rsp_iocbq)
17285 {
17286         struct lpfc_nodelist *ndlp;
17287
17288         if (cmd_iocbq) {
17289                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17290                 lpfc_nlp_put(ndlp);
17291                 lpfc_nlp_not_used(ndlp);
17292                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
17293         }
17294
17295         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17296         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17297                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17298                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
17299                         rsp_iocbq->iocb.ulpStatus,
17300                         rsp_iocbq->iocb.un.ulpWord[4]);
17301 }
17302
17303 /**
17304  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17305  * @phba: Pointer to HBA context object.
17306  * @xri: xri id in transaction.
17307  *
17308  * This function validates the xri maps to the known range of XRIs allocated an
17309  * used by the driver.
17310  **/
17311 uint16_t
17312 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17313                       uint16_t xri)
17314 {
17315         uint16_t i;
17316
17317         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17318                 if (xri == phba->sli4_hba.xri_ids[i])
17319                         return i;
17320         }
17321         return NO_XRI;
17322 }
17323
17324 /**
17325  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
17326  * @phba: Pointer to HBA context object.
17327  * @fc_hdr: pointer to a FC frame header.
17328  *
17329  * This function sends a basic response to a previous unsol sequence abort
17330  * event after aborting the sequence handling.
17331  **/
17332 void
17333 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17334                         struct fc_frame_header *fc_hdr, bool aborted)
17335 {
17336         struct lpfc_hba *phba = vport->phba;
17337         struct lpfc_iocbq *ctiocb = NULL;
17338         struct lpfc_nodelist *ndlp;
17339         uint16_t oxid, rxid, xri, lxri;
17340         uint32_t sid, fctl;
17341         IOCB_t *icmd;
17342         int rc;
17343
17344         if (!lpfc_is_link_up(phba))
17345                 return;
17346
17347         sid = sli4_sid_from_fc_hdr(fc_hdr);
17348         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
17349         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
17350
17351         ndlp = lpfc_findnode_did(vport, sid);
17352         if (!ndlp) {
17353                 ndlp = lpfc_nlp_init(vport, sid);
17354                 if (!ndlp) {
17355                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17356                                          "1268 Failed to allocate ndlp for "
17357                                          "oxid:x%x SID:x%x\n", oxid, sid);
17358                         return;
17359                 }
17360                 /* Put ndlp onto pport node list */
17361                 lpfc_enqueue_node(vport, ndlp);
17362         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17363                 /* re-setup ndlp without removing from node list */
17364                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17365                 if (!ndlp) {
17366                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17367                                          "3275 Failed to active ndlp found "
17368                                          "for oxid:x%x SID:x%x\n", oxid, sid);
17369                         return;
17370                 }
17371         }
17372
17373         /* Allocate buffer for rsp iocb */
17374         ctiocb = lpfc_sli_get_iocbq(phba);
17375         if (!ctiocb)
17376                 return;
17377
17378         /* Extract the F_CTL field from FC_HDR */
17379         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17380
17381         icmd = &ctiocb->iocb;
17382         icmd->un.xseq64.bdl.bdeSize = 0;
17383         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
17384         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17385         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17386         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17387
17388         /* Fill in the rest of iocb fields */
17389         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17390         icmd->ulpBdeCount = 0;
17391         icmd->ulpLe = 1;
17392         icmd->ulpClass = CLASS3;
17393         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
17394         ctiocb->context1 = lpfc_nlp_get(ndlp);
17395
17396         ctiocb->iocb_cmpl = NULL;
17397         ctiocb->vport = phba->pport;
17398         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
17399         ctiocb->sli4_lxritag = NO_XRI;
17400         ctiocb->sli4_xritag = NO_XRI;
17401
17402         if (fctl & FC_FC_EX_CTX)
17403                 /* Exchange responder sent the abort so we
17404                  * own the oxid.
17405                  */
17406                 xri = oxid;
17407         else
17408                 xri = rxid;
17409         lxri = lpfc_sli4_xri_inrange(phba, xri);
17410         if (lxri != NO_XRI)
17411                 lpfc_set_rrq_active(phba, ndlp, lxri,
17412                         (xri == oxid) ? rxid : oxid, 0);
17413         /* For BA_ABTS from exchange responder, if the logical xri with
17414          * the oxid maps to the FCP XRI range, the port no longer has
17415          * that exchange context, send a BLS_RJT. Override the IOCB for
17416          * a BA_RJT.
17417          */
17418         if ((fctl & FC_FC_EX_CTX) &&
17419             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
17420                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17421                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17422                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17423                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17424         }
17425
17426         /* If BA_ABTS failed to abort a partially assembled receive sequence,
17427          * the driver no longer has that exchange, send a BLS_RJT. Override
17428          * the IOCB for a BA_RJT.
17429          */
17430         if (aborted == false) {
17431                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17432                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17433                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17434                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17435         }
17436
17437         if (fctl & FC_FC_EX_CTX) {
17438                 /* ABTS sent by responder to CT exchange, construction
17439                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17440                  * field and RX_ID from ABTS for RX_ID field.
17441                  */
17442                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
17443         } else {
17444                 /* ABTS sent by initiator to CT exchange, construction
17445                  * of BA_ACC will need to allocate a new XRI as for the
17446                  * XRI_TAG field.
17447                  */
17448                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
17449         }
17450         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
17451         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
17452
17453         /* Xmit CT abts response on exchange <xid> */
17454         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17455                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17456                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
17457
17458         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17459         if (rc == IOCB_ERROR) {
17460                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17461                                  "2925 Failed to issue CT ABTS RSP x%x on "
17462                                  "xri x%x, Data x%x\n",
17463                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17464                                  phba->link_state);
17465                 lpfc_nlp_put(ndlp);
17466                 ctiocb->context1 = NULL;
17467                 lpfc_sli_release_iocbq(phba, ctiocb);
17468         }
17469 }
17470
17471 /**
17472  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17473  * @vport: Pointer to the vport on which this sequence was received
17474  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17475  *
17476  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17477  * receive sequence is only partially assembed by the driver, it shall abort
17478  * the partially assembled frames for the sequence. Otherwise, if the
17479  * unsolicited receive sequence has been completely assembled and passed to
17480  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17481  * unsolicited sequence has been aborted. After that, it will issue a basic
17482  * accept to accept the abort.
17483  **/
17484 static void
17485 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17486                              struct hbq_dmabuf *dmabuf)
17487 {
17488         struct lpfc_hba *phba = vport->phba;
17489         struct fc_frame_header fc_hdr;
17490         uint32_t fctl;
17491         bool aborted;
17492
17493         /* Make a copy of fc_hdr before the dmabuf being released */
17494         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
17495         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
17496
17497         if (fctl & FC_FC_EX_CTX) {
17498                 /* ABTS by responder to exchange, no cleanup needed */
17499                 aborted = true;
17500         } else {
17501                 /* ABTS by initiator to exchange, need to do cleanup */
17502                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17503                 if (aborted == false)
17504                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
17505         }
17506         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17507
17508         if (phba->nvmet_support) {
17509                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17510                 return;
17511         }
17512
17513         /* Respond with BA_ACC or BA_RJT accordingly */
17514         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
17515 }
17516
17517 /**
17518  * lpfc_seq_complete - Indicates if a sequence is complete
17519  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17520  *
17521  * This function checks the sequence, starting with the frame described by
17522  * @dmabuf, to see if all the frames associated with this sequence are present.
17523  * the frames associated with this sequence are linked to the @dmabuf using the
17524  * dbuf list. This function looks for two major things. 1) That the first frame
17525  * has a sequence count of zero. 2) There is a frame with last frame of sequence
17526  * set. 3) That there are no holes in the sequence count. The function will
17527  * return 1 when the sequence is complete, otherwise it will return 0.
17528  **/
17529 static int
17530 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17531 {
17532         struct fc_frame_header *hdr;
17533         struct lpfc_dmabuf *d_buf;
17534         struct hbq_dmabuf *seq_dmabuf;
17535         uint32_t fctl;
17536         int seq_count = 0;
17537
17538         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17539         /* make sure first fame of sequence has a sequence count of zero */
17540         if (hdr->fh_seq_cnt != seq_count)
17541                 return 0;
17542         fctl = (hdr->fh_f_ctl[0] << 16 |
17543                 hdr->fh_f_ctl[1] << 8 |
17544                 hdr->fh_f_ctl[2]);
17545         /* If last frame of sequence we can return success. */
17546         if (fctl & FC_FC_END_SEQ)
17547                 return 1;
17548         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17549                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17550                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17551                 /* If there is a hole in the sequence count then fail. */
17552                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
17553                         return 0;
17554                 fctl = (hdr->fh_f_ctl[0] << 16 |
17555                         hdr->fh_f_ctl[1] << 8 |
17556                         hdr->fh_f_ctl[2]);
17557                 /* If last frame of sequence we can return success. */
17558                 if (fctl & FC_FC_END_SEQ)
17559                         return 1;
17560         }
17561         return 0;
17562 }
17563
17564 /**
17565  * lpfc_prep_seq - Prep sequence for ULP processing
17566  * @vport: Pointer to the vport on which this sequence was received
17567  * @dmabuf: pointer to a dmabuf that describes the FC sequence
17568  *
17569  * This function takes a sequence, described by a list of frames, and creates
17570  * a list of iocbq structures to describe the sequence. This iocbq list will be
17571  * used to issue to the generic unsolicited sequence handler. This routine
17572  * returns a pointer to the first iocbq in the list. If the function is unable
17573  * to allocate an iocbq then it throw out the received frames that were not
17574  * able to be described and return a pointer to the first iocbq. If unable to
17575  * allocate any iocbqs (including the first) this function will return NULL.
17576  **/
17577 static struct lpfc_iocbq *
17578 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17579 {
17580         struct hbq_dmabuf *hbq_buf;
17581         struct lpfc_dmabuf *d_buf, *n_buf;
17582         struct lpfc_iocbq *first_iocbq, *iocbq;
17583         struct fc_frame_header *fc_hdr;
17584         uint32_t sid;
17585         uint32_t len, tot_len;
17586         struct ulp_bde64 *pbde;
17587
17588         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17589         /* remove from receive buffer list */
17590         list_del_init(&seq_dmabuf->hbuf.list);
17591         lpfc_update_rcv_time_stamp(vport);
17592         /* get the Remote Port's SID */
17593         sid = sli4_sid_from_fc_hdr(fc_hdr);
17594         tot_len = 0;
17595         /* Get an iocbq struct to fill in. */
17596         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17597         if (first_iocbq) {
17598                 /* Initialize the first IOCB. */
17599                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
17600                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
17601                 first_iocbq->vport = vport;
17602
17603                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17604                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17605                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17606                         first_iocbq->iocb.un.rcvels.parmRo =
17607                                 sli4_did_from_fc_hdr(fc_hdr);
17608                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17609                 } else
17610                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
17611                 first_iocbq->iocb.ulpContext = NO_XRI;
17612                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17613                         be16_to_cpu(fc_hdr->fh_ox_id);
17614                 /* iocbq is prepped for internal consumption.  Physical vpi. */
17615                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17616                         vport->phba->vpi_ids[vport->vpi];
17617                 /* put the first buffer into the first IOCBq */
17618                 tot_len = bf_get(lpfc_rcqe_length,
17619                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17620
17621                 first_iocbq->context2 = &seq_dmabuf->dbuf;
17622                 first_iocbq->context3 = NULL;
17623                 first_iocbq->iocb.ulpBdeCount = 1;
17624                 if (tot_len > LPFC_DATA_BUF_SIZE)
17625                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17626                                                         LPFC_DATA_BUF_SIZE;
17627                 else
17628                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17629
17630                 first_iocbq->iocb.un.rcvels.remoteID = sid;
17631
17632                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17633         }
17634         iocbq = first_iocbq;
17635         /*
17636          * Each IOCBq can have two Buffers assigned, so go through the list
17637          * of buffers for this sequence and save two buffers in each IOCBq
17638          */
17639         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17640                 if (!iocbq) {
17641                         lpfc_in_buf_free(vport->phba, d_buf);
17642                         continue;
17643                 }
17644                 if (!iocbq->context3) {
17645                         iocbq->context3 = d_buf;
17646                         iocbq->iocb.ulpBdeCount++;
17647                         /* We need to get the size out of the right CQE */
17648                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17649                         len = bf_get(lpfc_rcqe_length,
17650                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
17651                         pbde = (struct ulp_bde64 *)
17652                                         &iocbq->iocb.unsli3.sli3Words[4];
17653                         if (len > LPFC_DATA_BUF_SIZE)
17654                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17655                         else
17656                                 pbde->tus.f.bdeSize = len;
17657
17658                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17659                         tot_len += len;
17660                 } else {
17661                         iocbq = lpfc_sli_get_iocbq(vport->phba);
17662                         if (!iocbq) {
17663                                 if (first_iocbq) {
17664                                         first_iocbq->iocb.ulpStatus =
17665                                                         IOSTAT_FCP_RSP_ERROR;
17666                                         first_iocbq->iocb.un.ulpWord[4] =
17667                                                         IOERR_NO_RESOURCES;
17668                                 }
17669                                 lpfc_in_buf_free(vport->phba, d_buf);
17670                                 continue;
17671                         }
17672                         /* We need to get the size out of the right CQE */
17673                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17674                         len = bf_get(lpfc_rcqe_length,
17675                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
17676                         iocbq->context2 = d_buf;
17677                         iocbq->context3 = NULL;
17678                         iocbq->iocb.ulpBdeCount = 1;
17679                         if (len > LPFC_DATA_BUF_SIZE)
17680                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17681                                                         LPFC_DATA_BUF_SIZE;
17682                         else
17683                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17684
17685                         tot_len += len;
17686                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17687
17688                         iocbq->iocb.un.rcvels.remoteID = sid;
17689                         list_add_tail(&iocbq->list, &first_iocbq->list);
17690                 }
17691         }
17692         return first_iocbq;
17693 }
17694
17695 static void
17696 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17697                           struct hbq_dmabuf *seq_dmabuf)
17698 {
17699         struct fc_frame_header *fc_hdr;
17700         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17701         struct lpfc_hba *phba = vport->phba;
17702
17703         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17704         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17705         if (!iocbq) {
17706                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17707                                 "2707 Ring %d handler: Failed to allocate "
17708                                 "iocb Rctl x%x Type x%x received\n",
17709                                 LPFC_ELS_RING,
17710                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17711                 return;
17712         }
17713         if (!lpfc_complete_unsol_iocb(phba,
17714                                       phba->sli4_hba.els_wq->pring,
17715                                       iocbq, fc_hdr->fh_r_ctl,
17716                                       fc_hdr->fh_type))
17717                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17718                                 "2540 Ring %d handler: unexpected Rctl "
17719                                 "x%x Type x%x received\n",
17720                                 LPFC_ELS_RING,
17721                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17722
17723         /* Free iocb created in lpfc_prep_seq */
17724         list_for_each_entry_safe(curr_iocb, next_iocb,
17725                 &iocbq->list, list) {
17726                 list_del_init(&curr_iocb->list);
17727                 lpfc_sli_release_iocbq(phba, curr_iocb);
17728         }
17729         lpfc_sli_release_iocbq(phba, iocbq);
17730 }
17731
17732 static void
17733 lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17734                             struct lpfc_iocbq *rspiocb)
17735 {
17736         struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17737
17738         if (pcmd && pcmd->virt)
17739                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17740         kfree(pcmd);
17741         lpfc_sli_release_iocbq(phba, cmdiocb);
17742         lpfc_drain_txq(phba);
17743 }
17744
17745 static void
17746 lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17747                               struct hbq_dmabuf *dmabuf)
17748 {
17749         struct fc_frame_header *fc_hdr;
17750         struct lpfc_hba *phba = vport->phba;
17751         struct lpfc_iocbq *iocbq = NULL;
17752         union  lpfc_wqe *wqe;
17753         struct lpfc_dmabuf *pcmd = NULL;
17754         uint32_t frame_len;
17755         int rc;
17756         unsigned long iflags;
17757
17758         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17759         frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17760
17761         /* Send the received frame back */
17762         iocbq = lpfc_sli_get_iocbq(phba);
17763         if (!iocbq) {
17764                 /* Queue cq event and wakeup worker thread to process it */
17765                 spin_lock_irqsave(&phba->hbalock, iflags);
17766                 list_add_tail(&dmabuf->cq_event.list,
17767                               &phba->sli4_hba.sp_queue_event);
17768                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
17769                 spin_unlock_irqrestore(&phba->hbalock, iflags);
17770                 lpfc_worker_wake_up(phba);
17771                 return;
17772         }
17773
17774         /* Allocate buffer for command payload */
17775         pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17776         if (pcmd)
17777                 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
17778                                             &pcmd->phys);
17779         if (!pcmd || !pcmd->virt)
17780                 goto exit;
17781
17782         INIT_LIST_HEAD(&pcmd->list);
17783
17784         /* copyin the payload */
17785         memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17786
17787         /* fill in BDE's for command */
17788         iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17789         iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17790         iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17791         iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17792
17793         iocbq->context2 = pcmd;
17794         iocbq->vport = vport;
17795         iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17796         iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17797
17798         /*
17799          * Setup rest of the iocb as though it were a WQE
17800          * Build the SEND_FRAME WQE
17801          */
17802         wqe = (union lpfc_wqe *)&iocbq->iocb;
17803
17804         wqe->send_frame.frame_len = frame_len;
17805         wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17806         wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17807         wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17808         wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17809         wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17810         wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17811
17812         iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17813         iocbq->iocb.ulpLe = 1;
17814         iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17815         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17816         if (rc == IOCB_ERROR)
17817                 goto exit;
17818
17819         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17820         return;
17821
17822 exit:
17823         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17824                         "2023 Unable to process MDS loopback frame\n");
17825         if (pcmd && pcmd->virt)
17826                 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17827         kfree(pcmd);
17828         if (iocbq)
17829                 lpfc_sli_release_iocbq(phba, iocbq);
17830         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17831 }
17832
17833 /**
17834  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17835  * @phba: Pointer to HBA context object.
17836  *
17837  * This function is called with no lock held. This function processes all
17838  * the received buffers and gives it to upper layers when a received buffer
17839  * indicates that it is the final frame in the sequence. The interrupt
17840  * service routine processes received buffers at interrupt contexts.
17841  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17842  * appropriate receive function when the final frame in a sequence is received.
17843  **/
17844 void
17845 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17846                                  struct hbq_dmabuf *dmabuf)
17847 {
17848         struct hbq_dmabuf *seq_dmabuf;
17849         struct fc_frame_header *fc_hdr;
17850         struct lpfc_vport *vport;
17851         uint32_t fcfi;
17852         uint32_t did;
17853
17854         /* Process each received buffer */
17855         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17856
17857         if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
17858             fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
17859                 vport = phba->pport;
17860                 /* Handle MDS Loopback frames */
17861                 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17862                 return;
17863         }
17864
17865         /* check to see if this a valid type of frame */
17866         if (lpfc_fc_frame_check(phba, fc_hdr)) {
17867                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17868                 return;
17869         }
17870
17871         if ((bf_get(lpfc_cqe_code,
17872                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17873                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17874                               &dmabuf->cq_event.cqe.rcqe_cmpl);
17875         else
17876                 fcfi = bf_get(lpfc_rcqe_fcf_id,
17877                               &dmabuf->cq_event.cqe.rcqe_cmpl);
17878
17879         /* d_id this frame is directed to */
17880         did = sli4_did_from_fc_hdr(fc_hdr);
17881
17882         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
17883         if (!vport) {
17884                 /* throw out the frame */
17885                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17886                 return;
17887         }
17888
17889         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17890         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17891                 (did != Fabric_DID)) {
17892                 /*
17893                  * Throw out the frame if we are not pt2pt.
17894                  * The pt2pt protocol allows for discovery frames
17895                  * to be received without a registered VPI.
17896                  */
17897                 if (!(vport->fc_flag & FC_PT2PT) ||
17898                         (phba->link_state == LPFC_HBA_READY)) {
17899                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
17900                         return;
17901                 }
17902         }
17903
17904         /* Handle the basic abort sequence (BA_ABTS) event */
17905         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17906                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17907                 return;
17908         }
17909
17910         /* Link this frame */
17911         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17912         if (!seq_dmabuf) {
17913                 /* unable to add frame to vport - throw it out */
17914                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17915                 return;
17916         }
17917         /* If not last frame in sequence continue processing frames. */
17918         if (!lpfc_seq_complete(seq_dmabuf))
17919                 return;
17920
17921         /* Send the complete sequence to the upper layer protocol */
17922         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
17923 }
17924
17925 /**
17926  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17927  * @phba: pointer to lpfc hba data structure.
17928  *
17929  * This routine is invoked to post rpi header templates to the
17930  * HBA consistent with the SLI-4 interface spec.  This routine
17931  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17932  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17933  *
17934  * This routine does not require any locks.  It's usage is expected
17935  * to be driver load or reset recovery when the driver is
17936  * sequential.
17937  *
17938  * Return codes
17939  *      0 - successful
17940  *      -EIO - The mailbox failed to complete successfully.
17941  *      When this error occurs, the driver is not guaranteed
17942  *      to have any rpi regions posted to the device and
17943  *      must either attempt to repost the regions or take a
17944  *      fatal error.
17945  **/
17946 int
17947 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17948 {
17949         struct lpfc_rpi_hdr *rpi_page;
17950         uint32_t rc = 0;
17951         uint16_t lrpi = 0;
17952
17953         /* SLI4 ports that support extents do not require RPI headers. */
17954         if (!phba->sli4_hba.rpi_hdrs_in_use)
17955                 goto exit;
17956         if (phba->sli4_hba.extents_in_use)
17957                 return -EIO;
17958
17959         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
17960                 /*
17961                  * Assign the rpi headers a physical rpi only if the driver
17962                  * has not initialized those resources.  A port reset only
17963                  * needs the headers posted.
17964                  */
17965                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17966                     LPFC_RPI_RSRC_RDY)
17967                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17968
17969                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17970                 if (rc != MBX_SUCCESS) {
17971                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17972                                         "2008 Error %d posting all rpi "
17973                                         "headers\n", rc);
17974                         rc = -EIO;
17975                         break;
17976                 }
17977         }
17978
17979  exit:
17980         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17981                LPFC_RPI_RSRC_RDY);
17982         return rc;
17983 }
17984
17985 /**
17986  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17987  * @phba: pointer to lpfc hba data structure.
17988  * @rpi_page:  pointer to the rpi memory region.
17989  *
17990  * This routine is invoked to post a single rpi header to the
17991  * HBA consistent with the SLI-4 interface spec.  This memory region
17992  * maps up to 64 rpi context regions.
17993  *
17994  * Return codes
17995  *      0 - successful
17996  *      -ENOMEM - No available memory
17997  *      -EIO - The mailbox failed to complete successfully.
17998  **/
17999 int
18000 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18001 {
18002         LPFC_MBOXQ_t *mboxq;
18003         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18004         uint32_t rc = 0;
18005         uint32_t shdr_status, shdr_add_status;
18006         union lpfc_sli4_cfg_shdr *shdr;
18007
18008         /* SLI4 ports that support extents do not require RPI headers. */
18009         if (!phba->sli4_hba.rpi_hdrs_in_use)
18010                 return rc;
18011         if (phba->sli4_hba.extents_in_use)
18012                 return -EIO;
18013
18014         /* The port is notified of the header region via a mailbox command. */
18015         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18016         if (!mboxq) {
18017                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18018                                 "2001 Unable to allocate memory for issuing "
18019                                 "SLI_CONFIG_SPECIAL mailbox command\n");
18020                 return -ENOMEM;
18021         }
18022
18023         /* Post all rpi memory regions to the port. */
18024         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
18025         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18026                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18027                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
18028                          sizeof(struct lpfc_sli4_cfg_mhdr),
18029                          LPFC_SLI4_MBX_EMBED);
18030
18031
18032         /* Post the physical rpi to the port for this rpi header. */
18033         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18034                rpi_page->start_rpi);
18035         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18036                hdr_tmpl, rpi_page->page_count);
18037
18038         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18039         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
18040         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18041         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18042         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18043         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18044         if (rc != MBX_TIMEOUT)
18045                 mempool_free(mboxq, phba->mbox_mem_pool);
18046         if (shdr_status || shdr_add_status || rc) {
18047                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18048                                 "2514 POST_RPI_HDR mailbox failed with "
18049                                 "status x%x add_status x%x, mbx status x%x\n",
18050                                 shdr_status, shdr_add_status, rc);
18051                 rc = -ENXIO;
18052         } else {
18053                 /*
18054                  * The next_rpi stores the next logical module-64 rpi value used
18055                  * to post physical rpis in subsequent rpi postings.
18056                  */
18057                 spin_lock_irq(&phba->hbalock);
18058                 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18059                 spin_unlock_irq(&phba->hbalock);
18060         }
18061         return rc;
18062 }
18063
18064 /**
18065  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18066  * @phba: pointer to lpfc hba data structure.
18067  *
18068  * This routine is invoked to post rpi header templates to the
18069  * HBA consistent with the SLI-4 interface spec.  This routine
18070  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18071  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18072  *
18073  * Returns
18074  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18075  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
18076  **/
18077 int
18078 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18079 {
18080         unsigned long rpi;
18081         uint16_t max_rpi, rpi_limit;
18082         uint16_t rpi_remaining, lrpi = 0;
18083         struct lpfc_rpi_hdr *rpi_hdr;
18084         unsigned long iflag;
18085
18086         /*
18087          * Fetch the next logical rpi.  Because this index is logical,
18088          * the  driver starts at 0 each time.
18089          */
18090         spin_lock_irqsave(&phba->hbalock, iflag);
18091         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18092         rpi_limit = phba->sli4_hba.next_rpi;
18093
18094         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18095         if (rpi >= rpi_limit)
18096                 rpi = LPFC_RPI_ALLOC_ERROR;
18097         else {
18098                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18099                 phba->sli4_hba.max_cfg_param.rpi_used++;
18100                 phba->sli4_hba.rpi_count++;
18101         }
18102         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18103                         "0001 rpi:%x max:%x lim:%x\n",
18104                         (int) rpi, max_rpi, rpi_limit);
18105
18106         /*
18107          * Don't try to allocate more rpi header regions if the device limit
18108          * has been exhausted.
18109          */
18110         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18111             (phba->sli4_hba.rpi_count >= max_rpi)) {
18112                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18113                 return rpi;
18114         }
18115
18116         /*
18117          * RPI header postings are not required for SLI4 ports capable of
18118          * extents.
18119          */
18120         if (!phba->sli4_hba.rpi_hdrs_in_use) {
18121                 spin_unlock_irqrestore(&phba->hbalock, iflag);
18122                 return rpi;
18123         }
18124
18125         /*
18126          * If the driver is running low on rpi resources, allocate another
18127          * page now.  Note that the next_rpi value is used because
18128          * it represents how many are actually in use whereas max_rpi notes
18129          * how many are supported max by the device.
18130          */
18131         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
18132         spin_unlock_irqrestore(&phba->hbalock, iflag);
18133         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18134                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18135                 if (!rpi_hdr) {
18136                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18137                                         "2002 Error Could not grow rpi "
18138                                         "count\n");
18139                 } else {
18140                         lrpi = rpi_hdr->start_rpi;
18141                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18142                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18143                 }
18144         }
18145
18146         return rpi;
18147 }
18148
18149 /**
18150  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18151  * @phba: pointer to lpfc hba data structure.
18152  *
18153  * This routine is invoked to release an rpi to the pool of
18154  * available rpis maintained by the driver.
18155  **/
18156 static void
18157 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18158 {
18159         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18160                 phba->sli4_hba.rpi_count--;
18161                 phba->sli4_hba.max_cfg_param.rpi_used--;
18162         }
18163 }
18164
18165 /**
18166  * lpfc_sli4_free_rpi - Release an rpi for reuse.
18167  * @phba: pointer to lpfc hba data structure.
18168  *
18169  * This routine is invoked to release an rpi to the pool of
18170  * available rpis maintained by the driver.
18171  **/
18172 void
18173 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18174 {
18175         spin_lock_irq(&phba->hbalock);
18176         __lpfc_sli4_free_rpi(phba, rpi);
18177         spin_unlock_irq(&phba->hbalock);
18178 }
18179
18180 /**
18181  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18182  * @phba: pointer to lpfc hba data structure.
18183  *
18184  * This routine is invoked to remove the memory region that
18185  * provided rpi via a bitmask.
18186  **/
18187 void
18188 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18189 {
18190         kfree(phba->sli4_hba.rpi_bmask);
18191         kfree(phba->sli4_hba.rpi_ids);
18192         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
18193 }
18194
18195 /**
18196  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18197  * @phba: pointer to lpfc hba data structure.
18198  *
18199  * This routine is invoked to remove the memory region that
18200  * provided rpi via a bitmask.
18201  **/
18202 int
18203 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18204         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
18205 {
18206         LPFC_MBOXQ_t *mboxq;
18207         struct lpfc_hba *phba = ndlp->phba;
18208         int rc;
18209
18210         /* The port is notified of the header region via a mailbox command. */
18211         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18212         if (!mboxq)
18213                 return -ENOMEM;
18214
18215         /* Post all rpi memory regions to the port. */
18216         lpfc_resume_rpi(mboxq, ndlp);
18217         if (cmpl) {
18218                 mboxq->mbox_cmpl = cmpl;
18219                 mboxq->ctx_buf = arg;
18220                 mboxq->ctx_ndlp = ndlp;
18221         } else
18222                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18223         mboxq->vport = ndlp->vport;
18224         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18225         if (rc == MBX_NOT_FINISHED) {
18226                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18227                                 "2010 Resume RPI Mailbox failed "
18228                                 "status %d, mbxStatus x%x\n", rc,
18229                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18230                 mempool_free(mboxq, phba->mbox_mem_pool);
18231                 return -EIO;
18232         }
18233         return 0;
18234 }
18235
18236 /**
18237  * lpfc_sli4_init_vpi - Initialize a vpi with the port
18238  * @vport: Pointer to the vport for which the vpi is being initialized
18239  *
18240  * This routine is invoked to activate a vpi with the port.
18241  *
18242  * Returns:
18243  *    0 success
18244  *    -Evalue otherwise
18245  **/
18246 int
18247 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
18248 {
18249         LPFC_MBOXQ_t *mboxq;
18250         int rc = 0;
18251         int retval = MBX_SUCCESS;
18252         uint32_t mbox_tmo;
18253         struct lpfc_hba *phba = vport->phba;
18254         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18255         if (!mboxq)
18256                 return -ENOMEM;
18257         lpfc_init_vpi(phba, mboxq, vport->vpi);
18258         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
18259         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
18260         if (rc != MBX_SUCCESS) {
18261                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
18262                                 "2022 INIT VPI Mailbox failed "
18263                                 "status %d, mbxStatus x%x\n", rc,
18264                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18265                 retval = -EIO;
18266         }
18267         if (rc != MBX_TIMEOUT)
18268                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
18269
18270         return retval;
18271 }
18272
18273 /**
18274  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18275  * @phba: pointer to lpfc hba data structure.
18276  * @mboxq: Pointer to mailbox object.
18277  *
18278  * This routine is invoked to manually add a single FCF record. The caller
18279  * must pass a completely initialized FCF_Record.  This routine takes
18280  * care of the nonembedded mailbox operations.
18281  **/
18282 static void
18283 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18284 {
18285         void *virt_addr;
18286         union lpfc_sli4_cfg_shdr *shdr;
18287         uint32_t shdr_status, shdr_add_status;
18288
18289         virt_addr = mboxq->sge_array->addr[0];
18290         /* The IOCTL status is embedded in the mailbox subheader. */
18291         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18292         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18293         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18294
18295         if ((shdr_status || shdr_add_status) &&
18296                 (shdr_status != STATUS_FCF_IN_USE))
18297                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18298                         "2558 ADD_FCF_RECORD mailbox failed with "
18299                         "status x%x add_status x%x\n",
18300                         shdr_status, shdr_add_status);
18301
18302         lpfc_sli4_mbox_cmd_free(phba, mboxq);
18303 }
18304
18305 /**
18306  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18307  * @phba: pointer to lpfc hba data structure.
18308  * @fcf_record:  pointer to the initialized fcf record to add.
18309  *
18310  * This routine is invoked to manually add a single FCF record. The caller
18311  * must pass a completely initialized FCF_Record.  This routine takes
18312  * care of the nonembedded mailbox operations.
18313  **/
18314 int
18315 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18316 {
18317         int rc = 0;
18318         LPFC_MBOXQ_t *mboxq;
18319         uint8_t *bytep;
18320         void *virt_addr;
18321         struct lpfc_mbx_sge sge;
18322         uint32_t alloc_len, req_len;
18323         uint32_t fcfindex;
18324
18325         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18326         if (!mboxq) {
18327                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18328                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18329                 return -ENOMEM;
18330         }
18331
18332         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18333                   sizeof(uint32_t);
18334
18335         /* Allocate DMA memory and set up the non-embedded mailbox command */
18336         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18337                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18338                                      req_len, LPFC_SLI4_MBX_NEMBED);
18339         if (alloc_len < req_len) {
18340                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18341                         "2523 Allocated DMA memory size (x%x) is "
18342                         "less than the requested DMA memory "
18343                         "size (x%x)\n", alloc_len, req_len);
18344                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18345                 return -ENOMEM;
18346         }
18347
18348         /*
18349          * Get the first SGE entry from the non-embedded DMA memory.  This
18350          * routine only uses a single SGE.
18351          */
18352         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
18353         virt_addr = mboxq->sge_array->addr[0];
18354         /*
18355          * Configure the FCF record for FCFI 0.  This is the driver's
18356          * hardcoded default and gets used in nonFIP mode.
18357          */
18358         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18359         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18360         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18361
18362         /*
18363          * Copy the fcf_index and the FCF Record Data. The data starts after
18364          * the FCoE header plus word10. The data copy needs to be endian
18365          * correct.
18366          */
18367         bytep += sizeof(uint32_t);
18368         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18369         mboxq->vport = phba->pport;
18370         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18371         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18372         if (rc == MBX_NOT_FINISHED) {
18373                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18374                         "2515 ADD_FCF_RECORD mailbox failed with "
18375                         "status 0x%x\n", rc);
18376                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18377                 rc = -EIO;
18378         } else
18379                 rc = 0;
18380
18381         return rc;
18382 }
18383
18384 /**
18385  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18386  * @phba: pointer to lpfc hba data structure.
18387  * @fcf_record:  pointer to the fcf record to write the default data.
18388  * @fcf_index: FCF table entry index.
18389  *
18390  * This routine is invoked to build the driver's default FCF record.  The
18391  * values used are hardcoded.  This routine handles memory initialization.
18392  *
18393  **/
18394 void
18395 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18396                                 struct fcf_record *fcf_record,
18397                                 uint16_t fcf_index)
18398 {
18399         memset(fcf_record, 0, sizeof(struct fcf_record));
18400         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18401         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18402         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18403         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18404         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18405         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18406         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18407         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18408         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18409         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18410         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18411         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18412         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
18413         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
18414         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18415         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18416                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18417         /* Set the VLAN bit map */
18418         if (phba->valid_vlan) {
18419                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18420                         = 1 << (phba->vlan_id % 8);
18421         }
18422 }
18423
18424 /**
18425  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
18426  * @phba: pointer to lpfc hba data structure.
18427  * @fcf_index: FCF table entry offset.
18428  *
18429  * This routine is invoked to scan the entire FCF table by reading FCF
18430  * record and processing it one at a time starting from the @fcf_index
18431  * for initial FCF discovery or fast FCF failover rediscovery.
18432  *
18433  * Return 0 if the mailbox command is submitted successfully, none 0
18434  * otherwise.
18435  **/
18436 int
18437 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18438 {
18439         int rc = 0, error;
18440         LPFC_MBOXQ_t *mboxq;
18441
18442         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
18443         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
18444         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18445         if (!mboxq) {
18446                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18447                                 "2000 Failed to allocate mbox for "
18448                                 "READ_FCF cmd\n");
18449                 error = -ENOMEM;
18450                 goto fail_fcf_scan;
18451         }
18452         /* Construct the read FCF record mailbox command */
18453         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18454         if (rc) {
18455                 error = -EINVAL;
18456                 goto fail_fcf_scan;
18457         }
18458         /* Issue the mailbox command asynchronously */
18459         mboxq->vport = phba->pport;
18460         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
18461
18462         spin_lock_irq(&phba->hbalock);
18463         phba->hba_flag |= FCF_TS_INPROG;
18464         spin_unlock_irq(&phba->hbalock);
18465
18466         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18467         if (rc == MBX_NOT_FINISHED)
18468                 error = -EIO;
18469         else {
18470                 /* Reset eligible FCF count for new scan */
18471                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
18472                         phba->fcf.eligible_fcf_cnt = 0;
18473                 error = 0;
18474         }
18475 fail_fcf_scan:
18476         if (error) {
18477                 if (mboxq)
18478                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
18479                 /* FCF scan failed, clear FCF_TS_INPROG flag */
18480                 spin_lock_irq(&phba->hbalock);
18481                 phba->hba_flag &= ~FCF_TS_INPROG;
18482                 spin_unlock_irq(&phba->hbalock);
18483         }
18484         return error;
18485 }
18486
18487 /**
18488  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
18489  * @phba: pointer to lpfc hba data structure.
18490  * @fcf_index: FCF table entry offset.
18491  *
18492  * This routine is invoked to read an FCF record indicated by @fcf_index
18493  * and to use it for FLOGI roundrobin FCF failover.
18494  *
18495  * Return 0 if the mailbox command is submitted successfully, none 0
18496  * otherwise.
18497  **/
18498 int
18499 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18500 {
18501         int rc = 0, error;
18502         LPFC_MBOXQ_t *mboxq;
18503
18504         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18505         if (!mboxq) {
18506                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18507                                 "2763 Failed to allocate mbox for "
18508                                 "READ_FCF cmd\n");
18509                 error = -ENOMEM;
18510                 goto fail_fcf_read;
18511         }
18512         /* Construct the read FCF record mailbox command */
18513         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18514         if (rc) {
18515                 error = -EINVAL;
18516                 goto fail_fcf_read;
18517         }
18518         /* Issue the mailbox command asynchronously */
18519         mboxq->vport = phba->pport;
18520         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18521         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18522         if (rc == MBX_NOT_FINISHED)
18523                 error = -EIO;
18524         else
18525                 error = 0;
18526
18527 fail_fcf_read:
18528         if (error && mboxq)
18529                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18530         return error;
18531 }
18532
18533 /**
18534  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18535  * @phba: pointer to lpfc hba data structure.
18536  * @fcf_index: FCF table entry offset.
18537  *
18538  * This routine is invoked to read an FCF record indicated by @fcf_index to
18539  * determine whether it's eligible for FLOGI roundrobin failover list.
18540  *
18541  * Return 0 if the mailbox command is submitted successfully, none 0
18542  * otherwise.
18543  **/
18544 int
18545 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18546 {
18547         int rc = 0, error;
18548         LPFC_MBOXQ_t *mboxq;
18549
18550         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18551         if (!mboxq) {
18552                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18553                                 "2758 Failed to allocate mbox for "
18554                                 "READ_FCF cmd\n");
18555                                 error = -ENOMEM;
18556                                 goto fail_fcf_read;
18557         }
18558         /* Construct the read FCF record mailbox command */
18559         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18560         if (rc) {
18561                 error = -EINVAL;
18562                 goto fail_fcf_read;
18563         }
18564         /* Issue the mailbox command asynchronously */
18565         mboxq->vport = phba->pport;
18566         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18567         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18568         if (rc == MBX_NOT_FINISHED)
18569                 error = -EIO;
18570         else
18571                 error = 0;
18572
18573 fail_fcf_read:
18574         if (error && mboxq)
18575                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18576         return error;
18577 }
18578
18579 /**
18580  * lpfc_check_next_fcf_pri_level
18581  * phba pointer to the lpfc_hba struct for this port.
18582  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18583  * routine when the rr_bmask is empty. The FCF indecies are put into the
18584  * rr_bmask based on their priority level. Starting from the highest priority
18585  * to the lowest. The most likely FCF candidate will be in the highest
18586  * priority group. When this routine is called it searches the fcf_pri list for
18587  * next lowest priority group and repopulates the rr_bmask with only those
18588  * fcf_indexes.
18589  * returns:
18590  * 1=success 0=failure
18591  **/
18592 static int
18593 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18594 {
18595         uint16_t next_fcf_pri;
18596         uint16_t last_index;
18597         struct lpfc_fcf_pri *fcf_pri;
18598         int rc;
18599         int ret = 0;
18600
18601         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18602                         LPFC_SLI4_FCF_TBL_INDX_MAX);
18603         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18604                         "3060 Last IDX %d\n", last_index);
18605
18606         /* Verify the priority list has 2 or more entries */
18607         spin_lock_irq(&phba->hbalock);
18608         if (list_empty(&phba->fcf.fcf_pri_list) ||
18609             list_is_singular(&phba->fcf.fcf_pri_list)) {
18610                 spin_unlock_irq(&phba->hbalock);
18611                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18612                         "3061 Last IDX %d\n", last_index);
18613                 return 0; /* Empty rr list */
18614         }
18615         spin_unlock_irq(&phba->hbalock);
18616
18617         next_fcf_pri = 0;
18618         /*
18619          * Clear the rr_bmask and set all of the bits that are at this
18620          * priority.
18621          */
18622         memset(phba->fcf.fcf_rr_bmask, 0,
18623                         sizeof(*phba->fcf.fcf_rr_bmask));
18624         spin_lock_irq(&phba->hbalock);
18625         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18626                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18627                         continue;
18628                 /*
18629                  * the 1st priority that has not FLOGI failed
18630                  * will be the highest.
18631                  */
18632                 if (!next_fcf_pri)
18633                         next_fcf_pri = fcf_pri->fcf_rec.priority;
18634                 spin_unlock_irq(&phba->hbalock);
18635                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18636                         rc = lpfc_sli4_fcf_rr_index_set(phba,
18637                                                 fcf_pri->fcf_rec.fcf_index);
18638                         if (rc)
18639                                 return 0;
18640                 }
18641                 spin_lock_irq(&phba->hbalock);
18642         }
18643         /*
18644          * if next_fcf_pri was not set above and the list is not empty then
18645          * we have failed flogis on all of them. So reset flogi failed
18646          * and start at the beginning.
18647          */
18648         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18649                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18650                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18651                         /*
18652                          * the 1st priority that has not FLOGI failed
18653                          * will be the highest.
18654                          */
18655                         if (!next_fcf_pri)
18656                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
18657                         spin_unlock_irq(&phba->hbalock);
18658                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18659                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
18660                                                 fcf_pri->fcf_rec.fcf_index);
18661                                 if (rc)
18662                                         return 0;
18663                         }
18664                         spin_lock_irq(&phba->hbalock);
18665                 }
18666         } else
18667                 ret = 1;
18668         spin_unlock_irq(&phba->hbalock);
18669
18670         return ret;
18671 }
18672 /**
18673  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18674  * @phba: pointer to lpfc hba data structure.
18675  *
18676  * This routine is to get the next eligible FCF record index in a round
18677  * robin fashion. If the next eligible FCF record index equals to the
18678  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
18679  * shall be returned, otherwise, the next eligible FCF record's index
18680  * shall be returned.
18681  **/
18682 uint16_t
18683 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18684 {
18685         uint16_t next_fcf_index;
18686
18687 initial_priority:
18688         /* Search start from next bit of currently registered FCF index */
18689         next_fcf_index = phba->fcf.current_rec.fcf_indx;
18690
18691 next_priority:
18692         /* Determine the next fcf index to check */
18693         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
18694         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18695                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
18696                                        next_fcf_index);
18697
18698         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
18699         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18700                 /*
18701                  * If we have wrapped then we need to clear the bits that
18702                  * have been tested so that we can detect when we should
18703                  * change the priority level.
18704                  */
18705                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18706                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
18707         }
18708
18709
18710         /* Check roundrobin failover list empty condition */
18711         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18712                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18713                 /*
18714                  * If next fcf index is not found check if there are lower
18715                  * Priority level fcf's in the fcf_priority list.
18716                  * Set up the rr_bmask with all of the avaiable fcf bits
18717                  * at that level and continue the selection process.
18718                  */
18719                 if (lpfc_check_next_fcf_pri_level(phba))
18720                         goto initial_priority;
18721                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18722                                 "2844 No roundrobin failover FCF available\n");
18723
18724                 return LPFC_FCOE_FCF_NEXT_NONE;
18725         }
18726
18727         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18728                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
18729                 LPFC_FCF_FLOGI_FAILED) {
18730                 if (list_is_singular(&phba->fcf.fcf_pri_list))
18731                         return LPFC_FCOE_FCF_NEXT_NONE;
18732
18733                 goto next_priority;
18734         }
18735
18736         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18737                         "2845 Get next roundrobin failover FCF (x%x)\n",
18738                         next_fcf_index);
18739
18740         return next_fcf_index;
18741 }
18742
18743 /**
18744  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18745  * @phba: pointer to lpfc hba data structure.
18746  *
18747  * This routine sets the FCF record index in to the eligible bmask for
18748  * roundrobin failover search. It checks to make sure that the index
18749  * does not go beyond the range of the driver allocated bmask dimension
18750  * before setting the bit.
18751  *
18752  * Returns 0 if the index bit successfully set, otherwise, it returns
18753  * -EINVAL.
18754  **/
18755 int
18756 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18757 {
18758         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18759                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18760                                 "2610 FCF (x%x) reached driver's book "
18761                                 "keeping dimension:x%x\n",
18762                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18763                 return -EINVAL;
18764         }
18765         /* Set the eligible FCF record index bmask */
18766         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18767
18768         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18769                         "2790 Set FCF (x%x) to roundrobin FCF failover "
18770                         "bmask\n", fcf_index);
18771
18772         return 0;
18773 }
18774
18775 /**
18776  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
18777  * @phba: pointer to lpfc hba data structure.
18778  *
18779  * This routine clears the FCF record index from the eligible bmask for
18780  * roundrobin failover search. It checks to make sure that the index
18781  * does not go beyond the range of the driver allocated bmask dimension
18782  * before clearing the bit.
18783  **/
18784 void
18785 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18786 {
18787         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
18788         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18789                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18790                                 "2762 FCF (x%x) reached driver's book "
18791                                 "keeping dimension:x%x\n",
18792                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18793                 return;
18794         }
18795         /* Clear the eligible FCF record index bmask */
18796         spin_lock_irq(&phba->hbalock);
18797         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18798                                  list) {
18799                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18800                         list_del_init(&fcf_pri->list);
18801                         break;
18802                 }
18803         }
18804         spin_unlock_irq(&phba->hbalock);
18805         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18806
18807         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18808                         "2791 Clear FCF (x%x) from roundrobin failover "
18809                         "bmask\n", fcf_index);
18810 }
18811
18812 /**
18813  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18814  * @phba: pointer to lpfc hba data structure.
18815  *
18816  * This routine is the completion routine for the rediscover FCF table mailbox
18817  * command. If the mailbox command returned failure, it will try to stop the
18818  * FCF rediscover wait timer.
18819  **/
18820 static void
18821 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18822 {
18823         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18824         uint32_t shdr_status, shdr_add_status;
18825
18826         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18827
18828         shdr_status = bf_get(lpfc_mbox_hdr_status,
18829                              &redisc_fcf->header.cfg_shdr.response);
18830         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18831                              &redisc_fcf->header.cfg_shdr.response);
18832         if (shdr_status || shdr_add_status) {
18833                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18834                                 "2746 Requesting for FCF rediscovery failed "
18835                                 "status x%x add_status x%x\n",
18836                                 shdr_status, shdr_add_status);
18837                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
18838                         spin_lock_irq(&phba->hbalock);
18839                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
18840                         spin_unlock_irq(&phba->hbalock);
18841                         /*
18842                          * CVL event triggered FCF rediscover request failed,
18843                          * last resort to re-try current registered FCF entry.
18844                          */
18845                         lpfc_retry_pport_discovery(phba);
18846                 } else {
18847                         spin_lock_irq(&phba->hbalock);
18848                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
18849                         spin_unlock_irq(&phba->hbalock);
18850                         /*
18851                          * DEAD FCF event triggered FCF rediscover request
18852                          * failed, last resort to fail over as a link down
18853                          * to FCF registration.
18854                          */
18855                         lpfc_sli4_fcf_dead_failthrough(phba);
18856                 }
18857         } else {
18858                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18859                                 "2775 Start FCF rediscover quiescent timer\n");
18860                 /*
18861                  * Start FCF rediscovery wait timer for pending FCF
18862                  * before rescan FCF record table.
18863                  */
18864                 lpfc_fcf_redisc_wait_start_timer(phba);
18865         }
18866
18867         mempool_free(mbox, phba->mbox_mem_pool);
18868 }
18869
18870 /**
18871  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
18872  * @phba: pointer to lpfc hba data structure.
18873  *
18874  * This routine is invoked to request for rediscovery of the entire FCF table
18875  * by the port.
18876  **/
18877 int
18878 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18879 {
18880         LPFC_MBOXQ_t *mbox;
18881         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18882         int rc, length;
18883
18884         /* Cancel retry delay timers to all vports before FCF rediscover */
18885         lpfc_cancel_all_vport_retry_delay_timer(phba);
18886
18887         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18888         if (!mbox) {
18889                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18890                                 "2745 Failed to allocate mbox for "
18891                                 "requesting FCF rediscover.\n");
18892                 return -ENOMEM;
18893         }
18894
18895         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18896                   sizeof(struct lpfc_sli4_cfg_mhdr));
18897         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18898                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18899                          length, LPFC_SLI4_MBX_EMBED);
18900
18901         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18902         /* Set count to 0 for invalidating the entire FCF database */
18903         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18904
18905         /* Issue the mailbox command asynchronously */
18906         mbox->vport = phba->pport;
18907         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18908         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18909
18910         if (rc == MBX_NOT_FINISHED) {
18911                 mempool_free(mbox, phba->mbox_mem_pool);
18912                 return -EIO;
18913         }
18914         return 0;
18915 }
18916
18917 /**
18918  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18919  * @phba: pointer to lpfc hba data structure.
18920  *
18921  * This function is the failover routine as a last resort to the FCF DEAD
18922  * event when driver failed to perform fast FCF failover.
18923  **/
18924 void
18925 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18926 {
18927         uint32_t link_state;
18928
18929         /*
18930          * Last resort as FCF DEAD event failover will treat this as
18931          * a link down, but save the link state because we don't want
18932          * it to be changed to Link Down unless it is already down.
18933          */
18934         link_state = phba->link_state;
18935         lpfc_linkdown(phba);
18936         phba->link_state = link_state;
18937
18938         /* Unregister FCF if no devices connected to it */
18939         lpfc_unregister_unused_fcf(phba);
18940 }
18941
18942 /**
18943  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
18944  * @phba: pointer to lpfc hba data structure.
18945  * @rgn23_data: pointer to configure region 23 data.
18946  *
18947  * This function gets SLI3 port configure region 23 data through memory dump
18948  * mailbox command. When it successfully retrieves data, the size of the data
18949  * will be returned, otherwise, 0 will be returned.
18950  **/
18951 static uint32_t
18952 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18953 {
18954         LPFC_MBOXQ_t *pmb = NULL;
18955         MAILBOX_t *mb;
18956         uint32_t offset = 0;
18957         int rc;
18958
18959         if (!rgn23_data)
18960                 return 0;
18961
18962         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18963         if (!pmb) {
18964                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18965                                 "2600 failed to allocate mailbox memory\n");
18966                 return 0;
18967         }
18968         mb = &pmb->u.mb;
18969
18970         do {
18971                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18972                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18973
18974                 if (rc != MBX_SUCCESS) {
18975                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
18976                                         "2601 failed to read config "
18977                                         "region 23, rc 0x%x Status 0x%x\n",
18978                                         rc, mb->mbxStatus);
18979                         mb->un.varDmp.word_cnt = 0;
18980                 }
18981                 /*
18982                  * dump mem may return a zero when finished or we got a
18983                  * mailbox error, either way we are done.
18984                  */
18985                 if (mb->un.varDmp.word_cnt == 0)
18986                         break;
18987                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18988                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18989
18990                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18991                                        rgn23_data + offset,
18992                                        mb->un.varDmp.word_cnt);
18993                 offset += mb->un.varDmp.word_cnt;
18994         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18995
18996         mempool_free(pmb, phba->mbox_mem_pool);
18997         return offset;
18998 }
18999
19000 /**
19001  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19002  * @phba: pointer to lpfc hba data structure.
19003  * @rgn23_data: pointer to configure region 23 data.
19004  *
19005  * This function gets SLI4 port configure region 23 data through memory dump
19006  * mailbox command. When it successfully retrieves data, the size of the data
19007  * will be returned, otherwise, 0 will be returned.
19008  **/
19009 static uint32_t
19010 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19011 {
19012         LPFC_MBOXQ_t *mboxq = NULL;
19013         struct lpfc_dmabuf *mp = NULL;
19014         struct lpfc_mqe *mqe;
19015         uint32_t data_length = 0;
19016         int rc;
19017
19018         if (!rgn23_data)
19019                 return 0;
19020
19021         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19022         if (!mboxq) {
19023                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19024                                 "3105 failed to allocate mailbox memory\n");
19025                 return 0;
19026         }
19027
19028         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19029                 goto out;
19030         mqe = &mboxq->u.mqe;
19031         mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
19032         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19033         if (rc)
19034                 goto out;
19035         data_length = mqe->un.mb_words[5];
19036         if (data_length == 0)
19037                 goto out;
19038         if (data_length > DMP_RGN23_SIZE) {
19039                 data_length = 0;
19040                 goto out;
19041         }
19042         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19043 out:
19044         mempool_free(mboxq, phba->mbox_mem_pool);
19045         if (mp) {
19046                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19047                 kfree(mp);
19048         }
19049         return data_length;
19050 }
19051
19052 /**
19053  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19054  * @phba: pointer to lpfc hba data structure.
19055  *
19056  * This function read region 23 and parse TLV for port status to
19057  * decide if the user disaled the port. If the TLV indicates the
19058  * port is disabled, the hba_flag is set accordingly.
19059  **/
19060 void
19061 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19062 {
19063         uint8_t *rgn23_data = NULL;
19064         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19065         uint32_t offset = 0;
19066
19067         /* Get adapter Region 23 data */
19068         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19069         if (!rgn23_data)
19070                 goto out;
19071
19072         if (phba->sli_rev < LPFC_SLI_REV4)
19073                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19074         else {
19075                 if_type = bf_get(lpfc_sli_intf_if_type,
19076                                  &phba->sli4_hba.sli_intf);
19077                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19078                         goto out;
19079                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19080         }
19081
19082         if (!data_size)
19083                 goto out;
19084
19085         /* Check the region signature first */
19086         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19087                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19088                         "2619 Config region 23 has bad signature\n");
19089                         goto out;
19090         }
19091         offset += 4;
19092
19093         /* Check the data structure version */
19094         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19095                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19096                         "2620 Config region 23 has bad version\n");
19097                 goto out;
19098         }
19099         offset += 4;
19100
19101         /* Parse TLV entries in the region */
19102         while (offset < data_size) {
19103                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19104                         break;
19105                 /*
19106                  * If the TLV is not driver specific TLV or driver id is
19107                  * not linux driver id, skip the record.
19108                  */
19109                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19110                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19111                     (rgn23_data[offset + 3] != 0)) {
19112                         offset += rgn23_data[offset + 1] * 4 + 4;
19113                         continue;
19114                 }
19115
19116                 /* Driver found a driver specific TLV in the config region */
19117                 sub_tlv_len = rgn23_data[offset + 1] * 4;
19118                 offset += 4;
19119                 tlv_offset = 0;
19120
19121                 /*
19122                  * Search for configured port state sub-TLV.
19123                  */
19124                 while ((offset < data_size) &&
19125                         (tlv_offset < sub_tlv_len)) {
19126                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19127                                 offset += 4;
19128                                 tlv_offset += 4;
19129                                 break;
19130                         }
19131                         if (rgn23_data[offset] != PORT_STE_TYPE) {
19132                                 offset += rgn23_data[offset + 1] * 4 + 4;
19133                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19134                                 continue;
19135                         }
19136
19137                         /* This HBA contains PORT_STE configured */
19138                         if (!rgn23_data[offset + 2])
19139                                 phba->hba_flag |= LINK_DISABLED;
19140
19141                         goto out;
19142                 }
19143         }
19144
19145 out:
19146         kfree(rgn23_data);
19147         return;
19148 }
19149
19150 /**
19151  * lpfc_wr_object - write an object to the firmware
19152  * @phba: HBA structure that indicates port to create a queue on.
19153  * @dmabuf_list: list of dmabufs to write to the port.
19154  * @size: the total byte value of the objects to write to the port.
19155  * @offset: the current offset to be used to start the transfer.
19156  *
19157  * This routine will create a wr_object mailbox command to send to the port.
19158  * the mailbox command will be constructed using the dma buffers described in
19159  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19160  * BDEs that the imbedded mailbox can support. The @offset variable will be
19161  * used to indicate the starting offset of the transfer and will also return
19162  * the offset after the write object mailbox has completed. @size is used to
19163  * determine the end of the object and whether the eof bit should be set.
19164  *
19165  * Return 0 is successful and offset will contain the the new offset to use
19166  * for the next write.
19167  * Return negative value for error cases.
19168  **/
19169 int
19170 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19171                uint32_t size, uint32_t *offset)
19172 {
19173         struct lpfc_mbx_wr_object *wr_object;
19174         LPFC_MBOXQ_t *mbox;
19175         int rc = 0, i = 0;
19176         uint32_t shdr_status, shdr_add_status, shdr_change_status;
19177         uint32_t mbox_tmo;
19178         struct lpfc_dmabuf *dmabuf;
19179         uint32_t written = 0;
19180         bool check_change_status = false;
19181
19182         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19183         if (!mbox)
19184                 return -ENOMEM;
19185
19186         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19187                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
19188                         sizeof(struct lpfc_mbx_wr_object) -
19189                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19190
19191         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19192         wr_object->u.request.write_offset = *offset;
19193         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19194         wr_object->u.request.object_name[0] =
19195                 cpu_to_le32(wr_object->u.request.object_name[0]);
19196         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19197         list_for_each_entry(dmabuf, dmabuf_list, list) {
19198                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19199                         break;
19200                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19201                 wr_object->u.request.bde[i].addrHigh =
19202                         putPaddrHigh(dmabuf->phys);
19203                 if (written + SLI4_PAGE_SIZE >= size) {
19204                         wr_object->u.request.bde[i].tus.f.bdeSize =
19205                                 (size - written);
19206                         written += (size - written);
19207                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
19208                         bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19209                         check_change_status = true;
19210                 } else {
19211                         wr_object->u.request.bde[i].tus.f.bdeSize =
19212                                 SLI4_PAGE_SIZE;
19213                         written += SLI4_PAGE_SIZE;
19214                 }
19215                 i++;
19216         }
19217         wr_object->u.request.bde_count = i;
19218         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19219         if (!phba->sli4_hba.intr_enable)
19220                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19221         else {
19222                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
19223                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19224         }
19225         /* The IOCTL status is embedded in the mailbox subheader. */
19226         shdr_status = bf_get(lpfc_mbox_hdr_status,
19227                              &wr_object->header.cfg_shdr.response);
19228         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19229                                  &wr_object->header.cfg_shdr.response);
19230         if (check_change_status) {
19231                 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19232                                             &wr_object->u.response);
19233                 switch (shdr_change_status) {
19234                 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19235                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19236                                         "3198 Firmware write complete: System "
19237                                         "reboot required to instantiate\n");
19238                         break;
19239                 case (LPFC_CHANGE_STATUS_FW_RESET):
19240                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19241                                         "3199 Firmware write complete: Firmware"
19242                                         " reset required to instantiate\n");
19243                         break;
19244                 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19245                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19246                                         "3200 Firmware write complete: Port "
19247                                         "Migration or PCI Reset required to "
19248                                         "instantiate\n");
19249                         break;
19250                 case (LPFC_CHANGE_STATUS_PCI_RESET):
19251                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19252                                         "3201 Firmware write complete: PCI "
19253                                         "Reset required to instantiate\n");
19254                         break;
19255                 default:
19256                         break;
19257                 }
19258         }
19259         if (rc != MBX_TIMEOUT)
19260                 mempool_free(mbox, phba->mbox_mem_pool);
19261         if (shdr_status || shdr_add_status || rc) {
19262                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19263                                 "3025 Write Object mailbox failed with "
19264                                 "status x%x add_status x%x, mbx status x%x\n",
19265                                 shdr_status, shdr_add_status, rc);
19266                 rc = -ENXIO;
19267                 *offset = shdr_add_status;
19268         } else
19269                 *offset += wr_object->u.response.actual_write_length;
19270         return rc;
19271 }
19272
19273 /**
19274  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19275  * @vport: pointer to vport data structure.
19276  *
19277  * This function iterate through the mailboxq and clean up all REG_LOGIN
19278  * and REG_VPI mailbox commands associated with the vport. This function
19279  * is called when driver want to restart discovery of the vport due to
19280  * a Clear Virtual Link event.
19281  **/
19282 void
19283 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19284 {
19285         struct lpfc_hba *phba = vport->phba;
19286         LPFC_MBOXQ_t *mb, *nextmb;
19287         struct lpfc_dmabuf *mp;
19288         struct lpfc_nodelist *ndlp;
19289         struct lpfc_nodelist *act_mbx_ndlp = NULL;
19290         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
19291         LIST_HEAD(mbox_cmd_list);
19292         uint8_t restart_loop;
19293
19294         /* Clean up internally queued mailbox commands with the vport */
19295         spin_lock_irq(&phba->hbalock);
19296         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19297                 if (mb->vport != vport)
19298                         continue;
19299
19300                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19301                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
19302                         continue;
19303
19304                 list_del(&mb->list);
19305                 list_add_tail(&mb->list, &mbox_cmd_list);
19306         }
19307         /* Clean up active mailbox command with the vport */
19308         mb = phba->sli.mbox_active;
19309         if (mb && (mb->vport == vport)) {
19310                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19311                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
19312                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19313                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19314                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19315                         /* Put reference count for delayed processing */
19316                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19317                         /* Unregister the RPI when mailbox complete */
19318                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19319                 }
19320         }
19321         /* Cleanup any mailbox completions which are not yet processed */
19322         do {
19323                 restart_loop = 0;
19324                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19325                         /*
19326                          * If this mailox is already processed or it is
19327                          * for another vport ignore it.
19328                          */
19329                         if ((mb->vport != vport) ||
19330                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19331                                 continue;
19332
19333                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19334                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19335                                 continue;
19336
19337                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19338                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19339                                 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19340                                 /* Unregister the RPI when mailbox complete */
19341                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19342                                 restart_loop = 1;
19343                                 spin_unlock_irq(&phba->hbalock);
19344                                 spin_lock(shost->host_lock);
19345                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19346                                 spin_unlock(shost->host_lock);
19347                                 spin_lock_irq(&phba->hbalock);
19348                                 break;
19349                         }
19350                 }
19351         } while (restart_loop);
19352
19353         spin_unlock_irq(&phba->hbalock);
19354
19355         /* Release the cleaned-up mailbox commands */
19356         while (!list_empty(&mbox_cmd_list)) {
19357                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
19358                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19359                         mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
19360                         if (mp) {
19361                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19362                                 kfree(mp);
19363                         }
19364                         mb->ctx_buf = NULL;
19365                         ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19366                         mb->ctx_ndlp = NULL;
19367                         if (ndlp) {
19368                                 spin_lock(shost->host_lock);
19369                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19370                                 spin_unlock(shost->host_lock);
19371                                 lpfc_nlp_put(ndlp);
19372                         }
19373                 }
19374                 mempool_free(mb, phba->mbox_mem_pool);
19375         }
19376
19377         /* Release the ndlp with the cleaned-up active mailbox command */
19378         if (act_mbx_ndlp) {
19379                 spin_lock(shost->host_lock);
19380                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19381                 spin_unlock(shost->host_lock);
19382                 lpfc_nlp_put(act_mbx_ndlp);
19383         }
19384 }
19385
19386 /**
19387  * lpfc_drain_txq - Drain the txq
19388  * @phba: Pointer to HBA context object.
19389  *
19390  * This function attempt to submit IOCBs on the txq
19391  * to the adapter.  For SLI4 adapters, the txq contains
19392  * ELS IOCBs that have been deferred because the there
19393  * are no SGLs.  This congestion can occur with large
19394  * vport counts during node discovery.
19395  **/
19396
19397 uint32_t
19398 lpfc_drain_txq(struct lpfc_hba *phba)
19399 {
19400         LIST_HEAD(completions);
19401         struct lpfc_sli_ring *pring;
19402         struct lpfc_iocbq *piocbq = NULL;
19403         unsigned long iflags = 0;
19404         char *fail_msg = NULL;
19405         struct lpfc_sglq *sglq;
19406         union lpfc_wqe128 wqe;
19407         uint32_t txq_cnt = 0;
19408         struct lpfc_queue *wq;
19409
19410         if (phba->link_flag & LS_MDS_LOOPBACK) {
19411                 /* MDS WQE are posted only to first WQ*/
19412                 wq = phba->sli4_hba.hdwq[0].fcp_wq;
19413                 if (unlikely(!wq))
19414                         return 0;
19415                 pring = wq->pring;
19416         } else {
19417                 wq = phba->sli4_hba.els_wq;
19418                 if (unlikely(!wq))
19419                         return 0;
19420                 pring = lpfc_phba_elsring(phba);
19421         }
19422
19423         if (unlikely(!pring) || list_empty(&pring->txq))
19424                 return 0;
19425
19426         spin_lock_irqsave(&pring->ring_lock, iflags);
19427         list_for_each_entry(piocbq, &pring->txq, list) {
19428                 txq_cnt++;
19429         }
19430
19431         if (txq_cnt > pring->txq_max)
19432                 pring->txq_max = txq_cnt;
19433
19434         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19435
19436         while (!list_empty(&pring->txq)) {
19437                 spin_lock_irqsave(&pring->ring_lock, iflags);
19438
19439                 piocbq = lpfc_sli_ringtx_get(phba, pring);
19440                 if (!piocbq) {
19441                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19442                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19443                                 "2823 txq empty and txq_cnt is %d\n ",
19444                                 txq_cnt);
19445                         break;
19446                 }
19447                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
19448                 if (!sglq) {
19449                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
19450                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19451                         break;
19452                 }
19453                 txq_cnt--;
19454
19455                 /* The xri and iocb resources secured,
19456                  * attempt to issue request
19457                  */
19458                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
19459                 piocbq->sli4_xritag = sglq->sli4_xritag;
19460                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19461                         fail_msg = "to convert bpl to sgl";
19462                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
19463                         fail_msg = "to convert iocb to wqe";
19464                 else if (lpfc_sli4_wq_put(wq, &wqe))
19465                         fail_msg = " - Wq is full";
19466                 else
19467                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19468
19469                 if (fail_msg) {
19470                         /* Failed means we can't issue and need to cancel */
19471                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19472                                         "2822 IOCB failed %s iotag 0x%x "
19473                                         "xri 0x%x\n",
19474                                         fail_msg,
19475                                         piocbq->iotag, piocbq->sli4_xritag);
19476                         list_add_tail(&piocbq->list, &completions);
19477                 }
19478                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19479         }
19480
19481         /* Cancel all the IOCBs that cannot be issued */
19482         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19483                                 IOERR_SLI_ABORTED);
19484
19485         return txq_cnt;
19486 }
19487
19488 /**
19489  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19490  * @phba: Pointer to HBA context object.
19491  * @pwqe: Pointer to command WQE.
19492  * @sglq: Pointer to the scatter gather queue object.
19493  *
19494  * This routine converts the bpl or bde that is in the WQE
19495  * to a sgl list for the sli4 hardware. The physical address
19496  * of the bpl/bde is converted back to a virtual address.
19497  * If the WQE contains a BPL then the list of BDE's is
19498  * converted to sli4_sge's. If the WQE contains a single
19499  * BDE then it is converted to a single sli_sge.
19500  * The WQE is still in cpu endianness so the contents of
19501  * the bpl can be used without byte swapping.
19502  *
19503  * Returns valid XRI = Success, NO_XRI = Failure.
19504  */
19505 static uint16_t
19506 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19507                  struct lpfc_sglq *sglq)
19508 {
19509         uint16_t xritag = NO_XRI;
19510         struct ulp_bde64 *bpl = NULL;
19511         struct ulp_bde64 bde;
19512         struct sli4_sge *sgl  = NULL;
19513         struct lpfc_dmabuf *dmabuf;
19514         union lpfc_wqe128 *wqe;
19515         int numBdes = 0;
19516         int i = 0;
19517         uint32_t offset = 0; /* accumulated offset in the sg request list */
19518         int inbound = 0; /* number of sg reply entries inbound from firmware */
19519         uint32_t cmd;
19520
19521         if (!pwqeq || !sglq)
19522                 return xritag;
19523
19524         sgl  = (struct sli4_sge *)sglq->sgl;
19525         wqe = &pwqeq->wqe;
19526         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19527
19528         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19529         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19530                 return sglq->sli4_xritag;
19531         numBdes = pwqeq->rsvd2;
19532         if (numBdes) {
19533                 /* The addrHigh and addrLow fields within the WQE
19534                  * have not been byteswapped yet so there is no
19535                  * need to swap them back.
19536                  */
19537                 if (pwqeq->context3)
19538                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19539                 else
19540                         return xritag;
19541
19542                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
19543                 if (!bpl)
19544                         return xritag;
19545
19546                 for (i = 0; i < numBdes; i++) {
19547                         /* Should already be byte swapped. */
19548                         sgl->addr_hi = bpl->addrHigh;
19549                         sgl->addr_lo = bpl->addrLow;
19550
19551                         sgl->word2 = le32_to_cpu(sgl->word2);
19552                         if ((i+1) == numBdes)
19553                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
19554                         else
19555                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
19556                         /* swap the size field back to the cpu so we
19557                          * can assign it to the sgl.
19558                          */
19559                         bde.tus.w = le32_to_cpu(bpl->tus.w);
19560                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19561                         /* The offsets in the sgl need to be accumulated
19562                          * separately for the request and reply lists.
19563                          * The request is always first, the reply follows.
19564                          */
19565                         switch (cmd) {
19566                         case CMD_GEN_REQUEST64_WQE:
19567                                 /* add up the reply sg entries */
19568                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19569                                         inbound++;
19570                                 /* first inbound? reset the offset */
19571                                 if (inbound == 1)
19572                                         offset = 0;
19573                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19574                                 bf_set(lpfc_sli4_sge_type, sgl,
19575                                         LPFC_SGE_TYPE_DATA);
19576                                 offset += bde.tus.f.bdeSize;
19577                                 break;
19578                         case CMD_FCP_TRSP64_WQE:
19579                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19580                                 bf_set(lpfc_sli4_sge_type, sgl,
19581                                         LPFC_SGE_TYPE_DATA);
19582                                 break;
19583                         case CMD_FCP_TSEND64_WQE:
19584                         case CMD_FCP_TRECEIVE64_WQE:
19585                                 bf_set(lpfc_sli4_sge_type, sgl,
19586                                         bpl->tus.f.bdeFlags);
19587                                 if (i < 3)
19588                                         offset = 0;
19589                                 else
19590                                         offset += bde.tus.f.bdeSize;
19591                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19592                                 break;
19593                         }
19594                         sgl->word2 = cpu_to_le32(sgl->word2);
19595                         bpl++;
19596                         sgl++;
19597                 }
19598         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19599                 /* The addrHigh and addrLow fields of the BDE have not
19600                  * been byteswapped yet so they need to be swapped
19601                  * before putting them in the sgl.
19602                  */
19603                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19604                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19605                 sgl->word2 = le32_to_cpu(sgl->word2);
19606                 bf_set(lpfc_sli4_sge_last, sgl, 1);
19607                 sgl->word2 = cpu_to_le32(sgl->word2);
19608                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19609         }
19610         return sglq->sli4_xritag;
19611 }
19612
19613 /**
19614  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19615  * @phba: Pointer to HBA context object.
19616  * @ring_number: Base sli ring number
19617  * @pwqe: Pointer to command WQE.
19618  **/
19619 int
19620 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19621                     struct lpfc_iocbq *pwqe)
19622 {
19623         union lpfc_wqe128 *wqe = &pwqe->wqe;
19624         struct lpfc_nvmet_rcv_ctx *ctxp;
19625         struct lpfc_queue *wq;
19626         struct lpfc_sglq *sglq;
19627         struct lpfc_sli_ring *pring;
19628         unsigned long iflags;
19629         uint32_t ret = 0;
19630
19631         /* NVME_LS and NVME_LS ABTS requests. */
19632         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19633                 pring =  phba->sli4_hba.nvmels_wq->pring;
19634                 spin_lock_irqsave(&pring->ring_lock, iflags);
19635                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19636                 if (!sglq) {
19637                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19638                         return WQE_BUSY;
19639                 }
19640                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19641                 pwqe->sli4_xritag = sglq->sli4_xritag;
19642                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19643                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19644                         return WQE_ERROR;
19645                 }
19646                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19647                        pwqe->sli4_xritag);
19648                 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19649                 if (ret) {
19650                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19651                         return ret;
19652                 }
19653
19654                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19655                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19656                 return 0;
19657         }
19658
19659         /* NVME_FCREQ and NVME_ABTS requests */
19660         if (pwqe->iocb_flag & LPFC_IO_NVME) {
19661                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19662                 pring = phba->sli4_hba.hdwq[pwqe->hba_wqidx].nvme_wq->pring;
19663
19664                 spin_lock_irqsave(&pring->ring_lock, iflags);
19665                 wq = phba->sli4_hba.hdwq[pwqe->hba_wqidx].nvme_wq;
19666                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19667                       phba->sli4_hba.hdwq[pwqe->hba_wqidx].nvme_cq->queue_id);
19668                 ret = lpfc_sli4_wq_put(wq, wqe);
19669                 if (ret) {
19670                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19671                         return ret;
19672                 }
19673                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19674                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19675                 return 0;
19676         }
19677
19678         /* NVMET requests */
19679         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19680                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19681                 pring = phba->sli4_hba.hdwq[pwqe->hba_wqidx].nvme_wq->pring;
19682
19683                 spin_lock_irqsave(&pring->ring_lock, iflags);
19684                 ctxp = pwqe->context2;
19685                 sglq = ctxp->ctxbuf->sglq;
19686                 if (pwqe->sli4_xritag ==  NO_XRI) {
19687                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
19688                         pwqe->sli4_xritag = sglq->sli4_xritag;
19689                 }
19690                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19691                        pwqe->sli4_xritag);
19692                 wq = phba->sli4_hba.hdwq[pwqe->hba_wqidx].nvme_wq;
19693                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19694                       phba->sli4_hba.hdwq[pwqe->hba_wqidx].nvme_cq->queue_id);
19695                 ret = lpfc_sli4_wq_put(wq, wqe);
19696                 if (ret) {
19697                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
19698                         return ret;
19699                 }
19700                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19701                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19702                 return 0;
19703         }
19704         return WQE_ERROR;
19705 }