Merge remote-tracking branches 'spi/topic/devprop', 'spi/topic/fsl', 'spi/topic/fsl...
[sfrench/cifs-2.6.git] / drivers / scsi / lpfc / lpfc_sli.c
1
2 /*******************************************************************
3  * This file is part of the Emulex Linux Device Driver for         *
4  * Fibre Channel Host Bus Adapters.                                *
5  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
6  * “Broadcom” refers to Broadcom Limited and/or its subsidiaries.  *
7  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
8  * EMULEX and SLI are trademarks of Emulex.                        *
9  * www.broadcom.com                                                *
10  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
11  *                                                                 *
12  * This program is free software; you can redistribute it and/or   *
13  * modify it under the terms of version 2 of the GNU General       *
14  * Public License as published by the Free Software Foundation.    *
15  * This program is distributed in the hope that it will be useful. *
16  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
17  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
18  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
19  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
20  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
21  * more details, a copy of which can be found in the file COPYING  *
22  * included with this package.                                     *
23  *******************************************************************/
24
25 #include <linux/blkdev.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/lockdep.h>
31
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38 #include <linux/aer.h>
39
40 #include <linux/nvme-fc-driver.h>
41
42 #include "lpfc_hw4.h"
43 #include "lpfc_hw.h"
44 #include "lpfc_sli.h"
45 #include "lpfc_sli4.h"
46 #include "lpfc_nl.h"
47 #include "lpfc_disc.h"
48 #include "lpfc.h"
49 #include "lpfc_scsi.h"
50 #include "lpfc_nvme.h"
51 #include "lpfc_nvmet.h"
52 #include "lpfc_crtn.h"
53 #include "lpfc_logmsg.h"
54 #include "lpfc_compat.h"
55 #include "lpfc_debugfs.h"
56 #include "lpfc_vport.h"
57 #include "lpfc_version.h"
58
59 /* There are only four IOCB completion types. */
60 typedef enum _lpfc_iocb_type {
61         LPFC_UNKNOWN_IOCB,
62         LPFC_UNSOL_IOCB,
63         LPFC_SOL_IOCB,
64         LPFC_ABORT_IOCB
65 } lpfc_iocb_type;
66
67
68 /* Provide function prototypes local to this module. */
69 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70                                   uint32_t);
71 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
72                               uint8_t *, uint32_t *);
73 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74                                                          struct lpfc_iocbq *);
75 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76                                       struct hbq_dmabuf *);
77 static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
78                                     struct lpfc_cqe *);
79 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
80                                        int);
81 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
82                         uint32_t);
83 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
84 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
85 static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
86                                    struct lpfc_sli_ring *pring,
87                                    struct lpfc_iocbq *cmdiocb);
88
89 static IOCB_t *
90 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
91 {
92         return &iocbq->iocb;
93 }
94
95 /**
96  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
97  * @q: The Work Queue to operate on.
98  * @wqe: The work Queue Entry to put on the Work queue.
99  *
100  * This routine will copy the contents of @wqe to the next available entry on
101  * the @q. This function will then ring the Work Queue Doorbell to signal the
102  * HBA to start processing the Work Queue Entry. This function returns 0 if
103  * successful. If no entries are available on @q then this function will return
104  * -ENOMEM.
105  * The caller is expected to hold the hbalock when calling this routine.
106  **/
107 static uint32_t
108 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
109 {
110         union lpfc_wqe *temp_wqe;
111         struct lpfc_register doorbell;
112         uint32_t host_index;
113         uint32_t idx;
114
115         /* sanity check on queue memory */
116         if (unlikely(!q))
117                 return -ENOMEM;
118         temp_wqe = q->qe[q->host_index].wqe;
119
120         /* If the host has not yet processed the next entry then we are done */
121         idx = ((q->host_index + 1) % q->entry_count);
122         if (idx == q->hba_index) {
123                 q->WQ_overflow++;
124                 return -ENOMEM;
125         }
126         q->WQ_posted++;
127         /* set consumption flag every once in a while */
128         if (!((q->host_index + 1) % q->entry_repost))
129                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
130         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
131                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
132         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
133         /* ensure WQE bcopy flushed before doorbell write */
134         wmb();
135
136         /* Update the host index before invoking device */
137         host_index = q->host_index;
138
139         q->host_index = idx;
140
141         /* Ring Doorbell */
142         doorbell.word0 = 0;
143         if (q->db_format == LPFC_DB_LIST_FORMAT) {
144                 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
145                 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
146                 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
147         } else if (q->db_format == LPFC_DB_RING_FORMAT) {
148                 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
149                 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
150         } else {
151                 return -EINVAL;
152         }
153         writel(doorbell.word0, q->db_regaddr);
154
155         return 0;
156 }
157
158 /**
159  * lpfc_sli4_wq_release - Updates internal hba index for WQ
160  * @q: The Work Queue to operate on.
161  * @index: The index to advance the hba index to.
162  *
163  * This routine will update the HBA index of a queue to reflect consumption of
164  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
165  * an entry the host calls this function to update the queue's internal
166  * pointers. This routine returns the number of entries that were consumed by
167  * the HBA.
168  **/
169 static uint32_t
170 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
171 {
172         uint32_t released = 0;
173
174         /* sanity check on queue memory */
175         if (unlikely(!q))
176                 return 0;
177
178         if (q->hba_index == index)
179                 return 0;
180         do {
181                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
182                 released++;
183         } while (q->hba_index != index);
184         return released;
185 }
186
187 /**
188  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
189  * @q: The Mailbox Queue to operate on.
190  * @wqe: The Mailbox Queue Entry to put on the Work queue.
191  *
192  * This routine will copy the contents of @mqe to the next available entry on
193  * the @q. This function will then ring the Work Queue Doorbell to signal the
194  * HBA to start processing the Work Queue Entry. This function returns 0 if
195  * successful. If no entries are available on @q then this function will return
196  * -ENOMEM.
197  * The caller is expected to hold the hbalock when calling this routine.
198  **/
199 static uint32_t
200 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
201 {
202         struct lpfc_mqe *temp_mqe;
203         struct lpfc_register doorbell;
204
205         /* sanity check on queue memory */
206         if (unlikely(!q))
207                 return -ENOMEM;
208         temp_mqe = q->qe[q->host_index].mqe;
209
210         /* If the host has not yet processed the next entry then we are done */
211         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
212                 return -ENOMEM;
213         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
214         /* Save off the mailbox pointer for completion */
215         q->phba->mbox = (MAILBOX_t *)temp_mqe;
216
217         /* Update the host index before invoking device */
218         q->host_index = ((q->host_index + 1) % q->entry_count);
219
220         /* Ring Doorbell */
221         doorbell.word0 = 0;
222         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
223         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
224         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
225         return 0;
226 }
227
228 /**
229  * lpfc_sli4_mq_release - Updates internal hba index for MQ
230  * @q: The Mailbox Queue to operate on.
231  *
232  * This routine will update the HBA index of a queue to reflect consumption of
233  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
234  * an entry the host calls this function to update the queue's internal
235  * pointers. This routine returns the number of entries that were consumed by
236  * the HBA.
237  **/
238 static uint32_t
239 lpfc_sli4_mq_release(struct lpfc_queue *q)
240 {
241         /* sanity check on queue memory */
242         if (unlikely(!q))
243                 return 0;
244
245         /* Clear the mailbox pointer for completion */
246         q->phba->mbox = NULL;
247         q->hba_index = ((q->hba_index + 1) % q->entry_count);
248         return 1;
249 }
250
251 /**
252  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
253  * @q: The Event Queue to get the first valid EQE from
254  *
255  * This routine will get the first valid Event Queue Entry from @q, update
256  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
257  * the Queue (no more work to do), or the Queue is full of EQEs that have been
258  * processed, but not popped back to the HBA then this routine will return NULL.
259  **/
260 static struct lpfc_eqe *
261 lpfc_sli4_eq_get(struct lpfc_queue *q)
262 {
263         struct lpfc_eqe *eqe;
264         uint32_t idx;
265
266         /* sanity check on queue memory */
267         if (unlikely(!q))
268                 return NULL;
269         eqe = q->qe[q->hba_index].eqe;
270
271         /* If the next EQE is not valid then we are done */
272         if (!bf_get_le32(lpfc_eqe_valid, eqe))
273                 return NULL;
274         /* If the host has not yet processed the next entry then we are done */
275         idx = ((q->hba_index + 1) % q->entry_count);
276         if (idx == q->host_index)
277                 return NULL;
278
279         q->hba_index = idx;
280
281         /*
282          * insert barrier for instruction interlock : data from the hardware
283          * must have the valid bit checked before it can be copied and acted
284          * upon. Speculative instructions were allowing a bcopy at the start
285          * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
286          * after our return, to copy data before the valid bit check above
287          * was done. As such, some of the copied data was stale. The barrier
288          * ensures the check is before any data is copied.
289          */
290         mb();
291         return eqe;
292 }
293
294 /**
295  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
296  * @q: The Event Queue to disable interrupts
297  *
298  **/
299 static inline void
300 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
301 {
302         struct lpfc_register doorbell;
303
304         doorbell.word0 = 0;
305         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
306         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
307         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
308                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
309         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
310         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
311 }
312
313 /**
314  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
315  * @q: The Event Queue that the host has completed processing for.
316  * @arm: Indicates whether the host wants to arms this CQ.
317  *
318  * This routine will mark all Event Queue Entries on @q, from the last
319  * known completed entry to the last entry that was processed, as completed
320  * by clearing the valid bit for each completion queue entry. Then it will
321  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
322  * The internal host index in the @q will be updated by this routine to indicate
323  * that the host has finished processing the entries. The @arm parameter
324  * indicates that the queue should be rearmed when ringing the doorbell.
325  *
326  * This function will return the number of EQEs that were popped.
327  **/
328 uint32_t
329 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
330 {
331         uint32_t released = 0;
332         struct lpfc_eqe *temp_eqe;
333         struct lpfc_register doorbell;
334
335         /* sanity check on queue memory */
336         if (unlikely(!q))
337                 return 0;
338
339         /* while there are valid entries */
340         while (q->hba_index != q->host_index) {
341                 temp_eqe = q->qe[q->host_index].eqe;
342                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
343                 released++;
344                 q->host_index = ((q->host_index + 1) % q->entry_count);
345         }
346         if (unlikely(released == 0 && !arm))
347                 return 0;
348
349         /* ring doorbell for number popped */
350         doorbell.word0 = 0;
351         if (arm) {
352                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
353                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
354         }
355         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
356         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
357         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
358                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
359         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
360         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
361         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
362         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
363                 readl(q->phba->sli4_hba.EQCQDBregaddr);
364         return released;
365 }
366
367 /**
368  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
369  * @q: The Completion Queue to get the first valid CQE from
370  *
371  * This routine will get the first valid Completion Queue Entry from @q, update
372  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
373  * the Queue (no more work to do), or the Queue is full of CQEs that have been
374  * processed, but not popped back to the HBA then this routine will return NULL.
375  **/
376 static struct lpfc_cqe *
377 lpfc_sli4_cq_get(struct lpfc_queue *q)
378 {
379         struct lpfc_cqe *cqe;
380         uint32_t idx;
381
382         /* sanity check on queue memory */
383         if (unlikely(!q))
384                 return NULL;
385
386         /* If the next CQE is not valid then we are done */
387         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
388                 return NULL;
389         /* If the host has not yet processed the next entry then we are done */
390         idx = ((q->hba_index + 1) % q->entry_count);
391         if (idx == q->host_index)
392                 return NULL;
393
394         cqe = q->qe[q->hba_index].cqe;
395         q->hba_index = idx;
396
397         /*
398          * insert barrier for instruction interlock : data from the hardware
399          * must have the valid bit checked before it can be copied and acted
400          * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
401          * instructions allowing action on content before valid bit checked,
402          * add barrier here as well. May not be needed as "content" is a
403          * single 32-bit entity here (vs multi word structure for cq's).
404          */
405         mb();
406         return cqe;
407 }
408
409 /**
410  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
411  * @q: The Completion Queue that the host has completed processing for.
412  * @arm: Indicates whether the host wants to arms this CQ.
413  *
414  * This routine will mark all Completion queue entries on @q, from the last
415  * known completed entry to the last entry that was processed, as completed
416  * by clearing the valid bit for each completion queue entry. Then it will
417  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
418  * The internal host index in the @q will be updated by this routine to indicate
419  * that the host has finished processing the entries. The @arm parameter
420  * indicates that the queue should be rearmed when ringing the doorbell.
421  *
422  * This function will return the number of CQEs that were released.
423  **/
424 uint32_t
425 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
426 {
427         uint32_t released = 0;
428         struct lpfc_cqe *temp_qe;
429         struct lpfc_register doorbell;
430
431         /* sanity check on queue memory */
432         if (unlikely(!q))
433                 return 0;
434         /* while there are valid entries */
435         while (q->hba_index != q->host_index) {
436                 temp_qe = q->qe[q->host_index].cqe;
437                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
438                 released++;
439                 q->host_index = ((q->host_index + 1) % q->entry_count);
440         }
441         if (unlikely(released == 0 && !arm))
442                 return 0;
443
444         /* ring doorbell for number popped */
445         doorbell.word0 = 0;
446         if (arm)
447                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
448         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
449         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
450         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
451                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
452         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
453         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
454         return released;
455 }
456
457 /**
458  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
459  * @q: The Header Receive Queue to operate on.
460  * @wqe: The Receive Queue Entry to put on the Receive queue.
461  *
462  * This routine will copy the contents of @wqe to the next available entry on
463  * the @q. This function will then ring the Receive Queue Doorbell to signal the
464  * HBA to start processing the Receive Queue Entry. This function returns the
465  * index that the rqe was copied to if successful. If no entries are available
466  * on @q then this function will return -ENOMEM.
467  * The caller is expected to hold the hbalock when calling this routine.
468  **/
469 int
470 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
471                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
472 {
473         struct lpfc_rqe *temp_hrqe;
474         struct lpfc_rqe *temp_drqe;
475         struct lpfc_register doorbell;
476         int put_index;
477
478         /* sanity check on queue memory */
479         if (unlikely(!hq) || unlikely(!dq))
480                 return -ENOMEM;
481         put_index = hq->host_index;
482         temp_hrqe = hq->qe[hq->host_index].rqe;
483         temp_drqe = dq->qe[dq->host_index].rqe;
484
485         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
486                 return -EINVAL;
487         if (hq->host_index != dq->host_index)
488                 return -EINVAL;
489         /* If the host has not yet processed the next entry then we are done */
490         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
491                 return -EBUSY;
492         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
493         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
494
495         /* Update the host index to point to the next slot */
496         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
497         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
498
499         /* Ring The Header Receive Queue Doorbell */
500         if (!(hq->host_index % hq->entry_repost)) {
501                 doorbell.word0 = 0;
502                 if (hq->db_format == LPFC_DB_RING_FORMAT) {
503                         bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
504                                hq->entry_repost);
505                         bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
506                 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
507                         bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
508                                hq->entry_repost);
509                         bf_set(lpfc_rq_db_list_fm_index, &doorbell,
510                                hq->host_index);
511                         bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
512                 } else {
513                         return -EINVAL;
514                 }
515                 writel(doorbell.word0, hq->db_regaddr);
516         }
517         return put_index;
518 }
519
520 /**
521  * lpfc_sli4_rq_release - Updates internal hba index for RQ
522  * @q: The Header Receive Queue to operate on.
523  *
524  * This routine will update the HBA index of a queue to reflect consumption of
525  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
526  * consumed an entry the host calls this function to update the queue's
527  * internal pointers. This routine returns the number of entries that were
528  * consumed by the HBA.
529  **/
530 static uint32_t
531 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
532 {
533         /* sanity check on queue memory */
534         if (unlikely(!hq) || unlikely(!dq))
535                 return 0;
536
537         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
538                 return 0;
539         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
540         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
541         return 1;
542 }
543
544 /**
545  * lpfc_cmd_iocb - Get next command iocb entry in the ring
546  * @phba: Pointer to HBA context object.
547  * @pring: Pointer to driver SLI ring object.
548  *
549  * This function returns pointer to next command iocb entry
550  * in the command ring. The caller must hold hbalock to prevent
551  * other threads consume the next command iocb.
552  * SLI-2/SLI-3 provide different sized iocbs.
553  **/
554 static inline IOCB_t *
555 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
556 {
557         return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
558                            pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
559 }
560
561 /**
562  * lpfc_resp_iocb - Get next response iocb entry in the ring
563  * @phba: Pointer to HBA context object.
564  * @pring: Pointer to driver SLI ring object.
565  *
566  * This function returns pointer to next response iocb entry
567  * in the response ring. The caller must hold hbalock to make sure
568  * that no other thread consume the next response iocb.
569  * SLI-2/SLI-3 provide different sized iocbs.
570  **/
571 static inline IOCB_t *
572 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
573 {
574         return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
575                            pring->sli.sli3.rspidx * phba->iocb_rsp_size);
576 }
577
578 /**
579  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
580  * @phba: Pointer to HBA context object.
581  *
582  * This function is called with hbalock held. This function
583  * allocates a new driver iocb object from the iocb pool. If the
584  * allocation is successful, it returns pointer to the newly
585  * allocated iocb object else it returns NULL.
586  **/
587 struct lpfc_iocbq *
588 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
589 {
590         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
591         struct lpfc_iocbq * iocbq = NULL;
592
593         lockdep_assert_held(&phba->hbalock);
594
595         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
596         if (iocbq)
597                 phba->iocb_cnt++;
598         if (phba->iocb_cnt > phba->iocb_max)
599                 phba->iocb_max = phba->iocb_cnt;
600         return iocbq;
601 }
602
603 /**
604  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
605  * @phba: Pointer to HBA context object.
606  * @xritag: XRI value.
607  *
608  * This function clears the sglq pointer from the array of acive
609  * sglq's. The xritag that is passed in is used to index into the
610  * array. Before the xritag can be used it needs to be adjusted
611  * by subtracting the xribase.
612  *
613  * Returns sglq ponter = success, NULL = Failure.
614  **/
615 struct lpfc_sglq *
616 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
617 {
618         struct lpfc_sglq *sglq;
619
620         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
621         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
622         return sglq;
623 }
624
625 /**
626  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
627  * @phba: Pointer to HBA context object.
628  * @xritag: XRI value.
629  *
630  * This function returns the sglq pointer from the array of acive
631  * sglq's. The xritag that is passed in is used to index into the
632  * array. Before the xritag can be used it needs to be adjusted
633  * by subtracting the xribase.
634  *
635  * Returns sglq ponter = success, NULL = Failure.
636  **/
637 struct lpfc_sglq *
638 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
639 {
640         struct lpfc_sglq *sglq;
641
642         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
643         return sglq;
644 }
645
646 /**
647  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
648  * @phba: Pointer to HBA context object.
649  * @xritag: xri used in this exchange.
650  * @rrq: The RRQ to be cleared.
651  *
652  **/
653 void
654 lpfc_clr_rrq_active(struct lpfc_hba *phba,
655                     uint16_t xritag,
656                     struct lpfc_node_rrq *rrq)
657 {
658         struct lpfc_nodelist *ndlp = NULL;
659
660         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
661                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
662
663         /* The target DID could have been swapped (cable swap)
664          * we should use the ndlp from the findnode if it is
665          * available.
666          */
667         if ((!ndlp) && rrq->ndlp)
668                 ndlp = rrq->ndlp;
669
670         if (!ndlp)
671                 goto out;
672
673         if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
674                 rrq->send_rrq = 0;
675                 rrq->xritag = 0;
676                 rrq->rrq_stop_time = 0;
677         }
678 out:
679         mempool_free(rrq, phba->rrq_pool);
680 }
681
682 /**
683  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
684  * @phba: Pointer to HBA context object.
685  *
686  * This function is called with hbalock held. This function
687  * Checks if stop_time (ratov from setting rrq active) has
688  * been reached, if it has and the send_rrq flag is set then
689  * it will call lpfc_send_rrq. If the send_rrq flag is not set
690  * then it will just call the routine to clear the rrq and
691  * free the rrq resource.
692  * The timer is set to the next rrq that is going to expire before
693  * leaving the routine.
694  *
695  **/
696 void
697 lpfc_handle_rrq_active(struct lpfc_hba *phba)
698 {
699         struct lpfc_node_rrq *rrq;
700         struct lpfc_node_rrq *nextrrq;
701         unsigned long next_time;
702         unsigned long iflags;
703         LIST_HEAD(send_rrq);
704
705         spin_lock_irqsave(&phba->hbalock, iflags);
706         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
707         next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
708         list_for_each_entry_safe(rrq, nextrrq,
709                                  &phba->active_rrq_list, list) {
710                 if (time_after(jiffies, rrq->rrq_stop_time))
711                         list_move(&rrq->list, &send_rrq);
712                 else if (time_before(rrq->rrq_stop_time, next_time))
713                         next_time = rrq->rrq_stop_time;
714         }
715         spin_unlock_irqrestore(&phba->hbalock, iflags);
716         if ((!list_empty(&phba->active_rrq_list)) &&
717             (!(phba->pport->load_flag & FC_UNLOADING)))
718                 mod_timer(&phba->rrq_tmr, next_time);
719         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
720                 list_del(&rrq->list);
721                 if (!rrq->send_rrq)
722                         /* this call will free the rrq */
723                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
724                 else if (lpfc_send_rrq(phba, rrq)) {
725                         /* if we send the rrq then the completion handler
726                         *  will clear the bit in the xribitmap.
727                         */
728                         lpfc_clr_rrq_active(phba, rrq->xritag,
729                                             rrq);
730                 }
731         }
732 }
733
734 /**
735  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
736  * @vport: Pointer to vport context object.
737  * @xri: The xri used in the exchange.
738  * @did: The targets DID for this exchange.
739  *
740  * returns NULL = rrq not found in the phba->active_rrq_list.
741  *         rrq = rrq for this xri and target.
742  **/
743 struct lpfc_node_rrq *
744 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
745 {
746         struct lpfc_hba *phba = vport->phba;
747         struct lpfc_node_rrq *rrq;
748         struct lpfc_node_rrq *nextrrq;
749         unsigned long iflags;
750
751         if (phba->sli_rev != LPFC_SLI_REV4)
752                 return NULL;
753         spin_lock_irqsave(&phba->hbalock, iflags);
754         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
755                 if (rrq->vport == vport && rrq->xritag == xri &&
756                                 rrq->nlp_DID == did){
757                         list_del(&rrq->list);
758                         spin_unlock_irqrestore(&phba->hbalock, iflags);
759                         return rrq;
760                 }
761         }
762         spin_unlock_irqrestore(&phba->hbalock, iflags);
763         return NULL;
764 }
765
766 /**
767  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
768  * @vport: Pointer to vport context object.
769  * @ndlp: Pointer to the lpfc_node_list structure.
770  * If ndlp is NULL Remove all active RRQs for this vport from the
771  * phba->active_rrq_list and clear the rrq.
772  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
773  **/
774 void
775 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
776
777 {
778         struct lpfc_hba *phba = vport->phba;
779         struct lpfc_node_rrq *rrq;
780         struct lpfc_node_rrq *nextrrq;
781         unsigned long iflags;
782         LIST_HEAD(rrq_list);
783
784         if (phba->sli_rev != LPFC_SLI_REV4)
785                 return;
786         if (!ndlp) {
787                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
788                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
789         }
790         spin_lock_irqsave(&phba->hbalock, iflags);
791         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
792                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
793                         list_move(&rrq->list, &rrq_list);
794         spin_unlock_irqrestore(&phba->hbalock, iflags);
795
796         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
797                 list_del(&rrq->list);
798                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
799         }
800 }
801
802 /**
803  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
804  * @phba: Pointer to HBA context object.
805  * @ndlp: Targets nodelist pointer for this exchange.
806  * @xritag the xri in the bitmap to test.
807  *
808  * This function is called with hbalock held. This function
809  * returns 0 = rrq not active for this xri
810  *         1 = rrq is valid for this xri.
811  **/
812 int
813 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
814                         uint16_t  xritag)
815 {
816         lockdep_assert_held(&phba->hbalock);
817         if (!ndlp)
818                 return 0;
819         if (!ndlp->active_rrqs_xri_bitmap)
820                 return 0;
821         if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
822                         return 1;
823         else
824                 return 0;
825 }
826
827 /**
828  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
829  * @phba: Pointer to HBA context object.
830  * @ndlp: nodelist pointer for this target.
831  * @xritag: xri used in this exchange.
832  * @rxid: Remote Exchange ID.
833  * @send_rrq: Flag used to determine if we should send rrq els cmd.
834  *
835  * This function takes the hbalock.
836  * The active bit is always set in the active rrq xri_bitmap even
837  * if there is no slot avaiable for the other rrq information.
838  *
839  * returns 0 rrq actived for this xri
840  *         < 0 No memory or invalid ndlp.
841  **/
842 int
843 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
844                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
845 {
846         unsigned long iflags;
847         struct lpfc_node_rrq *rrq;
848         int empty;
849
850         if (!ndlp)
851                 return -EINVAL;
852
853         if (!phba->cfg_enable_rrq)
854                 return -EINVAL;
855
856         spin_lock_irqsave(&phba->hbalock, iflags);
857         if (phba->pport->load_flag & FC_UNLOADING) {
858                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
859                 goto out;
860         }
861
862         /*
863          * set the active bit even if there is no mem available.
864          */
865         if (NLP_CHK_FREE_REQ(ndlp))
866                 goto out;
867
868         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
869                 goto out;
870
871         if (!ndlp->active_rrqs_xri_bitmap)
872                 goto out;
873
874         if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
875                 goto out;
876
877         spin_unlock_irqrestore(&phba->hbalock, iflags);
878         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
879         if (!rrq) {
880                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
881                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
882                                 " DID:0x%x Send:%d\n",
883                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
884                 return -EINVAL;
885         }
886         if (phba->cfg_enable_rrq == 1)
887                 rrq->send_rrq = send_rrq;
888         else
889                 rrq->send_rrq = 0;
890         rrq->xritag = xritag;
891         rrq->rrq_stop_time = jiffies +
892                                 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
893         rrq->ndlp = ndlp;
894         rrq->nlp_DID = ndlp->nlp_DID;
895         rrq->vport = ndlp->vport;
896         rrq->rxid = rxid;
897         spin_lock_irqsave(&phba->hbalock, iflags);
898         empty = list_empty(&phba->active_rrq_list);
899         list_add_tail(&rrq->list, &phba->active_rrq_list);
900         phba->hba_flag |= HBA_RRQ_ACTIVE;
901         if (empty)
902                 lpfc_worker_wake_up(phba);
903         spin_unlock_irqrestore(&phba->hbalock, iflags);
904         return 0;
905 out:
906         spin_unlock_irqrestore(&phba->hbalock, iflags);
907         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
908                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
909                         " DID:0x%x Send:%d\n",
910                         xritag, rxid, ndlp->nlp_DID, send_rrq);
911         return -EINVAL;
912 }
913
914 /**
915  * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
916  * @phba: Pointer to HBA context object.
917  * @piocb: Pointer to the iocbq.
918  *
919  * This function is called with the ring lock held. This function
920  * gets a new driver sglq object from the sglq list. If the
921  * list is not empty then it is successful, it returns pointer to the newly
922  * allocated sglq object else it returns NULL.
923  **/
924 static struct lpfc_sglq *
925 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
926 {
927         struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
928         struct lpfc_sglq *sglq = NULL;
929         struct lpfc_sglq *start_sglq = NULL;
930         struct lpfc_scsi_buf *lpfc_cmd;
931         struct lpfc_nodelist *ndlp;
932         int found = 0;
933
934         lockdep_assert_held(&phba->hbalock);
935
936         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
937                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
938                 ndlp = lpfc_cmd->rdata->pnode;
939         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
940                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
941                 ndlp = piocbq->context_un.ndlp;
942         } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
943                 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
944                         ndlp = NULL;
945                 else
946                         ndlp = piocbq->context_un.ndlp;
947         } else {
948                 ndlp = piocbq->context1;
949         }
950
951         spin_lock(&phba->sli4_hba.sgl_list_lock);
952         list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
953         start_sglq = sglq;
954         while (!found) {
955                 if (!sglq)
956                         break;
957                 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
958                     test_bit(sglq->sli4_lxritag,
959                     ndlp->active_rrqs_xri_bitmap)) {
960                         /* This xri has an rrq outstanding for this DID.
961                          * put it back in the list and get another xri.
962                          */
963                         list_add_tail(&sglq->list, lpfc_els_sgl_list);
964                         sglq = NULL;
965                         list_remove_head(lpfc_els_sgl_list, sglq,
966                                                 struct lpfc_sglq, list);
967                         if (sglq == start_sglq) {
968                                 sglq = NULL;
969                                 break;
970                         } else
971                                 continue;
972                 }
973                 sglq->ndlp = ndlp;
974                 found = 1;
975                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
976                 sglq->state = SGL_ALLOCATED;
977         }
978         spin_unlock(&phba->sli4_hba.sgl_list_lock);
979         return sglq;
980 }
981
982 /**
983  * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
984  * @phba: Pointer to HBA context object.
985  * @piocb: Pointer to the iocbq.
986  *
987  * This function is called with the sgl_list lock held. This function
988  * gets a new driver sglq object from the sglq list. If the
989  * list is not empty then it is successful, it returns pointer to the newly
990  * allocated sglq object else it returns NULL.
991  **/
992 struct lpfc_sglq *
993 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
994 {
995         struct list_head *lpfc_nvmet_sgl_list;
996         struct lpfc_sglq *sglq = NULL;
997
998         lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
999
1000         lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1001
1002         list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1003         if (!sglq)
1004                 return NULL;
1005         phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1006         sglq->state = SGL_ALLOCATED;
1007         return sglq;
1008 }
1009
1010 /**
1011  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1012  * @phba: Pointer to HBA context object.
1013  *
1014  * This function is called with no lock held. This function
1015  * allocates a new driver iocb object from the iocb pool. If the
1016  * allocation is successful, it returns pointer to the newly
1017  * allocated iocb object else it returns NULL.
1018  **/
1019 struct lpfc_iocbq *
1020 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1021 {
1022         struct lpfc_iocbq * iocbq = NULL;
1023         unsigned long iflags;
1024
1025         spin_lock_irqsave(&phba->hbalock, iflags);
1026         iocbq = __lpfc_sli_get_iocbq(phba);
1027         spin_unlock_irqrestore(&phba->hbalock, iflags);
1028         return iocbq;
1029 }
1030
1031 /**
1032  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1033  * @phba: Pointer to HBA context object.
1034  * @iocbq: Pointer to driver iocb object.
1035  *
1036  * This function is called with hbalock held to release driver
1037  * iocb object to the iocb pool. The iotag in the iocb object
1038  * does not change for each use of the iocb object. This function
1039  * clears all other fields of the iocb object when it is freed.
1040  * The sqlq structure that holds the xritag and phys and virtual
1041  * mappings for the scatter gather list is retrieved from the
1042  * active array of sglq. The get of the sglq pointer also clears
1043  * the entry in the array. If the status of the IO indiactes that
1044  * this IO was aborted then the sglq entry it put on the
1045  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1046  * IO has good status or fails for any other reason then the sglq
1047  * entry is added to the free list (lpfc_els_sgl_list).
1048  **/
1049 static void
1050 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1051 {
1052         struct lpfc_sglq *sglq;
1053         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1054         unsigned long iflag = 0;
1055         struct lpfc_sli_ring *pring;
1056
1057         lockdep_assert_held(&phba->hbalock);
1058
1059         if (iocbq->sli4_xritag == NO_XRI)
1060                 sglq = NULL;
1061         else
1062                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1063
1064
1065         if (sglq)  {
1066                 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1067                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1068                                           iflag);
1069                         sglq->state = SGL_FREED;
1070                         sglq->ndlp = NULL;
1071                         list_add_tail(&sglq->list,
1072                                       &phba->sli4_hba.lpfc_nvmet_sgl_list);
1073                         spin_unlock_irqrestore(
1074                                 &phba->sli4_hba.sgl_list_lock, iflag);
1075                         goto out;
1076                 }
1077
1078                 pring = phba->sli4_hba.els_wq->pring;
1079                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1080                         (sglq->state != SGL_XRI_ABORTED)) {
1081                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1082                                           iflag);
1083                         list_add(&sglq->list,
1084                                  &phba->sli4_hba.lpfc_abts_els_sgl_list);
1085                         spin_unlock_irqrestore(
1086                                 &phba->sli4_hba.sgl_list_lock, iflag);
1087                 } else {
1088                         spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1089                                           iflag);
1090                         sglq->state = SGL_FREED;
1091                         sglq->ndlp = NULL;
1092                         list_add_tail(&sglq->list,
1093                                       &phba->sli4_hba.lpfc_els_sgl_list);
1094                         spin_unlock_irqrestore(
1095                                 &phba->sli4_hba.sgl_list_lock, iflag);
1096
1097                         /* Check if TXQ queue needs to be serviced */
1098                         if (!list_empty(&pring->txq))
1099                                 lpfc_worker_wake_up(phba);
1100                 }
1101         }
1102
1103 out:
1104         /*
1105          * Clean all volatile data fields, preserve iotag and node struct.
1106          */
1107         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1108         iocbq->sli4_lxritag = NO_XRI;
1109         iocbq->sli4_xritag = NO_XRI;
1110         iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1111                               LPFC_IO_NVME_LS);
1112         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1113 }
1114
1115
1116 /**
1117  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1118  * @phba: Pointer to HBA context object.
1119  * @iocbq: Pointer to driver iocb object.
1120  *
1121  * This function is called with hbalock held to release driver
1122  * iocb object to the iocb pool. The iotag in the iocb object
1123  * does not change for each use of the iocb object. This function
1124  * clears all other fields of the iocb object when it is freed.
1125  **/
1126 static void
1127 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1128 {
1129         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1130
1131         lockdep_assert_held(&phba->hbalock);
1132
1133         /*
1134          * Clean all volatile data fields, preserve iotag and node struct.
1135          */
1136         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1137         iocbq->sli4_xritag = NO_XRI;
1138         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1139 }
1140
1141 /**
1142  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1143  * @phba: Pointer to HBA context object.
1144  * @iocbq: Pointer to driver iocb object.
1145  *
1146  * This function is called with hbalock held to release driver
1147  * iocb object to the iocb pool. The iotag in the iocb object
1148  * does not change for each use of the iocb object. This function
1149  * clears all other fields of the iocb object when it is freed.
1150  **/
1151 static void
1152 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1153 {
1154         lockdep_assert_held(&phba->hbalock);
1155
1156         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1157         phba->iocb_cnt--;
1158 }
1159
1160 /**
1161  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1162  * @phba: Pointer to HBA context object.
1163  * @iocbq: Pointer to driver iocb object.
1164  *
1165  * This function is called with no lock held to release the iocb to
1166  * iocb pool.
1167  **/
1168 void
1169 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1170 {
1171         unsigned long iflags;
1172
1173         /*
1174          * Clean all volatile data fields, preserve iotag and node struct.
1175          */
1176         spin_lock_irqsave(&phba->hbalock, iflags);
1177         __lpfc_sli_release_iocbq(phba, iocbq);
1178         spin_unlock_irqrestore(&phba->hbalock, iflags);
1179 }
1180
1181 /**
1182  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1183  * @phba: Pointer to HBA context object.
1184  * @iocblist: List of IOCBs.
1185  * @ulpstatus: ULP status in IOCB command field.
1186  * @ulpWord4: ULP word-4 in IOCB command field.
1187  *
1188  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1189  * on the list by invoking the complete callback function associated with the
1190  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1191  * fields.
1192  **/
1193 void
1194 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1195                       uint32_t ulpstatus, uint32_t ulpWord4)
1196 {
1197         struct lpfc_iocbq *piocb;
1198
1199         while (!list_empty(iocblist)) {
1200                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1201                 if (!piocb->iocb_cmpl)
1202                         lpfc_sli_release_iocbq(phba, piocb);
1203                 else {
1204                         piocb->iocb.ulpStatus = ulpstatus;
1205                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1206                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1207                 }
1208         }
1209         return;
1210 }
1211
1212 /**
1213  * lpfc_sli_iocb_cmd_type - Get the iocb type
1214  * @iocb_cmnd: iocb command code.
1215  *
1216  * This function is called by ring event handler function to get the iocb type.
1217  * This function translates the iocb command to an iocb command type used to
1218  * decide the final disposition of each completed IOCB.
1219  * The function returns
1220  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1221  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1222  * LPFC_ABORT_IOCB   if it is an abort iocb
1223  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1224  *
1225  * The caller is not required to hold any lock.
1226  **/
1227 static lpfc_iocb_type
1228 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1229 {
1230         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1231
1232         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1233                 return 0;
1234
1235         switch (iocb_cmnd) {
1236         case CMD_XMIT_SEQUENCE_CR:
1237         case CMD_XMIT_SEQUENCE_CX:
1238         case CMD_XMIT_BCAST_CN:
1239         case CMD_XMIT_BCAST_CX:
1240         case CMD_ELS_REQUEST_CR:
1241         case CMD_ELS_REQUEST_CX:
1242         case CMD_CREATE_XRI_CR:
1243         case CMD_CREATE_XRI_CX:
1244         case CMD_GET_RPI_CN:
1245         case CMD_XMIT_ELS_RSP_CX:
1246         case CMD_GET_RPI_CR:
1247         case CMD_FCP_IWRITE_CR:
1248         case CMD_FCP_IWRITE_CX:
1249         case CMD_FCP_IREAD_CR:
1250         case CMD_FCP_IREAD_CX:
1251         case CMD_FCP_ICMND_CR:
1252         case CMD_FCP_ICMND_CX:
1253         case CMD_FCP_TSEND_CX:
1254         case CMD_FCP_TRSP_CX:
1255         case CMD_FCP_TRECEIVE_CX:
1256         case CMD_FCP_AUTO_TRSP_CX:
1257         case CMD_ADAPTER_MSG:
1258         case CMD_ADAPTER_DUMP:
1259         case CMD_XMIT_SEQUENCE64_CR:
1260         case CMD_XMIT_SEQUENCE64_CX:
1261         case CMD_XMIT_BCAST64_CN:
1262         case CMD_XMIT_BCAST64_CX:
1263         case CMD_ELS_REQUEST64_CR:
1264         case CMD_ELS_REQUEST64_CX:
1265         case CMD_FCP_IWRITE64_CR:
1266         case CMD_FCP_IWRITE64_CX:
1267         case CMD_FCP_IREAD64_CR:
1268         case CMD_FCP_IREAD64_CX:
1269         case CMD_FCP_ICMND64_CR:
1270         case CMD_FCP_ICMND64_CX:
1271         case CMD_FCP_TSEND64_CX:
1272         case CMD_FCP_TRSP64_CX:
1273         case CMD_FCP_TRECEIVE64_CX:
1274         case CMD_GEN_REQUEST64_CR:
1275         case CMD_GEN_REQUEST64_CX:
1276         case CMD_XMIT_ELS_RSP64_CX:
1277         case DSSCMD_IWRITE64_CR:
1278         case DSSCMD_IWRITE64_CX:
1279         case DSSCMD_IREAD64_CR:
1280         case DSSCMD_IREAD64_CX:
1281                 type = LPFC_SOL_IOCB;
1282                 break;
1283         case CMD_ABORT_XRI_CN:
1284         case CMD_ABORT_XRI_CX:
1285         case CMD_CLOSE_XRI_CN:
1286         case CMD_CLOSE_XRI_CX:
1287         case CMD_XRI_ABORTED_CX:
1288         case CMD_ABORT_MXRI64_CN:
1289         case CMD_XMIT_BLS_RSP64_CX:
1290                 type = LPFC_ABORT_IOCB;
1291                 break;
1292         case CMD_RCV_SEQUENCE_CX:
1293         case CMD_RCV_ELS_REQ_CX:
1294         case CMD_RCV_SEQUENCE64_CX:
1295         case CMD_RCV_ELS_REQ64_CX:
1296         case CMD_ASYNC_STATUS:
1297         case CMD_IOCB_RCV_SEQ64_CX:
1298         case CMD_IOCB_RCV_ELS64_CX:
1299         case CMD_IOCB_RCV_CONT64_CX:
1300         case CMD_IOCB_RET_XRI64_CX:
1301                 type = LPFC_UNSOL_IOCB;
1302                 break;
1303         case CMD_IOCB_XMIT_MSEQ64_CR:
1304         case CMD_IOCB_XMIT_MSEQ64_CX:
1305         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1306         case CMD_IOCB_RCV_ELS_LIST64_CX:
1307         case CMD_IOCB_CLOSE_EXTENDED_CN:
1308         case CMD_IOCB_ABORT_EXTENDED_CN:
1309         case CMD_IOCB_RET_HBQE64_CN:
1310         case CMD_IOCB_FCP_IBIDIR64_CR:
1311         case CMD_IOCB_FCP_IBIDIR64_CX:
1312         case CMD_IOCB_FCP_ITASKMGT64_CX:
1313         case CMD_IOCB_LOGENTRY_CN:
1314         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1315                 printk("%s - Unhandled SLI-3 Command x%x\n",
1316                                 __func__, iocb_cmnd);
1317                 type = LPFC_UNKNOWN_IOCB;
1318                 break;
1319         default:
1320                 type = LPFC_UNKNOWN_IOCB;
1321                 break;
1322         }
1323
1324         return type;
1325 }
1326
1327 /**
1328  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1329  * @phba: Pointer to HBA context object.
1330  *
1331  * This function is called from SLI initialization code
1332  * to configure every ring of the HBA's SLI interface. The
1333  * caller is not required to hold any lock. This function issues
1334  * a config_ring mailbox command for each ring.
1335  * This function returns zero if successful else returns a negative
1336  * error code.
1337  **/
1338 static int
1339 lpfc_sli_ring_map(struct lpfc_hba *phba)
1340 {
1341         struct lpfc_sli *psli = &phba->sli;
1342         LPFC_MBOXQ_t *pmb;
1343         MAILBOX_t *pmbox;
1344         int i, rc, ret = 0;
1345
1346         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1347         if (!pmb)
1348                 return -ENOMEM;
1349         pmbox = &pmb->u.mb;
1350         phba->link_state = LPFC_INIT_MBX_CMDS;
1351         for (i = 0; i < psli->num_rings; i++) {
1352                 lpfc_config_ring(phba, i, pmb);
1353                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1354                 if (rc != MBX_SUCCESS) {
1355                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1356                                         "0446 Adapter failed to init (%d), "
1357                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1358                                         "ring %d\n",
1359                                         rc, pmbox->mbxCommand,
1360                                         pmbox->mbxStatus, i);
1361                         phba->link_state = LPFC_HBA_ERROR;
1362                         ret = -ENXIO;
1363                         break;
1364                 }
1365         }
1366         mempool_free(pmb, phba->mbox_mem_pool);
1367         return ret;
1368 }
1369
1370 /**
1371  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1372  * @phba: Pointer to HBA context object.
1373  * @pring: Pointer to driver SLI ring object.
1374  * @piocb: Pointer to the driver iocb object.
1375  *
1376  * This function is called with hbalock held. The function adds the
1377  * new iocb to txcmplq of the given ring. This function always returns
1378  * 0. If this function is called for ELS ring, this function checks if
1379  * there is a vport associated with the ELS command. This function also
1380  * starts els_tmofunc timer if this is an ELS command.
1381  **/
1382 static int
1383 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1384                         struct lpfc_iocbq *piocb)
1385 {
1386         lockdep_assert_held(&phba->hbalock);
1387
1388         BUG_ON(!piocb);
1389
1390         list_add_tail(&piocb->list, &pring->txcmplq);
1391         piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1392
1393         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1394            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1395            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1396                 BUG_ON(!piocb->vport);
1397                 if (!(piocb->vport->load_flag & FC_UNLOADING))
1398                         mod_timer(&piocb->vport->els_tmofunc,
1399                                   jiffies +
1400                                   msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1401         }
1402
1403         return 0;
1404 }
1405
1406 /**
1407  * lpfc_sli_ringtx_get - Get first element of the txq
1408  * @phba: Pointer to HBA context object.
1409  * @pring: Pointer to driver SLI ring object.
1410  *
1411  * This function is called with hbalock held to get next
1412  * iocb in txq of the given ring. If there is any iocb in
1413  * the txq, the function returns first iocb in the list after
1414  * removing the iocb from the list, else it returns NULL.
1415  **/
1416 struct lpfc_iocbq *
1417 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1418 {
1419         struct lpfc_iocbq *cmd_iocb;
1420
1421         lockdep_assert_held(&phba->hbalock);
1422
1423         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1424         return cmd_iocb;
1425 }
1426
1427 /**
1428  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1429  * @phba: Pointer to HBA context object.
1430  * @pring: Pointer to driver SLI ring object.
1431  *
1432  * This function is called with hbalock held and the caller must post the
1433  * iocb without releasing the lock. If the caller releases the lock,
1434  * iocb slot returned by the function is not guaranteed to be available.
1435  * The function returns pointer to the next available iocb slot if there
1436  * is available slot in the ring, else it returns NULL.
1437  * If the get index of the ring is ahead of the put index, the function
1438  * will post an error attention event to the worker thread to take the
1439  * HBA to offline state.
1440  **/
1441 static IOCB_t *
1442 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1443 {
1444         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1445         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1446
1447         lockdep_assert_held(&phba->hbalock);
1448
1449         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1450            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1451                 pring->sli.sli3.next_cmdidx = 0;
1452
1453         if (unlikely(pring->sli.sli3.local_getidx ==
1454                 pring->sli.sli3.next_cmdidx)) {
1455
1456                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1457
1458                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1459                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1460                                         "0315 Ring %d issue: portCmdGet %d "
1461                                         "is bigger than cmd ring %d\n",
1462                                         pring->ringno,
1463                                         pring->sli.sli3.local_getidx,
1464                                         max_cmd_idx);
1465
1466                         phba->link_state = LPFC_HBA_ERROR;
1467                         /*
1468                          * All error attention handlers are posted to
1469                          * worker thread
1470                          */
1471                         phba->work_ha |= HA_ERATT;
1472                         phba->work_hs = HS_FFER3;
1473
1474                         lpfc_worker_wake_up(phba);
1475
1476                         return NULL;
1477                 }
1478
1479                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1480                         return NULL;
1481         }
1482
1483         return lpfc_cmd_iocb(phba, pring);
1484 }
1485
1486 /**
1487  * lpfc_sli_next_iotag - Get an iotag for the iocb
1488  * @phba: Pointer to HBA context object.
1489  * @iocbq: Pointer to driver iocb object.
1490  *
1491  * This function gets an iotag for the iocb. If there is no unused iotag and
1492  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1493  * array and assigns a new iotag.
1494  * The function returns the allocated iotag if successful, else returns zero.
1495  * Zero is not a valid iotag.
1496  * The caller is not required to hold any lock.
1497  **/
1498 uint16_t
1499 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1500 {
1501         struct lpfc_iocbq **new_arr;
1502         struct lpfc_iocbq **old_arr;
1503         size_t new_len;
1504         struct lpfc_sli *psli = &phba->sli;
1505         uint16_t iotag;
1506
1507         spin_lock_irq(&phba->hbalock);
1508         iotag = psli->last_iotag;
1509         if(++iotag < psli->iocbq_lookup_len) {
1510                 psli->last_iotag = iotag;
1511                 psli->iocbq_lookup[iotag] = iocbq;
1512                 spin_unlock_irq(&phba->hbalock);
1513                 iocbq->iotag = iotag;
1514                 return iotag;
1515         } else if (psli->iocbq_lookup_len < (0xffff
1516                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1517                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1518                 spin_unlock_irq(&phba->hbalock);
1519                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1520                                   GFP_KERNEL);
1521                 if (new_arr) {
1522                         spin_lock_irq(&phba->hbalock);
1523                         old_arr = psli->iocbq_lookup;
1524                         if (new_len <= psli->iocbq_lookup_len) {
1525                                 /* highly unprobable case */
1526                                 kfree(new_arr);
1527                                 iotag = psli->last_iotag;
1528                                 if(++iotag < psli->iocbq_lookup_len) {
1529                                         psli->last_iotag = iotag;
1530                                         psli->iocbq_lookup[iotag] = iocbq;
1531                                         spin_unlock_irq(&phba->hbalock);
1532                                         iocbq->iotag = iotag;
1533                                         return iotag;
1534                                 }
1535                                 spin_unlock_irq(&phba->hbalock);
1536                                 return 0;
1537                         }
1538                         if (psli->iocbq_lookup)
1539                                 memcpy(new_arr, old_arr,
1540                                        ((psli->last_iotag  + 1) *
1541                                         sizeof (struct lpfc_iocbq *)));
1542                         psli->iocbq_lookup = new_arr;
1543                         psli->iocbq_lookup_len = new_len;
1544                         psli->last_iotag = iotag;
1545                         psli->iocbq_lookup[iotag] = iocbq;
1546                         spin_unlock_irq(&phba->hbalock);
1547                         iocbq->iotag = iotag;
1548                         kfree(old_arr);
1549                         return iotag;
1550                 }
1551         } else
1552                 spin_unlock_irq(&phba->hbalock);
1553
1554         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1555                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1556                         psli->last_iotag);
1557
1558         return 0;
1559 }
1560
1561 /**
1562  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1563  * @phba: Pointer to HBA context object.
1564  * @pring: Pointer to driver SLI ring object.
1565  * @iocb: Pointer to iocb slot in the ring.
1566  * @nextiocb: Pointer to driver iocb object which need to be
1567  *            posted to firmware.
1568  *
1569  * This function is called with hbalock held to post a new iocb to
1570  * the firmware. This function copies the new iocb to ring iocb slot and
1571  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1572  * a completion call back for this iocb else the function will free the
1573  * iocb object.
1574  **/
1575 static void
1576 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1577                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1578 {
1579         lockdep_assert_held(&phba->hbalock);
1580         /*
1581          * Set up an iotag
1582          */
1583         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1584
1585
1586         if (pring->ringno == LPFC_ELS_RING) {
1587                 lpfc_debugfs_slow_ring_trc(phba,
1588                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1589                         *(((uint32_t *) &nextiocb->iocb) + 4),
1590                         *(((uint32_t *) &nextiocb->iocb) + 6),
1591                         *(((uint32_t *) &nextiocb->iocb) + 7));
1592         }
1593
1594         /*
1595          * Issue iocb command to adapter
1596          */
1597         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1598         wmb();
1599         pring->stats.iocb_cmd++;
1600
1601         /*
1602          * If there is no completion routine to call, we can release the
1603          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1604          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1605          */
1606         if (nextiocb->iocb_cmpl)
1607                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1608         else
1609                 __lpfc_sli_release_iocbq(phba, nextiocb);
1610
1611         /*
1612          * Let the HBA know what IOCB slot will be the next one the
1613          * driver will put a command into.
1614          */
1615         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1616         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1617 }
1618
1619 /**
1620  * lpfc_sli_update_full_ring - Update the chip attention register
1621  * @phba: Pointer to HBA context object.
1622  * @pring: Pointer to driver SLI ring object.
1623  *
1624  * The caller is not required to hold any lock for calling this function.
1625  * This function updates the chip attention bits for the ring to inform firmware
1626  * that there are pending work to be done for this ring and requests an
1627  * interrupt when there is space available in the ring. This function is
1628  * called when the driver is unable to post more iocbs to the ring due
1629  * to unavailability of space in the ring.
1630  **/
1631 static void
1632 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1633 {
1634         int ringno = pring->ringno;
1635
1636         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1637
1638         wmb();
1639
1640         /*
1641          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1642          * The HBA will tell us when an IOCB entry is available.
1643          */
1644         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1645         readl(phba->CAregaddr); /* flush */
1646
1647         pring->stats.iocb_cmd_full++;
1648 }
1649
1650 /**
1651  * lpfc_sli_update_ring - Update chip attention register
1652  * @phba: Pointer to HBA context object.
1653  * @pring: Pointer to driver SLI ring object.
1654  *
1655  * This function updates the chip attention register bit for the
1656  * given ring to inform HBA that there is more work to be done
1657  * in this ring. The caller is not required to hold any lock.
1658  **/
1659 static void
1660 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1661 {
1662         int ringno = pring->ringno;
1663
1664         /*
1665          * Tell the HBA that there is work to do in this ring.
1666          */
1667         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1668                 wmb();
1669                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1670                 readl(phba->CAregaddr); /* flush */
1671         }
1672 }
1673
1674 /**
1675  * lpfc_sli_resume_iocb - Process iocbs in the txq
1676  * @phba: Pointer to HBA context object.
1677  * @pring: Pointer to driver SLI ring object.
1678  *
1679  * This function is called with hbalock held to post pending iocbs
1680  * in the txq to the firmware. This function is called when driver
1681  * detects space available in the ring.
1682  **/
1683 static void
1684 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1685 {
1686         IOCB_t *iocb;
1687         struct lpfc_iocbq *nextiocb;
1688
1689         lockdep_assert_held(&phba->hbalock);
1690
1691         /*
1692          * Check to see if:
1693          *  (a) there is anything on the txq to send
1694          *  (b) link is up
1695          *  (c) link attention events can be processed (fcp ring only)
1696          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1697          */
1698
1699         if (lpfc_is_link_up(phba) &&
1700             (!list_empty(&pring->txq)) &&
1701             (pring->ringno != LPFC_FCP_RING ||
1702              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1703
1704                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1705                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1706                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1707
1708                 if (iocb)
1709                         lpfc_sli_update_ring(phba, pring);
1710                 else
1711                         lpfc_sli_update_full_ring(phba, pring);
1712         }
1713
1714         return;
1715 }
1716
1717 /**
1718  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1719  * @phba: Pointer to HBA context object.
1720  * @hbqno: HBQ number.
1721  *
1722  * This function is called with hbalock held to get the next
1723  * available slot for the given HBQ. If there is free slot
1724  * available for the HBQ it will return pointer to the next available
1725  * HBQ entry else it will return NULL.
1726  **/
1727 static struct lpfc_hbq_entry *
1728 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1729 {
1730         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1731
1732         lockdep_assert_held(&phba->hbalock);
1733
1734         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1735             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1736                 hbqp->next_hbqPutIdx = 0;
1737
1738         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1739                 uint32_t raw_index = phba->hbq_get[hbqno];
1740                 uint32_t getidx = le32_to_cpu(raw_index);
1741
1742                 hbqp->local_hbqGetIdx = getidx;
1743
1744                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1745                         lpfc_printf_log(phba, KERN_ERR,
1746                                         LOG_SLI | LOG_VPORT,
1747                                         "1802 HBQ %d: local_hbqGetIdx "
1748                                         "%u is > than hbqp->entry_count %u\n",
1749                                         hbqno, hbqp->local_hbqGetIdx,
1750                                         hbqp->entry_count);
1751
1752                         phba->link_state = LPFC_HBA_ERROR;
1753                         return NULL;
1754                 }
1755
1756                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1757                         return NULL;
1758         }
1759
1760         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1761                         hbqp->hbqPutIdx;
1762 }
1763
1764 /**
1765  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1766  * @phba: Pointer to HBA context object.
1767  *
1768  * This function is called with no lock held to free all the
1769  * hbq buffers while uninitializing the SLI interface. It also
1770  * frees the HBQ buffers returned by the firmware but not yet
1771  * processed by the upper layers.
1772  **/
1773 void
1774 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1775 {
1776         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1777         struct hbq_dmabuf *hbq_buf;
1778         unsigned long flags;
1779         int i, hbq_count;
1780
1781         hbq_count = lpfc_sli_hbq_count();
1782         /* Return all memory used by all HBQs */
1783         spin_lock_irqsave(&phba->hbalock, flags);
1784         for (i = 0; i < hbq_count; ++i) {
1785                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1786                                 &phba->hbqs[i].hbq_buffer_list, list) {
1787                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1788                         list_del(&hbq_buf->dbuf.list);
1789                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1790                 }
1791                 phba->hbqs[i].buffer_count = 0;
1792         }
1793
1794         /* Mark the HBQs not in use */
1795         phba->hbq_in_use = 0;
1796         spin_unlock_irqrestore(&phba->hbalock, flags);
1797 }
1798
1799 /**
1800  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1801  * @phba: Pointer to HBA context object.
1802  * @hbqno: HBQ number.
1803  * @hbq_buf: Pointer to HBQ buffer.
1804  *
1805  * This function is called with the hbalock held to post a
1806  * hbq buffer to the firmware. If the function finds an empty
1807  * slot in the HBQ, it will post the buffer. The function will return
1808  * pointer to the hbq entry if it successfully post the buffer
1809  * else it will return NULL.
1810  **/
1811 static int
1812 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1813                          struct hbq_dmabuf *hbq_buf)
1814 {
1815         lockdep_assert_held(&phba->hbalock);
1816         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1817 }
1818
1819 /**
1820  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1821  * @phba: Pointer to HBA context object.
1822  * @hbqno: HBQ number.
1823  * @hbq_buf: Pointer to HBQ buffer.
1824  *
1825  * This function is called with the hbalock held to post a hbq buffer to the
1826  * firmware. If the function finds an empty slot in the HBQ, it will post the
1827  * buffer and place it on the hbq_buffer_list. The function will return zero if
1828  * it successfully post the buffer else it will return an error.
1829  **/
1830 static int
1831 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1832                             struct hbq_dmabuf *hbq_buf)
1833 {
1834         struct lpfc_hbq_entry *hbqe;
1835         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1836
1837         lockdep_assert_held(&phba->hbalock);
1838         /* Get next HBQ entry slot to use */
1839         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1840         if (hbqe) {
1841                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1842
1843                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1844                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1845                 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
1846                 hbqe->bde.tus.f.bdeFlags = 0;
1847                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1848                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1849                                 /* Sync SLIM */
1850                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1851                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1852                                 /* flush */
1853                 readl(phba->hbq_put + hbqno);
1854                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1855                 return 0;
1856         } else
1857                 return -ENOMEM;
1858 }
1859
1860 /**
1861  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1862  * @phba: Pointer to HBA context object.
1863  * @hbqno: HBQ number.
1864  * @hbq_buf: Pointer to HBQ buffer.
1865  *
1866  * This function is called with the hbalock held to post an RQE to the SLI4
1867  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1868  * the hbq_buffer_list and return zero, otherwise it will return an error.
1869  **/
1870 static int
1871 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1872                             struct hbq_dmabuf *hbq_buf)
1873 {
1874         int rc;
1875         struct lpfc_rqe hrqe;
1876         struct lpfc_rqe drqe;
1877         struct lpfc_queue *hrq;
1878         struct lpfc_queue *drq;
1879
1880         if (hbqno != LPFC_ELS_HBQ)
1881                 return 1;
1882         hrq = phba->sli4_hba.hdr_rq;
1883         drq = phba->sli4_hba.dat_rq;
1884
1885         lockdep_assert_held(&phba->hbalock);
1886         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1887         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1888         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1889         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1890         rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
1891         if (rc < 0)
1892                 return rc;
1893         hbq_buf->tag = (rc | (hbqno << 16));
1894         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1895         return 0;
1896 }
1897
1898 /* HBQ for ELS and CT traffic. */
1899 static struct lpfc_hbq_init lpfc_els_hbq = {
1900         .rn = 1,
1901         .entry_count = 256,
1902         .mask_count = 0,
1903         .profile = 0,
1904         .ring_mask = (1 << LPFC_ELS_RING),
1905         .buffer_count = 0,
1906         .init_count = 40,
1907         .add_count = 40,
1908 };
1909
1910 /* Array of HBQs */
1911 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1912         &lpfc_els_hbq,
1913 };
1914
1915 /**
1916  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1917  * @phba: Pointer to HBA context object.
1918  * @hbqno: HBQ number.
1919  * @count: Number of HBQ buffers to be posted.
1920  *
1921  * This function is called with no lock held to post more hbq buffers to the
1922  * given HBQ. The function returns the number of HBQ buffers successfully
1923  * posted.
1924  **/
1925 static int
1926 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1927 {
1928         uint32_t i, posted = 0;
1929         unsigned long flags;
1930         struct hbq_dmabuf *hbq_buffer;
1931         LIST_HEAD(hbq_buf_list);
1932         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1933                 return 0;
1934
1935         if ((phba->hbqs[hbqno].buffer_count + count) >
1936             lpfc_hbq_defs[hbqno]->entry_count)
1937                 count = lpfc_hbq_defs[hbqno]->entry_count -
1938                                         phba->hbqs[hbqno].buffer_count;
1939         if (!count)
1940                 return 0;
1941         /* Allocate HBQ entries */
1942         for (i = 0; i < count; i++) {
1943                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1944                 if (!hbq_buffer)
1945                         break;
1946                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1947         }
1948         /* Check whether HBQ is still in use */
1949         spin_lock_irqsave(&phba->hbalock, flags);
1950         if (!phba->hbq_in_use)
1951                 goto err;
1952         while (!list_empty(&hbq_buf_list)) {
1953                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1954                                  dbuf.list);
1955                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1956                                       (hbqno << 16));
1957                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1958                         phba->hbqs[hbqno].buffer_count++;
1959                         posted++;
1960                 } else
1961                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1962         }
1963         spin_unlock_irqrestore(&phba->hbalock, flags);
1964         return posted;
1965 err:
1966         spin_unlock_irqrestore(&phba->hbalock, flags);
1967         while (!list_empty(&hbq_buf_list)) {
1968                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1969                                  dbuf.list);
1970                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1971         }
1972         return 0;
1973 }
1974
1975 /**
1976  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1977  * @phba: Pointer to HBA context object.
1978  * @qno: HBQ number.
1979  *
1980  * This function posts more buffers to the HBQ. This function
1981  * is called with no lock held. The function returns the number of HBQ entries
1982  * successfully allocated.
1983  **/
1984 int
1985 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1986 {
1987         if (phba->sli_rev == LPFC_SLI_REV4)
1988                 return 0;
1989         else
1990                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1991                                          lpfc_hbq_defs[qno]->add_count);
1992 }
1993
1994 /**
1995  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1996  * @phba: Pointer to HBA context object.
1997  * @qno:  HBQ queue number.
1998  *
1999  * This function is called from SLI initialization code path with
2000  * no lock held to post initial HBQ buffers to firmware. The
2001  * function returns the number of HBQ entries successfully allocated.
2002  **/
2003 static int
2004 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2005 {
2006         if (phba->sli_rev == LPFC_SLI_REV4)
2007                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2008                                         lpfc_hbq_defs[qno]->entry_count);
2009         else
2010                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2011                                          lpfc_hbq_defs[qno]->init_count);
2012 }
2013
2014 /**
2015  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2016  * @phba: Pointer to HBA context object.
2017  * @hbqno: HBQ number.
2018  *
2019  * This function removes the first hbq buffer on an hbq list and returns a
2020  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2021  **/
2022 static struct hbq_dmabuf *
2023 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2024 {
2025         struct lpfc_dmabuf *d_buf;
2026
2027         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2028         if (!d_buf)
2029                 return NULL;
2030         return container_of(d_buf, struct hbq_dmabuf, dbuf);
2031 }
2032
2033 /**
2034  * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2035  * @phba: Pointer to HBA context object.
2036  * @hbqno: HBQ number.
2037  *
2038  * This function removes the first RQ buffer on an RQ buffer list and returns a
2039  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2040  **/
2041 static struct rqb_dmabuf *
2042 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2043 {
2044         struct lpfc_dmabuf *h_buf;
2045         struct lpfc_rqb *rqbp;
2046
2047         rqbp = hrq->rqbp;
2048         list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2049                          struct lpfc_dmabuf, list);
2050         if (!h_buf)
2051                 return NULL;
2052         rqbp->buffer_count--;
2053         return container_of(h_buf, struct rqb_dmabuf, hbuf);
2054 }
2055
2056 /**
2057  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2058  * @phba: Pointer to HBA context object.
2059  * @tag: Tag of the hbq buffer.
2060  *
2061  * This function searches for the hbq buffer associated with the given tag in
2062  * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2063  * otherwise it returns NULL.
2064  **/
2065 static struct hbq_dmabuf *
2066 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2067 {
2068         struct lpfc_dmabuf *d_buf;
2069         struct hbq_dmabuf *hbq_buf;
2070         uint32_t hbqno;
2071
2072         hbqno = tag >> 16;
2073         if (hbqno >= LPFC_MAX_HBQS)
2074                 return NULL;
2075
2076         spin_lock_irq(&phba->hbalock);
2077         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2078                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2079                 if (hbq_buf->tag == tag) {
2080                         spin_unlock_irq(&phba->hbalock);
2081                         return hbq_buf;
2082                 }
2083         }
2084         spin_unlock_irq(&phba->hbalock);
2085         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2086                         "1803 Bad hbq tag. Data: x%x x%x\n",
2087                         tag, phba->hbqs[tag >> 16].buffer_count);
2088         return NULL;
2089 }
2090
2091 /**
2092  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2093  * @phba: Pointer to HBA context object.
2094  * @hbq_buffer: Pointer to HBQ buffer.
2095  *
2096  * This function is called with hbalock. This function gives back
2097  * the hbq buffer to firmware. If the HBQ does not have space to
2098  * post the buffer, it will free the buffer.
2099  **/
2100 void
2101 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2102 {
2103         uint32_t hbqno;
2104
2105         if (hbq_buffer) {
2106                 hbqno = hbq_buffer->tag >> 16;
2107                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2108                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2109         }
2110 }
2111
2112 /**
2113  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2114  * @mbxCommand: mailbox command code.
2115  *
2116  * This function is called by the mailbox event handler function to verify
2117  * that the completed mailbox command is a legitimate mailbox command. If the
2118  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2119  * and the mailbox event handler will take the HBA offline.
2120  **/
2121 static int
2122 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2123 {
2124         uint8_t ret;
2125
2126         switch (mbxCommand) {
2127         case MBX_LOAD_SM:
2128         case MBX_READ_NV:
2129         case MBX_WRITE_NV:
2130         case MBX_WRITE_VPARMS:
2131         case MBX_RUN_BIU_DIAG:
2132         case MBX_INIT_LINK:
2133         case MBX_DOWN_LINK:
2134         case MBX_CONFIG_LINK:
2135         case MBX_CONFIG_RING:
2136         case MBX_RESET_RING:
2137         case MBX_READ_CONFIG:
2138         case MBX_READ_RCONFIG:
2139         case MBX_READ_SPARM:
2140         case MBX_READ_STATUS:
2141         case MBX_READ_RPI:
2142         case MBX_READ_XRI:
2143         case MBX_READ_REV:
2144         case MBX_READ_LNK_STAT:
2145         case MBX_REG_LOGIN:
2146         case MBX_UNREG_LOGIN:
2147         case MBX_CLEAR_LA:
2148         case MBX_DUMP_MEMORY:
2149         case MBX_DUMP_CONTEXT:
2150         case MBX_RUN_DIAGS:
2151         case MBX_RESTART:
2152         case MBX_UPDATE_CFG:
2153         case MBX_DOWN_LOAD:
2154         case MBX_DEL_LD_ENTRY:
2155         case MBX_RUN_PROGRAM:
2156         case MBX_SET_MASK:
2157         case MBX_SET_VARIABLE:
2158         case MBX_UNREG_D_ID:
2159         case MBX_KILL_BOARD:
2160         case MBX_CONFIG_FARP:
2161         case MBX_BEACON:
2162         case MBX_LOAD_AREA:
2163         case MBX_RUN_BIU_DIAG64:
2164         case MBX_CONFIG_PORT:
2165         case MBX_READ_SPARM64:
2166         case MBX_READ_RPI64:
2167         case MBX_REG_LOGIN64:
2168         case MBX_READ_TOPOLOGY:
2169         case MBX_WRITE_WWN:
2170         case MBX_SET_DEBUG:
2171         case MBX_LOAD_EXP_ROM:
2172         case MBX_ASYNCEVT_ENABLE:
2173         case MBX_REG_VPI:
2174         case MBX_UNREG_VPI:
2175         case MBX_HEARTBEAT:
2176         case MBX_PORT_CAPABILITIES:
2177         case MBX_PORT_IOV_CONTROL:
2178         case MBX_SLI4_CONFIG:
2179         case MBX_SLI4_REQ_FTRS:
2180         case MBX_REG_FCFI:
2181         case MBX_UNREG_FCFI:
2182         case MBX_REG_VFI:
2183         case MBX_UNREG_VFI:
2184         case MBX_INIT_VPI:
2185         case MBX_INIT_VFI:
2186         case MBX_RESUME_RPI:
2187         case MBX_READ_EVENT_LOG_STATUS:
2188         case MBX_READ_EVENT_LOG:
2189         case MBX_SECURITY_MGMT:
2190         case MBX_AUTH_PORT:
2191         case MBX_ACCESS_VDATA:
2192                 ret = mbxCommand;
2193                 break;
2194         default:
2195                 ret = MBX_SHUTDOWN;
2196                 break;
2197         }
2198         return ret;
2199 }
2200
2201 /**
2202  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2203  * @phba: Pointer to HBA context object.
2204  * @pmboxq: Pointer to mailbox command.
2205  *
2206  * This is completion handler function for mailbox commands issued from
2207  * lpfc_sli_issue_mbox_wait function. This function is called by the
2208  * mailbox event handler function with no lock held. This function
2209  * will wake up thread waiting on the wait queue pointed by context1
2210  * of the mailbox.
2211  **/
2212 void
2213 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2214 {
2215         wait_queue_head_t *pdone_q;
2216         unsigned long drvr_flag;
2217
2218         /*
2219          * If pdone_q is empty, the driver thread gave up waiting and
2220          * continued running.
2221          */
2222         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2223         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2224         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2225         if (pdone_q)
2226                 wake_up_interruptible(pdone_q);
2227         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2228         return;
2229 }
2230
2231
2232 /**
2233  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2234  * @phba: Pointer to HBA context object.
2235  * @pmb: Pointer to mailbox object.
2236  *
2237  * This function is the default mailbox completion handler. It
2238  * frees the memory resources associated with the completed mailbox
2239  * command. If the completed command is a REG_LOGIN mailbox command,
2240  * this function will issue a UREG_LOGIN to re-claim the RPI.
2241  **/
2242 void
2243 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2244 {
2245         struct lpfc_vport  *vport = pmb->vport;
2246         struct lpfc_dmabuf *mp;
2247         struct lpfc_nodelist *ndlp;
2248         struct Scsi_Host *shost;
2249         uint16_t rpi, vpi;
2250         int rc;
2251
2252         mp = (struct lpfc_dmabuf *) (pmb->context1);
2253
2254         if (mp) {
2255                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2256                 kfree(mp);
2257         }
2258
2259         /*
2260          * If a REG_LOGIN succeeded  after node is destroyed or node
2261          * is in re-discovery driver need to cleanup the RPI.
2262          */
2263         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2264             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2265             !pmb->u.mb.mbxStatus) {
2266                 rpi = pmb->u.mb.un.varWords[0];
2267                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2268                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2269                 pmb->vport = vport;
2270                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2271                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2272                 if (rc != MBX_NOT_FINISHED)
2273                         return;
2274         }
2275
2276         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2277                 !(phba->pport->load_flag & FC_UNLOADING) &&
2278                 !pmb->u.mb.mbxStatus) {
2279                 shost = lpfc_shost_from_vport(vport);
2280                 spin_lock_irq(shost->host_lock);
2281                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2282                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2283                 spin_unlock_irq(shost->host_lock);
2284         }
2285
2286         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2287                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2288                 lpfc_nlp_put(ndlp);
2289                 pmb->context2 = NULL;
2290         }
2291
2292         /* Check security permission status on INIT_LINK mailbox command */
2293         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2294             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2295                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2296                                 "2860 SLI authentication is required "
2297                                 "for INIT_LINK but has not done yet\n");
2298
2299         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2300                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2301         else
2302                 mempool_free(pmb, phba->mbox_mem_pool);
2303 }
2304  /**
2305  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2306  * @phba: Pointer to HBA context object.
2307  * @pmb: Pointer to mailbox object.
2308  *
2309  * This function is the unreg rpi mailbox completion handler. It
2310  * frees the memory resources associated with the completed mailbox
2311  * command. An additional refrenece is put on the ndlp to prevent
2312  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2313  * the unreg mailbox command completes, this routine puts the
2314  * reference back.
2315  *
2316  **/
2317 void
2318 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2319 {
2320         struct lpfc_vport  *vport = pmb->vport;
2321         struct lpfc_nodelist *ndlp;
2322
2323         ndlp = pmb->context1;
2324         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2325                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2326                     (bf_get(lpfc_sli_intf_if_type,
2327                      &phba->sli4_hba.sli_intf) ==
2328                      LPFC_SLI_INTF_IF_TYPE_2)) {
2329                         if (ndlp) {
2330                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2331                                                  "0010 UNREG_LOGIN vpi:%x "
2332                                                  "rpi:%x DID:%x map:%x %p\n",
2333                                                  vport->vpi, ndlp->nlp_rpi,
2334                                                  ndlp->nlp_DID,
2335                                                  ndlp->nlp_usg_map, ndlp);
2336                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2337                                 lpfc_nlp_put(ndlp);
2338                         }
2339                 }
2340         }
2341
2342         mempool_free(pmb, phba->mbox_mem_pool);
2343 }
2344
2345 /**
2346  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2347  * @phba: Pointer to HBA context object.
2348  *
2349  * This function is called with no lock held. This function processes all
2350  * the completed mailbox commands and gives it to upper layers. The interrupt
2351  * service routine processes mailbox completion interrupt and adds completed
2352  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2353  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2354  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2355  * function returns the mailbox commands to the upper layer by calling the
2356  * completion handler function of each mailbox.
2357  **/
2358 int
2359 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2360 {
2361         MAILBOX_t *pmbox;
2362         LPFC_MBOXQ_t *pmb;
2363         int rc;
2364         LIST_HEAD(cmplq);
2365
2366         phba->sli.slistat.mbox_event++;
2367
2368         /* Get all completed mailboxe buffers into the cmplq */
2369         spin_lock_irq(&phba->hbalock);
2370         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2371         spin_unlock_irq(&phba->hbalock);
2372
2373         /* Get a Mailbox buffer to setup mailbox commands for callback */
2374         do {
2375                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2376                 if (pmb == NULL)
2377                         break;
2378
2379                 pmbox = &pmb->u.mb;
2380
2381                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2382                         if (pmb->vport) {
2383                                 lpfc_debugfs_disc_trc(pmb->vport,
2384                                         LPFC_DISC_TRC_MBOX_VPORT,
2385                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2386                                         (uint32_t)pmbox->mbxCommand,
2387                                         pmbox->un.varWords[0],
2388                                         pmbox->un.varWords[1]);
2389                         }
2390                         else {
2391                                 lpfc_debugfs_disc_trc(phba->pport,
2392                                         LPFC_DISC_TRC_MBOX,
2393                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2394                                         (uint32_t)pmbox->mbxCommand,
2395                                         pmbox->un.varWords[0],
2396                                         pmbox->un.varWords[1]);
2397                         }
2398                 }
2399
2400                 /*
2401                  * It is a fatal error if unknown mbox command completion.
2402                  */
2403                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2404                     MBX_SHUTDOWN) {
2405                         /* Unknown mailbox command compl */
2406                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2407                                         "(%d):0323 Unknown Mailbox command "
2408                                         "x%x (x%x/x%x) Cmpl\n",
2409                                         pmb->vport ? pmb->vport->vpi : 0,
2410                                         pmbox->mbxCommand,
2411                                         lpfc_sli_config_mbox_subsys_get(phba,
2412                                                                         pmb),
2413                                         lpfc_sli_config_mbox_opcode_get(phba,
2414                                                                         pmb));
2415                         phba->link_state = LPFC_HBA_ERROR;
2416                         phba->work_hs = HS_FFER3;
2417                         lpfc_handle_eratt(phba);
2418                         continue;
2419                 }
2420
2421                 if (pmbox->mbxStatus) {
2422                         phba->sli.slistat.mbox_stat_err++;
2423                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2424                                 /* Mbox cmd cmpl error - RETRYing */
2425                                 lpfc_printf_log(phba, KERN_INFO,
2426                                         LOG_MBOX | LOG_SLI,
2427                                         "(%d):0305 Mbox cmd cmpl "
2428                                         "error - RETRYing Data: x%x "
2429                                         "(x%x/x%x) x%x x%x x%x\n",
2430                                         pmb->vport ? pmb->vport->vpi : 0,
2431                                         pmbox->mbxCommand,
2432                                         lpfc_sli_config_mbox_subsys_get(phba,
2433                                                                         pmb),
2434                                         lpfc_sli_config_mbox_opcode_get(phba,
2435                                                                         pmb),
2436                                         pmbox->mbxStatus,
2437                                         pmbox->un.varWords[0],
2438                                         pmb->vport->port_state);
2439                                 pmbox->mbxStatus = 0;
2440                                 pmbox->mbxOwner = OWN_HOST;
2441                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2442                                 if (rc != MBX_NOT_FINISHED)
2443                                         continue;
2444                         }
2445                 }
2446
2447                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2448                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2449                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2450                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2451                                 "x%x x%x x%x\n",
2452                                 pmb->vport ? pmb->vport->vpi : 0,
2453                                 pmbox->mbxCommand,
2454                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2455                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2456                                 pmb->mbox_cmpl,
2457                                 *((uint32_t *) pmbox),
2458                                 pmbox->un.varWords[0],
2459                                 pmbox->un.varWords[1],
2460                                 pmbox->un.varWords[2],
2461                                 pmbox->un.varWords[3],
2462                                 pmbox->un.varWords[4],
2463                                 pmbox->un.varWords[5],
2464                                 pmbox->un.varWords[6],
2465                                 pmbox->un.varWords[7],
2466                                 pmbox->un.varWords[8],
2467                                 pmbox->un.varWords[9],
2468                                 pmbox->un.varWords[10]);
2469
2470                 if (pmb->mbox_cmpl)
2471                         pmb->mbox_cmpl(phba,pmb);
2472         } while (1);
2473         return 0;
2474 }
2475
2476 /**
2477  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2478  * @phba: Pointer to HBA context object.
2479  * @pring: Pointer to driver SLI ring object.
2480  * @tag: buffer tag.
2481  *
2482  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2483  * is set in the tag the buffer is posted for a particular exchange,
2484  * the function will return the buffer without replacing the buffer.
2485  * If the buffer is for unsolicited ELS or CT traffic, this function
2486  * returns the buffer and also posts another buffer to the firmware.
2487  **/
2488 static struct lpfc_dmabuf *
2489 lpfc_sli_get_buff(struct lpfc_hba *phba,
2490                   struct lpfc_sli_ring *pring,
2491                   uint32_t tag)
2492 {
2493         struct hbq_dmabuf *hbq_entry;
2494
2495         if (tag & QUE_BUFTAG_BIT)
2496                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2497         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2498         if (!hbq_entry)
2499                 return NULL;
2500         return &hbq_entry->dbuf;
2501 }
2502
2503 /**
2504  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2505  * @phba: Pointer to HBA context object.
2506  * @pring: Pointer to driver SLI ring object.
2507  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2508  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2509  * @fch_type: the type for the first frame of the sequence.
2510  *
2511  * This function is called with no lock held. This function uses the r_ctl and
2512  * type of the received sequence to find the correct callback function to call
2513  * to process the sequence.
2514  **/
2515 static int
2516 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2517                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2518                          uint32_t fch_type)
2519 {
2520         int i;
2521
2522         switch (fch_type) {
2523         case FC_TYPE_NVME:
2524                 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
2525                 return 1;
2526         default:
2527                 break;
2528         }
2529
2530         /* unSolicited Responses */
2531         if (pring->prt[0].profile) {
2532                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2533                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2534                                                                         saveq);
2535                 return 1;
2536         }
2537         /* We must search, based on rctl / type
2538            for the right routine */
2539         for (i = 0; i < pring->num_mask; i++) {
2540                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2541                     (pring->prt[i].type == fch_type)) {
2542                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2543                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2544                                                 (phba, pring, saveq);
2545                         return 1;
2546                 }
2547         }
2548         return 0;
2549 }
2550
2551 /**
2552  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2553  * @phba: Pointer to HBA context object.
2554  * @pring: Pointer to driver SLI ring object.
2555  * @saveq: Pointer to the unsolicited iocb.
2556  *
2557  * This function is called with no lock held by the ring event handler
2558  * when there is an unsolicited iocb posted to the response ring by the
2559  * firmware. This function gets the buffer associated with the iocbs
2560  * and calls the event handler for the ring. This function handles both
2561  * qring buffers and hbq buffers.
2562  * When the function returns 1 the caller can free the iocb object otherwise
2563  * upper layer functions will free the iocb objects.
2564  **/
2565 static int
2566 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2567                             struct lpfc_iocbq *saveq)
2568 {
2569         IOCB_t           * irsp;
2570         WORD5            * w5p;
2571         uint32_t           Rctl, Type;
2572         struct lpfc_iocbq *iocbq;
2573         struct lpfc_dmabuf *dmzbuf;
2574
2575         irsp = &(saveq->iocb);
2576
2577         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2578                 if (pring->lpfc_sli_rcv_async_status)
2579                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2580                 else
2581                         lpfc_printf_log(phba,
2582                                         KERN_WARNING,
2583                                         LOG_SLI,
2584                                         "0316 Ring %d handler: unexpected "
2585                                         "ASYNC_STATUS iocb received evt_code "
2586                                         "0x%x\n",
2587                                         pring->ringno,
2588                                         irsp->un.asyncstat.evt_code);
2589                 return 1;
2590         }
2591
2592         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2593                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2594                 if (irsp->ulpBdeCount > 0) {
2595                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2596                                         irsp->un.ulpWord[3]);
2597                         lpfc_in_buf_free(phba, dmzbuf);
2598                 }
2599
2600                 if (irsp->ulpBdeCount > 1) {
2601                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2602                                         irsp->unsli3.sli3Words[3]);
2603                         lpfc_in_buf_free(phba, dmzbuf);
2604                 }
2605
2606                 if (irsp->ulpBdeCount > 2) {
2607                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2608                                 irsp->unsli3.sli3Words[7]);
2609                         lpfc_in_buf_free(phba, dmzbuf);
2610                 }
2611
2612                 return 1;
2613         }
2614
2615         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2616                 if (irsp->ulpBdeCount != 0) {
2617                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2618                                                 irsp->un.ulpWord[3]);
2619                         if (!saveq->context2)
2620                                 lpfc_printf_log(phba,
2621                                         KERN_ERR,
2622                                         LOG_SLI,
2623                                         "0341 Ring %d Cannot find buffer for "
2624                                         "an unsolicited iocb. tag 0x%x\n",
2625                                         pring->ringno,
2626                                         irsp->un.ulpWord[3]);
2627                 }
2628                 if (irsp->ulpBdeCount == 2) {
2629                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2630                                                 irsp->unsli3.sli3Words[7]);
2631                         if (!saveq->context3)
2632                                 lpfc_printf_log(phba,
2633                                         KERN_ERR,
2634                                         LOG_SLI,
2635                                         "0342 Ring %d Cannot find buffer for an"
2636                                         " unsolicited iocb. tag 0x%x\n",
2637                                         pring->ringno,
2638                                         irsp->unsli3.sli3Words[7]);
2639                 }
2640                 list_for_each_entry(iocbq, &saveq->list, list) {
2641                         irsp = &(iocbq->iocb);
2642                         if (irsp->ulpBdeCount != 0) {
2643                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2644                                                         irsp->un.ulpWord[3]);
2645                                 if (!iocbq->context2)
2646                                         lpfc_printf_log(phba,
2647                                                 KERN_ERR,
2648                                                 LOG_SLI,
2649                                                 "0343 Ring %d Cannot find "
2650                                                 "buffer for an unsolicited iocb"
2651                                                 ". tag 0x%x\n", pring->ringno,
2652                                                 irsp->un.ulpWord[3]);
2653                         }
2654                         if (irsp->ulpBdeCount == 2) {
2655                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2656                                                 irsp->unsli3.sli3Words[7]);
2657                                 if (!iocbq->context3)
2658                                         lpfc_printf_log(phba,
2659                                                 KERN_ERR,
2660                                                 LOG_SLI,
2661                                                 "0344 Ring %d Cannot find "
2662                                                 "buffer for an unsolicited "
2663                                                 "iocb. tag 0x%x\n",
2664                                                 pring->ringno,
2665                                                 irsp->unsli3.sli3Words[7]);
2666                         }
2667                 }
2668         }
2669         if (irsp->ulpBdeCount != 0 &&
2670             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2671              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2672                 int found = 0;
2673
2674                 /* search continue save q for same XRI */
2675                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2676                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2677                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
2678                                 list_add_tail(&saveq->list, &iocbq->list);
2679                                 found = 1;
2680                                 break;
2681                         }
2682                 }
2683                 if (!found)
2684                         list_add_tail(&saveq->clist,
2685                                       &pring->iocb_continue_saveq);
2686                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2687                         list_del_init(&iocbq->clist);
2688                         saveq = iocbq;
2689                         irsp = &(saveq->iocb);
2690                 } else
2691                         return 0;
2692         }
2693         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2694             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2695             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2696                 Rctl = FC_RCTL_ELS_REQ;
2697                 Type = FC_TYPE_ELS;
2698         } else {
2699                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2700                 Rctl = w5p->hcsw.Rctl;
2701                 Type = w5p->hcsw.Type;
2702
2703                 /* Firmware Workaround */
2704                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2705                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2706                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2707                         Rctl = FC_RCTL_ELS_REQ;
2708                         Type = FC_TYPE_ELS;
2709                         w5p->hcsw.Rctl = Rctl;
2710                         w5p->hcsw.Type = Type;
2711                 }
2712         }
2713
2714         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2715                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2716                                 "0313 Ring %d handler: unexpected Rctl x%x "
2717                                 "Type x%x received\n",
2718                                 pring->ringno, Rctl, Type);
2719
2720         return 1;
2721 }
2722
2723 /**
2724  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2725  * @phba: Pointer to HBA context object.
2726  * @pring: Pointer to driver SLI ring object.
2727  * @prspiocb: Pointer to response iocb object.
2728  *
2729  * This function looks up the iocb_lookup table to get the command iocb
2730  * corresponding to the given response iocb using the iotag of the
2731  * response iocb. This function is called with the hbalock held.
2732  * This function returns the command iocb object if it finds the command
2733  * iocb else returns NULL.
2734  **/
2735 static struct lpfc_iocbq *
2736 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2737                       struct lpfc_sli_ring *pring,
2738                       struct lpfc_iocbq *prspiocb)
2739 {
2740         struct lpfc_iocbq *cmd_iocb = NULL;
2741         uint16_t iotag;
2742         lockdep_assert_held(&phba->hbalock);
2743
2744         iotag = prspiocb->iocb.ulpIoTag;
2745
2746         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2747                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2748                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2749                         /* remove from txcmpl queue list */
2750                         list_del_init(&cmd_iocb->list);
2751                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2752                         return cmd_iocb;
2753                 }
2754         }
2755
2756         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2757                         "0317 iotag x%x is out of "
2758                         "range: max iotag x%x wd0 x%x\n",
2759                         iotag, phba->sli.last_iotag,
2760                         *(((uint32_t *) &prspiocb->iocb) + 7));
2761         return NULL;
2762 }
2763
2764 /**
2765  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2766  * @phba: Pointer to HBA context object.
2767  * @pring: Pointer to driver SLI ring object.
2768  * @iotag: IOCB tag.
2769  *
2770  * This function looks up the iocb_lookup table to get the command iocb
2771  * corresponding to the given iotag. This function is called with the
2772  * hbalock held.
2773  * This function returns the command iocb object if it finds the command
2774  * iocb else returns NULL.
2775  **/
2776 static struct lpfc_iocbq *
2777 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2778                              struct lpfc_sli_ring *pring, uint16_t iotag)
2779 {
2780         struct lpfc_iocbq *cmd_iocb = NULL;
2781
2782         lockdep_assert_held(&phba->hbalock);
2783         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2784                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2785                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2786                         /* remove from txcmpl queue list */
2787                         list_del_init(&cmd_iocb->list);
2788                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2789                         return cmd_iocb;
2790                 }
2791         }
2792
2793         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2794                         "0372 iotag x%x lookup error: max iotag (x%x) "
2795                         "iocb_flag x%x\n",
2796                         iotag, phba->sli.last_iotag,
2797                         cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
2798         return NULL;
2799 }
2800
2801 /**
2802  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2803  * @phba: Pointer to HBA context object.
2804  * @pring: Pointer to driver SLI ring object.
2805  * @saveq: Pointer to the response iocb to be processed.
2806  *
2807  * This function is called by the ring event handler for non-fcp
2808  * rings when there is a new response iocb in the response ring.
2809  * The caller is not required to hold any locks. This function
2810  * gets the command iocb associated with the response iocb and
2811  * calls the completion handler for the command iocb. If there
2812  * is no completion handler, the function will free the resources
2813  * associated with command iocb. If the response iocb is for
2814  * an already aborted command iocb, the status of the completion
2815  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2816  * This function always returns 1.
2817  **/
2818 static int
2819 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2820                           struct lpfc_iocbq *saveq)
2821 {
2822         struct lpfc_iocbq *cmdiocbp;
2823         int rc = 1;
2824         unsigned long iflag;
2825
2826         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2827         spin_lock_irqsave(&phba->hbalock, iflag);
2828         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2829         spin_unlock_irqrestore(&phba->hbalock, iflag);
2830
2831         if (cmdiocbp) {
2832                 if (cmdiocbp->iocb_cmpl) {
2833                         /*
2834                          * If an ELS command failed send an event to mgmt
2835                          * application.
2836                          */
2837                         if (saveq->iocb.ulpStatus &&
2838                              (pring->ringno == LPFC_ELS_RING) &&
2839                              (cmdiocbp->iocb.ulpCommand ==
2840                                 CMD_ELS_REQUEST64_CR))
2841                                 lpfc_send_els_failure_event(phba,
2842                                         cmdiocbp, saveq);
2843
2844                         /*
2845                          * Post all ELS completions to the worker thread.
2846                          * All other are passed to the completion callback.
2847                          */
2848                         if (pring->ringno == LPFC_ELS_RING) {
2849                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2850                                     (cmdiocbp->iocb_flag &
2851                                                         LPFC_DRIVER_ABORTED)) {
2852                                         spin_lock_irqsave(&phba->hbalock,
2853                                                           iflag);
2854                                         cmdiocbp->iocb_flag &=
2855                                                 ~LPFC_DRIVER_ABORTED;
2856                                         spin_unlock_irqrestore(&phba->hbalock,
2857                                                                iflag);
2858                                         saveq->iocb.ulpStatus =
2859                                                 IOSTAT_LOCAL_REJECT;
2860                                         saveq->iocb.un.ulpWord[4] =
2861                                                 IOERR_SLI_ABORTED;
2862
2863                                         /* Firmware could still be in progress
2864                                          * of DMAing payload, so don't free data
2865                                          * buffer till after a hbeat.
2866                                          */
2867                                         spin_lock_irqsave(&phba->hbalock,
2868                                                           iflag);
2869                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2870                                         spin_unlock_irqrestore(&phba->hbalock,
2871                                                                iflag);
2872                                 }
2873                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2874                                         if (saveq->iocb_flag &
2875                                             LPFC_EXCHANGE_BUSY) {
2876                                                 /* Set cmdiocb flag for the
2877                                                  * exchange busy so sgl (xri)
2878                                                  * will not be released until
2879                                                  * the abort xri is received
2880                                                  * from hba.
2881                                                  */
2882                                                 spin_lock_irqsave(
2883                                                         &phba->hbalock, iflag);
2884                                                 cmdiocbp->iocb_flag |=
2885                                                         LPFC_EXCHANGE_BUSY;
2886                                                 spin_unlock_irqrestore(
2887                                                         &phba->hbalock, iflag);
2888                                         }
2889                                         if (cmdiocbp->iocb_flag &
2890                                             LPFC_DRIVER_ABORTED) {
2891                                                 /*
2892                                                  * Clear LPFC_DRIVER_ABORTED
2893                                                  * bit in case it was driver
2894                                                  * initiated abort.
2895                                                  */
2896                                                 spin_lock_irqsave(
2897                                                         &phba->hbalock, iflag);
2898                                                 cmdiocbp->iocb_flag &=
2899                                                         ~LPFC_DRIVER_ABORTED;
2900                                                 spin_unlock_irqrestore(
2901                                                         &phba->hbalock, iflag);
2902                                                 cmdiocbp->iocb.ulpStatus =
2903                                                         IOSTAT_LOCAL_REJECT;
2904                                                 cmdiocbp->iocb.un.ulpWord[4] =
2905                                                         IOERR_ABORT_REQUESTED;
2906                                                 /*
2907                                                  * For SLI4, irsiocb contains
2908                                                  * NO_XRI in sli_xritag, it
2909                                                  * shall not affect releasing
2910                                                  * sgl (xri) process.
2911                                                  */
2912                                                 saveq->iocb.ulpStatus =
2913                                                         IOSTAT_LOCAL_REJECT;
2914                                                 saveq->iocb.un.ulpWord[4] =
2915                                                         IOERR_SLI_ABORTED;
2916                                                 spin_lock_irqsave(
2917                                                         &phba->hbalock, iflag);
2918                                                 saveq->iocb_flag |=
2919                                                         LPFC_DELAY_MEM_FREE;
2920                                                 spin_unlock_irqrestore(
2921                                                         &phba->hbalock, iflag);
2922                                         }
2923                                 }
2924                         }
2925                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2926                 } else
2927                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2928         } else {
2929                 /*
2930                  * Unknown initiating command based on the response iotag.
2931                  * This could be the case on the ELS ring because of
2932                  * lpfc_els_abort().
2933                  */
2934                 if (pring->ringno != LPFC_ELS_RING) {
2935                         /*
2936                          * Ring <ringno> handler: unexpected completion IoTag
2937                          * <IoTag>
2938                          */
2939                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2940                                          "0322 Ring %d handler: "
2941                                          "unexpected completion IoTag x%x "
2942                                          "Data: x%x x%x x%x x%x\n",
2943                                          pring->ringno,
2944                                          saveq->iocb.ulpIoTag,
2945                                          saveq->iocb.ulpStatus,
2946                                          saveq->iocb.un.ulpWord[4],
2947                                          saveq->iocb.ulpCommand,
2948                                          saveq->iocb.ulpContext);
2949                 }
2950         }
2951
2952         return rc;
2953 }
2954
2955 /**
2956  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2957  * @phba: Pointer to HBA context object.
2958  * @pring: Pointer to driver SLI ring object.
2959  *
2960  * This function is called from the iocb ring event handlers when
2961  * put pointer is ahead of the get pointer for a ring. This function signal
2962  * an error attention condition to the worker thread and the worker
2963  * thread will transition the HBA to offline state.
2964  **/
2965 static void
2966 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2967 {
2968         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2969         /*
2970          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2971          * rsp ring <portRspMax>
2972          */
2973         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2974                         "0312 Ring %d handler: portRspPut %d "
2975                         "is bigger than rsp ring %d\n",
2976                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2977                         pring->sli.sli3.numRiocb);
2978
2979         phba->link_state = LPFC_HBA_ERROR;
2980
2981         /*
2982          * All error attention handlers are posted to
2983          * worker thread
2984          */
2985         phba->work_ha |= HA_ERATT;
2986         phba->work_hs = HS_FFER3;
2987
2988         lpfc_worker_wake_up(phba);
2989
2990         return;
2991 }
2992
2993 /**
2994  * lpfc_poll_eratt - Error attention polling timer timeout handler
2995  * @ptr: Pointer to address of HBA context object.
2996  *
2997  * This function is invoked by the Error Attention polling timer when the
2998  * timer times out. It will check the SLI Error Attention register for
2999  * possible attention events. If so, it will post an Error Attention event
3000  * and wake up worker thread to process it. Otherwise, it will set up the
3001  * Error Attention polling timer for the next poll.
3002  **/
3003 void lpfc_poll_eratt(unsigned long ptr)
3004 {
3005         struct lpfc_hba *phba;
3006         uint32_t eratt = 0;
3007         uint64_t sli_intr, cnt;
3008
3009         phba = (struct lpfc_hba *)ptr;
3010
3011         /* Here we will also keep track of interrupts per sec of the hba */
3012         sli_intr = phba->sli.slistat.sli_intr;
3013
3014         if (phba->sli.slistat.sli_prev_intr > sli_intr)
3015                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3016                         sli_intr);
3017         else
3018                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3019
3020         /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3021         do_div(cnt, phba->eratt_poll_interval);
3022         phba->sli.slistat.sli_ips = cnt;
3023
3024         phba->sli.slistat.sli_prev_intr = sli_intr;
3025
3026         /* Check chip HA register for error event */
3027         eratt = lpfc_sli_check_eratt(phba);
3028
3029         if (eratt)
3030                 /* Tell the worker thread there is work to do */
3031                 lpfc_worker_wake_up(phba);
3032         else
3033                 /* Restart the timer for next eratt poll */
3034                 mod_timer(&phba->eratt_poll,
3035                           jiffies +
3036                           msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3037         return;
3038 }
3039
3040
3041 /**
3042  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3043  * @phba: Pointer to HBA context object.
3044  * @pring: Pointer to driver SLI ring object.
3045  * @mask: Host attention register mask for this ring.
3046  *
3047  * This function is called from the interrupt context when there is a ring
3048  * event for the fcp ring. The caller does not hold any lock.
3049  * The function processes each response iocb in the response ring until it
3050  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3051  * LE bit set. The function will call the completion handler of the command iocb
3052  * if the response iocb indicates a completion for a command iocb or it is
3053  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3054  * function if this is an unsolicited iocb.
3055  * This routine presumes LPFC_FCP_RING handling and doesn't bother
3056  * to check it explicitly.
3057  */
3058 int
3059 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3060                                 struct lpfc_sli_ring *pring, uint32_t mask)
3061 {
3062         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3063         IOCB_t *irsp = NULL;
3064         IOCB_t *entry = NULL;
3065         struct lpfc_iocbq *cmdiocbq = NULL;
3066         struct lpfc_iocbq rspiocbq;
3067         uint32_t status;
3068         uint32_t portRspPut, portRspMax;
3069         int rc = 1;
3070         lpfc_iocb_type type;
3071         unsigned long iflag;
3072         uint32_t rsp_cmpl = 0;
3073
3074         spin_lock_irqsave(&phba->hbalock, iflag);
3075         pring->stats.iocb_event++;
3076
3077         /*
3078          * The next available response entry should never exceed the maximum
3079          * entries.  If it does, treat it as an adapter hardware error.
3080          */
3081         portRspMax = pring->sli.sli3.numRiocb;
3082         portRspPut = le32_to_cpu(pgp->rspPutInx);
3083         if (unlikely(portRspPut >= portRspMax)) {
3084                 lpfc_sli_rsp_pointers_error(phba, pring);
3085                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3086                 return 1;
3087         }
3088         if (phba->fcp_ring_in_use) {
3089                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3090                 return 1;
3091         } else
3092                 phba->fcp_ring_in_use = 1;
3093
3094         rmb();
3095         while (pring->sli.sli3.rspidx != portRspPut) {
3096                 /*
3097                  * Fetch an entry off the ring and copy it into a local data
3098                  * structure.  The copy involves a byte-swap since the
3099                  * network byte order and pci byte orders are different.
3100                  */
3101                 entry = lpfc_resp_iocb(phba, pring);
3102                 phba->last_completion_time = jiffies;
3103
3104                 if (++pring->sli.sli3.rspidx >= portRspMax)
3105                         pring->sli.sli3.rspidx = 0;
3106
3107                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3108                                       (uint32_t *) &rspiocbq.iocb,
3109                                       phba->iocb_rsp_size);
3110                 INIT_LIST_HEAD(&(rspiocbq.list));
3111                 irsp = &rspiocbq.iocb;
3112
3113                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3114                 pring->stats.iocb_rsp++;
3115                 rsp_cmpl++;
3116
3117                 if (unlikely(irsp->ulpStatus)) {
3118                         /*
3119                          * If resource errors reported from HBA, reduce
3120                          * queuedepths of the SCSI device.
3121                          */
3122                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3123                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3124                              IOERR_NO_RESOURCES)) {
3125                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3126                                 phba->lpfc_rampdown_queue_depth(phba);
3127                                 spin_lock_irqsave(&phba->hbalock, iflag);
3128                         }
3129
3130                         /* Rsp ring <ringno> error: IOCB */
3131                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3132                                         "0336 Rsp Ring %d error: IOCB Data: "
3133                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3134                                         pring->ringno,
3135                                         irsp->un.ulpWord[0],
3136                                         irsp->un.ulpWord[1],
3137                                         irsp->un.ulpWord[2],
3138                                         irsp->un.ulpWord[3],
3139                                         irsp->un.ulpWord[4],
3140                                         irsp->un.ulpWord[5],
3141                                         *(uint32_t *)&irsp->un1,
3142                                         *((uint32_t *)&irsp->un1 + 1));
3143                 }
3144
3145                 switch (type) {
3146                 case LPFC_ABORT_IOCB:
3147                 case LPFC_SOL_IOCB:
3148                         /*
3149                          * Idle exchange closed via ABTS from port.  No iocb
3150                          * resources need to be recovered.
3151                          */
3152                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3153                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3154                                                 "0333 IOCB cmd 0x%x"
3155                                                 " processed. Skipping"
3156                                                 " completion\n",
3157                                                 irsp->ulpCommand);
3158                                 break;
3159                         }
3160
3161                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3162                                                          &rspiocbq);
3163                         if (unlikely(!cmdiocbq))
3164                                 break;
3165                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3166                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3167                         if (cmdiocbq->iocb_cmpl) {
3168                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3170                                                       &rspiocbq);
3171                                 spin_lock_irqsave(&phba->hbalock, iflag);
3172                         }
3173                         break;
3174                 case LPFC_UNSOL_IOCB:
3175                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3176                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3177                         spin_lock_irqsave(&phba->hbalock, iflag);
3178                         break;
3179                 default:
3180                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3181                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3182                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3183                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3184                                        MAX_MSG_DATA);
3185                                 dev_warn(&((phba->pcidev)->dev),
3186                                          "lpfc%d: %s\n",
3187                                          phba->brd_no, adaptermsg);
3188                         } else {
3189                                 /* Unknown IOCB command */
3190                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3191                                                 "0334 Unknown IOCB command "
3192                                                 "Data: x%x, x%x x%x x%x x%x\n",
3193                                                 type, irsp->ulpCommand,
3194                                                 irsp->ulpStatus,
3195                                                 irsp->ulpIoTag,
3196                                                 irsp->ulpContext);
3197                         }
3198                         break;
3199                 }
3200
3201                 /*
3202                  * The response IOCB has been processed.  Update the ring
3203                  * pointer in SLIM.  If the port response put pointer has not
3204                  * been updated, sync the pgp->rspPutInx and fetch the new port
3205                  * response put pointer.
3206                  */
3207                 writel(pring->sli.sli3.rspidx,
3208                         &phba->host_gp[pring->ringno].rspGetInx);
3209
3210                 if (pring->sli.sli3.rspidx == portRspPut)
3211                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3212         }
3213
3214         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3215                 pring->stats.iocb_rsp_full++;
3216                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3217                 writel(status, phba->CAregaddr);
3218                 readl(phba->CAregaddr);
3219         }
3220         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3221                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3222                 pring->stats.iocb_cmd_empty++;
3223
3224                 /* Force update of the local copy of cmdGetInx */
3225                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3226                 lpfc_sli_resume_iocb(phba, pring);
3227
3228                 if ((pring->lpfc_sli_cmd_available))
3229                         (pring->lpfc_sli_cmd_available) (phba, pring);
3230
3231         }
3232
3233         phba->fcp_ring_in_use = 0;
3234         spin_unlock_irqrestore(&phba->hbalock, iflag);
3235         return rc;
3236 }
3237
3238 /**
3239  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3240  * @phba: Pointer to HBA context object.
3241  * @pring: Pointer to driver SLI ring object.
3242  * @rspiocbp: Pointer to driver response IOCB object.
3243  *
3244  * This function is called from the worker thread when there is a slow-path
3245  * response IOCB to process. This function chains all the response iocbs until
3246  * seeing the iocb with the LE bit set. The function will call
3247  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3248  * completion of a command iocb. The function will call the
3249  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3250  * The function frees the resources or calls the completion handler if this
3251  * iocb is an abort completion. The function returns NULL when the response
3252  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3253  * this function shall chain the iocb on to the iocb_continueq and return the
3254  * response iocb passed in.
3255  **/
3256 static struct lpfc_iocbq *
3257 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3258                         struct lpfc_iocbq *rspiocbp)
3259 {
3260         struct lpfc_iocbq *saveq;
3261         struct lpfc_iocbq *cmdiocbp;
3262         struct lpfc_iocbq *next_iocb;
3263         IOCB_t *irsp = NULL;
3264         uint32_t free_saveq;
3265         uint8_t iocb_cmd_type;
3266         lpfc_iocb_type type;
3267         unsigned long iflag;
3268         int rc;
3269
3270         spin_lock_irqsave(&phba->hbalock, iflag);
3271         /* First add the response iocb to the countinueq list */
3272         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3273         pring->iocb_continueq_cnt++;
3274
3275         /* Now, determine whether the list is completed for processing */
3276         irsp = &rspiocbp->iocb;
3277         if (irsp->ulpLe) {
3278                 /*
3279                  * By default, the driver expects to free all resources
3280                  * associated with this iocb completion.
3281                  */
3282                 free_saveq = 1;
3283                 saveq = list_get_first(&pring->iocb_continueq,
3284                                        struct lpfc_iocbq, list);
3285                 irsp = &(saveq->iocb);
3286                 list_del_init(&pring->iocb_continueq);
3287                 pring->iocb_continueq_cnt = 0;
3288
3289                 pring->stats.iocb_rsp++;
3290
3291                 /*
3292                  * If resource errors reported from HBA, reduce
3293                  * queuedepths of the SCSI device.
3294                  */
3295                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3296                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3297                      IOERR_NO_RESOURCES)) {
3298                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3299                         phba->lpfc_rampdown_queue_depth(phba);
3300                         spin_lock_irqsave(&phba->hbalock, iflag);
3301                 }
3302
3303                 if (irsp->ulpStatus) {
3304                         /* Rsp ring <ringno> error: IOCB */
3305                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3306                                         "0328 Rsp Ring %d error: "
3307                                         "IOCB Data: "
3308                                         "x%x x%x x%x x%x "
3309                                         "x%x x%x x%x x%x "
3310                                         "x%x x%x x%x x%x "
3311                                         "x%x x%x x%x x%x\n",
3312                                         pring->ringno,
3313                                         irsp->un.ulpWord[0],
3314                                         irsp->un.ulpWord[1],
3315                                         irsp->un.ulpWord[2],
3316                                         irsp->un.ulpWord[3],
3317                                         irsp->un.ulpWord[4],
3318                                         irsp->un.ulpWord[5],
3319                                         *(((uint32_t *) irsp) + 6),
3320                                         *(((uint32_t *) irsp) + 7),
3321                                         *(((uint32_t *) irsp) + 8),
3322                                         *(((uint32_t *) irsp) + 9),
3323                                         *(((uint32_t *) irsp) + 10),
3324                                         *(((uint32_t *) irsp) + 11),
3325                                         *(((uint32_t *) irsp) + 12),
3326                                         *(((uint32_t *) irsp) + 13),
3327                                         *(((uint32_t *) irsp) + 14),
3328                                         *(((uint32_t *) irsp) + 15));
3329                 }
3330
3331                 /*
3332                  * Fetch the IOCB command type and call the correct completion
3333                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3334                  * get freed back to the lpfc_iocb_list by the discovery
3335                  * kernel thread.
3336                  */
3337                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3338                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3339                 switch (type) {
3340                 case LPFC_SOL_IOCB:
3341                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3342                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3343                         spin_lock_irqsave(&phba->hbalock, iflag);
3344                         break;
3345
3346                 case LPFC_UNSOL_IOCB:
3347                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3348                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3349                         spin_lock_irqsave(&phba->hbalock, iflag);
3350                         if (!rc)
3351                                 free_saveq = 0;
3352                         break;
3353
3354                 case LPFC_ABORT_IOCB:
3355                         cmdiocbp = NULL;
3356                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3357                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3358                                                                  saveq);
3359                         if (cmdiocbp) {
3360                                 /* Call the specified completion routine */
3361                                 if (cmdiocbp->iocb_cmpl) {
3362                                         spin_unlock_irqrestore(&phba->hbalock,
3363                                                                iflag);
3364                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3365                                                               saveq);
3366                                         spin_lock_irqsave(&phba->hbalock,
3367                                                           iflag);
3368                                 } else
3369                                         __lpfc_sli_release_iocbq(phba,
3370                                                                  cmdiocbp);
3371                         }
3372                         break;
3373
3374                 case LPFC_UNKNOWN_IOCB:
3375                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3376                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3377                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3378                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3379                                        MAX_MSG_DATA);
3380                                 dev_warn(&((phba->pcidev)->dev),
3381                                          "lpfc%d: %s\n",
3382                                          phba->brd_no, adaptermsg);
3383                         } else {
3384                                 /* Unknown IOCB command */
3385                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3386                                                 "0335 Unknown IOCB "
3387                                                 "command Data: x%x "
3388                                                 "x%x x%x x%x\n",
3389                                                 irsp->ulpCommand,
3390                                                 irsp->ulpStatus,
3391                                                 irsp->ulpIoTag,
3392                                                 irsp->ulpContext);
3393                         }
3394                         break;
3395                 }
3396
3397                 if (free_saveq) {
3398                         list_for_each_entry_safe(rspiocbp, next_iocb,
3399                                                  &saveq->list, list) {
3400                                 list_del_init(&rspiocbp->list);
3401                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3402                         }
3403                         __lpfc_sli_release_iocbq(phba, saveq);
3404                 }
3405                 rspiocbp = NULL;
3406         }
3407         spin_unlock_irqrestore(&phba->hbalock, iflag);
3408         return rspiocbp;
3409 }
3410
3411 /**
3412  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3413  * @phba: Pointer to HBA context object.
3414  * @pring: Pointer to driver SLI ring object.
3415  * @mask: Host attention register mask for this ring.
3416  *
3417  * This routine wraps the actual slow_ring event process routine from the
3418  * API jump table function pointer from the lpfc_hba struct.
3419  **/
3420 void
3421 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3422                                 struct lpfc_sli_ring *pring, uint32_t mask)
3423 {
3424         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3425 }
3426
3427 /**
3428  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3429  * @phba: Pointer to HBA context object.
3430  * @pring: Pointer to driver SLI ring object.
3431  * @mask: Host attention register mask for this ring.
3432  *
3433  * This function is called from the worker thread when there is a ring event
3434  * for non-fcp rings. The caller does not hold any lock. The function will
3435  * remove each response iocb in the response ring and calls the handle
3436  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3437  **/
3438 static void
3439 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3440                                    struct lpfc_sli_ring *pring, uint32_t mask)
3441 {
3442         struct lpfc_pgp *pgp;
3443         IOCB_t *entry;
3444         IOCB_t *irsp = NULL;
3445         struct lpfc_iocbq *rspiocbp = NULL;
3446         uint32_t portRspPut, portRspMax;
3447         unsigned long iflag;
3448         uint32_t status;
3449
3450         pgp = &phba->port_gp[pring->ringno];
3451         spin_lock_irqsave(&phba->hbalock, iflag);
3452         pring->stats.iocb_event++;
3453
3454         /*
3455          * The next available response entry should never exceed the maximum
3456          * entries.  If it does, treat it as an adapter hardware error.
3457          */
3458         portRspMax = pring->sli.sli3.numRiocb;
3459         portRspPut = le32_to_cpu(pgp->rspPutInx);
3460         if (portRspPut >= portRspMax) {
3461                 /*
3462                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3463                  * rsp ring <portRspMax>
3464                  */
3465                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3466                                 "0303 Ring %d handler: portRspPut %d "
3467                                 "is bigger than rsp ring %d\n",
3468                                 pring->ringno, portRspPut, portRspMax);
3469
3470                 phba->link_state = LPFC_HBA_ERROR;
3471                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3472
3473                 phba->work_hs = HS_FFER3;
3474                 lpfc_handle_eratt(phba);
3475
3476                 return;
3477         }
3478
3479         rmb();
3480         while (pring->sli.sli3.rspidx != portRspPut) {
3481                 /*
3482                  * Build a completion list and call the appropriate handler.
3483                  * The process is to get the next available response iocb, get
3484                  * a free iocb from the list, copy the response data into the
3485                  * free iocb, insert to the continuation list, and update the
3486                  * next response index to slim.  This process makes response
3487                  * iocb's in the ring available to DMA as fast as possible but
3488                  * pays a penalty for a copy operation.  Since the iocb is
3489                  * only 32 bytes, this penalty is considered small relative to
3490                  * the PCI reads for register values and a slim write.  When
3491                  * the ulpLe field is set, the entire Command has been
3492                  * received.
3493                  */
3494                 entry = lpfc_resp_iocb(phba, pring);
3495
3496                 phba->last_completion_time = jiffies;
3497                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3498                 if (rspiocbp == NULL) {
3499                         printk(KERN_ERR "%s: out of buffers! Failing "
3500                                "completion.\n", __func__);
3501                         break;
3502                 }
3503
3504                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3505                                       phba->iocb_rsp_size);
3506                 irsp = &rspiocbp->iocb;
3507
3508                 if (++pring->sli.sli3.rspidx >= portRspMax)
3509                         pring->sli.sli3.rspidx = 0;
3510
3511                 if (pring->ringno == LPFC_ELS_RING) {
3512                         lpfc_debugfs_slow_ring_trc(phba,
3513                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3514                                 *(((uint32_t *) irsp) + 4),
3515                                 *(((uint32_t *) irsp) + 6),
3516                                 *(((uint32_t *) irsp) + 7));
3517                 }
3518
3519                 writel(pring->sli.sli3.rspidx,
3520                         &phba->host_gp[pring->ringno].rspGetInx);
3521
3522                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3523                 /* Handle the response IOCB */
3524                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3525                 spin_lock_irqsave(&phba->hbalock, iflag);
3526
3527                 /*
3528                  * If the port response put pointer has not been updated, sync
3529                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3530                  * response put pointer.
3531                  */
3532                 if (pring->sli.sli3.rspidx == portRspPut) {
3533                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3534                 }
3535         } /* while (pring->sli.sli3.rspidx != portRspPut) */
3536
3537         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3538                 /* At least one response entry has been freed */
3539                 pring->stats.iocb_rsp_full++;
3540                 /* SET RxRE_RSP in Chip Att register */
3541                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3542                 writel(status, phba->CAregaddr);
3543                 readl(phba->CAregaddr); /* flush */
3544         }
3545         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3546                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3547                 pring->stats.iocb_cmd_empty++;
3548
3549                 /* Force update of the local copy of cmdGetInx */
3550                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3551                 lpfc_sli_resume_iocb(phba, pring);
3552
3553                 if ((pring->lpfc_sli_cmd_available))
3554                         (pring->lpfc_sli_cmd_available) (phba, pring);
3555
3556         }
3557
3558         spin_unlock_irqrestore(&phba->hbalock, iflag);
3559         return;
3560 }
3561
3562 /**
3563  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3564  * @phba: Pointer to HBA context object.
3565  * @pring: Pointer to driver SLI ring object.
3566  * @mask: Host attention register mask for this ring.
3567  *
3568  * This function is called from the worker thread when there is a pending
3569  * ELS response iocb on the driver internal slow-path response iocb worker
3570  * queue. The caller does not hold any lock. The function will remove each
3571  * response iocb from the response worker queue and calls the handle
3572  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3573  **/
3574 static void
3575 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3576                                    struct lpfc_sli_ring *pring, uint32_t mask)
3577 {
3578         struct lpfc_iocbq *irspiocbq;
3579         struct hbq_dmabuf *dmabuf;
3580         struct lpfc_cq_event *cq_event;
3581         unsigned long iflag;
3582
3583         spin_lock_irqsave(&phba->hbalock, iflag);
3584         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3585         spin_unlock_irqrestore(&phba->hbalock, iflag);
3586         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3587                 /* Get the response iocb from the head of work queue */
3588                 spin_lock_irqsave(&phba->hbalock, iflag);
3589                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3590                                  cq_event, struct lpfc_cq_event, list);
3591                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3592
3593                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3594                 case CQE_CODE_COMPL_WQE:
3595                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3596                                                  cq_event);
3597                         /* Translate ELS WCQE to response IOCBQ */
3598                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3599                                                                    irspiocbq);
3600                         if (irspiocbq)
3601                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3602                                                            irspiocbq);
3603                         break;
3604                 case CQE_CODE_RECEIVE:
3605                 case CQE_CODE_RECEIVE_V1:
3606                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3607                                               cq_event);
3608                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3609                         break;
3610                 default:
3611                         break;
3612                 }
3613         }
3614 }
3615
3616 /**
3617  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3618  * @phba: Pointer to HBA context object.
3619  * @pring: Pointer to driver SLI ring object.
3620  *
3621  * This function aborts all iocbs in the given ring and frees all the iocb
3622  * objects in txq. This function issues an abort iocb for all the iocb commands
3623  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3624  * the return of this function. The caller is not required to hold any locks.
3625  **/
3626 void
3627 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3628 {
3629         LIST_HEAD(completions);
3630         struct lpfc_iocbq *iocb, *next_iocb;
3631
3632         if (pring->ringno == LPFC_ELS_RING) {
3633                 lpfc_fabric_abort_hba(phba);
3634         }
3635
3636         /* Error everything on txq and txcmplq
3637          * First do the txq.
3638          */
3639         if (phba->sli_rev >= LPFC_SLI_REV4) {
3640                 spin_lock_irq(&pring->ring_lock);
3641                 list_splice_init(&pring->txq, &completions);
3642                 pring->txq_cnt = 0;
3643                 spin_unlock_irq(&pring->ring_lock);
3644
3645                 spin_lock_irq(&phba->hbalock);
3646                 /* Next issue ABTS for everything on the txcmplq */
3647                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3648                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3649                 spin_unlock_irq(&phba->hbalock);
3650         } else {
3651                 spin_lock_irq(&phba->hbalock);
3652                 list_splice_init(&pring->txq, &completions);
3653                 pring->txq_cnt = 0;
3654
3655                 /* Next issue ABTS for everything on the txcmplq */
3656                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3657                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3658                 spin_unlock_irq(&phba->hbalock);
3659         }
3660
3661         /* Cancel all the IOCBs from the completions list */
3662         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3663                               IOERR_SLI_ABORTED);
3664 }
3665
3666 /**
3667  * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3668  * @phba: Pointer to HBA context object.
3669  * @pring: Pointer to driver SLI ring object.
3670  *
3671  * This function aborts all iocbs in the given ring and frees all the iocb
3672  * objects in txq. This function issues an abort iocb for all the iocb commands
3673  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3674  * the return of this function. The caller is not required to hold any locks.
3675  **/
3676 void
3677 lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3678 {
3679         LIST_HEAD(completions);
3680         struct lpfc_iocbq *iocb, *next_iocb;
3681
3682         if (pring->ringno == LPFC_ELS_RING)
3683                 lpfc_fabric_abort_hba(phba);
3684
3685         spin_lock_irq(&phba->hbalock);
3686         /* Next issue ABTS for everything on the txcmplq */
3687         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3688                 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3689         spin_unlock_irq(&phba->hbalock);
3690 }
3691
3692
3693 /**
3694  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3695  * @phba: Pointer to HBA context object.
3696  * @pring: Pointer to driver SLI ring object.
3697  *
3698  * This function aborts all iocbs in FCP rings and frees all the iocb
3699  * objects in txq. This function issues an abort iocb for all the iocb commands
3700  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3701  * the return of this function. The caller is not required to hold any locks.
3702  **/
3703 void
3704 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3705 {
3706         struct lpfc_sli *psli = &phba->sli;
3707         struct lpfc_sli_ring  *pring;
3708         uint32_t i;
3709
3710         /* Look on all the FCP Rings for the iotag */
3711         if (phba->sli_rev >= LPFC_SLI_REV4) {
3712                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3713                         pring = phba->sli4_hba.fcp_wq[i]->pring;
3714                         lpfc_sli_abort_iocb_ring(phba, pring);
3715                 }
3716         } else {
3717                 pring = &psli->sli3_ring[LPFC_FCP_RING];
3718                 lpfc_sli_abort_iocb_ring(phba, pring);
3719         }
3720 }
3721
3722 /**
3723  * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3724  * @phba: Pointer to HBA context object.
3725  *
3726  * This function aborts all wqes in NVME rings. This function issues an
3727  * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3728  * the txcmplq is not guaranteed to complete before the return of this
3729  * function. The caller is not required to hold any locks.
3730  **/
3731 void
3732 lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3733 {
3734         struct lpfc_sli_ring  *pring;
3735         uint32_t i;
3736
3737         if (phba->sli_rev < LPFC_SLI_REV4)
3738                 return;
3739
3740         /* Abort all IO on each NVME ring. */
3741         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3742                 pring = phba->sli4_hba.nvme_wq[i]->pring;
3743                 lpfc_sli_abort_wqe_ring(phba, pring);
3744         }
3745 }
3746
3747
3748 /**
3749  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3750  * @phba: Pointer to HBA context object.
3751  *
3752  * This function flushes all iocbs in the fcp ring and frees all the iocb
3753  * objects in txq and txcmplq. This function will not issue abort iocbs
3754  * for all the iocb commands in txcmplq, they will just be returned with
3755  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3756  * slot has been permanently disabled.
3757  **/
3758 void
3759 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3760 {
3761         LIST_HEAD(txq);
3762         LIST_HEAD(txcmplq);
3763         struct lpfc_sli *psli = &phba->sli;
3764         struct lpfc_sli_ring  *pring;
3765         uint32_t i;
3766
3767         spin_lock_irq(&phba->hbalock);
3768         /* Indicate the I/O queues are flushed */
3769         phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3770         spin_unlock_irq(&phba->hbalock);
3771
3772         /* Look on all the FCP Rings for the iotag */
3773         if (phba->sli_rev >= LPFC_SLI_REV4) {
3774                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3775                         pring = phba->sli4_hba.fcp_wq[i]->pring;
3776
3777                         spin_lock_irq(&pring->ring_lock);
3778                         /* Retrieve everything on txq */
3779                         list_splice_init(&pring->txq, &txq);
3780                         /* Retrieve everything on the txcmplq */
3781                         list_splice_init(&pring->txcmplq, &txcmplq);
3782                         pring->txq_cnt = 0;
3783                         pring->txcmplq_cnt = 0;
3784                         spin_unlock_irq(&pring->ring_lock);
3785
3786                         /* Flush the txq */
3787                         lpfc_sli_cancel_iocbs(phba, &txq,
3788                                               IOSTAT_LOCAL_REJECT,
3789                                               IOERR_SLI_DOWN);
3790                         /* Flush the txcmpq */
3791                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
3792                                               IOSTAT_LOCAL_REJECT,
3793                                               IOERR_SLI_DOWN);
3794                 }
3795         } else {
3796                 pring = &psli->sli3_ring[LPFC_FCP_RING];
3797
3798                 spin_lock_irq(&phba->hbalock);
3799                 /* Retrieve everything on txq */
3800                 list_splice_init(&pring->txq, &txq);
3801                 /* Retrieve everything on the txcmplq */
3802                 list_splice_init(&pring->txcmplq, &txcmplq);
3803                 pring->txq_cnt = 0;
3804                 pring->txcmplq_cnt = 0;
3805                 spin_unlock_irq(&phba->hbalock);
3806
3807                 /* Flush the txq */
3808                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3809                                       IOERR_SLI_DOWN);
3810                 /* Flush the txcmpq */
3811                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3812                                       IOERR_SLI_DOWN);
3813         }
3814 }
3815
3816 /**
3817  * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3818  * @phba: Pointer to HBA context object.
3819  *
3820  * This function flushes all wqes in the nvme rings and frees all resources
3821  * in the txcmplq. This function does not issue abort wqes for the IO
3822  * commands in txcmplq, they will just be returned with
3823  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3824  * slot has been permanently disabled.
3825  **/
3826 void
3827 lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3828 {
3829         LIST_HEAD(txcmplq);
3830         struct lpfc_sli_ring  *pring;
3831         uint32_t i;
3832
3833         if (phba->sli_rev < LPFC_SLI_REV4)
3834                 return;
3835
3836         /* Hint to other driver operations that a flush is in progress. */
3837         spin_lock_irq(&phba->hbalock);
3838         phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3839         spin_unlock_irq(&phba->hbalock);
3840
3841         /* Cycle through all NVME rings and complete each IO with
3842          * a local driver reason code.  This is a flush so no
3843          * abort exchange to FW.
3844          */
3845         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3846                 pring = phba->sli4_hba.nvme_wq[i]->pring;
3847
3848                 /* Retrieve everything on the txcmplq */
3849                 spin_lock_irq(&pring->ring_lock);
3850                 list_splice_init(&pring->txcmplq, &txcmplq);
3851                 pring->txcmplq_cnt = 0;
3852                 spin_unlock_irq(&pring->ring_lock);
3853
3854                 /* Flush the txcmpq &&&PAE */
3855                 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3856                                       IOSTAT_LOCAL_REJECT,
3857                                       IOERR_SLI_DOWN);
3858         }
3859 }
3860
3861 /**
3862  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3863  * @phba: Pointer to HBA context object.
3864  * @mask: Bit mask to be checked.
3865  *
3866  * This function reads the host status register and compares
3867  * with the provided bit mask to check if HBA completed
3868  * the restart. This function will wait in a loop for the
3869  * HBA to complete restart. If the HBA does not restart within
3870  * 15 iterations, the function will reset the HBA again. The
3871  * function returns 1 when HBA fail to restart otherwise returns
3872  * zero.
3873  **/
3874 static int
3875 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3876 {
3877         uint32_t status;
3878         int i = 0;
3879         int retval = 0;
3880
3881         /* Read the HBA Host Status Register */
3882         if (lpfc_readl(phba->HSregaddr, &status))
3883                 return 1;
3884
3885         /*
3886          * Check status register every 100ms for 5 retries, then every
3887          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3888          * every 2.5 sec for 4.
3889          * Break our of the loop if errors occurred during init.
3890          */
3891         while (((status & mask) != mask) &&
3892                !(status & HS_FFERM) &&
3893                i++ < 20) {
3894
3895                 if (i <= 5)
3896                         msleep(10);
3897                 else if (i <= 10)
3898                         msleep(500);
3899                 else
3900                         msleep(2500);
3901
3902                 if (i == 15) {
3903                                 /* Do post */
3904                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3905                         lpfc_sli_brdrestart(phba);
3906                 }
3907                 /* Read the HBA Host Status Register */
3908                 if (lpfc_readl(phba->HSregaddr, &status)) {
3909                         retval = 1;
3910                         break;
3911                 }
3912         }
3913
3914         /* Check to see if any errors occurred during init */
3915         if ((status & HS_FFERM) || (i >= 20)) {
3916                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3917                                 "2751 Adapter failed to restart, "
3918                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3919                                 status,
3920                                 readl(phba->MBslimaddr + 0xa8),
3921                                 readl(phba->MBslimaddr + 0xac));
3922                 phba->link_state = LPFC_HBA_ERROR;
3923                 retval = 1;
3924         }
3925
3926         return retval;
3927 }
3928
3929 /**
3930  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3931  * @phba: Pointer to HBA context object.
3932  * @mask: Bit mask to be checked.
3933  *
3934  * This function checks the host status register to check if HBA is
3935  * ready. This function will wait in a loop for the HBA to be ready
3936  * If the HBA is not ready , the function will will reset the HBA PCI
3937  * function again. The function returns 1 when HBA fail to be ready
3938  * otherwise returns zero.
3939  **/
3940 static int
3941 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3942 {
3943         uint32_t status;
3944         int retval = 0;
3945
3946         /* Read the HBA Host Status Register */
3947         status = lpfc_sli4_post_status_check(phba);
3948
3949         if (status) {
3950                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3951                 lpfc_sli_brdrestart(phba);
3952                 status = lpfc_sli4_post_status_check(phba);
3953         }
3954
3955         /* Check to see if any errors occurred during init */
3956         if (status) {
3957                 phba->link_state = LPFC_HBA_ERROR;
3958                 retval = 1;
3959         } else
3960                 phba->sli4_hba.intr_enable = 0;
3961
3962         return retval;
3963 }
3964
3965 /**
3966  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3967  * @phba: Pointer to HBA context object.
3968  * @mask: Bit mask to be checked.
3969  *
3970  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3971  * from the API jump table function pointer from the lpfc_hba struct.
3972  **/
3973 int
3974 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3975 {
3976         return phba->lpfc_sli_brdready(phba, mask);
3977 }
3978
3979 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3980
3981 /**
3982  * lpfc_reset_barrier - Make HBA ready for HBA reset
3983  * @phba: Pointer to HBA context object.
3984  *
3985  * This function is called before resetting an HBA. This function is called
3986  * with hbalock held and requests HBA to quiesce DMAs before a reset.
3987  **/
3988 void lpfc_reset_barrier(struct lpfc_hba *phba)
3989 {
3990         uint32_t __iomem *resp_buf;
3991         uint32_t __iomem *mbox_buf;
3992         volatile uint32_t mbox;
3993         uint32_t hc_copy, ha_copy, resp_data;
3994         int  i;
3995         uint8_t hdrtype;
3996
3997         lockdep_assert_held(&phba->hbalock);
3998
3999         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4000         if (hdrtype != 0x80 ||
4001             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4002              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4003                 return;
4004
4005         /*
4006          * Tell the other part of the chip to suspend temporarily all
4007          * its DMA activity.
4008          */
4009         resp_buf = phba->MBslimaddr;
4010
4011         /* Disable the error attention */
4012         if (lpfc_readl(phba->HCregaddr, &hc_copy))
4013                 return;
4014         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4015         readl(phba->HCregaddr); /* flush */
4016         phba->link_flag |= LS_IGNORE_ERATT;
4017
4018         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4019                 return;
4020         if (ha_copy & HA_ERATT) {
4021                 /* Clear Chip error bit */
4022                 writel(HA_ERATT, phba->HAregaddr);
4023                 phba->pport->stopped = 1;
4024         }
4025
4026         mbox = 0;
4027         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4028         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4029
4030         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4031         mbox_buf = phba->MBslimaddr;
4032         writel(mbox, mbox_buf);
4033
4034         for (i = 0; i < 50; i++) {
4035                 if (lpfc_readl((resp_buf + 1), &resp_data))
4036                         return;
4037                 if (resp_data != ~(BARRIER_TEST_PATTERN))
4038                         mdelay(1);
4039                 else
4040                         break;
4041         }
4042         resp_data = 0;
4043         if (lpfc_readl((resp_buf + 1), &resp_data))
4044                 return;
4045         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4046                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4047                     phba->pport->stopped)
4048                         goto restore_hc;
4049                 else
4050                         goto clear_errat;
4051         }
4052
4053         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4054         resp_data = 0;
4055         for (i = 0; i < 500; i++) {
4056                 if (lpfc_readl(resp_buf, &resp_data))
4057                         return;
4058                 if (resp_data != mbox)
4059                         mdelay(1);
4060                 else
4061                         break;
4062         }
4063
4064 clear_errat:
4065
4066         while (++i < 500) {
4067                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4068                         return;
4069                 if (!(ha_copy & HA_ERATT))
4070                         mdelay(1);
4071                 else
4072                         break;
4073         }
4074
4075         if (readl(phba->HAregaddr) & HA_ERATT) {
4076                 writel(HA_ERATT, phba->HAregaddr);
4077                 phba->pport->stopped = 1;
4078         }
4079
4080 restore_hc:
4081         phba->link_flag &= ~LS_IGNORE_ERATT;
4082         writel(hc_copy, phba->HCregaddr);
4083         readl(phba->HCregaddr); /* flush */
4084 }
4085
4086 /**
4087  * lpfc_sli_brdkill - Issue a kill_board mailbox command
4088  * @phba: Pointer to HBA context object.
4089  *
4090  * This function issues a kill_board mailbox command and waits for
4091  * the error attention interrupt. This function is called for stopping
4092  * the firmware processing. The caller is not required to hold any
4093  * locks. This function calls lpfc_hba_down_post function to free
4094  * any pending commands after the kill. The function will return 1 when it
4095  * fails to kill the board else will return 0.
4096  **/
4097 int
4098 lpfc_sli_brdkill(struct lpfc_hba *phba)
4099 {
4100         struct lpfc_sli *psli;
4101         LPFC_MBOXQ_t *pmb;
4102         uint32_t status;
4103         uint32_t ha_copy;
4104         int retval;
4105         int i = 0;
4106
4107         psli = &phba->sli;
4108
4109         /* Kill HBA */
4110         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4111                         "0329 Kill HBA Data: x%x x%x\n",
4112                         phba->pport->port_state, psli->sli_flag);
4113
4114         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4115         if (!pmb)
4116                 return 1;
4117
4118         /* Disable the error attention */
4119         spin_lock_irq(&phba->hbalock);
4120         if (lpfc_readl(phba->HCregaddr, &status)) {
4121                 spin_unlock_irq(&phba->hbalock);
4122                 mempool_free(pmb, phba->mbox_mem_pool);
4123                 return 1;
4124         }
4125         status &= ~HC_ERINT_ENA;
4126         writel(status, phba->HCregaddr);
4127         readl(phba->HCregaddr); /* flush */
4128         phba->link_flag |= LS_IGNORE_ERATT;
4129         spin_unlock_irq(&phba->hbalock);
4130
4131         lpfc_kill_board(phba, pmb);
4132         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4133         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4134
4135         if (retval != MBX_SUCCESS) {
4136                 if (retval != MBX_BUSY)
4137                         mempool_free(pmb, phba->mbox_mem_pool);
4138                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4139                                 "2752 KILL_BOARD command failed retval %d\n",
4140                                 retval);
4141                 spin_lock_irq(&phba->hbalock);
4142                 phba->link_flag &= ~LS_IGNORE_ERATT;
4143                 spin_unlock_irq(&phba->hbalock);
4144                 return 1;
4145         }
4146
4147         spin_lock_irq(&phba->hbalock);
4148         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4149         spin_unlock_irq(&phba->hbalock);
4150
4151         mempool_free(pmb, phba->mbox_mem_pool);
4152
4153         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4154          * attention every 100ms for 3 seconds. If we don't get ERATT after
4155          * 3 seconds we still set HBA_ERROR state because the status of the
4156          * board is now undefined.
4157          */
4158         if (lpfc_readl(phba->HAregaddr, &ha_copy))
4159                 return 1;
4160         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4161                 mdelay(100);
4162                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4163                         return 1;
4164         }
4165
4166         del_timer_sync(&psli->mbox_tmo);
4167         if (ha_copy & HA_ERATT) {
4168                 writel(HA_ERATT, phba->HAregaddr);
4169                 phba->pport->stopped = 1;
4170         }
4171         spin_lock_irq(&phba->hbalock);
4172         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4173         psli->mbox_active = NULL;
4174         phba->link_flag &= ~LS_IGNORE_ERATT;
4175         spin_unlock_irq(&phba->hbalock);
4176
4177         lpfc_hba_down_post(phba);
4178         phba->link_state = LPFC_HBA_ERROR;
4179
4180         return ha_copy & HA_ERATT ? 0 : 1;
4181 }
4182
4183 /**
4184  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4185  * @phba: Pointer to HBA context object.
4186  *
4187  * This function resets the HBA by writing HC_INITFF to the control
4188  * register. After the HBA resets, this function resets all the iocb ring
4189  * indices. This function disables PCI layer parity checking during
4190  * the reset.
4191  * This function returns 0 always.
4192  * The caller is not required to hold any locks.
4193  **/
4194 int
4195 lpfc_sli_brdreset(struct lpfc_hba *phba)
4196 {
4197         struct lpfc_sli *psli;
4198         struct lpfc_sli_ring *pring;
4199         uint16_t cfg_value;
4200         int i;
4201
4202         psli = &phba->sli;
4203
4204         /* Reset HBA */
4205         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4206                         "0325 Reset HBA Data: x%x x%x\n",
4207                         phba->pport->port_state, psli->sli_flag);
4208
4209         /* perform board reset */
4210         phba->fc_eventTag = 0;
4211         phba->link_events = 0;
4212         phba->pport->fc_myDID = 0;
4213         phba->pport->fc_prevDID = 0;
4214
4215         /* Turn off parity checking and serr during the physical reset */
4216         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4217         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4218                               (cfg_value &
4219                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4220
4221         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4222
4223         /* Now toggle INITFF bit in the Host Control Register */
4224         writel(HC_INITFF, phba->HCregaddr);
4225         mdelay(1);
4226         readl(phba->HCregaddr); /* flush */
4227         writel(0, phba->HCregaddr);
4228         readl(phba->HCregaddr); /* flush */
4229
4230         /* Restore PCI cmd register */
4231         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4232
4233         /* Initialize relevant SLI info */
4234         for (i = 0; i < psli->num_rings; i++) {
4235                 pring = &psli->sli3_ring[i];
4236                 pring->flag = 0;
4237                 pring->sli.sli3.rspidx = 0;
4238                 pring->sli.sli3.next_cmdidx  = 0;
4239                 pring->sli.sli3.local_getidx = 0;
4240                 pring->sli.sli3.cmdidx = 0;
4241                 pring->missbufcnt = 0;
4242         }
4243
4244         phba->link_state = LPFC_WARM_START;
4245         return 0;
4246 }
4247
4248 /**
4249  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4250  * @phba: Pointer to HBA context object.
4251  *
4252  * This function resets a SLI4 HBA. This function disables PCI layer parity
4253  * checking during resets the device. The caller is not required to hold
4254  * any locks.
4255  *
4256  * This function returns 0 always.
4257  **/
4258 int
4259 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4260 {
4261         struct lpfc_sli *psli = &phba->sli;
4262         uint16_t cfg_value;
4263         int rc = 0;
4264
4265         /* Reset HBA */
4266         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4267                         "0295 Reset HBA Data: x%x x%x x%x\n",
4268                         phba->pport->port_state, psli->sli_flag,
4269                         phba->hba_flag);
4270
4271         /* perform board reset */
4272         phba->fc_eventTag = 0;
4273         phba->link_events = 0;
4274         phba->pport->fc_myDID = 0;
4275         phba->pport->fc_prevDID = 0;
4276
4277         spin_lock_irq(&phba->hbalock);
4278         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4279         phba->fcf.fcf_flag = 0;
4280         spin_unlock_irq(&phba->hbalock);
4281
4282         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4283         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4284                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4285                 return rc;
4286         }
4287
4288         /* Now physically reset the device */
4289         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4290                         "0389 Performing PCI function reset!\n");
4291
4292         /* Turn off parity checking and serr during the physical reset */
4293         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4294         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4295                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4296
4297         /* Perform FCoE PCI function reset before freeing queue memory */
4298         rc = lpfc_pci_function_reset(phba);
4299         lpfc_sli4_queue_destroy(phba);
4300
4301         /* Restore PCI cmd register */
4302         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4303
4304         return rc;
4305 }
4306
4307 /**
4308  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4309  * @phba: Pointer to HBA context object.
4310  *
4311  * This function is called in the SLI initialization code path to
4312  * restart the HBA. The caller is not required to hold any lock.
4313  * This function writes MBX_RESTART mailbox command to the SLIM and
4314  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4315  * function to free any pending commands. The function enables
4316  * POST only during the first initialization. The function returns zero.
4317  * The function does not guarantee completion of MBX_RESTART mailbox
4318  * command before the return of this function.
4319  **/
4320 static int
4321 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4322 {
4323         MAILBOX_t *mb;
4324         struct lpfc_sli *psli;
4325         volatile uint32_t word0;
4326         void __iomem *to_slim;
4327         uint32_t hba_aer_enabled;
4328
4329         spin_lock_irq(&phba->hbalock);
4330
4331         /* Take PCIe device Advanced Error Reporting (AER) state */
4332         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4333
4334         psli = &phba->sli;
4335
4336         /* Restart HBA */
4337         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4338                         "0337 Restart HBA Data: x%x x%x\n",
4339                         phba->pport->port_state, psli->sli_flag);
4340
4341         word0 = 0;
4342         mb = (MAILBOX_t *) &word0;
4343         mb->mbxCommand = MBX_RESTART;
4344         mb->mbxHc = 1;
4345
4346         lpfc_reset_barrier(phba);
4347
4348         to_slim = phba->MBslimaddr;
4349         writel(*(uint32_t *) mb, to_slim);
4350         readl(to_slim); /* flush */
4351
4352         /* Only skip post after fc_ffinit is completed */
4353         if (phba->pport->port_state)
4354                 word0 = 1;      /* This is really setting up word1 */
4355         else
4356                 word0 = 0;      /* This is really setting up word1 */
4357         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4358         writel(*(uint32_t *) mb, to_slim);
4359         readl(to_slim); /* flush */
4360
4361         lpfc_sli_brdreset(phba);
4362         phba->pport->stopped = 0;
4363         phba->link_state = LPFC_INIT_START;
4364         phba->hba_flag = 0;
4365         spin_unlock_irq(&phba->hbalock);
4366
4367         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4368         psli->stats_start = get_seconds();
4369
4370         /* Give the INITFF and Post time to settle. */
4371         mdelay(100);
4372
4373         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4374         if (hba_aer_enabled)
4375                 pci_disable_pcie_error_reporting(phba->pcidev);
4376
4377         lpfc_hba_down_post(phba);
4378
4379         return 0;
4380 }
4381
4382 /**
4383  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4384  * @phba: Pointer to HBA context object.
4385  *
4386  * This function is called in the SLI initialization code path to restart
4387  * a SLI4 HBA. The caller is not required to hold any lock.
4388  * At the end of the function, it calls lpfc_hba_down_post function to
4389  * free any pending commands.
4390  **/
4391 static int
4392 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4393 {
4394         struct lpfc_sli *psli = &phba->sli;
4395         uint32_t hba_aer_enabled;
4396         int rc;
4397
4398         /* Restart HBA */
4399         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4400                         "0296 Restart HBA Data: x%x x%x\n",
4401                         phba->pport->port_state, psli->sli_flag);
4402
4403         /* Take PCIe device Advanced Error Reporting (AER) state */
4404         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4405
4406         rc = lpfc_sli4_brdreset(phba);
4407
4408         spin_lock_irq(&phba->hbalock);
4409         phba->pport->stopped = 0;
4410         phba->link_state = LPFC_INIT_START;
4411         phba->hba_flag = 0;
4412         spin_unlock_irq(&phba->hbalock);
4413
4414         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4415         psli->stats_start = get_seconds();
4416
4417         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4418         if (hba_aer_enabled)
4419                 pci_disable_pcie_error_reporting(phba->pcidev);
4420
4421         lpfc_hba_down_post(phba);
4422
4423         return rc;
4424 }
4425
4426 /**
4427  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4428  * @phba: Pointer to HBA context object.
4429  *
4430  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4431  * API jump table function pointer from the lpfc_hba struct.
4432 **/
4433 int
4434 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4435 {
4436         return phba->lpfc_sli_brdrestart(phba);
4437 }
4438
4439 /**
4440  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4441  * @phba: Pointer to HBA context object.
4442  *
4443  * This function is called after a HBA restart to wait for successful
4444  * restart of the HBA. Successful restart of the HBA is indicated by
4445  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4446  * iteration, the function will restart the HBA again. The function returns
4447  * zero if HBA successfully restarted else returns negative error code.
4448  **/
4449 static int
4450 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4451 {
4452         uint32_t status, i = 0;
4453
4454         /* Read the HBA Host Status Register */
4455         if (lpfc_readl(phba->HSregaddr, &status))
4456                 return -EIO;
4457
4458         /* Check status register to see what current state is */
4459         i = 0;
4460         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4461
4462                 /* Check every 10ms for 10 retries, then every 100ms for 90
4463                  * retries, then every 1 sec for 50 retires for a total of
4464                  * ~60 seconds before reset the board again and check every
4465                  * 1 sec for 50 retries. The up to 60 seconds before the
4466                  * board ready is required by the Falcon FIPS zeroization
4467                  * complete, and any reset the board in between shall cause
4468                  * restart of zeroization, further delay the board ready.
4469                  */
4470                 if (i++ >= 200) {
4471                         /* Adapter failed to init, timeout, status reg
4472                            <status> */
4473                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4474                                         "0436 Adapter failed to init, "
4475                                         "timeout, status reg x%x, "
4476                                         "FW Data: A8 x%x AC x%x\n", status,
4477                                         readl(phba->MBslimaddr + 0xa8),
4478                                         readl(phba->MBslimaddr + 0xac));
4479                         phba->link_state = LPFC_HBA_ERROR;
4480                         return -ETIMEDOUT;
4481                 }
4482
4483                 /* Check to see if any errors occurred during init */
4484                 if (status & HS_FFERM) {
4485                         /* ERROR: During chipset initialization */
4486                         /* Adapter failed to init, chipset, status reg
4487                            <status> */
4488                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4489                                         "0437 Adapter failed to init, "
4490                                         "chipset, status reg x%x, "
4491                                         "FW Data: A8 x%x AC x%x\n", status,
4492                                         readl(phba->MBslimaddr + 0xa8),
4493                                         readl(phba->MBslimaddr + 0xac));
4494                         phba->link_state = LPFC_HBA_ERROR;
4495                         return -EIO;
4496                 }
4497
4498                 if (i <= 10)
4499                         msleep(10);
4500                 else if (i <= 100)
4501                         msleep(100);
4502                 else
4503                         msleep(1000);
4504
4505                 if (i == 150) {
4506                         /* Do post */
4507                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4508                         lpfc_sli_brdrestart(phba);
4509                 }
4510                 /* Read the HBA Host Status Register */
4511                 if (lpfc_readl(phba->HSregaddr, &status))
4512                         return -EIO;
4513         }
4514
4515         /* Check to see if any errors occurred during init */
4516         if (status & HS_FFERM) {
4517                 /* ERROR: During chipset initialization */
4518                 /* Adapter failed to init, chipset, status reg <status> */
4519                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4520                                 "0438 Adapter failed to init, chipset, "
4521                                 "status reg x%x, "
4522                                 "FW Data: A8 x%x AC x%x\n", status,
4523                                 readl(phba->MBslimaddr + 0xa8),
4524                                 readl(phba->MBslimaddr + 0xac));
4525                 phba->link_state = LPFC_HBA_ERROR;
4526                 return -EIO;
4527         }
4528
4529         /* Clear all interrupt enable conditions */
4530         writel(0, phba->HCregaddr);
4531         readl(phba->HCregaddr); /* flush */
4532
4533         /* setup host attn register */
4534         writel(0xffffffff, phba->HAregaddr);
4535         readl(phba->HAregaddr); /* flush */
4536         return 0;
4537 }
4538
4539 /**
4540  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4541  *
4542  * This function calculates and returns the number of HBQs required to be
4543  * configured.
4544  **/
4545 int
4546 lpfc_sli_hbq_count(void)
4547 {
4548         return ARRAY_SIZE(lpfc_hbq_defs);
4549 }
4550
4551 /**
4552  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4553  *
4554  * This function adds the number of hbq entries in every HBQ to get
4555  * the total number of hbq entries required for the HBA and returns
4556  * the total count.
4557  **/
4558 static int
4559 lpfc_sli_hbq_entry_count(void)
4560 {
4561         int  hbq_count = lpfc_sli_hbq_count();
4562         int  count = 0;
4563         int  i;
4564
4565         for (i = 0; i < hbq_count; ++i)
4566                 count += lpfc_hbq_defs[i]->entry_count;
4567         return count;
4568 }
4569
4570 /**
4571  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4572  *
4573  * This function calculates amount of memory required for all hbq entries
4574  * to be configured and returns the total memory required.
4575  **/
4576 int
4577 lpfc_sli_hbq_size(void)
4578 {
4579         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4580 }
4581
4582 /**
4583  * lpfc_sli_hbq_setup - configure and initialize HBQs
4584  * @phba: Pointer to HBA context object.
4585  *
4586  * This function is called during the SLI initialization to configure
4587  * all the HBQs and post buffers to the HBQ. The caller is not
4588  * required to hold any locks. This function will return zero if successful
4589  * else it will return negative error code.
4590  **/
4591 static int
4592 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4593 {
4594         int  hbq_count = lpfc_sli_hbq_count();
4595         LPFC_MBOXQ_t *pmb;
4596         MAILBOX_t *pmbox;
4597         uint32_t hbqno;
4598         uint32_t hbq_entry_index;
4599
4600                                 /* Get a Mailbox buffer to setup mailbox
4601                                  * commands for HBA initialization
4602                                  */
4603         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4604
4605         if (!pmb)
4606                 return -ENOMEM;
4607
4608         pmbox = &pmb->u.mb;
4609
4610         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4611         phba->link_state = LPFC_INIT_MBX_CMDS;
4612         phba->hbq_in_use = 1;
4613
4614         hbq_entry_index = 0;
4615         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4616                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4617                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4618                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4619                 phba->hbqs[hbqno].entry_count =
4620                         lpfc_hbq_defs[hbqno]->entry_count;
4621                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4622                         hbq_entry_index, pmb);
4623                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4624
4625                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4626                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4627                            mbxStatus <status>, ring <num> */
4628
4629                         lpfc_printf_log(phba, KERN_ERR,
4630                                         LOG_SLI | LOG_VPORT,
4631                                         "1805 Adapter failed to init. "
4632                                         "Data: x%x x%x x%x\n",
4633                                         pmbox->mbxCommand,
4634                                         pmbox->mbxStatus, hbqno);
4635
4636                         phba->link_state = LPFC_HBA_ERROR;
4637                         mempool_free(pmb, phba->mbox_mem_pool);
4638                         return -ENXIO;
4639                 }
4640         }
4641         phba->hbq_count = hbq_count;
4642
4643         mempool_free(pmb, phba->mbox_mem_pool);
4644
4645         /* Initially populate or replenish the HBQs */
4646         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4647                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4648         return 0;
4649 }
4650
4651 /**
4652  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4653  * @phba: Pointer to HBA context object.
4654  *
4655  * This function is called during the SLI initialization to configure
4656  * all the HBQs and post buffers to the HBQ. The caller is not
4657  * required to hold any locks. This function will return zero if successful
4658  * else it will return negative error code.
4659  **/
4660 static int
4661 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4662 {
4663         phba->hbq_in_use = 1;
4664         phba->hbqs[LPFC_ELS_HBQ].entry_count =
4665                 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4666         phba->hbq_count = 1;
4667         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4668         /* Initially populate or replenish the HBQs */
4669         return 0;
4670 }
4671
4672 /**
4673  * lpfc_sli_config_port - Issue config port mailbox command
4674  * @phba: Pointer to HBA context object.
4675  * @sli_mode: sli mode - 2/3
4676  *
4677  * This function is called by the sli initialization code path
4678  * to issue config_port mailbox command. This function restarts the
4679  * HBA firmware and issues a config_port mailbox command to configure
4680  * the SLI interface in the sli mode specified by sli_mode
4681  * variable. The caller is not required to hold any locks.
4682  * The function returns 0 if successful, else returns negative error
4683  * code.
4684  **/
4685 int
4686 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4687 {
4688         LPFC_MBOXQ_t *pmb;
4689         uint32_t resetcount = 0, rc = 0, done = 0;
4690
4691         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4692         if (!pmb) {
4693                 phba->link_state = LPFC_HBA_ERROR;
4694                 return -ENOMEM;
4695         }
4696
4697         phba->sli_rev = sli_mode;
4698         while (resetcount < 2 && !done) {
4699                 spin_lock_irq(&phba->hbalock);
4700                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4701                 spin_unlock_irq(&phba->hbalock);
4702                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4703                 lpfc_sli_brdrestart(phba);
4704                 rc = lpfc_sli_chipset_init(phba);
4705                 if (rc)
4706                         break;
4707
4708                 spin_lock_irq(&phba->hbalock);
4709                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4710                 spin_unlock_irq(&phba->hbalock);
4711                 resetcount++;
4712
4713                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4714                  * value of 0 means the call was successful.  Any other
4715                  * nonzero value is a failure, but if ERESTART is returned,
4716                  * the driver may reset the HBA and try again.
4717                  */
4718                 rc = lpfc_config_port_prep(phba);
4719                 if (rc == -ERESTART) {
4720                         phba->link_state = LPFC_LINK_UNKNOWN;
4721                         continue;
4722                 } else if (rc)
4723                         break;
4724
4725                 phba->link_state = LPFC_INIT_MBX_CMDS;
4726                 lpfc_config_port(phba, pmb);
4727                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4728                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4729                                         LPFC_SLI3_HBQ_ENABLED |
4730                                         LPFC_SLI3_CRP_ENABLED |
4731                                         LPFC_SLI3_BG_ENABLED |
4732                                         LPFC_SLI3_DSS_ENABLED);
4733                 if (rc != MBX_SUCCESS) {
4734                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4735                                 "0442 Adapter failed to init, mbxCmd x%x "
4736                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4737                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4738                         spin_lock_irq(&phba->hbalock);
4739                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4740                         spin_unlock_irq(&phba->hbalock);
4741                         rc = -ENXIO;
4742                 } else {
4743                         /* Allow asynchronous mailbox command to go through */
4744                         spin_lock_irq(&phba->hbalock);
4745                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4746                         spin_unlock_irq(&phba->hbalock);
4747                         done = 1;
4748
4749                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4750                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
4751                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4752                                         "3110 Port did not grant ASABT\n");
4753                 }
4754         }
4755         if (!done) {
4756                 rc = -EINVAL;
4757                 goto do_prep_failed;
4758         }
4759         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4760                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4761                         rc = -ENXIO;
4762                         goto do_prep_failed;
4763                 }
4764                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4765                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4766                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4767                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4768                                 phba->max_vpi : phba->max_vports;
4769
4770                 } else
4771                         phba->max_vpi = 0;
4772                 phba->fips_level = 0;
4773                 phba->fips_spec_rev = 0;
4774                 if (pmb->u.mb.un.varCfgPort.gdss) {
4775                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4776                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4777                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4778                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4779                                         "2850 Security Crypto Active. FIPS x%d "
4780                                         "(Spec Rev: x%d)",
4781                                         phba->fips_level, phba->fips_spec_rev);
4782                 }
4783                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4784                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4785                                         "2856 Config Port Security Crypto "
4786                                         "Error: x%x ",
4787                                         pmb->u.mb.un.varCfgPort.sec_err);
4788                 }
4789                 if (pmb->u.mb.un.varCfgPort.gerbm)
4790                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4791                 if (pmb->u.mb.un.varCfgPort.gcrp)
4792                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4793
4794                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4795                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4796
4797                 if (phba->cfg_enable_bg) {
4798                         if (pmb->u.mb.un.varCfgPort.gbg)
4799                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4800                         else
4801                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4802                                                 "0443 Adapter did not grant "
4803                                                 "BlockGuard\n");
4804                 }
4805         } else {
4806                 phba->hbq_get = NULL;
4807                 phba->port_gp = phba->mbox->us.s2.port;
4808                 phba->max_vpi = 0;
4809         }
4810 do_prep_failed:
4811         mempool_free(pmb, phba->mbox_mem_pool);
4812         return rc;
4813 }
4814
4815
4816 /**
4817  * lpfc_sli_hba_setup - SLI initialization function
4818  * @phba: Pointer to HBA context object.
4819  *
4820  * This function is the main SLI initialization function. This function
4821  * is called by the HBA initialization code, HBA reset code and HBA
4822  * error attention handler code. Caller is not required to hold any
4823  * locks. This function issues config_port mailbox command to configure
4824  * the SLI, setup iocb rings and HBQ rings. In the end the function
4825  * calls the config_port_post function to issue init_link mailbox
4826  * command and to start the discovery. The function will return zero
4827  * if successful, else it will return negative error code.
4828  **/
4829 int
4830 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4831 {
4832         uint32_t rc;
4833         int  mode = 3, i;
4834         int longs;
4835
4836         switch (phba->cfg_sli_mode) {
4837         case 2:
4838                 if (phba->cfg_enable_npiv) {
4839                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4840                                 "1824 NPIV enabled: Override sli_mode "
4841                                 "parameter (%d) to auto (0).\n",
4842                                 phba->cfg_sli_mode);
4843                         break;
4844                 }
4845                 mode = 2;
4846                 break;
4847         case 0:
4848         case 3:
4849                 break;
4850         default:
4851                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4852                                 "1819 Unrecognized sli_mode parameter: %d.\n",
4853                                 phba->cfg_sli_mode);
4854
4855                 break;
4856         }
4857         phba->fcp_embed_io = 0; /* SLI4 FC support only */
4858
4859         rc = lpfc_sli_config_port(phba, mode);
4860
4861         if (rc && phba->cfg_sli_mode == 3)
4862                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4863                                 "1820 Unable to select SLI-3.  "
4864                                 "Not supported by adapter.\n");
4865         if (rc && mode != 2)
4866                 rc = lpfc_sli_config_port(phba, 2);
4867         else if (rc && mode == 2)
4868                 rc = lpfc_sli_config_port(phba, 3);
4869         if (rc)
4870                 goto lpfc_sli_hba_setup_error;
4871
4872         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4873         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4874                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4875                 if (!rc) {
4876                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4877                                         "2709 This device supports "
4878                                         "Advanced Error Reporting (AER)\n");
4879                         spin_lock_irq(&phba->hbalock);
4880                         phba->hba_flag |= HBA_AER_ENABLED;
4881                         spin_unlock_irq(&phba->hbalock);
4882                 } else {
4883                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4884                                         "2708 This device does not support "
4885                                         "Advanced Error Reporting (AER): %d\n",
4886                                         rc);
4887                         phba->cfg_aer_support = 0;
4888                 }
4889         }
4890
4891         if (phba->sli_rev == 3) {
4892                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4893                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4894         } else {
4895                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4896                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4897                 phba->sli3_options = 0;
4898         }
4899
4900         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4901                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4902                         phba->sli_rev, phba->max_vpi);
4903         rc = lpfc_sli_ring_map(phba);
4904
4905         if (rc)
4906                 goto lpfc_sli_hba_setup_error;
4907
4908         /* Initialize VPIs. */
4909         if (phba->sli_rev == LPFC_SLI_REV3) {
4910                 /*
4911                  * The VPI bitmask and physical ID array are allocated
4912                  * and initialized once only - at driver load.  A port
4913                  * reset doesn't need to reinitialize this memory.
4914                  */
4915                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4916                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4917                         phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4918                                                   GFP_KERNEL);
4919                         if (!phba->vpi_bmask) {
4920                                 rc = -ENOMEM;
4921                                 goto lpfc_sli_hba_setup_error;
4922                         }
4923
4924                         phba->vpi_ids = kzalloc(
4925                                         (phba->max_vpi+1) * sizeof(uint16_t),
4926                                         GFP_KERNEL);
4927                         if (!phba->vpi_ids) {
4928                                 kfree(phba->vpi_bmask);
4929                                 rc = -ENOMEM;
4930                                 goto lpfc_sli_hba_setup_error;
4931                         }
4932                         for (i = 0; i < phba->max_vpi; i++)
4933                                 phba->vpi_ids[i] = i;
4934                 }
4935         }
4936
4937         /* Init HBQs */
4938         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4939                 rc = lpfc_sli_hbq_setup(phba);
4940                 if (rc)
4941                         goto lpfc_sli_hba_setup_error;
4942         }
4943         spin_lock_irq(&phba->hbalock);
4944         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4945         spin_unlock_irq(&phba->hbalock);
4946
4947         rc = lpfc_config_port_post(phba);
4948         if (rc)
4949                 goto lpfc_sli_hba_setup_error;
4950
4951         return rc;
4952
4953 lpfc_sli_hba_setup_error:
4954         phba->link_state = LPFC_HBA_ERROR;
4955         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4956                         "0445 Firmware initialization failed\n");
4957         return rc;
4958 }
4959
4960 /**
4961  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4962  * @phba: Pointer to HBA context object.
4963  * @mboxq: mailbox pointer.
4964  * This function issue a dump mailbox command to read config region
4965  * 23 and parse the records in the region and populate driver
4966  * data structure.
4967  **/
4968 static int
4969 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4970 {
4971         LPFC_MBOXQ_t *mboxq;
4972         struct lpfc_dmabuf *mp;
4973         struct lpfc_mqe *mqe;
4974         uint32_t data_length;
4975         int rc;
4976
4977         /* Program the default value of vlan_id and fc_map */
4978         phba->valid_vlan = 0;
4979         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4980         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4981         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4982
4983         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4984         if (!mboxq)
4985                 return -ENOMEM;
4986
4987         mqe = &mboxq->u.mqe;
4988         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4989                 rc = -ENOMEM;
4990                 goto out_free_mboxq;
4991         }
4992
4993         mp = (struct lpfc_dmabuf *) mboxq->context1;
4994         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4995
4996         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4997                         "(%d):2571 Mailbox cmd x%x Status x%x "
4998                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4999                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5000                         "CQ: x%x x%x x%x x%x\n",
5001                         mboxq->vport ? mboxq->vport->vpi : 0,
5002                         bf_get(lpfc_mqe_command, mqe),
5003                         bf_get(lpfc_mqe_status, mqe),
5004                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5005                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5006                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5007                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5008                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5009                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5010                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5011                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5012                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5013                         mboxq->mcqe.word0,
5014                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5015                         mboxq->mcqe.trailer);
5016
5017         if (rc) {
5018                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5019                 kfree(mp);
5020                 rc = -EIO;
5021                 goto out_free_mboxq;
5022         }
5023         data_length = mqe->un.mb_words[5];
5024         if (data_length > DMP_RGN23_SIZE) {
5025                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5026                 kfree(mp);
5027                 rc = -EIO;
5028                 goto out_free_mboxq;
5029         }
5030
5031         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5032         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5033         kfree(mp);
5034         rc = 0;
5035
5036 out_free_mboxq:
5037         mempool_free(mboxq, phba->mbox_mem_pool);
5038         return rc;
5039 }
5040
5041 /**
5042  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5043  * @phba: pointer to lpfc hba data structure.
5044  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5045  * @vpd: pointer to the memory to hold resulting port vpd data.
5046  * @vpd_size: On input, the number of bytes allocated to @vpd.
5047  *            On output, the number of data bytes in @vpd.
5048  *
5049  * This routine executes a READ_REV SLI4 mailbox command.  In
5050  * addition, this routine gets the port vpd data.
5051  *
5052  * Return codes
5053  *      0 - successful
5054  *      -ENOMEM - could not allocated memory.
5055  **/
5056 static int
5057 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5058                     uint8_t *vpd, uint32_t *vpd_size)
5059 {
5060         int rc = 0;
5061         uint32_t dma_size;
5062         struct lpfc_dmabuf *dmabuf;
5063         struct lpfc_mqe *mqe;
5064
5065         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5066         if (!dmabuf)
5067                 return -ENOMEM;
5068
5069         /*
5070          * Get a DMA buffer for the vpd data resulting from the READ_REV
5071          * mailbox command.
5072          */
5073         dma_size = *vpd_size;
5074         dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5075                                            &dmabuf->phys, GFP_KERNEL);
5076         if (!dmabuf->virt) {
5077                 kfree(dmabuf);
5078                 return -ENOMEM;
5079         }
5080
5081         /*
5082          * The SLI4 implementation of READ_REV conflicts at word1,
5083          * bits 31:16 and SLI4 adds vpd functionality not present
5084          * in SLI3.  This code corrects the conflicts.
5085          */
5086         lpfc_read_rev(phba, mboxq);
5087         mqe = &mboxq->u.mqe;
5088         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5089         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5090         mqe->un.read_rev.word1 &= 0x0000FFFF;
5091         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5092         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5093
5094         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5095         if (rc) {
5096                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5097                                   dmabuf->virt, dmabuf->phys);
5098                 kfree(dmabuf);
5099                 return -EIO;
5100         }
5101
5102         /*
5103          * The available vpd length cannot be bigger than the
5104          * DMA buffer passed to the port.  Catch the less than
5105          * case and update the caller's size.
5106          */
5107         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5108                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5109
5110         memcpy(vpd, dmabuf->virt, *vpd_size);
5111
5112         dma_free_coherent(&phba->pcidev->dev, dma_size,
5113                           dmabuf->virt, dmabuf->phys);
5114         kfree(dmabuf);
5115         return 0;
5116 }
5117
5118 /**
5119  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5120  * @phba: pointer to lpfc hba data structure.
5121  *
5122  * This routine retrieves SLI4 device physical port name this PCI function
5123  * is attached to.
5124  *
5125  * Return codes
5126  *      0 - successful
5127  *      otherwise - failed to retrieve physical port name
5128  **/
5129 static int
5130 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5131 {
5132         LPFC_MBOXQ_t *mboxq;
5133         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5134         struct lpfc_controller_attribute *cntl_attr;
5135         struct lpfc_mbx_get_port_name *get_port_name;
5136         void *virtaddr = NULL;
5137         uint32_t alloclen, reqlen;
5138         uint32_t shdr_status, shdr_add_status;
5139         union lpfc_sli4_cfg_shdr *shdr;
5140         char cport_name = 0;
5141         int rc;
5142
5143         /* We assume nothing at this point */
5144         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5145         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5146
5147         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5148         if (!mboxq)
5149                 return -ENOMEM;
5150         /* obtain link type and link number via READ_CONFIG */
5151         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5152         lpfc_sli4_read_config(phba);
5153         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5154                 goto retrieve_ppname;
5155
5156         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5157         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5158         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5159                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5160                         LPFC_SLI4_MBX_NEMBED);
5161         if (alloclen < reqlen) {
5162                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5163                                 "3084 Allocated DMA memory size (%d) is "
5164                                 "less than the requested DMA memory size "
5165                                 "(%d)\n", alloclen, reqlen);
5166                 rc = -ENOMEM;
5167                 goto out_free_mboxq;
5168         }
5169         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5170         virtaddr = mboxq->sge_array->addr[0];
5171         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5172         shdr = &mbx_cntl_attr->cfg_shdr;
5173         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5174         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5175         if (shdr_status || shdr_add_status || rc) {
5176                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5177                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5178                                 "rc:x%x, status:x%x, add_status:x%x\n",
5179                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5180                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5181                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5182                                 rc, shdr_status, shdr_add_status);
5183                 rc = -ENXIO;
5184                 goto out_free_mboxq;
5185         }
5186         cntl_attr = &mbx_cntl_attr->cntl_attr;
5187         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5188         phba->sli4_hba.lnk_info.lnk_tp =
5189                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5190         phba->sli4_hba.lnk_info.lnk_no =
5191                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5192         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5193                         "3086 lnk_type:%d, lnk_numb:%d\n",
5194                         phba->sli4_hba.lnk_info.lnk_tp,
5195                         phba->sli4_hba.lnk_info.lnk_no);
5196
5197 retrieve_ppname:
5198         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5199                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5200                 sizeof(struct lpfc_mbx_get_port_name) -
5201                 sizeof(struct lpfc_sli4_cfg_mhdr),
5202                 LPFC_SLI4_MBX_EMBED);
5203         get_port_name = &mboxq->u.mqe.un.get_port_name;
5204         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5205         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5206         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5207                 phba->sli4_hba.lnk_info.lnk_tp);
5208         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5209         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5210         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5211         if (shdr_status || shdr_add_status || rc) {
5212                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5213                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5214                                 "rc:x%x, status:x%x, add_status:x%x\n",
5215                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5216                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5217                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5218                                 rc, shdr_status, shdr_add_status);
5219                 rc = -ENXIO;
5220                 goto out_free_mboxq;
5221         }
5222         switch (phba->sli4_hba.lnk_info.lnk_no) {
5223         case LPFC_LINK_NUMBER_0:
5224                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5225                                 &get_port_name->u.response);
5226                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5227                 break;
5228         case LPFC_LINK_NUMBER_1:
5229                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5230                                 &get_port_name->u.response);
5231                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5232                 break;
5233         case LPFC_LINK_NUMBER_2:
5234                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5235                                 &get_port_name->u.response);
5236                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5237                 break;
5238         case LPFC_LINK_NUMBER_3:
5239                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5240                                 &get_port_name->u.response);
5241                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5242                 break;
5243         default:
5244                 break;
5245         }
5246
5247         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5248                 phba->Port[0] = cport_name;
5249                 phba->Port[1] = '\0';
5250                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5251                                 "3091 SLI get port name: %s\n", phba->Port);
5252         }
5253
5254 out_free_mboxq:
5255         if (rc != MBX_TIMEOUT) {
5256                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5257                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5258                 else
5259                         mempool_free(mboxq, phba->mbox_mem_pool);
5260         }
5261         return rc;
5262 }
5263
5264 /**
5265  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5266  * @phba: pointer to lpfc hba data structure.
5267  *
5268  * This routine is called to explicitly arm the SLI4 device's completion and
5269  * event queues
5270  **/
5271 static void
5272 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5273 {
5274         int qidx;
5275
5276         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5277         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
5278         if (phba->sli4_hba.nvmels_cq)
5279                 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5280                                                 LPFC_QUEUE_REARM);
5281
5282         if (phba->sli4_hba.fcp_cq)
5283                 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5284                         lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5285                                                 LPFC_QUEUE_REARM);
5286
5287         if (phba->sli4_hba.nvme_cq)
5288                 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5289                         lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5290                                                 LPFC_QUEUE_REARM);
5291
5292         if (phba->cfg_fof)
5293                 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5294
5295         if (phba->sli4_hba.hba_eq)
5296                 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5297                         lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5298                                                 LPFC_QUEUE_REARM);
5299
5300         if (phba->nvmet_support) {
5301                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5302                         lpfc_sli4_cq_release(
5303                                 phba->sli4_hba.nvmet_cqset[qidx],
5304                                 LPFC_QUEUE_REARM);
5305                 }
5306         }
5307
5308         if (phba->cfg_fof)
5309                 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
5310 }
5311
5312 /**
5313  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5314  * @phba: Pointer to HBA context object.
5315  * @type: The resource extent type.
5316  * @extnt_count: buffer to hold port available extent count.
5317  * @extnt_size: buffer to hold element count per extent.
5318  *
5319  * This function calls the port and retrievs the number of available
5320  * extents and their size for a particular extent type.
5321  *
5322  * Returns: 0 if successful.  Nonzero otherwise.
5323  **/
5324 int
5325 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5326                                uint16_t *extnt_count, uint16_t *extnt_size)
5327 {
5328         int rc = 0;
5329         uint32_t length;
5330         uint32_t mbox_tmo;
5331         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5332         LPFC_MBOXQ_t *mbox;
5333
5334         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5335         if (!mbox)
5336                 return -ENOMEM;
5337
5338         /* Find out how many extents are available for this resource type */
5339         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5340                   sizeof(struct lpfc_sli4_cfg_mhdr));
5341         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5342                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5343                          length, LPFC_SLI4_MBX_EMBED);
5344
5345         /* Send an extents count of 0 - the GET doesn't use it. */
5346         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5347                                         LPFC_SLI4_MBX_EMBED);
5348         if (unlikely(rc)) {
5349                 rc = -EIO;
5350                 goto err_exit;
5351         }
5352
5353         if (!phba->sli4_hba.intr_enable)
5354                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5355         else {
5356                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5357                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5358         }
5359         if (unlikely(rc)) {
5360                 rc = -EIO;
5361                 goto err_exit;
5362         }
5363
5364         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5365         if (bf_get(lpfc_mbox_hdr_status,
5366                    &rsrc_info->header.cfg_shdr.response)) {
5367                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5368                                 "2930 Failed to get resource extents "
5369                                 "Status 0x%x Add'l Status 0x%x\n",
5370                                 bf_get(lpfc_mbox_hdr_status,
5371                                        &rsrc_info->header.cfg_shdr.response),
5372                                 bf_get(lpfc_mbox_hdr_add_status,
5373                                        &rsrc_info->header.cfg_shdr.response));
5374                 rc = -EIO;
5375                 goto err_exit;
5376         }
5377
5378         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5379                               &rsrc_info->u.rsp);
5380         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5381                              &rsrc_info->u.rsp);
5382
5383         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5384                         "3162 Retrieved extents type-%d from port: count:%d, "
5385                         "size:%d\n", type, *extnt_count, *extnt_size);
5386
5387 err_exit:
5388         mempool_free(mbox, phba->mbox_mem_pool);
5389         return rc;
5390 }
5391
5392 /**
5393  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5394  * @phba: Pointer to HBA context object.
5395  * @type: The extent type to check.
5396  *
5397  * This function reads the current available extents from the port and checks
5398  * if the extent count or extent size has changed since the last access.
5399  * Callers use this routine post port reset to understand if there is a
5400  * extent reprovisioning requirement.
5401  *
5402  * Returns:
5403  *   -Error: error indicates problem.
5404  *   1: Extent count or size has changed.
5405  *   0: No changes.
5406  **/
5407 static int
5408 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5409 {
5410         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5411         uint16_t size_diff, rsrc_ext_size;
5412         int rc = 0;
5413         struct lpfc_rsrc_blks *rsrc_entry;
5414         struct list_head *rsrc_blk_list = NULL;
5415
5416         size_diff = 0;
5417         curr_ext_cnt = 0;
5418         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5419                                             &rsrc_ext_cnt,
5420                                             &rsrc_ext_size);
5421         if (unlikely(rc))
5422                 return -EIO;
5423
5424         switch (type) {
5425         case LPFC_RSC_TYPE_FCOE_RPI:
5426                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5427                 break;
5428         case LPFC_RSC_TYPE_FCOE_VPI:
5429                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5430                 break;
5431         case LPFC_RSC_TYPE_FCOE_XRI:
5432                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5433                 break;
5434         case LPFC_RSC_TYPE_FCOE_VFI:
5435                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5436                 break;
5437         default:
5438                 break;
5439         }
5440
5441         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5442                 curr_ext_cnt++;
5443                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5444                         size_diff++;
5445         }
5446
5447         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5448                 rc = 1;
5449
5450         return rc;
5451 }
5452
5453 /**
5454  * lpfc_sli4_cfg_post_extnts -
5455  * @phba: Pointer to HBA context object.
5456  * @extnt_cnt - number of available extents.
5457  * @type - the extent type (rpi, xri, vfi, vpi).
5458  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5459  * @mbox - pointer to the caller's allocated mailbox structure.
5460  *
5461  * This function executes the extents allocation request.  It also
5462  * takes care of the amount of memory needed to allocate or get the
5463  * allocated extents. It is the caller's responsibility to evaluate
5464  * the response.
5465  *
5466  * Returns:
5467  *   -Error:  Error value describes the condition found.
5468  *   0: if successful
5469  **/
5470 static int
5471 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5472                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5473 {
5474         int rc = 0;
5475         uint32_t req_len;
5476         uint32_t emb_len;
5477         uint32_t alloc_len, mbox_tmo;
5478
5479         /* Calculate the total requested length of the dma memory */
5480         req_len = extnt_cnt * sizeof(uint16_t);
5481
5482         /*
5483          * Calculate the size of an embedded mailbox.  The uint32_t
5484          * accounts for extents-specific word.
5485          */
5486         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5487                 sizeof(uint32_t);
5488
5489         /*
5490          * Presume the allocation and response will fit into an embedded
5491          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5492          */
5493         *emb = LPFC_SLI4_MBX_EMBED;
5494         if (req_len > emb_len) {
5495                 req_len = extnt_cnt * sizeof(uint16_t) +
5496                         sizeof(union lpfc_sli4_cfg_shdr) +
5497                         sizeof(uint32_t);
5498                 *emb = LPFC_SLI4_MBX_NEMBED;
5499         }
5500
5501         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5502                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5503                                      req_len, *emb);
5504         if (alloc_len < req_len) {
5505                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5506                         "2982 Allocated DMA memory size (x%x) is "
5507                         "less than the requested DMA memory "
5508                         "size (x%x)\n", alloc_len, req_len);
5509                 return -ENOMEM;
5510         }
5511         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5512         if (unlikely(rc))
5513                 return -EIO;
5514
5515         if (!phba->sli4_hba.intr_enable)
5516                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5517         else {
5518                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5519                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5520         }
5521
5522         if (unlikely(rc))
5523                 rc = -EIO;
5524         return rc;
5525 }
5526
5527 /**
5528  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5529  * @phba: Pointer to HBA context object.
5530  * @type:  The resource extent type to allocate.
5531  *
5532  * This function allocates the number of elements for the specified
5533  * resource type.
5534  **/
5535 static int
5536 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5537 {
5538         bool emb = false;
5539         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5540         uint16_t rsrc_id, rsrc_start, j, k;
5541         uint16_t *ids;
5542         int i, rc;
5543         unsigned long longs;
5544         unsigned long *bmask;
5545         struct lpfc_rsrc_blks *rsrc_blks;
5546         LPFC_MBOXQ_t *mbox;
5547         uint32_t length;
5548         struct lpfc_id_range *id_array = NULL;
5549         void *virtaddr = NULL;
5550         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5551         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5552         struct list_head *ext_blk_list;
5553
5554         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5555                                             &rsrc_cnt,
5556                                             &rsrc_size);
5557         if (unlikely(rc))
5558                 return -EIO;
5559
5560         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5561                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5562                         "3009 No available Resource Extents "
5563                         "for resource type 0x%x: Count: 0x%x, "
5564                         "Size 0x%x\n", type, rsrc_cnt,
5565                         rsrc_size);
5566                 return -ENOMEM;
5567         }
5568
5569         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5570                         "2903 Post resource extents type-0x%x: "
5571                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5572
5573         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5574         if (!mbox)
5575                 return -ENOMEM;
5576
5577         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5578         if (unlikely(rc)) {
5579                 rc = -EIO;
5580                 goto err_exit;
5581         }
5582
5583         /*
5584          * Figure out where the response is located.  Then get local pointers
5585          * to the response data.  The port does not guarantee to respond to
5586          * all extents counts request so update the local variable with the
5587          * allocated count from the port.
5588          */
5589         if (emb == LPFC_SLI4_MBX_EMBED) {
5590                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5591                 id_array = &rsrc_ext->u.rsp.id[0];
5592                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5593         } else {
5594                 virtaddr = mbox->sge_array->addr[0];
5595                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5596                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5597                 id_array = &n_rsrc->id;
5598         }
5599
5600         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5601         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5602
5603         /*
5604          * Based on the resource size and count, correct the base and max
5605          * resource values.
5606          */
5607         length = sizeof(struct lpfc_rsrc_blks);
5608         switch (type) {
5609         case LPFC_RSC_TYPE_FCOE_RPI:
5610                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5611                                                    sizeof(unsigned long),
5612                                                    GFP_KERNEL);
5613                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5614                         rc = -ENOMEM;
5615                         goto err_exit;
5616                 }
5617                 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5618                                                  sizeof(uint16_t),
5619                                                  GFP_KERNEL);
5620                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5621                         kfree(phba->sli4_hba.rpi_bmask);
5622                         rc = -ENOMEM;
5623                         goto err_exit;
5624                 }
5625
5626                 /*
5627                  * The next_rpi was initialized with the maximum available
5628                  * count but the port may allocate a smaller number.  Catch
5629                  * that case and update the next_rpi.
5630                  */
5631                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5632
5633                 /* Initialize local ptrs for common extent processing later. */
5634                 bmask = phba->sli4_hba.rpi_bmask;
5635                 ids = phba->sli4_hba.rpi_ids;
5636                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5637                 break;
5638         case LPFC_RSC_TYPE_FCOE_VPI:
5639                 phba->vpi_bmask = kzalloc(longs *
5640                                           sizeof(unsigned long),
5641                                           GFP_KERNEL);
5642                 if (unlikely(!phba->vpi_bmask)) {
5643                         rc = -ENOMEM;
5644                         goto err_exit;
5645                 }
5646                 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5647                                          sizeof(uint16_t),
5648                                          GFP_KERNEL);
5649                 if (unlikely(!phba->vpi_ids)) {
5650                         kfree(phba->vpi_bmask);
5651                         rc = -ENOMEM;
5652                         goto err_exit;
5653                 }
5654
5655                 /* Initialize local ptrs for common extent processing later. */
5656                 bmask = phba->vpi_bmask;
5657                 ids = phba->vpi_ids;
5658                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5659                 break;
5660         case LPFC_RSC_TYPE_FCOE_XRI:
5661                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5662                                                    sizeof(unsigned long),
5663                                                    GFP_KERNEL);
5664                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5665                         rc = -ENOMEM;
5666                         goto err_exit;
5667                 }
5668                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5669                 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5670                                                  sizeof(uint16_t),
5671                                                  GFP_KERNEL);
5672                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5673                         kfree(phba->sli4_hba.xri_bmask);
5674                         rc = -ENOMEM;
5675                         goto err_exit;
5676                 }
5677
5678                 /* Initialize local ptrs for common extent processing later. */
5679                 bmask = phba->sli4_hba.xri_bmask;
5680                 ids = phba->sli4_hba.xri_ids;
5681                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5682                 break;
5683         case LPFC_RSC_TYPE_FCOE_VFI:
5684                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5685                                                    sizeof(unsigned long),
5686                                                    GFP_KERNEL);
5687                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5688                         rc = -ENOMEM;
5689                         goto err_exit;
5690                 }
5691                 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5692                                                  sizeof(uint16_t),
5693                                                  GFP_KERNEL);
5694                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5695                         kfree(phba->sli4_hba.vfi_bmask);
5696                         rc = -ENOMEM;
5697                         goto err_exit;
5698                 }
5699
5700                 /* Initialize local ptrs for common extent processing later. */
5701                 bmask = phba->sli4_hba.vfi_bmask;
5702                 ids = phba->sli4_hba.vfi_ids;
5703                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5704                 break;
5705         default:
5706                 /* Unsupported Opcode.  Fail call. */
5707                 id_array = NULL;
5708                 bmask = NULL;
5709                 ids = NULL;
5710                 ext_blk_list = NULL;
5711                 goto err_exit;
5712         }
5713
5714         /*
5715          * Complete initializing the extent configuration with the
5716          * allocated ids assigned to this function.  The bitmask serves
5717          * as an index into the array and manages the available ids.  The
5718          * array just stores the ids communicated to the port via the wqes.
5719          */
5720         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5721                 if ((i % 2) == 0)
5722                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5723                                          &id_array[k]);
5724                 else
5725                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5726                                          &id_array[k]);
5727
5728                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5729                 if (unlikely(!rsrc_blks)) {
5730                         rc = -ENOMEM;
5731                         kfree(bmask);
5732                         kfree(ids);
5733                         goto err_exit;
5734                 }
5735                 rsrc_blks->rsrc_start = rsrc_id;
5736                 rsrc_blks->rsrc_size = rsrc_size;
5737                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5738                 rsrc_start = rsrc_id;
5739                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
5740                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5741                                 lpfc_sli4_get_iocb_cnt(phba);
5742                         phba->sli4_hba.nvme_xri_start =
5743                                 phba->sli4_hba.scsi_xri_start +
5744                                 phba->sli4_hba.scsi_xri_max;
5745                 }
5746
5747                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5748                         ids[j] = rsrc_id;
5749                         rsrc_id++;
5750                         j++;
5751                 }
5752                 /* Entire word processed.  Get next word.*/
5753                 if ((i % 2) == 1)
5754                         k++;
5755         }
5756  err_exit:
5757         lpfc_sli4_mbox_cmd_free(phba, mbox);
5758         return rc;
5759 }
5760
5761
5762
5763 /**
5764  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5765  * @phba: Pointer to HBA context object.
5766  * @type: the extent's type.
5767  *
5768  * This function deallocates all extents of a particular resource type.
5769  * SLI4 does not allow for deallocating a particular extent range.  It
5770  * is the caller's responsibility to release all kernel memory resources.
5771  **/
5772 static int
5773 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5774 {
5775         int rc;
5776         uint32_t length, mbox_tmo = 0;
5777         LPFC_MBOXQ_t *mbox;
5778         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5779         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5780
5781         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5782         if (!mbox)
5783                 return -ENOMEM;
5784
5785         /*
5786          * This function sends an embedded mailbox because it only sends the
5787          * the resource type.  All extents of this type are released by the
5788          * port.
5789          */
5790         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5791                   sizeof(struct lpfc_sli4_cfg_mhdr));
5792         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5793                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5794                          length, LPFC_SLI4_MBX_EMBED);
5795
5796         /* Send an extents count of 0 - the dealloc doesn't use it. */
5797         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5798                                         LPFC_SLI4_MBX_EMBED);
5799         if (unlikely(rc)) {
5800                 rc = -EIO;
5801                 goto out_free_mbox;
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         if (unlikely(rc)) {
5810                 rc = -EIO;
5811                 goto out_free_mbox;
5812         }
5813
5814         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5815         if (bf_get(lpfc_mbox_hdr_status,
5816                    &dealloc_rsrc->header.cfg_shdr.response)) {
5817                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5818                                 "2919 Failed to release resource extents "
5819                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
5820                                 "Resource memory not released.\n",
5821                                 type,
5822                                 bf_get(lpfc_mbox_hdr_status,
5823                                     &dealloc_rsrc->header.cfg_shdr.response),
5824                                 bf_get(lpfc_mbox_hdr_add_status,
5825                                     &dealloc_rsrc->header.cfg_shdr.response));
5826                 rc = -EIO;
5827                 goto out_free_mbox;
5828         }
5829
5830         /* Release kernel memory resources for the specific type. */
5831         switch (type) {
5832         case LPFC_RSC_TYPE_FCOE_VPI:
5833                 kfree(phba->vpi_bmask);
5834                 kfree(phba->vpi_ids);
5835                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5836                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5837                                     &phba->lpfc_vpi_blk_list, list) {
5838                         list_del_init(&rsrc_blk->list);
5839                         kfree(rsrc_blk);
5840                 }
5841                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5842                 break;
5843         case LPFC_RSC_TYPE_FCOE_XRI:
5844                 kfree(phba->sli4_hba.xri_bmask);
5845                 kfree(phba->sli4_hba.xri_ids);
5846                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5847                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
5848                         list_del_init(&rsrc_blk->list);
5849                         kfree(rsrc_blk);
5850                 }
5851                 break;
5852         case LPFC_RSC_TYPE_FCOE_VFI:
5853                 kfree(phba->sli4_hba.vfi_bmask);
5854                 kfree(phba->sli4_hba.vfi_ids);
5855                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5856                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5857                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5858                         list_del_init(&rsrc_blk->list);
5859                         kfree(rsrc_blk);
5860                 }
5861                 break;
5862         case LPFC_RSC_TYPE_FCOE_RPI:
5863                 /* RPI bitmask and physical id array are cleaned up earlier. */
5864                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5865                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5866                         list_del_init(&rsrc_blk->list);
5867                         kfree(rsrc_blk);
5868                 }
5869                 break;
5870         default:
5871                 break;
5872         }
5873
5874         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5875
5876  out_free_mbox:
5877         mempool_free(mbox, phba->mbox_mem_pool);
5878         return rc;
5879 }
5880
5881 static void
5882 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5883                   uint32_t feature)
5884 {
5885         uint32_t len;
5886
5887         len = sizeof(struct lpfc_mbx_set_feature) -
5888                 sizeof(struct lpfc_sli4_cfg_mhdr);
5889         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5890                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
5891                          LPFC_SLI4_MBX_EMBED);
5892
5893         switch (feature) {
5894         case LPFC_SET_UE_RECOVERY:
5895                 bf_set(lpfc_mbx_set_feature_UER,
5896                        &mbox->u.mqe.un.set_feature, 1);
5897                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5898                 mbox->u.mqe.un.set_feature.param_len = 8;
5899                 break;
5900         case LPFC_SET_MDS_DIAGS:
5901                 bf_set(lpfc_mbx_set_feature_mds,
5902                        &mbox->u.mqe.un.set_feature, 1);
5903                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5904                        &mbox->u.mqe.un.set_feature, 0);
5905                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5906                 mbox->u.mqe.un.set_feature.param_len = 8;
5907                 break;
5908         }
5909
5910         return;
5911 }
5912
5913 /**
5914  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5915  * @phba: Pointer to HBA context object.
5916  *
5917  * This function allocates all SLI4 resource identifiers.
5918  **/
5919 int
5920 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5921 {
5922         int i, rc, error = 0;
5923         uint16_t count, base;
5924         unsigned long longs;
5925
5926         if (!phba->sli4_hba.rpi_hdrs_in_use)
5927                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5928         if (phba->sli4_hba.extents_in_use) {
5929                 /*
5930                  * The port supports resource extents. The XRI, VPI, VFI, RPI
5931                  * resource extent count must be read and allocated before
5932                  * provisioning the resource id arrays.
5933                  */
5934                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5935                     LPFC_IDX_RSRC_RDY) {
5936                         /*
5937                          * Extent-based resources are set - the driver could
5938                          * be in a port reset. Figure out if any corrective
5939                          * actions need to be taken.
5940                          */
5941                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5942                                                  LPFC_RSC_TYPE_FCOE_VFI);
5943                         if (rc != 0)
5944                                 error++;
5945                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5946                                                  LPFC_RSC_TYPE_FCOE_VPI);
5947                         if (rc != 0)
5948                                 error++;
5949                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5950                                                  LPFC_RSC_TYPE_FCOE_XRI);
5951                         if (rc != 0)
5952                                 error++;
5953                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5954                                                  LPFC_RSC_TYPE_FCOE_RPI);
5955                         if (rc != 0)
5956                                 error++;
5957
5958                         /*
5959                          * It's possible that the number of resources
5960                          * provided to this port instance changed between
5961                          * resets.  Detect this condition and reallocate
5962                          * resources.  Otherwise, there is no action.
5963                          */
5964                         if (error) {
5965                                 lpfc_printf_log(phba, KERN_INFO,
5966                                                 LOG_MBOX | LOG_INIT,
5967                                                 "2931 Detected extent resource "
5968                                                 "change.  Reallocating all "
5969                                                 "extents.\n");
5970                                 rc = lpfc_sli4_dealloc_extent(phba,
5971                                                  LPFC_RSC_TYPE_FCOE_VFI);
5972                                 rc = lpfc_sli4_dealloc_extent(phba,
5973                                                  LPFC_RSC_TYPE_FCOE_VPI);
5974                                 rc = lpfc_sli4_dealloc_extent(phba,
5975                                                  LPFC_RSC_TYPE_FCOE_XRI);
5976                                 rc = lpfc_sli4_dealloc_extent(phba,
5977                                                  LPFC_RSC_TYPE_FCOE_RPI);
5978                         } else
5979                                 return 0;
5980                 }
5981
5982                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5983                 if (unlikely(rc))
5984                         goto err_exit;
5985
5986                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5987                 if (unlikely(rc))
5988                         goto err_exit;
5989
5990                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5991                 if (unlikely(rc))
5992                         goto err_exit;
5993
5994                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5995                 if (unlikely(rc))
5996                         goto err_exit;
5997                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5998                        LPFC_IDX_RSRC_RDY);
5999                 return rc;
6000         } else {
6001                 /*
6002                  * The port does not support resource extents.  The XRI, VPI,
6003                  * VFI, RPI resource ids were determined from READ_CONFIG.
6004                  * Just allocate the bitmasks and provision the resource id
6005                  * arrays.  If a port reset is active, the resources don't
6006                  * need any action - just exit.
6007                  */
6008                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6009                     LPFC_IDX_RSRC_RDY) {
6010                         lpfc_sli4_dealloc_resource_identifiers(phba);
6011                         lpfc_sli4_remove_rpis(phba);
6012                 }
6013                 /* RPIs. */
6014                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6015                 if (count <= 0) {
6016                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6017                                         "3279 Invalid provisioning of "
6018                                         "rpi:%d\n", count);
6019                         rc = -EINVAL;
6020                         goto err_exit;
6021                 }
6022                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6023                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6024                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6025                                                    sizeof(unsigned long),
6026                                                    GFP_KERNEL);
6027                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6028                         rc = -ENOMEM;
6029                         goto err_exit;
6030                 }
6031                 phba->sli4_hba.rpi_ids = kzalloc(count *
6032                                                  sizeof(uint16_t),
6033                                                  GFP_KERNEL);
6034                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6035                         rc = -ENOMEM;
6036                         goto free_rpi_bmask;
6037                 }
6038
6039                 for (i = 0; i < count; i++)
6040                         phba->sli4_hba.rpi_ids[i] = base + i;
6041
6042                 /* VPIs. */
6043                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6044                 if (count <= 0) {
6045                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6046                                         "3280 Invalid provisioning of "
6047                                         "vpi:%d\n", count);
6048                         rc = -EINVAL;
6049                         goto free_rpi_ids;
6050                 }
6051                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6052                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6053                 phba->vpi_bmask = kzalloc(longs *
6054                                           sizeof(unsigned long),
6055                                           GFP_KERNEL);
6056                 if (unlikely(!phba->vpi_bmask)) {
6057                         rc = -ENOMEM;
6058                         goto free_rpi_ids;
6059                 }
6060                 phba->vpi_ids = kzalloc(count *
6061                                         sizeof(uint16_t),
6062                                         GFP_KERNEL);
6063                 if (unlikely(!phba->vpi_ids)) {
6064                         rc = -ENOMEM;
6065                         goto free_vpi_bmask;
6066                 }
6067
6068                 for (i = 0; i < count; i++)
6069                         phba->vpi_ids[i] = base + i;
6070
6071                 /* XRIs. */
6072                 count = phba->sli4_hba.max_cfg_param.max_xri;
6073                 if (count <= 0) {
6074                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6075                                         "3281 Invalid provisioning of "
6076                                         "xri:%d\n", count);
6077                         rc = -EINVAL;
6078                         goto free_vpi_ids;
6079                 }
6080                 base = phba->sli4_hba.max_cfg_param.xri_base;
6081                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6082                 phba->sli4_hba.xri_bmask = kzalloc(longs *
6083                                                    sizeof(unsigned long),
6084                                                    GFP_KERNEL);
6085                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6086                         rc = -ENOMEM;
6087                         goto free_vpi_ids;
6088                 }
6089                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6090                 phba->sli4_hba.xri_ids = kzalloc(count *
6091                                                  sizeof(uint16_t),
6092                                                  GFP_KERNEL);
6093                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6094                         rc = -ENOMEM;
6095                         goto free_xri_bmask;
6096                 }
6097
6098                 for (i = 0; i < count; i++)
6099                         phba->sli4_hba.xri_ids[i] = base + i;
6100
6101                 /* VFIs. */
6102                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6103                 if (count <= 0) {
6104                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6105                                         "3282 Invalid provisioning of "
6106                                         "vfi:%d\n", count);
6107                         rc = -EINVAL;
6108                         goto free_xri_ids;
6109                 }
6110                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6111                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6112                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6113                                                    sizeof(unsigned long),
6114                                                    GFP_KERNEL);
6115                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6116                         rc = -ENOMEM;
6117                         goto free_xri_ids;
6118                 }
6119                 phba->sli4_hba.vfi_ids = kzalloc(count *
6120                                                  sizeof(uint16_t),
6121                                                  GFP_KERNEL);
6122                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6123                         rc = -ENOMEM;
6124                         goto free_vfi_bmask;
6125                 }
6126
6127                 for (i = 0; i < count; i++)
6128                         phba->sli4_hba.vfi_ids[i] = base + i;
6129
6130                 /*
6131                  * Mark all resources ready.  An HBA reset doesn't need
6132                  * to reset the initialization.
6133                  */
6134                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6135                        LPFC_IDX_RSRC_RDY);
6136                 return 0;
6137         }
6138
6139  free_vfi_bmask:
6140         kfree(phba->sli4_hba.vfi_bmask);
6141         phba->sli4_hba.vfi_bmask = NULL;
6142  free_xri_ids:
6143         kfree(phba->sli4_hba.xri_ids);
6144         phba->sli4_hba.xri_ids = NULL;
6145  free_xri_bmask:
6146         kfree(phba->sli4_hba.xri_bmask);
6147         phba->sli4_hba.xri_bmask = NULL;
6148  free_vpi_ids:
6149         kfree(phba->vpi_ids);
6150         phba->vpi_ids = NULL;
6151  free_vpi_bmask:
6152         kfree(phba->vpi_bmask);
6153         phba->vpi_bmask = NULL;
6154  free_rpi_ids:
6155         kfree(phba->sli4_hba.rpi_ids);
6156         phba->sli4_hba.rpi_ids = NULL;
6157  free_rpi_bmask:
6158         kfree(phba->sli4_hba.rpi_bmask);
6159         phba->sli4_hba.rpi_bmask = NULL;
6160  err_exit:
6161         return rc;
6162 }
6163
6164 /**
6165  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6166  * @phba: Pointer to HBA context object.
6167  *
6168  * This function allocates the number of elements for the specified
6169  * resource type.
6170  **/
6171 int
6172 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6173 {
6174         if (phba->sli4_hba.extents_in_use) {
6175                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6176                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6177                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6178                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6179         } else {
6180                 kfree(phba->vpi_bmask);
6181                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6182                 kfree(phba->vpi_ids);
6183                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6184                 kfree(phba->sli4_hba.xri_bmask);
6185                 kfree(phba->sli4_hba.xri_ids);
6186                 kfree(phba->sli4_hba.vfi_bmask);
6187                 kfree(phba->sli4_hba.vfi_ids);
6188                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6189                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6190         }
6191
6192         return 0;
6193 }
6194
6195 /**
6196  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6197  * @phba: Pointer to HBA context object.
6198  * @type: The resource extent type.
6199  * @extnt_count: buffer to hold port extent count response
6200  * @extnt_size: buffer to hold port extent size response.
6201  *
6202  * This function calls the port to read the host allocated extents
6203  * for a particular type.
6204  **/
6205 int
6206 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6207                                uint16_t *extnt_cnt, uint16_t *extnt_size)
6208 {
6209         bool emb;
6210         int rc = 0;
6211         uint16_t curr_blks = 0;
6212         uint32_t req_len, emb_len;
6213         uint32_t alloc_len, mbox_tmo;
6214         struct list_head *blk_list_head;
6215         struct lpfc_rsrc_blks *rsrc_blk;
6216         LPFC_MBOXQ_t *mbox;
6217         void *virtaddr = NULL;
6218         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6219         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6220         union  lpfc_sli4_cfg_shdr *shdr;
6221
6222         switch (type) {
6223         case LPFC_RSC_TYPE_FCOE_VPI:
6224                 blk_list_head = &phba->lpfc_vpi_blk_list;
6225                 break;
6226         case LPFC_RSC_TYPE_FCOE_XRI:
6227                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6228                 break;
6229         case LPFC_RSC_TYPE_FCOE_VFI:
6230                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6231                 break;
6232         case LPFC_RSC_TYPE_FCOE_RPI:
6233                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6234                 break;
6235         default:
6236                 return -EIO;
6237         }
6238
6239         /* Count the number of extents currently allocatd for this type. */
6240         list_for_each_entry(rsrc_blk, blk_list_head, list) {
6241                 if (curr_blks == 0) {
6242                         /*
6243                          * The GET_ALLOCATED mailbox does not return the size,
6244                          * just the count.  The size should be just the size
6245                          * stored in the current allocated block and all sizes
6246                          * for an extent type are the same so set the return
6247                          * value now.
6248                          */
6249                         *extnt_size = rsrc_blk->rsrc_size;
6250                 }
6251                 curr_blks++;
6252         }
6253
6254         /*
6255          * Calculate the size of an embedded mailbox.  The uint32_t
6256          * accounts for extents-specific word.
6257          */
6258         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6259                 sizeof(uint32_t);
6260
6261         /*
6262          * Presume the allocation and response will fit into an embedded
6263          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6264          */
6265         emb = LPFC_SLI4_MBX_EMBED;
6266         req_len = emb_len;
6267         if (req_len > emb_len) {
6268                 req_len = curr_blks * sizeof(uint16_t) +
6269                         sizeof(union lpfc_sli4_cfg_shdr) +
6270                         sizeof(uint32_t);
6271                 emb = LPFC_SLI4_MBX_NEMBED;
6272         }
6273
6274         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6275         if (!mbox)
6276                 return -ENOMEM;
6277         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6278
6279         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6280                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6281                                      req_len, emb);
6282         if (alloc_len < req_len) {
6283                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6284                         "2983 Allocated DMA memory size (x%x) is "
6285                         "less than the requested DMA memory "
6286                         "size (x%x)\n", alloc_len, req_len);
6287                 rc = -ENOMEM;
6288                 goto err_exit;
6289         }
6290         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6291         if (unlikely(rc)) {
6292                 rc = -EIO;
6293                 goto err_exit;
6294         }
6295
6296         if (!phba->sli4_hba.intr_enable)
6297                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6298         else {
6299                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6300                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6301         }
6302
6303         if (unlikely(rc)) {
6304                 rc = -EIO;
6305                 goto err_exit;
6306         }
6307
6308         /*
6309          * Figure out where the response is located.  Then get local pointers
6310          * to the response data.  The port does not guarantee to respond to
6311          * all extents counts request so update the local variable with the
6312          * allocated count from the port.
6313          */
6314         if (emb == LPFC_SLI4_MBX_EMBED) {
6315                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6316                 shdr = &rsrc_ext->header.cfg_shdr;
6317                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6318         } else {
6319                 virtaddr = mbox->sge_array->addr[0];
6320                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6321                 shdr = &n_rsrc->cfg_shdr;
6322                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6323         }
6324
6325         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6326                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6327                         "2984 Failed to read allocated resources "
6328                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6329                         type,
6330                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
6331                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6332                 rc = -EIO;
6333                 goto err_exit;
6334         }
6335  err_exit:
6336         lpfc_sli4_mbox_cmd_free(phba, mbox);
6337         return rc;
6338 }
6339
6340 /**
6341  * lpfc_sli4_repost_sgl_list - Repsot the buffers sgl pages as block
6342  * @phba: pointer to lpfc hba data structure.
6343  * @pring: Pointer to driver SLI ring object.
6344  * @sgl_list: linked link of sgl buffers to post
6345  * @cnt: number of linked list buffers
6346  *
6347  * This routine walks the list of buffers that have been allocated and
6348  * repost them to the port by using SGL block post. This is needed after a
6349  * pci_function_reset/warm_start or start. It attempts to construct blocks
6350  * of buffer sgls which contains contiguous xris and uses the non-embedded
6351  * SGL block post mailbox commands to post them to the port. For single
6352  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6353  * mailbox command for posting.
6354  *
6355  * Returns: 0 = success, non-zero failure.
6356  **/
6357 static int
6358 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6359                           struct list_head *sgl_list, int cnt)
6360 {
6361         struct lpfc_sglq *sglq_entry = NULL;
6362         struct lpfc_sglq *sglq_entry_next = NULL;
6363         struct lpfc_sglq *sglq_entry_first = NULL;
6364         int status, total_cnt;
6365         int post_cnt = 0, num_posted = 0, block_cnt = 0;
6366         int last_xritag = NO_XRI;
6367         LIST_HEAD(prep_sgl_list);
6368         LIST_HEAD(blck_sgl_list);
6369         LIST_HEAD(allc_sgl_list);
6370         LIST_HEAD(post_sgl_list);
6371         LIST_HEAD(free_sgl_list);
6372
6373         spin_lock_irq(&phba->hbalock);
6374         spin_lock(&phba->sli4_hba.sgl_list_lock);
6375         list_splice_init(sgl_list, &allc_sgl_list);
6376         spin_unlock(&phba->sli4_hba.sgl_list_lock);
6377         spin_unlock_irq(&phba->hbalock);
6378
6379         total_cnt = cnt;
6380         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6381                                  &allc_sgl_list, list) {
6382                 list_del_init(&sglq_entry->list);
6383                 block_cnt++;
6384                 if ((last_xritag != NO_XRI) &&
6385                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
6386                         /* a hole in xri block, form a sgl posting block */
6387                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
6388                         post_cnt = block_cnt - 1;
6389                         /* prepare list for next posting block */
6390                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6391                         block_cnt = 1;
6392                 } else {
6393                         /* prepare list for next posting block */
6394                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6395                         /* enough sgls for non-embed sgl mbox command */
6396                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6397                                 list_splice_init(&prep_sgl_list,
6398                                                  &blck_sgl_list);
6399                                 post_cnt = block_cnt;
6400                                 block_cnt = 0;
6401                         }
6402                 }
6403                 num_posted++;
6404
6405                 /* keep track of last sgl's xritag */
6406                 last_xritag = sglq_entry->sli4_xritag;
6407
6408                 /* end of repost sgl list condition for buffers */
6409                 if (num_posted == total_cnt) {
6410                         if (post_cnt == 0) {
6411                                 list_splice_init(&prep_sgl_list,
6412                                                  &blck_sgl_list);
6413                                 post_cnt = block_cnt;
6414                         } else if (block_cnt == 1) {
6415                                 status = lpfc_sli4_post_sgl(phba,
6416                                                 sglq_entry->phys, 0,
6417                                                 sglq_entry->sli4_xritag);
6418                                 if (!status) {
6419                                         /* successful, put sgl to posted list */
6420                                         list_add_tail(&sglq_entry->list,
6421                                                       &post_sgl_list);
6422                                 } else {
6423                                         /* Failure, put sgl to free list */
6424                                         lpfc_printf_log(phba, KERN_WARNING,
6425                                                 LOG_SLI,
6426                                                 "3159 Failed to post "
6427                                                 "sgl, xritag:x%x\n",
6428                                                 sglq_entry->sli4_xritag);
6429                                         list_add_tail(&sglq_entry->list,
6430                                                       &free_sgl_list);
6431                                         total_cnt--;
6432                                 }
6433                         }
6434                 }
6435
6436                 /* continue until a nembed page worth of sgls */
6437                 if (post_cnt == 0)
6438                         continue;
6439
6440                 /* post the buffer list sgls as a block */
6441                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6442                                                  post_cnt);
6443
6444                 if (!status) {
6445                         /* success, put sgl list to posted sgl list */
6446                         list_splice_init(&blck_sgl_list, &post_sgl_list);
6447                 } else {
6448                         /* Failure, put sgl list to free sgl list */
6449                         sglq_entry_first = list_first_entry(&blck_sgl_list,
6450                                                             struct lpfc_sglq,
6451                                                             list);
6452                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6453                                         "3160 Failed to post sgl-list, "
6454                                         "xritag:x%x-x%x\n",
6455                                         sglq_entry_first->sli4_xritag,
6456                                         (sglq_entry_first->sli4_xritag +
6457                                          post_cnt - 1));
6458                         list_splice_init(&blck_sgl_list, &free_sgl_list);
6459                         total_cnt -= post_cnt;
6460                 }
6461
6462                 /* don't reset xirtag due to hole in xri block */
6463                 if (block_cnt == 0)
6464                         last_xritag = NO_XRI;
6465
6466                 /* reset sgl post count for next round of posting */
6467                 post_cnt = 0;
6468         }
6469
6470         /* free the sgls failed to post */
6471         lpfc_free_sgl_list(phba, &free_sgl_list);
6472
6473         /* push sgls posted to the available list */
6474         if (!list_empty(&post_sgl_list)) {
6475                 spin_lock_irq(&phba->hbalock);
6476                 spin_lock(&phba->sli4_hba.sgl_list_lock);
6477                 list_splice_init(&post_sgl_list, sgl_list);
6478                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6479                 spin_unlock_irq(&phba->hbalock);
6480         } else {
6481                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6482                                 "3161 Failure to post sgl to port.\n");
6483                 return -EIO;
6484         }
6485
6486         /* return the number of XRIs actually posted */
6487         return total_cnt;
6488 }
6489
6490 void
6491 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6492 {
6493         uint32_t len;
6494
6495         len = sizeof(struct lpfc_mbx_set_host_data) -
6496                 sizeof(struct lpfc_sli4_cfg_mhdr);
6497         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6498                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6499                          LPFC_SLI4_MBX_EMBED);
6500
6501         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6502         mbox->u.mqe.un.set_host_data.param_len =
6503                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
6504         snprintf(mbox->u.mqe.un.set_host_data.data,
6505                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6506                  "Linux %s v"LPFC_DRIVER_VERSION,
6507                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6508 }
6509
6510 /**
6511  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
6512  * @phba: Pointer to HBA context object.
6513  *
6514  * This function is the main SLI4 device initialization PCI function. This
6515  * function is called by the HBA initialization code, HBA reset code and
6516  * HBA error attention handler code. Caller is not required to hold any
6517  * locks.
6518  **/
6519 int
6520 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6521 {
6522         int rc, i;
6523         LPFC_MBOXQ_t *mboxq;
6524         struct lpfc_mqe *mqe;
6525         uint8_t *vpd;
6526         uint32_t vpd_size;
6527         uint32_t ftr_rsp = 0;
6528         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6529         struct lpfc_vport *vport = phba->pport;
6530         struct lpfc_dmabuf *mp;
6531         struct lpfc_rqb *rqbp;
6532
6533         /* Perform a PCI function reset to start from clean */
6534         rc = lpfc_pci_function_reset(phba);
6535         if (unlikely(rc))
6536                 return -ENODEV;
6537
6538         /* Check the HBA Host Status Register for readyness */
6539         rc = lpfc_sli4_post_status_check(phba);
6540         if (unlikely(rc))
6541                 return -ENODEV;
6542         else {
6543                 spin_lock_irq(&phba->hbalock);
6544                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6545                 spin_unlock_irq(&phba->hbalock);
6546         }
6547
6548         /*
6549          * Allocate a single mailbox container for initializing the
6550          * port.
6551          */
6552         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6553         if (!mboxq)
6554                 return -ENOMEM;
6555
6556         /* Issue READ_REV to collect vpd and FW information. */
6557         vpd_size = SLI4_PAGE_SIZE;
6558         vpd = kzalloc(vpd_size, GFP_KERNEL);
6559         if (!vpd) {
6560                 rc = -ENOMEM;
6561                 goto out_free_mbox;
6562         }
6563
6564         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6565         if (unlikely(rc)) {
6566                 kfree(vpd);
6567                 goto out_free_mbox;
6568         }
6569
6570         mqe = &mboxq->u.mqe;
6571         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6572         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6573                 phba->hba_flag |= HBA_FCOE_MODE;
6574                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6575         } else {
6576                 phba->hba_flag &= ~HBA_FCOE_MODE;
6577         }
6578
6579         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6580                 LPFC_DCBX_CEE_MODE)
6581                 phba->hba_flag |= HBA_FIP_SUPPORT;
6582         else
6583                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6584
6585         phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6586
6587         if (phba->sli_rev != LPFC_SLI_REV4) {
6588                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6589                         "0376 READ_REV Error. SLI Level %d "
6590                         "FCoE enabled %d\n",
6591                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6592                 rc = -EIO;
6593                 kfree(vpd);
6594                 goto out_free_mbox;
6595         }
6596
6597         /*
6598          * Continue initialization with default values even if driver failed
6599          * to read FCoE param config regions, only read parameters if the
6600          * board is FCoE
6601          */
6602         if (phba->hba_flag & HBA_FCOE_MODE &&
6603             lpfc_sli4_read_fcoe_params(phba))
6604                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6605                         "2570 Failed to read FCoE parameters\n");
6606
6607         /*
6608          * Retrieve sli4 device physical port name, failure of doing it
6609          * is considered as non-fatal.
6610          */
6611         rc = lpfc_sli4_retrieve_pport_name(phba);
6612         if (!rc)
6613                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6614                                 "3080 Successful retrieving SLI4 device "
6615                                 "physical port name: %s.\n", phba->Port);
6616
6617         /*
6618          * Evaluate the read rev and vpd data. Populate the driver
6619          * state with the results. If this routine fails, the failure
6620          * is not fatal as the driver will use generic values.
6621          */
6622         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6623         if (unlikely(!rc)) {
6624                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6625                                 "0377 Error %d parsing vpd. "
6626                                 "Using defaults.\n", rc);
6627                 rc = 0;
6628         }
6629         kfree(vpd);
6630
6631         /* Save information as VPD data */
6632         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6633         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6634         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6635         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6636                                          &mqe->un.read_rev);
6637         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6638                                        &mqe->un.read_rev);
6639         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6640                                             &mqe->un.read_rev);
6641         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6642                                            &mqe->un.read_rev);
6643         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6644         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6645         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6646         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6647         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6648         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6649         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6650                         "(%d):0380 READ_REV Status x%x "
6651                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6652                         mboxq->vport ? mboxq->vport->vpi : 0,
6653                         bf_get(lpfc_mqe_status, mqe),
6654                         phba->vpd.rev.opFwName,
6655                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6656                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6657
6658         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
6659         rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6660         if (phba->pport->cfg_lun_queue_depth > rc) {
6661                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6662                                 "3362 LUN queue depth changed from %d to %d\n",
6663                                 phba->pport->cfg_lun_queue_depth, rc);
6664                 phba->pport->cfg_lun_queue_depth = rc;
6665         }
6666
6667         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6668             LPFC_SLI_INTF_IF_TYPE_0) {
6669                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6670                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6671                 if (rc == MBX_SUCCESS) {
6672                         phba->hba_flag |= HBA_RECOVERABLE_UE;
6673                         /* Set 1Sec interval to detect UE */
6674                         phba->eratt_poll_interval = 1;
6675                         phba->sli4_hba.ue_to_sr = bf_get(
6676                                         lpfc_mbx_set_feature_UESR,
6677                                         &mboxq->u.mqe.un.set_feature);
6678                         phba->sli4_hba.ue_to_rp = bf_get(
6679                                         lpfc_mbx_set_feature_UERP,
6680                                         &mboxq->u.mqe.un.set_feature);
6681                 }
6682         }
6683
6684         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6685                 /* Enable MDS Diagnostics only if the SLI Port supports it */
6686                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6687                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6688                 if (rc != MBX_SUCCESS)
6689                         phba->mds_diags_support = 0;
6690         }
6691
6692         /*
6693          * Discover the port's supported feature set and match it against the
6694          * hosts requests.
6695          */
6696         lpfc_request_features(phba, mboxq);
6697         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6698         if (unlikely(rc)) {
6699                 rc = -EIO;
6700                 goto out_free_mbox;
6701         }
6702
6703         /*
6704          * The port must support FCP initiator mode as this is the
6705          * only mode running in the host.
6706          */
6707         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6708                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6709                                 "0378 No support for fcpi mode.\n");
6710                 ftr_rsp++;
6711         }
6712         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6713                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6714         else
6715                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6716         /*
6717          * If the port cannot support the host's requested features
6718          * then turn off the global config parameters to disable the
6719          * feature in the driver.  This is not a fatal error.
6720          */
6721         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6722         if (phba->cfg_enable_bg) {
6723                 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6724                         phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6725                 else
6726                         ftr_rsp++;
6727         }
6728
6729         if (phba->max_vpi && phba->cfg_enable_npiv &&
6730             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6731                 ftr_rsp++;
6732
6733         if (ftr_rsp) {
6734                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6735                                 "0379 Feature Mismatch Data: x%08x %08x "
6736                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6737                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6738                                 phba->cfg_enable_npiv, phba->max_vpi);
6739                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6740                         phba->cfg_enable_bg = 0;
6741                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6742                         phba->cfg_enable_npiv = 0;
6743         }
6744
6745         /* These SLI3 features are assumed in SLI4 */
6746         spin_lock_irq(&phba->hbalock);
6747         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6748         spin_unlock_irq(&phba->hbalock);
6749
6750         /*
6751          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
6752          * calls depends on these resources to complete port setup.
6753          */
6754         rc = lpfc_sli4_alloc_resource_identifiers(phba);
6755         if (rc) {
6756                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6757                                 "2920 Failed to alloc Resource IDs "
6758                                 "rc = x%x\n", rc);
6759                 goto out_free_mbox;
6760         }
6761
6762         lpfc_set_host_data(phba, mboxq);
6763
6764         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6765         if (rc) {
6766                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6767                                 "2134 Failed to set host os driver version %x",
6768                                 rc);
6769         }
6770
6771         /* Read the port's service parameters. */
6772         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6773         if (rc) {
6774                 phba->link_state = LPFC_HBA_ERROR;
6775                 rc = -ENOMEM;
6776                 goto out_free_mbox;
6777         }
6778
6779         mboxq->vport = vport;
6780         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6781         mp = (struct lpfc_dmabuf *) mboxq->context1;
6782         if (rc == MBX_SUCCESS) {
6783                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6784                 rc = 0;
6785         }
6786
6787         /*
6788          * This memory was allocated by the lpfc_read_sparam routine. Release
6789          * it to the mbuf pool.
6790          */
6791         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6792         kfree(mp);
6793         mboxq->context1 = NULL;
6794         if (unlikely(rc)) {
6795                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6796                                 "0382 READ_SPARAM command failed "
6797                                 "status %d, mbxStatus x%x\n",
6798                                 rc, bf_get(lpfc_mqe_status, mqe));
6799                 phba->link_state = LPFC_HBA_ERROR;
6800                 rc = -EIO;
6801                 goto out_free_mbox;
6802         }
6803
6804         lpfc_update_vport_wwn(vport);
6805
6806         /* Update the fc_host data structures with new wwn. */
6807         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6808         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6809
6810         /* Create all the SLI4 queues */
6811         rc = lpfc_sli4_queue_create(phba);
6812         if (rc) {
6813                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6814                                 "3089 Failed to allocate queues\n");
6815                 rc = -ENODEV;
6816                 goto out_free_mbox;
6817         }
6818         /* Set up all the queues to the device */
6819         rc = lpfc_sli4_queue_setup(phba);
6820         if (unlikely(rc)) {
6821                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6822                                 "0381 Error %d during queue setup.\n ", rc);
6823                 goto out_stop_timers;
6824         }
6825         /* Initialize the driver internal SLI layer lists. */
6826         lpfc_sli4_setup(phba);
6827         lpfc_sli4_queue_init(phba);
6828
6829         /* update host els xri-sgl sizes and mappings */
6830         rc = lpfc_sli4_els_sgl_update(phba);
6831         if (unlikely(rc)) {
6832                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6833                                 "1400 Failed to update xri-sgl size and "
6834                                 "mapping: %d\n", rc);
6835                 goto out_destroy_queue;
6836         }
6837
6838         /* register the els sgl pool to the port */
6839         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6840                                        phba->sli4_hba.els_xri_cnt);
6841         if (unlikely(rc < 0)) {
6842                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6843                                 "0582 Error %d during els sgl post "
6844                                 "operation\n", rc);
6845                 rc = -ENODEV;
6846                 goto out_destroy_queue;
6847         }
6848         phba->sli4_hba.els_xri_cnt = rc;
6849
6850         if (phba->nvmet_support) {
6851                 /* update host nvmet xri-sgl sizes and mappings */
6852                 rc = lpfc_sli4_nvmet_sgl_update(phba);
6853                 if (unlikely(rc)) {
6854                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6855                                         "6308 Failed to update nvmet-sgl size "
6856                                         "and mapping: %d\n", rc);
6857                         goto out_destroy_queue;
6858                 }
6859
6860                 /* register the nvmet sgl pool to the port */
6861                 rc = lpfc_sli4_repost_sgl_list(
6862                         phba,
6863                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
6864                         phba->sli4_hba.nvmet_xri_cnt);
6865                 if (unlikely(rc < 0)) {
6866                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6867                                         "3117 Error %d during nvmet "
6868                                         "sgl post\n", rc);
6869                         rc = -ENODEV;
6870                         goto out_destroy_queue;
6871                 }
6872                 phba->sli4_hba.nvmet_xri_cnt = rc;
6873                 lpfc_nvmet_create_targetport(phba);
6874         } else {
6875                 /* update host scsi xri-sgl sizes and mappings */
6876                 rc = lpfc_sli4_scsi_sgl_update(phba);
6877                 if (unlikely(rc)) {
6878                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6879                                         "6309 Failed to update scsi-sgl size "
6880                                         "and mapping: %d\n", rc);
6881                         goto out_destroy_queue;
6882                 }
6883
6884                 /* update host nvme xri-sgl sizes and mappings */
6885                 rc = lpfc_sli4_nvme_sgl_update(phba);
6886                 if (unlikely(rc)) {
6887                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6888                                         "6082 Failed to update nvme-sgl size "
6889                                         "and mapping: %d\n", rc);
6890                         goto out_destroy_queue;
6891                 }
6892         }
6893
6894         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
6895
6896                 /* Post initial buffers to all RQs created */
6897                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6898                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6899                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6900                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6901                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
6902                         rqbp->entry_count = 256;
6903                         rqbp->buffer_count = 0;
6904
6905                         /* Divide by 4 and round down to multiple of 16 */
6906                         rc = (phba->cfg_nvmet_mrq_post >> 2) & 0xfff8;
6907                         phba->sli4_hba.nvmet_mrq_hdr[i]->entry_repost = rc;
6908                         phba->sli4_hba.nvmet_mrq_data[i]->entry_repost = rc;
6909
6910                         lpfc_post_rq_buffer(
6911                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6912                                 phba->sli4_hba.nvmet_mrq_data[i],
6913                                 phba->cfg_nvmet_mrq_post);
6914                 }
6915         }
6916
6917         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6918                 /* register the allocated scsi sgl pool to the port */
6919                 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6920                 if (unlikely(rc)) {
6921                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6922                                         "0383 Error %d during scsi sgl post "
6923                                         "operation\n", rc);
6924                         /* Some Scsi buffers were moved to abort scsi list */
6925                         /* A pci function reset will repost them */
6926                         rc = -ENODEV;
6927                         goto out_destroy_queue;
6928                 }
6929         }
6930
6931         if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6932             (phba->nvmet_support == 0)) {
6933
6934                 /* register the allocated nvme sgl pool to the port */
6935                 rc = lpfc_repost_nvme_sgl_list(phba);
6936                 if (unlikely(rc)) {
6937                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6938                                         "6116 Error %d during nvme sgl post "
6939                                         "operation\n", rc);
6940                         /* Some NVME buffers were moved to abort nvme list */
6941                         /* A pci function reset will repost them */
6942                         rc = -ENODEV;
6943                         goto out_destroy_queue;
6944                 }
6945         }
6946
6947         /* Post the rpi header region to the device. */
6948         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6949         if (unlikely(rc)) {
6950                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6951                                 "0393 Error %d during rpi post operation\n",
6952                                 rc);
6953                 rc = -ENODEV;
6954                 goto out_destroy_queue;
6955         }
6956         lpfc_sli4_node_prep(phba);
6957
6958         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6959                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
6960                         /*
6961                          * The FC Port needs to register FCFI (index 0)
6962                          */
6963                         lpfc_reg_fcfi(phba, mboxq);
6964                         mboxq->vport = phba->pport;
6965                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6966                         if (rc != MBX_SUCCESS)
6967                                 goto out_unset_queue;
6968                         rc = 0;
6969                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6970                                                 &mboxq->u.mqe.un.reg_fcfi);
6971                 } else {
6972                         /* We are a NVME Target mode with MRQ > 1 */
6973
6974                         /* First register the FCFI */
6975                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
6976                         mboxq->vport = phba->pport;
6977                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6978                         if (rc != MBX_SUCCESS)
6979                                 goto out_unset_queue;
6980                         rc = 0;
6981                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
6982                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
6983
6984                         /* Next register the MRQs */
6985                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
6986                         mboxq->vport = phba->pport;
6987                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6988                         if (rc != MBX_SUCCESS)
6989                                 goto out_unset_queue;
6990                         rc = 0;
6991                 }
6992                 /* Check if the port is configured to be disabled */
6993                 lpfc_sli_read_link_ste(phba);
6994         }
6995
6996         /* Arm the CQs and then EQs on device */
6997         lpfc_sli4_arm_cqeq_intr(phba);
6998
6999         /* Indicate device interrupt mode */
7000         phba->sli4_hba.intr_enable = 1;
7001
7002         /* Allow asynchronous mailbox command to go through */
7003         spin_lock_irq(&phba->hbalock);
7004         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7005         spin_unlock_irq(&phba->hbalock);
7006
7007         /* Post receive buffers to the device */
7008         lpfc_sli4_rb_setup(phba);
7009
7010         /* Reset HBA FCF states after HBA reset */
7011         phba->fcf.fcf_flag = 0;
7012         phba->fcf.current_rec.flag = 0;
7013
7014         /* Start the ELS watchdog timer */
7015         mod_timer(&vport->els_tmofunc,
7016                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7017
7018         /* Start heart beat timer */
7019         mod_timer(&phba->hb_tmofunc,
7020                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7021         phba->hb_outstanding = 0;
7022         phba->last_completion_time = jiffies;
7023
7024         /* Start error attention (ERATT) polling timer */
7025         mod_timer(&phba->eratt_poll,
7026                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7027
7028         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7029         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7030                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7031                 if (!rc) {
7032                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7033                                         "2829 This device supports "
7034                                         "Advanced Error Reporting (AER)\n");
7035                         spin_lock_irq(&phba->hbalock);
7036                         phba->hba_flag |= HBA_AER_ENABLED;
7037                         spin_unlock_irq(&phba->hbalock);
7038                 } else {
7039                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7040                                         "2830 This device does not support "
7041                                         "Advanced Error Reporting (AER)\n");
7042                         phba->cfg_aer_support = 0;
7043                 }
7044                 rc = 0;
7045         }
7046
7047         /*
7048          * The port is ready, set the host's link state to LINK_DOWN
7049          * in preparation for link interrupts.
7050          */
7051         spin_lock_irq(&phba->hbalock);
7052         phba->link_state = LPFC_LINK_DOWN;
7053         spin_unlock_irq(&phba->hbalock);
7054         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7055             (phba->hba_flag & LINK_DISABLED)) {
7056                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7057                                 "3103 Adapter Link is disabled.\n");
7058                 lpfc_down_link(phba, mboxq);
7059                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7060                 if (rc != MBX_SUCCESS) {
7061                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7062                                         "3104 Adapter failed to issue "
7063                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7064                         goto out_unset_queue;
7065                 }
7066         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7067                 /* don't perform init_link on SLI4 FC port loopback test */
7068                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7069                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7070                         if (rc)
7071                                 goto out_unset_queue;
7072                 }
7073         }
7074         mempool_free(mboxq, phba->mbox_mem_pool);
7075         return rc;
7076 out_unset_queue:
7077         /* Unset all the queues set up in this routine when error out */
7078         lpfc_sli4_queue_unset(phba);
7079 out_destroy_queue:
7080         lpfc_sli4_queue_destroy(phba);
7081 out_stop_timers:
7082         lpfc_stop_hba_timers(phba);
7083 out_free_mbox:
7084         mempool_free(mboxq, phba->mbox_mem_pool);
7085         return rc;
7086 }
7087
7088 /**
7089  * lpfc_mbox_timeout - Timeout call back function for mbox timer
7090  * @ptr: context object - pointer to hba structure.
7091  *
7092  * This is the callback function for mailbox timer. The mailbox
7093  * timer is armed when a new mailbox command is issued and the timer
7094  * is deleted when the mailbox complete. The function is called by
7095  * the kernel timer code when a mailbox does not complete within
7096  * expected time. This function wakes up the worker thread to
7097  * process the mailbox timeout and returns. All the processing is
7098  * done by the worker thread function lpfc_mbox_timeout_handler.
7099  **/
7100 void
7101 lpfc_mbox_timeout(unsigned long ptr)
7102 {
7103         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
7104         unsigned long iflag;
7105         uint32_t tmo_posted;
7106
7107         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7108         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7109         if (!tmo_posted)
7110                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7111         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7112
7113         if (!tmo_posted)
7114                 lpfc_worker_wake_up(phba);
7115         return;
7116 }
7117
7118 /**
7119  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7120  *                                    are pending
7121  * @phba: Pointer to HBA context object.
7122  *
7123  * This function checks if any mailbox completions are present on the mailbox
7124  * completion queue.
7125  **/
7126 static bool
7127 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7128 {
7129
7130         uint32_t idx;
7131         struct lpfc_queue *mcq;
7132         struct lpfc_mcqe *mcqe;
7133         bool pending_completions = false;
7134
7135         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7136                 return false;
7137
7138         /* Check for completions on mailbox completion queue */
7139
7140         mcq = phba->sli4_hba.mbx_cq;
7141         idx = mcq->hba_index;
7142         while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7143                 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7144                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7145                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7146                         pending_completions = true;
7147                         break;
7148                 }
7149                 idx = (idx + 1) % mcq->entry_count;
7150                 if (mcq->hba_index == idx)
7151                         break;
7152         }
7153         return pending_completions;
7154
7155 }
7156
7157 /**
7158  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7159  *                                            that were missed.
7160  * @phba: Pointer to HBA context object.
7161  *
7162  * For sli4, it is possible to miss an interrupt. As such mbox completions
7163  * maybe missed causing erroneous mailbox timeouts to occur. This function
7164  * checks to see if mbox completions are on the mailbox completion queue
7165  * and will process all the completions associated with the eq for the
7166  * mailbox completion queue.
7167  **/
7168 bool
7169 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7170 {
7171
7172         uint32_t eqidx;
7173         struct lpfc_queue *fpeq = NULL;
7174         struct lpfc_eqe *eqe;
7175         bool mbox_pending;
7176
7177         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7178                 return false;
7179
7180         /* Find the eq associated with the mcq */
7181
7182         if (phba->sli4_hba.hba_eq)
7183                 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
7184                         if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7185                             phba->sli4_hba.mbx_cq->assoc_qid) {
7186                                 fpeq = phba->sli4_hba.hba_eq[eqidx];
7187                                 break;
7188                         }
7189         if (!fpeq)
7190                 return false;
7191
7192         /* Turn off interrupts from this EQ */
7193
7194         lpfc_sli4_eq_clr_intr(fpeq);
7195
7196         /* Check to see if a mbox completion is pending */
7197
7198         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7199
7200         /*
7201          * If a mbox completion is pending, process all the events on EQ
7202          * associated with the mbox completion queue (this could include
7203          * mailbox commands, async events, els commands, receive queue data
7204          * and fcp commands)
7205          */
7206
7207         if (mbox_pending)
7208                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7209                         lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7210                         fpeq->EQ_processed++;
7211                 }
7212
7213         /* Always clear and re-arm the EQ */
7214
7215         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7216
7217         return mbox_pending;
7218
7219 }
7220
7221 /**
7222  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7223  * @phba: Pointer to HBA context object.
7224  *
7225  * This function is called from worker thread when a mailbox command times out.
7226  * The caller is not required to hold any locks. This function will reset the
7227  * HBA and recover all the pending commands.
7228  **/
7229 void
7230 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7231 {
7232         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7233         MAILBOX_t *mb = NULL;
7234
7235         struct lpfc_sli *psli = &phba->sli;
7236
7237         /* If the mailbox completed, process the completion and return */
7238         if (lpfc_sli4_process_missed_mbox_completions(phba))
7239                 return;
7240
7241         if (pmbox != NULL)
7242                 mb = &pmbox->u.mb;
7243         /* Check the pmbox pointer first.  There is a race condition
7244          * between the mbox timeout handler getting executed in the
7245          * worklist and the mailbox actually completing. When this
7246          * race condition occurs, the mbox_active will be NULL.
7247          */
7248         spin_lock_irq(&phba->hbalock);
7249         if (pmbox == NULL) {
7250                 lpfc_printf_log(phba, KERN_WARNING,
7251                                 LOG_MBOX | LOG_SLI,
7252                                 "0353 Active Mailbox cleared - mailbox timeout "
7253                                 "exiting\n");
7254                 spin_unlock_irq(&phba->hbalock);
7255                 return;
7256         }
7257
7258         /* Mbox cmd <mbxCommand> timeout */
7259         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7260                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7261                         mb->mbxCommand,
7262                         phba->pport->port_state,
7263                         phba->sli.sli_flag,
7264                         phba->sli.mbox_active);
7265         spin_unlock_irq(&phba->hbalock);
7266
7267         /* Setting state unknown so lpfc_sli_abort_iocb_ring
7268          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7269          * it to fail all outstanding SCSI IO.
7270          */
7271         spin_lock_irq(&phba->pport->work_port_lock);
7272         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7273         spin_unlock_irq(&phba->pport->work_port_lock);
7274         spin_lock_irq(&phba->hbalock);
7275         phba->link_state = LPFC_LINK_UNKNOWN;
7276         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7277         spin_unlock_irq(&phba->hbalock);
7278
7279         lpfc_sli_abort_fcp_rings(phba);
7280
7281         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7282                         "0345 Resetting board due to mailbox timeout\n");
7283
7284         /* Reset the HBA device */
7285         lpfc_reset_hba(phba);
7286 }
7287
7288 /**
7289  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7290  * @phba: Pointer to HBA context object.
7291  * @pmbox: Pointer to mailbox object.
7292  * @flag: Flag indicating how the mailbox need to be processed.
7293  *
7294  * This function is called by discovery code and HBA management code
7295  * to submit a mailbox command to firmware with SLI-3 interface spec. This
7296  * function gets the hbalock to protect the data structures.
7297  * The mailbox command can be submitted in polling mode, in which case
7298  * this function will wait in a polling loop for the completion of the
7299  * mailbox.
7300  * If the mailbox is submitted in no_wait mode (not polling) the
7301  * function will submit the command and returns immediately without waiting
7302  * for the mailbox completion. The no_wait is supported only when HBA
7303  * is in SLI2/SLI3 mode - interrupts are enabled.
7304  * The SLI interface allows only one mailbox pending at a time. If the
7305  * mailbox is issued in polling mode and there is already a mailbox
7306  * pending, then the function will return an error. If the mailbox is issued
7307  * in NO_WAIT mode and there is a mailbox pending already, the function
7308  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7309  * The sli layer owns the mailbox object until the completion of mailbox
7310  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7311  * return codes the caller owns the mailbox command after the return of
7312  * the function.
7313  **/
7314 static int
7315 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7316                        uint32_t flag)
7317 {
7318         MAILBOX_t *mbx;
7319         struct lpfc_sli *psli = &phba->sli;
7320         uint32_t status, evtctr;
7321         uint32_t ha_copy, hc_copy;
7322         int i;
7323         unsigned long timeout;
7324         unsigned long drvr_flag = 0;
7325         uint32_t word0, ldata;
7326         void __iomem *to_slim;
7327         int processing_queue = 0;
7328
7329         spin_lock_irqsave(&phba->hbalock, drvr_flag);
7330         if (!pmbox) {
7331                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7332                 /* processing mbox queue from intr_handler */
7333                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7334                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7335                         return MBX_SUCCESS;
7336                 }
7337                 processing_queue = 1;
7338                 pmbox = lpfc_mbox_get(phba);
7339                 if (!pmbox) {
7340                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7341                         return MBX_SUCCESS;
7342                 }
7343         }
7344
7345         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7346                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7347                 if(!pmbox->vport) {
7348                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7349                         lpfc_printf_log(phba, KERN_ERR,
7350                                         LOG_MBOX | LOG_VPORT,
7351                                         "1806 Mbox x%x failed. No vport\n",
7352                                         pmbox->u.mb.mbxCommand);
7353                         dump_stack();
7354                         goto out_not_finished;
7355                 }
7356         }
7357
7358         /* If the PCI channel is in offline state, do not post mbox. */
7359         if (unlikely(pci_channel_offline(phba->pcidev))) {
7360                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7361                 goto out_not_finished;
7362         }
7363
7364         /* If HBA has a deferred error attention, fail the iocb. */
7365         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7366                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7367                 goto out_not_finished;
7368         }
7369
7370         psli = &phba->sli;
7371
7372         mbx = &pmbox->u.mb;
7373         status = MBX_SUCCESS;
7374
7375         if (phba->link_state == LPFC_HBA_ERROR) {
7376                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7377
7378                 /* Mbox command <mbxCommand> cannot issue */
7379                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7380                                 "(%d):0311 Mailbox command x%x cannot "
7381                                 "issue Data: x%x x%x\n",
7382                                 pmbox->vport ? pmbox->vport->vpi : 0,
7383                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7384                 goto out_not_finished;
7385         }
7386
7387         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7388                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7389                         !(hc_copy & HC_MBINT_ENA)) {
7390                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7391                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7392                                 "(%d):2528 Mailbox command x%x cannot "
7393                                 "issue Data: x%x x%x\n",
7394                                 pmbox->vport ? pmbox->vport->vpi : 0,
7395                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7396                         goto out_not_finished;
7397                 }
7398         }
7399
7400         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7401                 /* Polling for a mbox command when another one is already active
7402                  * is not allowed in SLI. Also, the driver must have established
7403                  * SLI2 mode to queue and process multiple mbox commands.
7404                  */
7405
7406                 if (flag & MBX_POLL) {
7407                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7408
7409                         /* Mbox command <mbxCommand> cannot issue */
7410                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7411                                         "(%d):2529 Mailbox command x%x "
7412                                         "cannot issue Data: x%x x%x\n",
7413                                         pmbox->vport ? pmbox->vport->vpi : 0,
7414                                         pmbox->u.mb.mbxCommand,
7415                                         psli->sli_flag, flag);
7416                         goto out_not_finished;
7417                 }
7418
7419                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7420                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7421                         /* Mbox command <mbxCommand> cannot issue */
7422                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7423                                         "(%d):2530 Mailbox command x%x "
7424                                         "cannot issue Data: x%x x%x\n",
7425                                         pmbox->vport ? pmbox->vport->vpi : 0,
7426                                         pmbox->u.mb.mbxCommand,
7427                                         psli->sli_flag, flag);
7428                         goto out_not_finished;
7429                 }
7430
7431                 /* Another mailbox command is still being processed, queue this
7432                  * command to be processed later.
7433                  */
7434                 lpfc_mbox_put(phba, pmbox);
7435
7436                 /* Mbox cmd issue - BUSY */
7437                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7438                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
7439                                 "x%x x%x x%x x%x\n",
7440                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7441                                 mbx->mbxCommand, phba->pport->port_state,
7442                                 psli->sli_flag, flag);
7443
7444                 psli->slistat.mbox_busy++;
7445                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7446
7447                 if (pmbox->vport) {
7448                         lpfc_debugfs_disc_trc(pmbox->vport,
7449                                 LPFC_DISC_TRC_MBOX_VPORT,
7450                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
7451                                 (uint32_t)mbx->mbxCommand,
7452                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7453                 }
7454                 else {
7455                         lpfc_debugfs_disc_trc(phba->pport,
7456                                 LPFC_DISC_TRC_MBOX,
7457                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
7458                                 (uint32_t)mbx->mbxCommand,
7459                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7460                 }
7461
7462                 return MBX_BUSY;
7463         }
7464
7465         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7466
7467         /* If we are not polling, we MUST be in SLI2 mode */
7468         if (flag != MBX_POLL) {
7469                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7470                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
7471                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7472                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7473                         /* Mbox command <mbxCommand> cannot issue */
7474                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7475                                         "(%d):2531 Mailbox command x%x "
7476                                         "cannot issue Data: x%x x%x\n",
7477                                         pmbox->vport ? pmbox->vport->vpi : 0,
7478                                         pmbox->u.mb.mbxCommand,
7479                                         psli->sli_flag, flag);
7480                         goto out_not_finished;
7481                 }
7482                 /* timeout active mbox command */
7483                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7484                                            1000);
7485                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7486         }
7487
7488         /* Mailbox cmd <cmd> issue */
7489         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7490                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7491                         "x%x\n",
7492                         pmbox->vport ? pmbox->vport->vpi : 0,
7493                         mbx->mbxCommand, phba->pport->port_state,
7494                         psli->sli_flag, flag);
7495
7496         if (mbx->mbxCommand != MBX_HEARTBEAT) {
7497                 if (pmbox->vport) {
7498                         lpfc_debugfs_disc_trc(pmbox->vport,
7499                                 LPFC_DISC_TRC_MBOX_VPORT,
7500                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7501                                 (uint32_t)mbx->mbxCommand,
7502                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7503                 }
7504                 else {
7505                         lpfc_debugfs_disc_trc(phba->pport,
7506                                 LPFC_DISC_TRC_MBOX,
7507                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
7508                                 (uint32_t)mbx->mbxCommand,
7509                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7510                 }
7511         }
7512
7513         psli->slistat.mbox_cmd++;
7514         evtctr = psli->slistat.mbox_event;
7515
7516         /* next set own bit for the adapter and copy over command word */
7517         mbx->mbxOwner = OWN_CHIP;
7518
7519         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7520                 /* Populate mbox extension offset word. */
7521                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7522                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7523                                 = (uint8_t *)phba->mbox_ext
7524                                   - (uint8_t *)phba->mbox;
7525                 }
7526
7527                 /* Copy the mailbox extension data */
7528                 if (pmbox->in_ext_byte_len && pmbox->context2) {
7529                         lpfc_sli_pcimem_bcopy(pmbox->context2,
7530                                 (uint8_t *)phba->mbox_ext,
7531                                 pmbox->in_ext_byte_len);
7532                 }
7533                 /* Copy command data to host SLIM area */
7534                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7535         } else {
7536                 /* Populate mbox extension offset word. */
7537                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7538                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7539                                 = MAILBOX_HBA_EXT_OFFSET;
7540
7541                 /* Copy the mailbox extension data */
7542                 if (pmbox->in_ext_byte_len && pmbox->context2)
7543                         lpfc_memcpy_to_slim(phba->MBslimaddr +
7544                                 MAILBOX_HBA_EXT_OFFSET,
7545                                 pmbox->context2, pmbox->in_ext_byte_len);
7546
7547                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7548                         /* copy command data into host mbox for cmpl */
7549                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7550                                               MAILBOX_CMD_SIZE);
7551
7552                 /* First copy mbox command data to HBA SLIM, skip past first
7553                    word */
7554                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7555                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7556                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
7557
7558                 /* Next copy over first word, with mbxOwner set */
7559                 ldata = *((uint32_t *)mbx);
7560                 to_slim = phba->MBslimaddr;
7561                 writel(ldata, to_slim);
7562                 readl(to_slim); /* flush */
7563
7564                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7565                         /* switch over to host mailbox */
7566                         psli->sli_flag |= LPFC_SLI_ACTIVE;
7567         }
7568
7569         wmb();
7570
7571         switch (flag) {
7572         case MBX_NOWAIT:
7573                 /* Set up reference to mailbox command */
7574                 psli->mbox_active = pmbox;
7575                 /* Interrupt board to do it */
7576                 writel(CA_MBATT, phba->CAregaddr);
7577                 readl(phba->CAregaddr); /* flush */
7578                 /* Don't wait for it to finish, just return */
7579                 break;
7580
7581         case MBX_POLL:
7582                 /* Set up null reference to mailbox command */
7583                 psli->mbox_active = NULL;
7584                 /* Interrupt board to do it */
7585                 writel(CA_MBATT, phba->CAregaddr);
7586                 readl(phba->CAregaddr); /* flush */
7587
7588                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7589                         /* First read mbox status word */
7590                         word0 = *((uint32_t *)phba->mbox);
7591                         word0 = le32_to_cpu(word0);
7592                 } else {
7593                         /* First read mbox status word */
7594                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
7595                                 spin_unlock_irqrestore(&phba->hbalock,
7596                                                        drvr_flag);
7597                                 goto out_not_finished;
7598                         }
7599                 }
7600
7601                 /* Read the HBA Host Attention Register */
7602                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7603                         spin_unlock_irqrestore(&phba->hbalock,
7604                                                        drvr_flag);
7605                         goto out_not_finished;
7606                 }
7607                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7608                                                         1000) + jiffies;
7609                 i = 0;
7610                 /* Wait for command to complete */
7611                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7612                        (!(ha_copy & HA_MBATT) &&
7613                         (phba->link_state > LPFC_WARM_START))) {
7614                         if (time_after(jiffies, timeout)) {
7615                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7616                                 spin_unlock_irqrestore(&phba->hbalock,
7617                                                        drvr_flag);
7618                                 goto out_not_finished;
7619                         }
7620
7621                         /* Check if we took a mbox interrupt while we were
7622                            polling */
7623                         if (((word0 & OWN_CHIP) != OWN_CHIP)
7624                             && (evtctr != psli->slistat.mbox_event))
7625                                 break;
7626
7627                         if (i++ > 10) {
7628                                 spin_unlock_irqrestore(&phba->hbalock,
7629                                                        drvr_flag);
7630                                 msleep(1);
7631                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7632                         }
7633
7634                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7635                                 /* First copy command data */
7636                                 word0 = *((uint32_t *)phba->mbox);
7637                                 word0 = le32_to_cpu(word0);
7638                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7639                                         MAILBOX_t *slimmb;
7640                                         uint32_t slimword0;
7641                                         /* Check real SLIM for any errors */
7642                                         slimword0 = readl(phba->MBslimaddr);
7643                                         slimmb = (MAILBOX_t *) & slimword0;
7644                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7645                                             && slimmb->mbxStatus) {
7646                                                 psli->sli_flag &=
7647                                                     ~LPFC_SLI_ACTIVE;
7648                                                 word0 = slimword0;
7649                                         }
7650                                 }
7651                         } else {
7652                                 /* First copy command data */
7653                                 word0 = readl(phba->MBslimaddr);
7654                         }
7655                         /* Read the HBA Host Attention Register */
7656                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7657                                 spin_unlock_irqrestore(&phba->hbalock,
7658                                                        drvr_flag);
7659                                 goto out_not_finished;
7660                         }
7661                 }
7662
7663                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7664                         /* copy results back to user */
7665                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7666                                                 MAILBOX_CMD_SIZE);
7667                         /* Copy the mailbox extension data */
7668                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7669                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7670                                                       pmbox->context2,
7671                                                       pmbox->out_ext_byte_len);
7672                         }
7673                 } else {
7674                         /* First copy command data */
7675                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7676                                                 MAILBOX_CMD_SIZE);
7677                         /* Copy the mailbox extension data */
7678                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7679                                 lpfc_memcpy_from_slim(pmbox->context2,
7680                                         phba->MBslimaddr +
7681                                         MAILBOX_HBA_EXT_OFFSET,
7682                                         pmbox->out_ext_byte_len);
7683                         }
7684                 }
7685
7686                 writel(HA_MBATT, phba->HAregaddr);
7687                 readl(phba->HAregaddr); /* flush */
7688
7689                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7690                 status = mbx->mbxStatus;
7691         }
7692
7693         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7694         return status;
7695
7696 out_not_finished:
7697         if (processing_queue) {
7698                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7699                 lpfc_mbox_cmpl_put(phba, pmbox);
7700         }
7701         return MBX_NOT_FINISHED;
7702 }
7703
7704 /**
7705  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7706  * @phba: Pointer to HBA context object.
7707  *
7708  * The function blocks the posting of SLI4 asynchronous mailbox commands from
7709  * the driver internal pending mailbox queue. It will then try to wait out the
7710  * possible outstanding mailbox command before return.
7711  *
7712  * Returns:
7713  *      0 - the outstanding mailbox command completed; otherwise, the wait for
7714  *      the outstanding mailbox command timed out.
7715  **/
7716 static int
7717 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7718 {
7719         struct lpfc_sli *psli = &phba->sli;
7720         int rc = 0;
7721         unsigned long timeout = 0;
7722
7723         /* Mark the asynchronous mailbox command posting as blocked */
7724         spin_lock_irq(&phba->hbalock);
7725         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7726         /* Determine how long we might wait for the active mailbox
7727          * command to be gracefully completed by firmware.
7728          */
7729         if (phba->sli.mbox_active)
7730                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7731                                                 phba->sli.mbox_active) *
7732                                                 1000) + jiffies;
7733         spin_unlock_irq(&phba->hbalock);
7734
7735         /* Make sure the mailbox is really active */
7736         if (timeout)
7737                 lpfc_sli4_process_missed_mbox_completions(phba);
7738
7739         /* Wait for the outstnading mailbox command to complete */
7740         while (phba->sli.mbox_active) {
7741                 /* Check active mailbox complete status every 2ms */
7742                 msleep(2);
7743                 if (time_after(jiffies, timeout)) {
7744                         /* Timeout, marked the outstanding cmd not complete */
7745                         rc = 1;
7746                         break;
7747                 }
7748         }
7749
7750         /* Can not cleanly block async mailbox command, fails it */
7751         if (rc) {
7752                 spin_lock_irq(&phba->hbalock);
7753                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7754                 spin_unlock_irq(&phba->hbalock);
7755         }
7756         return rc;
7757 }
7758
7759 /**
7760  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7761  * @phba: Pointer to HBA context object.
7762  *
7763  * The function unblocks and resume posting of SLI4 asynchronous mailbox
7764  * commands from the driver internal pending mailbox queue. It makes sure
7765  * that there is no outstanding mailbox command before resuming posting
7766  * asynchronous mailbox commands. If, for any reason, there is outstanding
7767  * mailbox command, it will try to wait it out before resuming asynchronous
7768  * mailbox command posting.
7769  **/
7770 static void
7771 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7772 {
7773         struct lpfc_sli *psli = &phba->sli;
7774
7775         spin_lock_irq(&phba->hbalock);
7776         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7777                 /* Asynchronous mailbox posting is not blocked, do nothing */
7778                 spin_unlock_irq(&phba->hbalock);
7779                 return;
7780         }
7781
7782         /* Outstanding synchronous mailbox command is guaranteed to be done,
7783          * successful or timeout, after timing-out the outstanding mailbox
7784          * command shall always be removed, so just unblock posting async
7785          * mailbox command and resume
7786          */
7787         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7788         spin_unlock_irq(&phba->hbalock);
7789
7790         /* wake up worker thread to post asynchronlous mailbox command */
7791         lpfc_worker_wake_up(phba);
7792 }
7793
7794 /**
7795  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7796  * @phba: Pointer to HBA context object.
7797  * @mboxq: Pointer to mailbox object.
7798  *
7799  * The function waits for the bootstrap mailbox register ready bit from
7800  * port for twice the regular mailbox command timeout value.
7801  *
7802  *      0 - no timeout on waiting for bootstrap mailbox register ready.
7803  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7804  **/
7805 static int
7806 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7807 {
7808         uint32_t db_ready;
7809         unsigned long timeout;
7810         struct lpfc_register bmbx_reg;
7811
7812         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7813                                    * 1000) + jiffies;
7814
7815         do {
7816                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7817                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7818                 if (!db_ready)
7819                         msleep(2);
7820
7821                 if (time_after(jiffies, timeout))
7822                         return MBXERR_ERROR;
7823         } while (!db_ready);
7824
7825         return 0;
7826 }
7827
7828 /**
7829  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7830  * @phba: Pointer to HBA context object.
7831  * @mboxq: Pointer to mailbox object.
7832  *
7833  * The function posts a mailbox to the port.  The mailbox is expected
7834  * to be comletely filled in and ready for the port to operate on it.
7835  * This routine executes a synchronous completion operation on the
7836  * mailbox by polling for its completion.
7837  *
7838  * The caller must not be holding any locks when calling this routine.
7839  *
7840  * Returns:
7841  *      MBX_SUCCESS - mailbox posted successfully
7842  *      Any of the MBX error values.
7843  **/
7844 static int
7845 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7846 {
7847         int rc = MBX_SUCCESS;
7848         unsigned long iflag;
7849         uint32_t mcqe_status;
7850         uint32_t mbx_cmnd;
7851         struct lpfc_sli *psli = &phba->sli;
7852         struct lpfc_mqe *mb = &mboxq->u.mqe;
7853         struct lpfc_bmbx_create *mbox_rgn;
7854         struct dma_address *dma_address;
7855
7856         /*
7857          * Only one mailbox can be active to the bootstrap mailbox region
7858          * at a time and there is no queueing provided.
7859          */
7860         spin_lock_irqsave(&phba->hbalock, iflag);
7861         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7862                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7863                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7864                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7865                                 "cannot issue Data: x%x x%x\n",
7866                                 mboxq->vport ? mboxq->vport->vpi : 0,
7867                                 mboxq->u.mb.mbxCommand,
7868                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7869                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7870                                 psli->sli_flag, MBX_POLL);
7871                 return MBXERR_ERROR;
7872         }
7873         /* The server grabs the token and owns it until release */
7874         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7875         phba->sli.mbox_active = mboxq;
7876         spin_unlock_irqrestore(&phba->hbalock, iflag);
7877
7878         /* wait for bootstrap mbox register for readyness */
7879         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7880         if (rc)
7881                 goto exit;
7882
7883         /*
7884          * Initialize the bootstrap memory region to avoid stale data areas
7885          * in the mailbox post.  Then copy the caller's mailbox contents to
7886          * the bmbx mailbox region.
7887          */
7888         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7889         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7890         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7891                               sizeof(struct lpfc_mqe));
7892
7893         /* Post the high mailbox dma address to the port and wait for ready. */
7894         dma_address = &phba->sli4_hba.bmbx.dma_address;
7895         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7896
7897         /* wait for bootstrap mbox register for hi-address write done */
7898         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7899         if (rc)
7900                 goto exit;
7901
7902         /* Post the low mailbox dma address to the port. */
7903         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7904
7905         /* wait for bootstrap mbox register for low address write done */
7906         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7907         if (rc)
7908                 goto exit;
7909
7910         /*
7911          * Read the CQ to ensure the mailbox has completed.
7912          * If so, update the mailbox status so that the upper layers
7913          * can complete the request normally.
7914          */
7915         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7916                               sizeof(struct lpfc_mqe));
7917         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7918         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7919                               sizeof(struct lpfc_mcqe));
7920         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7921         /*
7922          * When the CQE status indicates a failure and the mailbox status
7923          * indicates success then copy the CQE status into the mailbox status
7924          * (and prefix it with x4000).
7925          */
7926         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7927                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7928                         bf_set(lpfc_mqe_status, mb,
7929                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
7930                 rc = MBXERR_ERROR;
7931         } else
7932                 lpfc_sli4_swap_str(phba, mboxq);
7933
7934         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7935                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7936                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7937                         " x%x x%x CQ: x%x x%x x%x x%x\n",
7938                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7939                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7940                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7941                         bf_get(lpfc_mqe_status, mb),
7942                         mb->un.mb_words[0], mb->un.mb_words[1],
7943                         mb->un.mb_words[2], mb->un.mb_words[3],
7944                         mb->un.mb_words[4], mb->un.mb_words[5],
7945                         mb->un.mb_words[6], mb->un.mb_words[7],
7946                         mb->un.mb_words[8], mb->un.mb_words[9],
7947                         mb->un.mb_words[10], mb->un.mb_words[11],
7948                         mb->un.mb_words[12], mboxq->mcqe.word0,
7949                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
7950                         mboxq->mcqe.trailer);
7951 exit:
7952         /* We are holding the token, no needed for lock when release */
7953         spin_lock_irqsave(&phba->hbalock, iflag);
7954         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7955         phba->sli.mbox_active = NULL;
7956         spin_unlock_irqrestore(&phba->hbalock, iflag);
7957         return rc;
7958 }
7959
7960 /**
7961  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7962  * @phba: Pointer to HBA context object.
7963  * @pmbox: Pointer to mailbox object.
7964  * @flag: Flag indicating how the mailbox need to be processed.
7965  *
7966  * This function is called by discovery code and HBA management code to submit
7967  * a mailbox command to firmware with SLI-4 interface spec.
7968  *
7969  * Return codes the caller owns the mailbox command after the return of the
7970  * function.
7971  **/
7972 static int
7973 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7974                        uint32_t flag)
7975 {
7976         struct lpfc_sli *psli = &phba->sli;
7977         unsigned long iflags;
7978         int rc;
7979
7980         /* dump from issue mailbox command if setup */
7981         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7982
7983         rc = lpfc_mbox_dev_check(phba);
7984         if (unlikely(rc)) {
7985                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7986                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7987                                 "cannot issue Data: x%x x%x\n",
7988                                 mboxq->vport ? mboxq->vport->vpi : 0,
7989                                 mboxq->u.mb.mbxCommand,
7990                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7991                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7992                                 psli->sli_flag, flag);
7993                 goto out_not_finished;
7994         }
7995
7996         /* Detect polling mode and jump to a handler */
7997         if (!phba->sli4_hba.intr_enable) {
7998                 if (flag == MBX_POLL)
7999                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8000                 else
8001                         rc = -EIO;
8002                 if (rc != MBX_SUCCESS)
8003                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8004                                         "(%d):2541 Mailbox command x%x "
8005                                         "(x%x/x%x) failure: "
8006                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8007                                         "Data: x%x x%x\n,",
8008                                         mboxq->vport ? mboxq->vport->vpi : 0,
8009                                         mboxq->u.mb.mbxCommand,
8010                                         lpfc_sli_config_mbox_subsys_get(phba,
8011                                                                         mboxq),
8012                                         lpfc_sli_config_mbox_opcode_get(phba,
8013                                                                         mboxq),
8014                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8015                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8016                                         bf_get(lpfc_mcqe_ext_status,
8017                                                &mboxq->mcqe),
8018                                         psli->sli_flag, flag);
8019                 return rc;
8020         } else if (flag == MBX_POLL) {
8021                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8022                                 "(%d):2542 Try to issue mailbox command "
8023                                 "x%x (x%x/x%x) synchronously ahead of async"
8024                                 "mailbox command queue: x%x x%x\n",
8025                                 mboxq->vport ? mboxq->vport->vpi : 0,
8026                                 mboxq->u.mb.mbxCommand,
8027                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8028                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8029                                 psli->sli_flag, flag);
8030                 /* Try to block the asynchronous mailbox posting */
8031                 rc = lpfc_sli4_async_mbox_block(phba);
8032                 if (!rc) {
8033                         /* Successfully blocked, now issue sync mbox cmd */
8034                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8035                         if (rc != MBX_SUCCESS)
8036                                 lpfc_printf_log(phba, KERN_WARNING,
8037                                         LOG_MBOX | LOG_SLI,
8038                                         "(%d):2597 Sync Mailbox command "
8039                                         "x%x (x%x/x%x) failure: "
8040                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8041                                         "Data: x%x x%x\n,",
8042                                         mboxq->vport ? mboxq->vport->vpi : 0,
8043                                         mboxq->u.mb.mbxCommand,
8044                                         lpfc_sli_config_mbox_subsys_get(phba,
8045                                                                         mboxq),
8046                                         lpfc_sli_config_mbox_opcode_get(phba,
8047                                                                         mboxq),
8048                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8049                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8050                                         bf_get(lpfc_mcqe_ext_status,
8051                                                &mboxq->mcqe),
8052                                         psli->sli_flag, flag);
8053                         /* Unblock the async mailbox posting afterward */
8054                         lpfc_sli4_async_mbox_unblock(phba);
8055                 }
8056                 return rc;
8057         }
8058
8059         /* Now, interrupt mode asynchrous mailbox command */
8060         rc = lpfc_mbox_cmd_check(phba, mboxq);
8061         if (rc) {
8062                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8063                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
8064                                 "cannot issue Data: x%x x%x\n",
8065                                 mboxq->vport ? mboxq->vport->vpi : 0,
8066                                 mboxq->u.mb.mbxCommand,
8067                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8068                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8069                                 psli->sli_flag, flag);
8070                 goto out_not_finished;
8071         }
8072
8073         /* Put the mailbox command to the driver internal FIFO */
8074         psli->slistat.mbox_busy++;
8075         spin_lock_irqsave(&phba->hbalock, iflags);
8076         lpfc_mbox_put(phba, mboxq);
8077         spin_unlock_irqrestore(&phba->hbalock, iflags);
8078         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8079                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
8080                         "x%x (x%x/x%x) x%x x%x x%x\n",
8081                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8082                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8083                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8084                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8085                         phba->pport->port_state,
8086                         psli->sli_flag, MBX_NOWAIT);
8087         /* Wake up worker thread to transport mailbox command from head */
8088         lpfc_worker_wake_up(phba);
8089
8090         return MBX_BUSY;
8091
8092 out_not_finished:
8093         return MBX_NOT_FINISHED;
8094 }
8095
8096 /**
8097  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8098  * @phba: Pointer to HBA context object.
8099  *
8100  * This function is called by worker thread to send a mailbox command to
8101  * SLI4 HBA firmware.
8102  *
8103  **/
8104 int
8105 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8106 {
8107         struct lpfc_sli *psli = &phba->sli;
8108         LPFC_MBOXQ_t *mboxq;
8109         int rc = MBX_SUCCESS;
8110         unsigned long iflags;
8111         struct lpfc_mqe *mqe;
8112         uint32_t mbx_cmnd;
8113
8114         /* Check interrupt mode before post async mailbox command */
8115         if (unlikely(!phba->sli4_hba.intr_enable))
8116                 return MBX_NOT_FINISHED;
8117
8118         /* Check for mailbox command service token */
8119         spin_lock_irqsave(&phba->hbalock, iflags);
8120         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8121                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8122                 return MBX_NOT_FINISHED;
8123         }
8124         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8125                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8126                 return MBX_NOT_FINISHED;
8127         }
8128         if (unlikely(phba->sli.mbox_active)) {
8129                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8130                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8131                                 "0384 There is pending active mailbox cmd\n");
8132                 return MBX_NOT_FINISHED;
8133         }
8134         /* Take the mailbox command service token */
8135         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8136
8137         /* Get the next mailbox command from head of queue */
8138         mboxq = lpfc_mbox_get(phba);
8139
8140         /* If no more mailbox command waiting for post, we're done */
8141         if (!mboxq) {
8142                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8143                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8144                 return MBX_SUCCESS;
8145         }
8146         phba->sli.mbox_active = mboxq;
8147         spin_unlock_irqrestore(&phba->hbalock, iflags);
8148
8149         /* Check device readiness for posting mailbox command */
8150         rc = lpfc_mbox_dev_check(phba);
8151         if (unlikely(rc))
8152                 /* Driver clean routine will clean up pending mailbox */
8153                 goto out_not_finished;
8154
8155         /* Prepare the mbox command to be posted */
8156         mqe = &mboxq->u.mqe;
8157         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8158
8159         /* Start timer for the mbox_tmo and log some mailbox post messages */
8160         mod_timer(&psli->mbox_tmo, (jiffies +
8161                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8162
8163         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8164                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8165                         "x%x x%x\n",
8166                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8167                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8168                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8169                         phba->pport->port_state, psli->sli_flag);
8170
8171         if (mbx_cmnd != MBX_HEARTBEAT) {
8172                 if (mboxq->vport) {
8173                         lpfc_debugfs_disc_trc(mboxq->vport,
8174                                 LPFC_DISC_TRC_MBOX_VPORT,
8175                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8176                                 mbx_cmnd, mqe->un.mb_words[0],
8177                                 mqe->un.mb_words[1]);
8178                 } else {
8179                         lpfc_debugfs_disc_trc(phba->pport,
8180                                 LPFC_DISC_TRC_MBOX,
8181                                 "MBOX Send: cmd:x%x mb:x%x x%x",
8182                                 mbx_cmnd, mqe->un.mb_words[0],
8183                                 mqe->un.mb_words[1]);
8184                 }
8185         }
8186         psli->slistat.mbox_cmd++;
8187
8188         /* Post the mailbox command to the port */
8189         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8190         if (rc != MBX_SUCCESS) {
8191                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8192                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
8193                                 "cannot issue Data: x%x x%x\n",
8194                                 mboxq->vport ? mboxq->vport->vpi : 0,
8195                                 mboxq->u.mb.mbxCommand,
8196                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8197                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8198                                 psli->sli_flag, MBX_NOWAIT);
8199                 goto out_not_finished;
8200         }
8201
8202         return rc;
8203
8204 out_not_finished:
8205         spin_lock_irqsave(&phba->hbalock, iflags);
8206         if (phba->sli.mbox_active) {
8207                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8208                 __lpfc_mbox_cmpl_put(phba, mboxq);
8209                 /* Release the token */
8210                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8211                 phba->sli.mbox_active = NULL;
8212         }
8213         spin_unlock_irqrestore(&phba->hbalock, iflags);
8214
8215         return MBX_NOT_FINISHED;
8216 }
8217
8218 /**
8219  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8220  * @phba: Pointer to HBA context object.
8221  * @pmbox: Pointer to mailbox object.
8222  * @flag: Flag indicating how the mailbox need to be processed.
8223  *
8224  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8225  * the API jump table function pointer from the lpfc_hba struct.
8226  *
8227  * Return codes the caller owns the mailbox command after the return of the
8228  * function.
8229  **/
8230 int
8231 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8232 {
8233         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8234 }
8235
8236 /**
8237  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8238  * @phba: The hba struct for which this call is being executed.
8239  * @dev_grp: The HBA PCI-Device group number.
8240  *
8241  * This routine sets up the mbox interface API function jump table in @phba
8242  * struct.
8243  * Returns: 0 - success, -ENODEV - failure.
8244  **/
8245 int
8246 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8247 {
8248
8249         switch (dev_grp) {
8250         case LPFC_PCI_DEV_LP:
8251                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8252                 phba->lpfc_sli_handle_slow_ring_event =
8253                                 lpfc_sli_handle_slow_ring_event_s3;
8254                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8255                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8256                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8257                 break;
8258         case LPFC_PCI_DEV_OC:
8259                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8260                 phba->lpfc_sli_handle_slow_ring_event =
8261                                 lpfc_sli_handle_slow_ring_event_s4;
8262                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8263                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8264                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8265                 break;
8266         default:
8267                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8268                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
8269                                 dev_grp);
8270                 return -ENODEV;
8271                 break;
8272         }
8273         return 0;
8274 }
8275
8276 /**
8277  * __lpfc_sli_ringtx_put - Add an iocb to the txq
8278  * @phba: Pointer to HBA context object.
8279  * @pring: Pointer to driver SLI ring object.
8280  * @piocb: Pointer to address of newly added command iocb.
8281  *
8282  * This function is called with hbalock held to add a command
8283  * iocb to the txq when SLI layer cannot submit the command iocb
8284  * to the ring.
8285  **/
8286 void
8287 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8288                     struct lpfc_iocbq *piocb)
8289 {
8290         lockdep_assert_held(&phba->hbalock);
8291         /* Insert the caller's iocb in the txq tail for later processing. */
8292         list_add_tail(&piocb->list, &pring->txq);
8293 }
8294
8295 /**
8296  * lpfc_sli_next_iocb - Get the next iocb in the txq
8297  * @phba: Pointer to HBA context object.
8298  * @pring: Pointer to driver SLI ring object.
8299  * @piocb: Pointer to address of newly added command iocb.
8300  *
8301  * This function is called with hbalock held before a new
8302  * iocb is submitted to the firmware. This function checks
8303  * txq to flush the iocbs in txq to Firmware before
8304  * submitting new iocbs to the Firmware.
8305  * If there are iocbs in the txq which need to be submitted
8306  * to firmware, lpfc_sli_next_iocb returns the first element
8307  * of the txq after dequeuing it from txq.
8308  * If there is no iocb in the txq then the function will return
8309  * *piocb and *piocb is set to NULL. Caller needs to check
8310  * *piocb to find if there are more commands in the txq.
8311  **/
8312 static struct lpfc_iocbq *
8313 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8314                    struct lpfc_iocbq **piocb)
8315 {
8316         struct lpfc_iocbq * nextiocb;
8317
8318         lockdep_assert_held(&phba->hbalock);
8319
8320         nextiocb = lpfc_sli_ringtx_get(phba, pring);
8321         if (!nextiocb) {
8322                 nextiocb = *piocb;
8323                 *piocb = NULL;
8324         }
8325
8326         return nextiocb;
8327 }
8328
8329 /**
8330  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8331  * @phba: Pointer to HBA context object.
8332  * @ring_number: SLI ring number to issue iocb on.
8333  * @piocb: Pointer to command iocb.
8334  * @flag: Flag indicating if this command can be put into txq.
8335  *
8336  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8337  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8338  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8339  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8340  * this function allows only iocbs for posting buffers. This function finds
8341  * next available slot in the command ring and posts the command to the
8342  * available slot and writes the port attention register to request HBA start
8343  * processing new iocb. If there is no slot available in the ring and
8344  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8345  * the function returns IOCB_BUSY.
8346  *
8347  * This function is called with hbalock held. The function will return success
8348  * after it successfully submit the iocb to firmware or after adding to the
8349  * txq.
8350  **/
8351 static int
8352 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8353                     struct lpfc_iocbq *piocb, uint32_t flag)
8354 {
8355         struct lpfc_iocbq *nextiocb;
8356         IOCB_t *iocb;
8357         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
8358
8359         lockdep_assert_held(&phba->hbalock);
8360
8361         if (piocb->iocb_cmpl && (!piocb->vport) &&
8362            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8363            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8364                 lpfc_printf_log(phba, KERN_ERR,
8365                                 LOG_SLI | LOG_VPORT,
8366                                 "1807 IOCB x%x failed. No vport\n",
8367                                 piocb->iocb.ulpCommand);
8368                 dump_stack();
8369                 return IOCB_ERROR;
8370         }
8371
8372
8373         /* If the PCI channel is in offline state, do not post iocbs. */
8374         if (unlikely(pci_channel_offline(phba->pcidev)))
8375                 return IOCB_ERROR;
8376
8377         /* If HBA has a deferred error attention, fail the iocb. */
8378         if (unlikely(phba->hba_flag & DEFER_ERATT))
8379                 return IOCB_ERROR;
8380
8381         /*
8382          * We should never get an IOCB if we are in a < LINK_DOWN state
8383          */
8384         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8385                 return IOCB_ERROR;
8386
8387         /*
8388          * Check to see if we are blocking IOCB processing because of a
8389          * outstanding event.
8390          */
8391         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8392                 goto iocb_busy;
8393
8394         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8395                 /*
8396                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8397                  * can be issued if the link is not up.
8398                  */
8399                 switch (piocb->iocb.ulpCommand) {
8400                 case CMD_GEN_REQUEST64_CR:
8401                 case CMD_GEN_REQUEST64_CX:
8402                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8403                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8404                                         FC_RCTL_DD_UNSOL_CMD) ||
8405                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8406                                         MENLO_TRANSPORT_TYPE))
8407
8408                                 goto iocb_busy;
8409                         break;
8410                 case CMD_QUE_RING_BUF_CN:
8411                 case CMD_QUE_RING_BUF64_CN:
8412                         /*
8413                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8414                          * completion, iocb_cmpl MUST be 0.
8415                          */
8416                         if (piocb->iocb_cmpl)
8417                                 piocb->iocb_cmpl = NULL;
8418                         /*FALLTHROUGH*/
8419                 case CMD_CREATE_XRI_CR:
8420                 case CMD_CLOSE_XRI_CN:
8421                 case CMD_CLOSE_XRI_CX:
8422                         break;
8423                 default:
8424                         goto iocb_busy;
8425                 }
8426
8427         /*
8428          * For FCP commands, we must be in a state where we can process link
8429          * attention events.
8430          */
8431         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
8432                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8433                 goto iocb_busy;
8434         }
8435
8436         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8437                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8438                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8439
8440         if (iocb)
8441                 lpfc_sli_update_ring(phba, pring);
8442         else
8443                 lpfc_sli_update_full_ring(phba, pring);
8444
8445         if (!piocb)
8446                 return IOCB_SUCCESS;
8447
8448         goto out_busy;
8449
8450  iocb_busy:
8451         pring->stats.iocb_cmd_delay++;
8452
8453  out_busy:
8454
8455         if (!(flag & SLI_IOCB_RET_IOCB)) {
8456                 __lpfc_sli_ringtx_put(phba, pring, piocb);
8457                 return IOCB_SUCCESS;
8458         }
8459
8460         return IOCB_BUSY;
8461 }
8462
8463 /**
8464  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8465  * @phba: Pointer to HBA context object.
8466  * @piocb: Pointer to command iocb.
8467  * @sglq: Pointer to the scatter gather queue object.
8468  *
8469  * This routine converts the bpl or bde that is in the IOCB
8470  * to a sgl list for the sli4 hardware. The physical address
8471  * of the bpl/bde is converted back to a virtual address.
8472  * If the IOCB contains a BPL then the list of BDE's is
8473  * converted to sli4_sge's. If the IOCB contains a single
8474  * BDE then it is converted to a single sli_sge.
8475  * The IOCB is still in cpu endianess so the contents of
8476  * the bpl can be used without byte swapping.
8477  *
8478  * Returns valid XRI = Success, NO_XRI = Failure.
8479 **/
8480 static uint16_t
8481 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8482                 struct lpfc_sglq *sglq)
8483 {
8484         uint16_t xritag = NO_XRI;
8485         struct ulp_bde64 *bpl = NULL;
8486         struct ulp_bde64 bde;
8487         struct sli4_sge *sgl  = NULL;
8488         struct lpfc_dmabuf *dmabuf;
8489         IOCB_t *icmd;
8490         int numBdes = 0;
8491         int i = 0;
8492         uint32_t offset = 0; /* accumulated offset in the sg request list */
8493         int inbound = 0; /* number of sg reply entries inbound from firmware */
8494
8495         if (!piocbq || !sglq)
8496                 return xritag;
8497
8498         sgl  = (struct sli4_sge *)sglq->sgl;
8499         icmd = &piocbq->iocb;
8500         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8501                 return sglq->sli4_xritag;
8502         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8503                 numBdes = icmd->un.genreq64.bdl.bdeSize /
8504                                 sizeof(struct ulp_bde64);
8505                 /* The addrHigh and addrLow fields within the IOCB
8506                  * have not been byteswapped yet so there is no
8507                  * need to swap them back.
8508                  */
8509                 if (piocbq->context3)
8510                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8511                 else
8512                         return xritag;
8513
8514                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
8515                 if (!bpl)
8516                         return xritag;
8517
8518                 for (i = 0; i < numBdes; i++) {
8519                         /* Should already be byte swapped. */
8520                         sgl->addr_hi = bpl->addrHigh;
8521                         sgl->addr_lo = bpl->addrLow;
8522
8523                         sgl->word2 = le32_to_cpu(sgl->word2);
8524                         if ((i+1) == numBdes)
8525                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
8526                         else
8527                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
8528                         /* swap the size field back to the cpu so we
8529                          * can assign it to the sgl.
8530                          */
8531                         bde.tus.w = le32_to_cpu(bpl->tus.w);
8532                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8533                         /* The offsets in the sgl need to be accumulated
8534                          * separately for the request and reply lists.
8535                          * The request is always first, the reply follows.
8536                          */
8537                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8538                                 /* add up the reply sg entries */
8539                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8540                                         inbound++;
8541                                 /* first inbound? reset the offset */
8542                                 if (inbound == 1)
8543                                         offset = 0;
8544                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8545                                 bf_set(lpfc_sli4_sge_type, sgl,
8546                                         LPFC_SGE_TYPE_DATA);
8547                                 offset += bde.tus.f.bdeSize;
8548                         }
8549                         sgl->word2 = cpu_to_le32(sgl->word2);
8550                         bpl++;
8551                         sgl++;
8552                 }
8553         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8554                         /* The addrHigh and addrLow fields of the BDE have not
8555                          * been byteswapped yet so they need to be swapped
8556                          * before putting them in the sgl.
8557                          */
8558                         sgl->addr_hi =
8559                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8560                         sgl->addr_lo =
8561                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8562                         sgl->word2 = le32_to_cpu(sgl->word2);
8563                         bf_set(lpfc_sli4_sge_last, sgl, 1);
8564                         sgl->word2 = cpu_to_le32(sgl->word2);
8565                         sgl->sge_len =
8566                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8567         }
8568         return sglq->sli4_xritag;
8569 }
8570
8571 /**
8572  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8573  * @phba: Pointer to HBA context object.
8574  * @piocb: Pointer to command iocb.
8575  * @wqe: Pointer to the work queue entry.
8576  *
8577  * This routine converts the iocb command to its Work Queue Entry
8578  * equivalent. The wqe pointer should not have any fields set when
8579  * this routine is called because it will memcpy over them.
8580  * This routine does not set the CQ_ID or the WQEC bits in the
8581  * wqe.
8582  *
8583  * Returns: 0 = Success, IOCB_ERROR = Failure.
8584  **/
8585 static int
8586 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8587                 union lpfc_wqe *wqe)
8588 {
8589         uint32_t xmit_len = 0, total_len = 0;
8590         uint8_t ct = 0;
8591         uint32_t fip;
8592         uint32_t abort_tag;
8593         uint8_t command_type = ELS_COMMAND_NON_FIP;
8594         uint8_t cmnd;
8595         uint16_t xritag;
8596         uint16_t abrt_iotag;
8597         struct lpfc_iocbq *abrtiocbq;
8598         struct ulp_bde64 *bpl = NULL;
8599         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8600         int numBdes, i;
8601         struct ulp_bde64 bde;
8602         struct lpfc_nodelist *ndlp;
8603         uint32_t *pcmd;
8604         uint32_t if_type;
8605
8606         fip = phba->hba_flag & HBA_FIP_SUPPORT;
8607         /* The fcp commands will set command type */
8608         if (iocbq->iocb_flag &  LPFC_IO_FCP)
8609                 command_type = FCP_COMMAND;
8610         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8611                 command_type = ELS_COMMAND_FIP;
8612         else
8613                 command_type = ELS_COMMAND_NON_FIP;
8614
8615         if (phba->fcp_embed_io)
8616                 memset(wqe, 0, sizeof(union lpfc_wqe128));
8617         /* Some of the fields are in the right position already */
8618         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8619         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8620         wqe->generic.wqe_com.word10 = 0;
8621
8622         abort_tag = (uint32_t) iocbq->iotag;
8623         xritag = iocbq->sli4_xritag;
8624         /* words0-2 bpl convert bde */
8625         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8626                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8627                                 sizeof(struct ulp_bde64);
8628                 bpl  = (struct ulp_bde64 *)
8629                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8630                 if (!bpl)
8631                         return IOCB_ERROR;
8632
8633                 /* Should already be byte swapped. */
8634                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
8635                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
8636                 /* swap the size field back to the cpu so we
8637                  * can assign it to the sgl.
8638                  */
8639                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
8640                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8641                 total_len = 0;
8642                 for (i = 0; i < numBdes; i++) {
8643                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
8644                         total_len += bde.tus.f.bdeSize;
8645                 }
8646         } else
8647                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8648
8649         iocbq->iocb.ulpIoTag = iocbq->iotag;
8650         cmnd = iocbq->iocb.ulpCommand;
8651
8652         switch (iocbq->iocb.ulpCommand) {
8653         case CMD_ELS_REQUEST64_CR:
8654                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8655                         ndlp = iocbq->context_un.ndlp;
8656                 else
8657                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
8658                 if (!iocbq->iocb.ulpLe) {
8659                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8660                                 "2007 Only Limited Edition cmd Format"
8661                                 " supported 0x%x\n",
8662                                 iocbq->iocb.ulpCommand);
8663                         return IOCB_ERROR;
8664                 }
8665
8666                 wqe->els_req.payload_len = xmit_len;
8667                 /* Els_reguest64 has a TMO */
8668                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8669                         iocbq->iocb.ulpTimeout);
8670                 /* Need a VF for word 4 set the vf bit*/
8671                 bf_set(els_req64_vf, &wqe->els_req, 0);
8672                 /* And a VFID for word 12 */
8673                 bf_set(els_req64_vfid, &wqe->els_req, 0);
8674                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8675                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8676                        iocbq->iocb.ulpContext);
8677                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8678                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8679                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8680                 if (command_type == ELS_COMMAND_FIP)
8681                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8682                                         >> LPFC_FIP_ELS_ID_SHIFT);
8683                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8684                                         iocbq->context2)->virt);
8685                 if_type = bf_get(lpfc_sli_intf_if_type,
8686                                         &phba->sli4_hba.sli_intf);
8687                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8688                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8689                                 *pcmd == ELS_CMD_SCR ||
8690                                 *pcmd == ELS_CMD_FDISC ||
8691                                 *pcmd == ELS_CMD_LOGO ||
8692                                 *pcmd == ELS_CMD_PLOGI)) {
8693                                 bf_set(els_req64_sp, &wqe->els_req, 1);
8694                                 bf_set(els_req64_sid, &wqe->els_req,
8695                                         iocbq->vport->fc_myDID);
8696                                 if ((*pcmd == ELS_CMD_FLOGI) &&
8697                                         !(phba->fc_topology ==
8698                                                 LPFC_TOPOLOGY_LOOP))
8699                                         bf_set(els_req64_sid, &wqe->els_req, 0);
8700                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8701                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8702                                         phba->vpi_ids[iocbq->vport->vpi]);
8703                         } else if (pcmd && iocbq->context1) {
8704                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8705                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8706                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8707                         }
8708                 }
8709                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8710                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8711                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8712                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8713                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8714                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8715                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8716                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8717                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
8718                 break;
8719         case CMD_XMIT_SEQUENCE64_CX:
8720                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8721                        iocbq->iocb.un.ulpWord[3]);
8722                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8723                        iocbq->iocb.unsli3.rcvsli3.ox_id);
8724                 /* The entire sequence is transmitted for this IOCB */
8725                 xmit_len = total_len;
8726                 cmnd = CMD_XMIT_SEQUENCE64_CR;
8727                 if (phba->link_flag & LS_LOOPBACK_MODE)
8728                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8729         case CMD_XMIT_SEQUENCE64_CR:
8730                 /* word3 iocb=io_tag32 wqe=reserved */
8731                 wqe->xmit_sequence.rsvd3 = 0;
8732                 /* word4 relative_offset memcpy */
8733                 /* word5 r_ctl/df_ctl memcpy */
8734                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8735                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8736                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8737                        LPFC_WQE_IOD_WRITE);
8738                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8739                        LPFC_WQE_LENLOC_WORD12);
8740                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8741                 wqe->xmit_sequence.xmit_len = xmit_len;
8742                 command_type = OTHER_COMMAND;
8743                 break;
8744         case CMD_XMIT_BCAST64_CN:
8745                 /* word3 iocb=iotag32 wqe=seq_payload_len */
8746                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8747                 /* word4 iocb=rsvd wqe=rsvd */
8748                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8749                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8750                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8751                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8752                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8753                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8754                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8755                        LPFC_WQE_LENLOC_WORD3);
8756                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8757                 break;
8758         case CMD_FCP_IWRITE64_CR:
8759                 command_type = FCP_COMMAND_DATA_OUT;
8760                 /* word3 iocb=iotag wqe=payload_offset_len */
8761                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8762                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8763                        xmit_len + sizeof(struct fcp_rsp));
8764                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8765                        0);
8766                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8767                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8768                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8769                        iocbq->iocb.ulpFCP2Rcvy);
8770                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8771                 /* Always open the exchange */
8772                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8773                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8774                        LPFC_WQE_LENLOC_WORD4);
8775                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8776                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8777                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8778                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8779                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8780                         if (iocbq->priority) {
8781                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8782                                        (iocbq->priority << 1));
8783                         } else {
8784                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8785                                        (phba->cfg_XLanePriority << 1));
8786                         }
8787                 }
8788                 /* Note, word 10 is already initialized to 0 */
8789
8790                 if (phba->fcp_embed_io) {
8791                         struct lpfc_scsi_buf *lpfc_cmd;
8792                         struct sli4_sge *sgl;
8793                         union lpfc_wqe128 *wqe128;
8794                         struct fcp_cmnd *fcp_cmnd;
8795                         uint32_t *ptr;
8796
8797                         /* 128 byte wqe support here */
8798                         wqe128 = (union lpfc_wqe128 *)wqe;
8799
8800                         lpfc_cmd = iocbq->context1;
8801                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8802                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
8803
8804                         /* Word 0-2 - FCP_CMND */
8805                         wqe128->generic.bde.tus.f.bdeFlags =
8806                                 BUFF_TYPE_BDE_IMMED;
8807                         wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8808                         wqe128->generic.bde.addrHigh = 0;
8809                         wqe128->generic.bde.addrLow =  88;  /* Word 22 */
8810
8811                         bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8812
8813                         /* Word 22-29  FCP CMND Payload */
8814                         ptr = &wqe128->words[22];
8815                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8816                 }
8817                 break;
8818         case CMD_FCP_IREAD64_CR:
8819                 /* word3 iocb=iotag wqe=payload_offset_len */
8820                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8821                 bf_set(payload_offset_len, &wqe->fcp_iread,
8822                        xmit_len + sizeof(struct fcp_rsp));
8823                 bf_set(cmd_buff_len, &wqe->fcp_iread,
8824                        0);
8825                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8826                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8827                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8828                        iocbq->iocb.ulpFCP2Rcvy);
8829                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8830                 /* Always open the exchange */
8831                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8832                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8833                        LPFC_WQE_LENLOC_WORD4);
8834                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8835                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8836                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8837                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8838                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8839                         if (iocbq->priority) {
8840                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8841                                        (iocbq->priority << 1));
8842                         } else {
8843                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8844                                        (phba->cfg_XLanePriority << 1));
8845                         }
8846                 }
8847                 /* Note, word 10 is already initialized to 0 */
8848
8849                 if (phba->fcp_embed_io) {
8850                         struct lpfc_scsi_buf *lpfc_cmd;
8851                         struct sli4_sge *sgl;
8852                         union lpfc_wqe128 *wqe128;
8853                         struct fcp_cmnd *fcp_cmnd;
8854                         uint32_t *ptr;
8855
8856                         /* 128 byte wqe support here */
8857                         wqe128 = (union lpfc_wqe128 *)wqe;
8858
8859                         lpfc_cmd = iocbq->context1;
8860                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8861                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
8862
8863                         /* Word 0-2 - FCP_CMND */
8864                         wqe128->generic.bde.tus.f.bdeFlags =
8865                                 BUFF_TYPE_BDE_IMMED;
8866                         wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8867                         wqe128->generic.bde.addrHigh = 0;
8868                         wqe128->generic.bde.addrLow =  88;  /* Word 22 */
8869
8870                         bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8871
8872                         /* Word 22-29  FCP CMND Payload */
8873                         ptr = &wqe128->words[22];
8874                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8875                 }
8876                 break;
8877         case CMD_FCP_ICMND64_CR:
8878                 /* word3 iocb=iotag wqe=payload_offset_len */
8879                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8880                 bf_set(payload_offset_len, &wqe->fcp_icmd,
8881                        xmit_len + sizeof(struct fcp_rsp));
8882                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8883                        0);
8884                 /* word3 iocb=IO_TAG wqe=reserved */
8885                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8886                 /* Always open the exchange */
8887                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8888                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8889                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8890                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8891                        LPFC_WQE_LENLOC_NONE);
8892                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8893                        iocbq->iocb.ulpFCP2Rcvy);
8894                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8895                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8896                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8897                         if (iocbq->priority) {
8898                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8899                                        (iocbq->priority << 1));
8900                         } else {
8901                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8902                                        (phba->cfg_XLanePriority << 1));
8903                         }
8904                 }
8905                 /* Note, word 10 is already initialized to 0 */
8906
8907                 if (phba->fcp_embed_io) {
8908                         struct lpfc_scsi_buf *lpfc_cmd;
8909                         struct sli4_sge *sgl;
8910                         union lpfc_wqe128 *wqe128;
8911                         struct fcp_cmnd *fcp_cmnd;
8912                         uint32_t *ptr;
8913
8914                         /* 128 byte wqe support here */
8915                         wqe128 = (union lpfc_wqe128 *)wqe;
8916
8917                         lpfc_cmd = iocbq->context1;
8918                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8919                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
8920
8921                         /* Word 0-2 - FCP_CMND */
8922                         wqe128->generic.bde.tus.f.bdeFlags =
8923                                 BUFF_TYPE_BDE_IMMED;
8924                         wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8925                         wqe128->generic.bde.addrHigh = 0;
8926                         wqe128->generic.bde.addrLow =  88;  /* Word 22 */
8927
8928                         bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8929
8930                         /* Word 22-29  FCP CMND Payload */
8931                         ptr = &wqe128->words[22];
8932                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8933                 }
8934                 break;
8935         case CMD_GEN_REQUEST64_CR:
8936                 /* For this command calculate the xmit length of the
8937                  * request bde.
8938                  */
8939                 xmit_len = 0;
8940                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8941                         sizeof(struct ulp_bde64);
8942                 for (i = 0; i < numBdes; i++) {
8943                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8944                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8945                                 break;
8946                         xmit_len += bde.tus.f.bdeSize;
8947                 }
8948                 /* word3 iocb=IO_TAG wqe=request_payload_len */
8949                 wqe->gen_req.request_payload_len = xmit_len;
8950                 /* word4 iocb=parameter wqe=relative_offset memcpy */
8951                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8952                 /* word6 context tag copied in memcpy */
8953                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
8954                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8955                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8956                                 "2015 Invalid CT %x command 0x%x\n",
8957                                 ct, iocbq->iocb.ulpCommand);
8958                         return IOCB_ERROR;
8959                 }
8960                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8961                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8962                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8963                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8964                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8965                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8966                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8967                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8968                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
8969                 command_type = OTHER_COMMAND;
8970                 break;
8971         case CMD_XMIT_ELS_RSP64_CX:
8972                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8973                 /* words0-2 BDE memcpy */
8974                 /* word3 iocb=iotag32 wqe=response_payload_len */
8975                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8976                 /* word4 */
8977                 wqe->xmit_els_rsp.word4 = 0;
8978                 /* word5 iocb=rsvd wge=did */
8979                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8980                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
8981
8982                 if_type = bf_get(lpfc_sli_intf_if_type,
8983                                         &phba->sli4_hba.sli_intf);
8984                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8985                         if (iocbq->vport->fc_flag & FC_PT2PT) {
8986                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8987                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8988                                         iocbq->vport->fc_myDID);
8989                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
8990                                         bf_set(wqe_els_did,
8991                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
8992                                 }
8993                         }
8994                 }
8995                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8996                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8997                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8998                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8999                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9000                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
9001                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9002                                phba->vpi_ids[iocbq->vport->vpi]);
9003                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9004                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9005                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9006                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9007                        LPFC_WQE_LENLOC_WORD3);
9008                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9009                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9010                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9011                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9012                                         iocbq->context2)->virt);
9013                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9014                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9015                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9016                                         iocbq->vport->fc_myDID);
9017                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9018                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9019                                         phba->vpi_ids[phba->pport->vpi]);
9020                 }
9021                 command_type = OTHER_COMMAND;
9022                 break;
9023         case CMD_CLOSE_XRI_CN:
9024         case CMD_ABORT_XRI_CN:
9025         case CMD_ABORT_XRI_CX:
9026                 /* words 0-2 memcpy should be 0 rserved */
9027                 /* port will send abts */
9028                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9029                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9030                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9031                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9032                 } else
9033                         fip = 0;
9034
9035                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9036                         /*
9037                          * The link is down, or the command was ELS_FIP
9038                          * so the fw does not need to send abts
9039                          * on the wire.
9040                          */
9041                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9042                 else
9043                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9044                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9045                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9046                 wqe->abort_cmd.rsrvd5 = 0;
9047                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9048                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9049                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9050                 /*
9051                  * The abort handler will send us CMD_ABORT_XRI_CN or
9052                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9053                  */
9054                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9055                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9056                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9057                        LPFC_WQE_LENLOC_NONE);
9058                 cmnd = CMD_ABORT_XRI_CX;
9059                 command_type = OTHER_COMMAND;
9060                 xritag = 0;
9061                 break;
9062         case CMD_XMIT_BLS_RSP64_CX:
9063                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9064                 /* As BLS ABTS RSP WQE is very different from other WQEs,
9065                  * we re-construct this WQE here based on information in
9066                  * iocbq from scratch.
9067                  */
9068                 memset(wqe, 0, sizeof(union lpfc_wqe));
9069                 /* OX_ID is invariable to who sent ABTS to CT exchange */
9070                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9071                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9072                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9073                     LPFC_ABTS_UNSOL_INT) {
9074                         /* ABTS sent by initiator to CT exchange, the
9075                          * RX_ID field will be filled with the newly
9076                          * allocated responder XRI.
9077                          */
9078                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9079                                iocbq->sli4_xritag);
9080                 } else {
9081                         /* ABTS sent by responder to CT exchange, the
9082                          * RX_ID field will be filled with the responder
9083                          * RX_ID from ABTS.
9084                          */
9085                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9086                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9087                 }
9088                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9089                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9090
9091                 /* Use CT=VPI */
9092                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9093                         ndlp->nlp_DID);
9094                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9095                         iocbq->iocb.ulpContext);
9096                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9097                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9098                         phba->vpi_ids[phba->pport->vpi]);
9099                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9100                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9101                        LPFC_WQE_LENLOC_NONE);
9102                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9103                 command_type = OTHER_COMMAND;
9104                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9105                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9106                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9107                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9108                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9109                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9110                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9111                 }
9112
9113                 break;
9114         case CMD_XRI_ABORTED_CX:
9115         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9116         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9117         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9118         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9119         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9120         default:
9121                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9122                                 "2014 Invalid command 0x%x\n",
9123                                 iocbq->iocb.ulpCommand);
9124                 return IOCB_ERROR;
9125                 break;
9126         }
9127
9128         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9129                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9130         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9131                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9132         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9133                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9134         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9135                               LPFC_IO_DIF_INSERT);
9136         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9137         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9138         wqe->generic.wqe_com.abort_tag = abort_tag;
9139         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9140         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9141         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9142         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9143         return 0;
9144 }
9145
9146 /**
9147  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9148  * @phba: Pointer to HBA context object.
9149  * @ring_number: SLI ring number to issue iocb on.
9150  * @piocb: Pointer to command iocb.
9151  * @flag: Flag indicating if this command can be put into txq.
9152  *
9153  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9154  * an iocb command to an HBA with SLI-4 interface spec.
9155  *
9156  * This function is called with hbalock held. The function will return success
9157  * after it successfully submit the iocb to firmware or after adding to the
9158  * txq.
9159  **/
9160 static int
9161 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9162                          struct lpfc_iocbq *piocb, uint32_t flag)
9163 {
9164         struct lpfc_sglq *sglq;
9165         union lpfc_wqe *wqe;
9166         union lpfc_wqe128 wqe128;
9167         struct lpfc_queue *wq;
9168         struct lpfc_sli_ring *pring;
9169
9170         /* Get the WQ */
9171         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9172             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9173                 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9174                         wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9175                 else
9176                         wq = phba->sli4_hba.oas_wq;
9177         } else {
9178                 wq = phba->sli4_hba.els_wq;
9179         }
9180
9181         /* Get corresponding ring */
9182         pring = wq->pring;
9183
9184         /*
9185          * The WQE can be either 64 or 128 bytes,
9186          * so allocate space on the stack assuming the largest.
9187          */
9188         wqe = (union lpfc_wqe *)&wqe128;
9189
9190         lockdep_assert_held(&phba->hbalock);
9191
9192         if (piocb->sli4_xritag == NO_XRI) {
9193                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9194                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9195                         sglq = NULL;
9196                 else {
9197                         if (!list_empty(&pring->txq)) {
9198                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
9199                                         __lpfc_sli_ringtx_put(phba,
9200                                                 pring, piocb);
9201                                         return IOCB_SUCCESS;
9202                                 } else {
9203                                         return IOCB_BUSY;
9204                                 }
9205                         } else {
9206                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9207                                 if (!sglq) {
9208                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
9209                                                 __lpfc_sli_ringtx_put(phba,
9210                                                                 pring,
9211                                                                 piocb);
9212                                                 return IOCB_SUCCESS;
9213                                         } else
9214                                                 return IOCB_BUSY;
9215                                 }
9216                         }
9217                 }
9218         } else if (piocb->iocb_flag &  LPFC_IO_FCP)
9219                 /* These IO's already have an XRI and a mapped sgl. */
9220                 sglq = NULL;
9221         else {
9222                 /*
9223                  * This is a continuation of a commandi,(CX) so this
9224                  * sglq is on the active list
9225                  */
9226                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9227                 if (!sglq)
9228                         return IOCB_ERROR;
9229         }
9230
9231         if (sglq) {
9232                 piocb->sli4_lxritag = sglq->sli4_lxritag;
9233                 piocb->sli4_xritag = sglq->sli4_xritag;
9234                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9235                         return IOCB_ERROR;
9236         }
9237
9238         if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
9239                 return IOCB_ERROR;
9240
9241         if (lpfc_sli4_wq_put(wq, wqe))
9242                 return IOCB_ERROR;
9243         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9244
9245         return 0;
9246 }
9247
9248 /**
9249  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9250  *
9251  * This routine wraps the actual lockless version for issusing IOCB function
9252  * pointer from the lpfc_hba struct.
9253  *
9254  * Return codes:
9255  * IOCB_ERROR - Error
9256  * IOCB_SUCCESS - Success
9257  * IOCB_BUSY - Busy
9258  **/
9259 int
9260 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9261                 struct lpfc_iocbq *piocb, uint32_t flag)
9262 {
9263         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9264 }
9265
9266 /**
9267  * lpfc_sli_api_table_setup - Set up sli api function jump table
9268  * @phba: The hba struct for which this call is being executed.
9269  * @dev_grp: The HBA PCI-Device group number.
9270  *
9271  * This routine sets up the SLI interface API function jump table in @phba
9272  * struct.
9273  * Returns: 0 - success, -ENODEV - failure.
9274  **/
9275 int
9276 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9277 {
9278
9279         switch (dev_grp) {
9280         case LPFC_PCI_DEV_LP:
9281                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9282                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9283                 break;
9284         case LPFC_PCI_DEV_OC:
9285                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9286                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9287                 break;
9288         default:
9289                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9290                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
9291                                 dev_grp);
9292                 return -ENODEV;
9293                 break;
9294         }
9295         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9296         return 0;
9297 }
9298
9299 /**
9300  * lpfc_sli4_calc_ring - Calculates which ring to use
9301  * @phba: Pointer to HBA context object.
9302  * @piocb: Pointer to command iocb.
9303  *
9304  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9305  * hba_wqidx, thus we need to calculate the corresponding ring.
9306  * Since ABORTS must go on the same WQ of the command they are
9307  * aborting, we use command's hba_wqidx.
9308  */
9309 struct lpfc_sli_ring *
9310 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9311 {
9312         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9313                 if (!(phba->cfg_fof) ||
9314                     (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9315                         if (unlikely(!phba->sli4_hba.fcp_wq))
9316                                 return NULL;
9317                         /*
9318                          * for abort iocb hba_wqidx should already
9319                          * be setup based on what work queue we used.
9320                          */
9321                         if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9322                                 piocb->hba_wqidx =
9323                                         lpfc_sli4_scmd_to_wqidx_distr(phba,
9324                                                               piocb->context1);
9325                         return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
9326                 } else {
9327                         if (unlikely(!phba->sli4_hba.oas_wq))
9328                                 return NULL;
9329                         piocb->hba_wqidx = 0;
9330                         return phba->sli4_hba.oas_wq->pring;
9331                 }
9332         } else {
9333                 if (unlikely(!phba->sli4_hba.els_wq))
9334                         return NULL;
9335                 piocb->hba_wqidx = 0;
9336                 return phba->sli4_hba.els_wq->pring;
9337         }
9338 }
9339
9340 /**
9341  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9342  * @phba: Pointer to HBA context object.
9343  * @pring: Pointer to driver SLI ring object.
9344  * @piocb: Pointer to command iocb.
9345  * @flag: Flag indicating if this command can be put into txq.
9346  *
9347  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9348  * function. This function gets the hbalock and calls
9349  * __lpfc_sli_issue_iocb function and will return the error returned
9350  * by __lpfc_sli_issue_iocb function. This wrapper is used by
9351  * functions which do not hold hbalock.
9352  **/
9353 int
9354 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9355                     struct lpfc_iocbq *piocb, uint32_t flag)
9356 {
9357         struct lpfc_hba_eq_hdl *hba_eq_hdl;
9358         struct lpfc_sli_ring *pring;
9359         struct lpfc_queue *fpeq;
9360         struct lpfc_eqe *eqe;
9361         unsigned long iflags;
9362         int rc, idx;
9363
9364         if (phba->sli_rev == LPFC_SLI_REV4) {
9365                 pring = lpfc_sli4_calc_ring(phba, piocb);
9366                 if (unlikely(pring == NULL))
9367                         return IOCB_ERROR;
9368
9369                 spin_lock_irqsave(&pring->ring_lock, iflags);
9370                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9371                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9372
9373                 if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
9374                         idx = piocb->hba_wqidx;
9375                         hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
9376
9377                         if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
9378
9379                                 /* Get associated EQ with this index */
9380                                 fpeq = phba->sli4_hba.hba_eq[idx];
9381
9382                                 /* Turn off interrupts from this EQ */
9383                                 lpfc_sli4_eq_clr_intr(fpeq);
9384
9385                                 /*
9386                                  * Process all the events on FCP EQ
9387                                  */
9388                                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9389                                         lpfc_sli4_hba_handle_eqe(phba,
9390                                                 eqe, idx);
9391                                         fpeq->EQ_processed++;
9392                                 }
9393
9394                                 /* Always clear and re-arm the EQ */
9395                                 lpfc_sli4_eq_release(fpeq,
9396                                         LPFC_QUEUE_REARM);
9397                         }
9398                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
9399                 }
9400         } else {
9401                 /* For now, SLI2/3 will still use hbalock */
9402                 spin_lock_irqsave(&phba->hbalock, iflags);
9403                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9404                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9405         }
9406         return rc;
9407 }
9408
9409 /**
9410  * lpfc_extra_ring_setup - Extra ring setup function
9411  * @phba: Pointer to HBA context object.
9412  *
9413  * This function is called while driver attaches with the
9414  * HBA to setup the extra ring. The extra ring is used
9415  * only when driver needs to support target mode functionality
9416  * or IP over FC functionalities.
9417  *
9418  * This function is called with no lock held. SLI3 only.
9419  **/
9420 static int
9421 lpfc_extra_ring_setup( struct lpfc_hba *phba)
9422 {
9423         struct lpfc_sli *psli;
9424         struct lpfc_sli_ring *pring;
9425
9426         psli = &phba->sli;
9427
9428         /* Adjust cmd/rsp ring iocb entries more evenly */
9429
9430         /* Take some away from the FCP ring */
9431         pring = &psli->sli3_ring[LPFC_FCP_RING];
9432         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9433         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9434         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9435         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9436
9437         /* and give them to the extra ring */
9438         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
9439
9440         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9441         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9442         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9443         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9444
9445         /* Setup default profile for this ring */
9446         pring->iotag_max = 4096;
9447         pring->num_mask = 1;
9448         pring->prt[0].profile = 0;      /* Mask 0 */
9449         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9450         pring->prt[0].type = phba->cfg_multi_ring_type;
9451         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9452         return 0;
9453 }
9454
9455 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9456  * @phba: Pointer to HBA context object.
9457  * @iocbq: Pointer to iocb object.
9458  *
9459  * The async_event handler calls this routine when it receives
9460  * an ASYNC_STATUS_CN event from the port.  The port generates
9461  * this event when an Abort Sequence request to an rport fails
9462  * twice in succession.  The abort could be originated by the
9463  * driver or by the port.  The ABTS could have been for an ELS
9464  * or FCP IO.  The port only generates this event when an ABTS
9465  * fails to complete after one retry.
9466  */
9467 static void
9468 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9469                           struct lpfc_iocbq *iocbq)
9470 {
9471         struct lpfc_nodelist *ndlp = NULL;
9472         uint16_t rpi = 0, vpi = 0;
9473         struct lpfc_vport *vport = NULL;
9474
9475         /* The rpi in the ulpContext is vport-sensitive. */
9476         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9477         rpi = iocbq->iocb.ulpContext;
9478
9479         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9480                         "3092 Port generated ABTS async event "
9481                         "on vpi %d rpi %d status 0x%x\n",
9482                         vpi, rpi, iocbq->iocb.ulpStatus);
9483
9484         vport = lpfc_find_vport_by_vpid(phba, vpi);
9485         if (!vport)
9486                 goto err_exit;
9487         ndlp = lpfc_findnode_rpi(vport, rpi);
9488         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9489                 goto err_exit;
9490
9491         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9492                 lpfc_sli_abts_recover_port(vport, ndlp);
9493         return;
9494
9495  err_exit:
9496         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9497                         "3095 Event Context not found, no "
9498                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9499                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9500                         vpi, rpi);
9501 }
9502
9503 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9504  * @phba: pointer to HBA context object.
9505  * @ndlp: nodelist pointer for the impacted rport.
9506  * @axri: pointer to the wcqe containing the failed exchange.
9507  *
9508  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9509  * port.  The port generates this event when an abort exchange request to an
9510  * rport fails twice in succession with no reply.  The abort could be originated
9511  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
9512  */
9513 void
9514 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9515                            struct lpfc_nodelist *ndlp,
9516                            struct sli4_wcqe_xri_aborted *axri)
9517 {
9518         struct lpfc_vport *vport;
9519         uint32_t ext_status = 0;
9520
9521         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9522                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9523                                 "3115 Node Context not found, driver "
9524                                 "ignoring abts err event\n");
9525                 return;
9526         }
9527
9528         vport = ndlp->vport;
9529         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9530                         "3116 Port generated FCP XRI ABORT event on "
9531                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9532                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9533                         bf_get(lpfc_wcqe_xa_xri, axri),
9534                         bf_get(lpfc_wcqe_xa_status, axri),
9535                         axri->parameter);
9536
9537         /*
9538          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
9539          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9540          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9541          */
9542         ext_status = axri->parameter & IOERR_PARAM_MASK;
9543         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9544             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9545                 lpfc_sli_abts_recover_port(vport, ndlp);
9546 }
9547
9548 /**
9549  * lpfc_sli_async_event_handler - ASYNC iocb handler function
9550  * @phba: Pointer to HBA context object.
9551  * @pring: Pointer to driver SLI ring object.
9552  * @iocbq: Pointer to iocb object.
9553  *
9554  * This function is called by the slow ring event handler
9555  * function when there is an ASYNC event iocb in the ring.
9556  * This function is called with no lock held.
9557  * Currently this function handles only temperature related
9558  * ASYNC events. The function decodes the temperature sensor
9559  * event message and posts events for the management applications.
9560  **/
9561 static void
9562 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9563         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9564 {
9565         IOCB_t *icmd;
9566         uint16_t evt_code;
9567         struct temp_event temp_event_data;
9568         struct Scsi_Host *shost;
9569         uint32_t *iocb_w;
9570
9571         icmd = &iocbq->iocb;
9572         evt_code = icmd->un.asyncstat.evt_code;
9573
9574         switch (evt_code) {
9575         case ASYNC_TEMP_WARN:
9576         case ASYNC_TEMP_SAFE:
9577                 temp_event_data.data = (uint32_t) icmd->ulpContext;
9578                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9579                 if (evt_code == ASYNC_TEMP_WARN) {
9580                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9581                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9582                                 "0347 Adapter is very hot, please take "
9583                                 "corrective action. temperature : %d Celsius\n",
9584                                 (uint32_t) icmd->ulpContext);
9585                 } else {
9586                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
9587                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9588                                 "0340 Adapter temperature is OK now. "
9589                                 "temperature : %d Celsius\n",
9590                                 (uint32_t) icmd->ulpContext);
9591                 }
9592
9593                 /* Send temperature change event to applications */
9594                 shost = lpfc_shost_from_vport(phba->pport);
9595                 fc_host_post_vendor_event(shost, fc_get_event_number(),
9596                         sizeof(temp_event_data), (char *) &temp_event_data,
9597                         LPFC_NL_VENDOR_ID);
9598                 break;
9599         case ASYNC_STATUS_CN:
9600                 lpfc_sli_abts_err_handler(phba, iocbq);
9601                 break;
9602         default:
9603                 iocb_w = (uint32_t *) icmd;
9604                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9605                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
9606                         " evt_code 0x%x\n"
9607                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
9608                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
9609                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
9610                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9611                         pring->ringno, icmd->un.asyncstat.evt_code,
9612                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9613                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9614                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9615                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9616
9617                 break;
9618         }
9619 }
9620
9621
9622 /**
9623  * lpfc_sli4_setup - SLI ring setup function
9624  * @phba: Pointer to HBA context object.
9625  *
9626  * lpfc_sli_setup sets up rings of the SLI interface with
9627  * number of iocbs per ring and iotags. This function is
9628  * called while driver attach to the HBA and before the
9629  * interrupts are enabled. So there is no need for locking.
9630  *
9631  * This function always returns 0.
9632  **/
9633 int
9634 lpfc_sli4_setup(struct lpfc_hba *phba)
9635 {
9636         struct lpfc_sli_ring *pring;
9637
9638         pring = phba->sli4_hba.els_wq->pring;
9639         pring->num_mask = LPFC_MAX_RING_MASK;
9640         pring->prt[0].profile = 0;      /* Mask 0 */
9641         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9642         pring->prt[0].type = FC_TYPE_ELS;
9643         pring->prt[0].lpfc_sli_rcv_unsol_event =
9644             lpfc_els_unsol_event;
9645         pring->prt[1].profile = 0;      /* Mask 1 */
9646         pring->prt[1].rctl = FC_RCTL_ELS_REP;
9647         pring->prt[1].type = FC_TYPE_ELS;
9648         pring->prt[1].lpfc_sli_rcv_unsol_event =
9649             lpfc_els_unsol_event;
9650         pring->prt[2].profile = 0;      /* Mask 2 */
9651         /* NameServer Inquiry */
9652         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9653         /* NameServer */
9654         pring->prt[2].type = FC_TYPE_CT;
9655         pring->prt[2].lpfc_sli_rcv_unsol_event =
9656             lpfc_ct_unsol_event;
9657         pring->prt[3].profile = 0;      /* Mask 3 */
9658         /* NameServer response */
9659         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9660         /* NameServer */
9661         pring->prt[3].type = FC_TYPE_CT;
9662         pring->prt[3].lpfc_sli_rcv_unsol_event =
9663             lpfc_ct_unsol_event;
9664         return 0;
9665 }
9666
9667 /**
9668  * lpfc_sli_setup - SLI ring setup function
9669  * @phba: Pointer to HBA context object.
9670  *
9671  * lpfc_sli_setup sets up rings of the SLI interface with
9672  * number of iocbs per ring and iotags. This function is
9673  * called while driver attach to the HBA and before the
9674  * interrupts are enabled. So there is no need for locking.
9675  *
9676  * This function always returns 0. SLI3 only.
9677  **/
9678 int
9679 lpfc_sli_setup(struct lpfc_hba *phba)
9680 {
9681         int i, totiocbsize = 0;
9682         struct lpfc_sli *psli = &phba->sli;
9683         struct lpfc_sli_ring *pring;
9684
9685         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9686         psli->sli_flag = 0;
9687
9688         psli->iocbq_lookup = NULL;
9689         psli->iocbq_lookup_len = 0;
9690         psli->last_iotag = 0;
9691
9692         for (i = 0; i < psli->num_rings; i++) {
9693                 pring = &psli->sli3_ring[i];
9694                 switch (i) {
9695                 case LPFC_FCP_RING:     /* ring 0 - FCP */
9696                         /* numCiocb and numRiocb are used in config_port */
9697                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9698                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9699                         pring->sli.sli3.numCiocb +=
9700                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9701                         pring->sli.sli3.numRiocb +=
9702                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9703                         pring->sli.sli3.numCiocb +=
9704                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9705                         pring->sli.sli3.numRiocb +=
9706                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9707                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9708                                                         SLI3_IOCB_CMD_SIZE :
9709                                                         SLI2_IOCB_CMD_SIZE;
9710                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9711                                                         SLI3_IOCB_RSP_SIZE :
9712                                                         SLI2_IOCB_RSP_SIZE;
9713                         pring->iotag_ctr = 0;
9714                         pring->iotag_max =
9715                             (phba->cfg_hba_queue_depth * 2);
9716                         pring->fast_iotag = pring->iotag_max;
9717                         pring->num_mask = 0;
9718                         break;
9719                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
9720                         /* numCiocb and numRiocb are used in config_port */
9721                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9722                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9723                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9724                                                         SLI3_IOCB_CMD_SIZE :
9725                                                         SLI2_IOCB_CMD_SIZE;
9726                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9727                                                         SLI3_IOCB_RSP_SIZE :
9728                                                         SLI2_IOCB_RSP_SIZE;
9729                         pring->iotag_max = phba->cfg_hba_queue_depth;
9730                         pring->num_mask = 0;
9731                         break;
9732                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
9733                         /* numCiocb and numRiocb are used in config_port */
9734                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9735                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9736                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9737                                                         SLI3_IOCB_CMD_SIZE :
9738                                                         SLI2_IOCB_CMD_SIZE;
9739                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9740                                                         SLI3_IOCB_RSP_SIZE :
9741                                                         SLI2_IOCB_RSP_SIZE;
9742                         pring->fast_iotag = 0;
9743                         pring->iotag_ctr = 0;
9744                         pring->iotag_max = 4096;
9745                         pring->lpfc_sli_rcv_async_status =
9746                                 lpfc_sli_async_event_handler;
9747                         pring->num_mask = LPFC_MAX_RING_MASK;
9748                         pring->prt[0].profile = 0;      /* Mask 0 */
9749                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9750                         pring->prt[0].type = FC_TYPE_ELS;
9751                         pring->prt[0].lpfc_sli_rcv_unsol_event =
9752                             lpfc_els_unsol_event;
9753                         pring->prt[1].profile = 0;      /* Mask 1 */
9754                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
9755                         pring->prt[1].type = FC_TYPE_ELS;
9756                         pring->prt[1].lpfc_sli_rcv_unsol_event =
9757                             lpfc_els_unsol_event;
9758                         pring->prt[2].profile = 0;      /* Mask 2 */
9759                         /* NameServer Inquiry */
9760                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9761                         /* NameServer */
9762                         pring->prt[2].type = FC_TYPE_CT;
9763                         pring->prt[2].lpfc_sli_rcv_unsol_event =
9764                             lpfc_ct_unsol_event;
9765                         pring->prt[3].profile = 0;      /* Mask 3 */
9766                         /* NameServer response */
9767                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9768                         /* NameServer */
9769                         pring->prt[3].type = FC_TYPE_CT;
9770                         pring->prt[3].lpfc_sli_rcv_unsol_event =
9771                             lpfc_ct_unsol_event;
9772                         break;
9773                 }
9774                 totiocbsize += (pring->sli.sli3.numCiocb *
9775                         pring->sli.sli3.sizeCiocb) +
9776                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
9777         }
9778         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
9779                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
9780                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9781                        "SLI2 SLIM Data: x%x x%lx\n",
9782                        phba->brd_no, totiocbsize,
9783                        (unsigned long) MAX_SLIM_IOCB_SIZE);
9784         }
9785         if (phba->cfg_multi_ring_support == 2)
9786                 lpfc_extra_ring_setup(phba);
9787
9788         return 0;
9789 }
9790
9791 /**
9792  * lpfc_sli4_queue_init - Queue initialization function
9793  * @phba: Pointer to HBA context object.
9794  *
9795  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
9796  * ring. This function also initializes ring indices of each ring.
9797  * This function is called during the initialization of the SLI
9798  * interface of an HBA.
9799  * This function is called with no lock held and always returns
9800  * 1.
9801  **/
9802 void
9803 lpfc_sli4_queue_init(struct lpfc_hba *phba)
9804 {
9805         struct lpfc_sli *psli;
9806         struct lpfc_sli_ring *pring;
9807         int i;
9808
9809         psli = &phba->sli;
9810         spin_lock_irq(&phba->hbalock);
9811         INIT_LIST_HEAD(&psli->mboxq);
9812         INIT_LIST_HEAD(&psli->mboxq_cmpl);
9813         /* Initialize list headers for txq and txcmplq as double linked lists */
9814         for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9815                 pring = phba->sli4_hba.fcp_wq[i]->pring;
9816                 pring->flag = 0;
9817                 pring->ringno = LPFC_FCP_RING;
9818                 INIT_LIST_HEAD(&pring->txq);
9819                 INIT_LIST_HEAD(&pring->txcmplq);
9820                 INIT_LIST_HEAD(&pring->iocb_continueq);
9821                 spin_lock_init(&pring->ring_lock);
9822         }
9823         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9824                 pring = phba->sli4_hba.nvme_wq[i]->pring;
9825                 pring->flag = 0;
9826                 pring->ringno = LPFC_FCP_RING;
9827                 INIT_LIST_HEAD(&pring->txq);
9828                 INIT_LIST_HEAD(&pring->txcmplq);
9829                 INIT_LIST_HEAD(&pring->iocb_continueq);
9830                 spin_lock_init(&pring->ring_lock);
9831         }
9832         pring = phba->sli4_hba.els_wq->pring;
9833         pring->flag = 0;
9834         pring->ringno = LPFC_ELS_RING;
9835         INIT_LIST_HEAD(&pring->txq);
9836         INIT_LIST_HEAD(&pring->txcmplq);
9837         INIT_LIST_HEAD(&pring->iocb_continueq);
9838         spin_lock_init(&pring->ring_lock);
9839
9840         if (phba->cfg_nvme_io_channel) {
9841                 pring = phba->sli4_hba.nvmels_wq->pring;
9842                 pring->flag = 0;
9843                 pring->ringno = LPFC_ELS_RING;
9844                 INIT_LIST_HEAD(&pring->txq);
9845                 INIT_LIST_HEAD(&pring->txcmplq);
9846                 INIT_LIST_HEAD(&pring->iocb_continueq);
9847                 spin_lock_init(&pring->ring_lock);
9848         }
9849
9850         if (phba->cfg_fof) {
9851                 pring = phba->sli4_hba.oas_wq->pring;
9852                 pring->flag = 0;
9853                 pring->ringno = LPFC_FCP_RING;
9854                 INIT_LIST_HEAD(&pring->txq);
9855                 INIT_LIST_HEAD(&pring->txcmplq);
9856                 INIT_LIST_HEAD(&pring->iocb_continueq);
9857                 spin_lock_init(&pring->ring_lock);
9858         }
9859
9860         spin_unlock_irq(&phba->hbalock);
9861 }
9862
9863 /**
9864  * lpfc_sli_queue_init - Queue initialization function
9865  * @phba: Pointer to HBA context object.
9866  *
9867  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9868  * ring. This function also initializes ring indices of each ring.
9869  * This function is called during the initialization of the SLI
9870  * interface of an HBA.
9871  * This function is called with no lock held and always returns
9872  * 1.
9873  **/
9874 void
9875 lpfc_sli_queue_init(struct lpfc_hba *phba)
9876 {
9877         struct lpfc_sli *psli;
9878         struct lpfc_sli_ring *pring;
9879         int i;
9880
9881         psli = &phba->sli;
9882         spin_lock_irq(&phba->hbalock);
9883         INIT_LIST_HEAD(&psli->mboxq);
9884         INIT_LIST_HEAD(&psli->mboxq_cmpl);
9885         /* Initialize list headers for txq and txcmplq as double linked lists */
9886         for (i = 0; i < psli->num_rings; i++) {
9887                 pring = &psli->sli3_ring[i];
9888                 pring->ringno = i;
9889                 pring->sli.sli3.next_cmdidx  = 0;
9890                 pring->sli.sli3.local_getidx = 0;
9891                 pring->sli.sli3.cmdidx = 0;
9892                 INIT_LIST_HEAD(&pring->iocb_continueq);
9893                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9894                 INIT_LIST_HEAD(&pring->postbufq);
9895                 pring->flag = 0;
9896                 INIT_LIST_HEAD(&pring->txq);
9897                 INIT_LIST_HEAD(&pring->txcmplq);
9898                 spin_lock_init(&pring->ring_lock);
9899         }
9900         spin_unlock_irq(&phba->hbalock);
9901 }
9902
9903 /**
9904  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9905  * @phba: Pointer to HBA context object.
9906  *
9907  * This routine flushes the mailbox command subsystem. It will unconditionally
9908  * flush all the mailbox commands in the three possible stages in the mailbox
9909  * command sub-system: pending mailbox command queue; the outstanding mailbox
9910  * command; and completed mailbox command queue. It is caller's responsibility
9911  * to make sure that the driver is in the proper state to flush the mailbox
9912  * command sub-system. Namely, the posting of mailbox commands into the
9913  * pending mailbox command queue from the various clients must be stopped;
9914  * either the HBA is in a state that it will never works on the outstanding
9915  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9916  * mailbox command has been completed.
9917  **/
9918 static void
9919 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9920 {
9921         LIST_HEAD(completions);
9922         struct lpfc_sli *psli = &phba->sli;
9923         LPFC_MBOXQ_t *pmb;
9924         unsigned long iflag;
9925
9926         /* Flush all the mailbox commands in the mbox system */
9927         spin_lock_irqsave(&phba->hbalock, iflag);
9928         /* The pending mailbox command queue */
9929         list_splice_init(&phba->sli.mboxq, &completions);
9930         /* The outstanding active mailbox command */
9931         if (psli->mbox_active) {
9932                 list_add_tail(&psli->mbox_active->list, &completions);
9933                 psli->mbox_active = NULL;
9934                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9935         }
9936         /* The completed mailbox command queue */
9937         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9938         spin_unlock_irqrestore(&phba->hbalock, iflag);
9939
9940         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9941         while (!list_empty(&completions)) {
9942                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9943                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9944                 if (pmb->mbox_cmpl)
9945                         pmb->mbox_cmpl(phba, pmb);
9946         }
9947 }
9948
9949 /**
9950  * lpfc_sli_host_down - Vport cleanup function
9951  * @vport: Pointer to virtual port object.
9952  *
9953  * lpfc_sli_host_down is called to clean up the resources
9954  * associated with a vport before destroying virtual
9955  * port data structures.
9956  * This function does following operations:
9957  * - Free discovery resources associated with this virtual
9958  *   port.
9959  * - Free iocbs associated with this virtual port in
9960  *   the txq.
9961  * - Send abort for all iocb commands associated with this
9962  *   vport in txcmplq.
9963  *
9964  * This function is called with no lock held and always returns 1.
9965  **/
9966 int
9967 lpfc_sli_host_down(struct lpfc_vport *vport)
9968 {
9969         LIST_HEAD(completions);
9970         struct lpfc_hba *phba = vport->phba;
9971         struct lpfc_sli *psli = &phba->sli;
9972         struct lpfc_queue *qp = NULL;
9973         struct lpfc_sli_ring *pring;
9974         struct lpfc_iocbq *iocb, *next_iocb;
9975         int i;
9976         unsigned long flags = 0;
9977         uint16_t prev_pring_flag;
9978
9979         lpfc_cleanup_discovery_resources(vport);
9980
9981         spin_lock_irqsave(&phba->hbalock, flags);
9982
9983         /*
9984          * Error everything on the txq since these iocbs
9985          * have not been given to the FW yet.
9986          * Also issue ABTS for everything on the txcmplq
9987          */
9988         if (phba->sli_rev != LPFC_SLI_REV4) {
9989                 for (i = 0; i < psli->num_rings; i++) {
9990                         pring = &psli->sli3_ring[i];
9991                         prev_pring_flag = pring->flag;
9992                         /* Only slow rings */
9993                         if (pring->ringno == LPFC_ELS_RING) {
9994                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9995                                 /* Set the lpfc data pending flag */
9996                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
9997                         }
9998                         list_for_each_entry_safe(iocb, next_iocb,
9999                                                  &pring->txq, list) {
10000                                 if (iocb->vport != vport)
10001                                         continue;
10002                                 list_move_tail(&iocb->list, &completions);
10003                         }
10004                         list_for_each_entry_safe(iocb, next_iocb,
10005                                                  &pring->txcmplq, list) {
10006                                 if (iocb->vport != vport)
10007                                         continue;
10008                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10009                         }
10010                         pring->flag = prev_pring_flag;
10011                 }
10012         } else {
10013                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10014                         pring = qp->pring;
10015                         if (!pring)
10016                                 continue;
10017                         if (pring == phba->sli4_hba.els_wq->pring) {
10018                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10019                                 /* Set the lpfc data pending flag */
10020                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10021                         }
10022                         prev_pring_flag = pring->flag;
10023                         spin_lock_irq(&pring->ring_lock);
10024                         list_for_each_entry_safe(iocb, next_iocb,
10025                                                  &pring->txq, list) {
10026                                 if (iocb->vport != vport)
10027                                         continue;
10028                                 list_move_tail(&iocb->list, &completions);
10029                         }
10030                         spin_unlock_irq(&pring->ring_lock);
10031                         list_for_each_entry_safe(iocb, next_iocb,
10032                                                  &pring->txcmplq, list) {
10033                                 if (iocb->vport != vport)
10034                                         continue;
10035                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10036                         }
10037                         pring->flag = prev_pring_flag;
10038                 }
10039         }
10040         spin_unlock_irqrestore(&phba->hbalock, flags);
10041
10042         /* Cancel all the IOCBs from the completions list */
10043         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10044                               IOERR_SLI_DOWN);
10045         return 1;
10046 }
10047
10048 /**
10049  * lpfc_sli_hba_down - Resource cleanup function for the HBA
10050  * @phba: Pointer to HBA context object.
10051  *
10052  * This function cleans up all iocb, buffers, mailbox commands
10053  * while shutting down the HBA. This function is called with no
10054  * lock held and always returns 1.
10055  * This function does the following to cleanup driver resources:
10056  * - Free discovery resources for each virtual port
10057  * - Cleanup any pending fabric iocbs
10058  * - Iterate through the iocb txq and free each entry
10059  *   in the list.
10060  * - Free up any buffer posted to the HBA
10061  * - Free mailbox commands in the mailbox queue.
10062  **/
10063 int
10064 lpfc_sli_hba_down(struct lpfc_hba *phba)
10065 {
10066         LIST_HEAD(completions);
10067         struct lpfc_sli *psli = &phba->sli;
10068         struct lpfc_queue *qp = NULL;
10069         struct lpfc_sli_ring *pring;
10070         struct lpfc_dmabuf *buf_ptr;
10071         unsigned long flags = 0;
10072         int i;
10073
10074         /* Shutdown the mailbox command sub-system */
10075         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10076
10077         lpfc_hba_down_prep(phba);
10078
10079         lpfc_fabric_abort_hba(phba);
10080
10081         spin_lock_irqsave(&phba->hbalock, flags);
10082
10083         /*
10084          * Error everything on the txq since these iocbs
10085          * have not been given to the FW yet.
10086          */
10087         if (phba->sli_rev != LPFC_SLI_REV4) {
10088                 for (i = 0; i < psli->num_rings; i++) {
10089                         pring = &psli->sli3_ring[i];
10090                         /* Only slow rings */
10091                         if (pring->ringno == LPFC_ELS_RING) {
10092                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10093                                 /* Set the lpfc data pending flag */
10094                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10095                         }
10096                         list_splice_init(&pring->txq, &completions);
10097                 }
10098         } else {
10099                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10100                         pring = qp->pring;
10101                         if (!pring)
10102                                 continue;
10103                         spin_lock_irq(&pring->ring_lock);
10104                         list_splice_init(&pring->txq, &completions);
10105                         spin_unlock_irq(&pring->ring_lock);
10106                         if (pring == phba->sli4_hba.els_wq->pring) {
10107                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10108                                 /* Set the lpfc data pending flag */
10109                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10110                         }
10111                 }
10112         }
10113         spin_unlock_irqrestore(&phba->hbalock, flags);
10114
10115         /* Cancel all the IOCBs from the completions list */
10116         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10117                               IOERR_SLI_DOWN);
10118
10119         spin_lock_irqsave(&phba->hbalock, flags);
10120         list_splice_init(&phba->elsbuf, &completions);
10121         phba->elsbuf_cnt = 0;
10122         phba->elsbuf_prev_cnt = 0;
10123         spin_unlock_irqrestore(&phba->hbalock, flags);
10124
10125         while (!list_empty(&completions)) {
10126                 list_remove_head(&completions, buf_ptr,
10127                         struct lpfc_dmabuf, list);
10128                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10129                 kfree(buf_ptr);
10130         }
10131
10132         /* Return any active mbox cmds */
10133         del_timer_sync(&psli->mbox_tmo);
10134
10135         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10136         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10137         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10138
10139         return 1;
10140 }
10141
10142 /**
10143  * lpfc_sli_pcimem_bcopy - SLI memory copy function
10144  * @srcp: Source memory pointer.
10145  * @destp: Destination memory pointer.
10146  * @cnt: Number of words required to be copied.
10147  *
10148  * This function is used for copying data between driver memory
10149  * and the SLI memory. This function also changes the endianness
10150  * of each word if native endianness is different from SLI
10151  * endianness. This function can be called with or without
10152  * lock.
10153  **/
10154 void
10155 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10156 {
10157         uint32_t *src = srcp;
10158         uint32_t *dest = destp;
10159         uint32_t ldata;
10160         int i;
10161
10162         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10163                 ldata = *src;
10164                 ldata = le32_to_cpu(ldata);
10165                 *dest = ldata;
10166                 src++;
10167                 dest++;
10168         }
10169 }
10170
10171
10172 /**
10173  * lpfc_sli_bemem_bcopy - SLI memory copy function
10174  * @srcp: Source memory pointer.
10175  * @destp: Destination memory pointer.
10176  * @cnt: Number of words required to be copied.
10177  *
10178  * This function is used for copying data between a data structure
10179  * with big endian representation to local endianness.
10180  * This function can be called with or without lock.
10181  **/
10182 void
10183 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10184 {
10185         uint32_t *src = srcp;
10186         uint32_t *dest = destp;
10187         uint32_t ldata;
10188         int i;
10189
10190         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10191                 ldata = *src;
10192                 ldata = be32_to_cpu(ldata);
10193                 *dest = ldata;
10194                 src++;
10195                 dest++;
10196         }
10197 }
10198
10199 /**
10200  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10201  * @phba: Pointer to HBA context object.
10202  * @pring: Pointer to driver SLI ring object.
10203  * @mp: Pointer to driver buffer object.
10204  *
10205  * This function is called with no lock held.
10206  * It always return zero after adding the buffer to the postbufq
10207  * buffer list.
10208  **/
10209 int
10210 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10211                          struct lpfc_dmabuf *mp)
10212 {
10213         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10214            later */
10215         spin_lock_irq(&phba->hbalock);
10216         list_add_tail(&mp->list, &pring->postbufq);
10217         pring->postbufq_cnt++;
10218         spin_unlock_irq(&phba->hbalock);
10219         return 0;
10220 }
10221
10222 /**
10223  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10224  * @phba: Pointer to HBA context object.
10225  *
10226  * When HBQ is enabled, buffers are searched based on tags. This function
10227  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10228  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10229  * does not conflict with tags of buffer posted for unsolicited events.
10230  * The function returns the allocated tag. The function is called with
10231  * no locks held.
10232  **/
10233 uint32_t
10234 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10235 {
10236         spin_lock_irq(&phba->hbalock);
10237         phba->buffer_tag_count++;
10238         /*
10239          * Always set the QUE_BUFTAG_BIT to distiguish between
10240          * a tag assigned by HBQ.
10241          */
10242         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10243         spin_unlock_irq(&phba->hbalock);
10244         return phba->buffer_tag_count;
10245 }
10246
10247 /**
10248  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10249  * @phba: Pointer to HBA context object.
10250  * @pring: Pointer to driver SLI ring object.
10251  * @tag: Buffer tag.
10252  *
10253  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10254  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10255  * iocb is posted to the response ring with the tag of the buffer.
10256  * This function searches the pring->postbufq list using the tag
10257  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10258  * iocb. If the buffer is found then lpfc_dmabuf object of the
10259  * buffer is returned to the caller else NULL is returned.
10260  * This function is called with no lock held.
10261  **/
10262 struct lpfc_dmabuf *
10263 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10264                         uint32_t tag)
10265 {
10266         struct lpfc_dmabuf *mp, *next_mp;
10267         struct list_head *slp = &pring->postbufq;
10268
10269         /* Search postbufq, from the beginning, looking for a match on tag */
10270         spin_lock_irq(&phba->hbalock);
10271         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10272                 if (mp->buffer_tag == tag) {
10273                         list_del_init(&mp->list);
10274                         pring->postbufq_cnt--;
10275                         spin_unlock_irq(&phba->hbalock);
10276                         return mp;
10277                 }
10278         }
10279
10280         spin_unlock_irq(&phba->hbalock);
10281         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10282                         "0402 Cannot find virtual addr for buffer tag on "
10283                         "ring %d Data x%lx x%p x%p x%x\n",
10284                         pring->ringno, (unsigned long) tag,
10285                         slp->next, slp->prev, pring->postbufq_cnt);
10286
10287         return NULL;
10288 }
10289
10290 /**
10291  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10292  * @phba: Pointer to HBA context object.
10293  * @pring: Pointer to driver SLI ring object.
10294  * @phys: DMA address of the buffer.
10295  *
10296  * This function searches the buffer list using the dma_address
10297  * of unsolicited event to find the driver's lpfc_dmabuf object
10298  * corresponding to the dma_address. The function returns the
10299  * lpfc_dmabuf object if a buffer is found else it returns NULL.
10300  * This function is called by the ct and els unsolicited event
10301  * handlers to get the buffer associated with the unsolicited
10302  * event.
10303  *
10304  * This function is called with no lock held.
10305  **/
10306 struct lpfc_dmabuf *
10307 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10308                          dma_addr_t phys)
10309 {
10310         struct lpfc_dmabuf *mp, *next_mp;
10311         struct list_head *slp = &pring->postbufq;
10312
10313         /* Search postbufq, from the beginning, looking for a match on phys */
10314         spin_lock_irq(&phba->hbalock);
10315         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10316                 if (mp->phys == phys) {
10317                         list_del_init(&mp->list);
10318                         pring->postbufq_cnt--;
10319                         spin_unlock_irq(&phba->hbalock);
10320                         return mp;
10321                 }
10322         }
10323
10324         spin_unlock_irq(&phba->hbalock);
10325         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10326                         "0410 Cannot find virtual addr for mapped buf on "
10327                         "ring %d Data x%llx x%p x%p x%x\n",
10328                         pring->ringno, (unsigned long long)phys,
10329                         slp->next, slp->prev, pring->postbufq_cnt);
10330         return NULL;
10331 }
10332
10333 /**
10334  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
10335  * @phba: Pointer to HBA context object.
10336  * @cmdiocb: Pointer to driver command iocb object.
10337  * @rspiocb: Pointer to driver response iocb object.
10338  *
10339  * This function is the completion handler for the abort iocbs for
10340  * ELS commands. This function is called from the ELS ring event
10341  * handler with no lock held. This function frees memory resources
10342  * associated with the abort iocb.
10343  **/
10344 static void
10345 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10346                         struct lpfc_iocbq *rspiocb)
10347 {
10348         IOCB_t *irsp = &rspiocb->iocb;
10349         uint16_t abort_iotag, abort_context;
10350         struct lpfc_iocbq *abort_iocb = NULL;
10351
10352         if (irsp->ulpStatus) {
10353
10354                 /*
10355                  * Assume that the port already completed and returned, or
10356                  * will return the iocb. Just Log the message.
10357                  */
10358                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10359                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10360
10361                 spin_lock_irq(&phba->hbalock);
10362                 if (phba->sli_rev < LPFC_SLI_REV4) {
10363                         if (abort_iotag != 0 &&
10364                                 abort_iotag <= phba->sli.last_iotag)
10365                                 abort_iocb =
10366                                         phba->sli.iocbq_lookup[abort_iotag];
10367                 } else
10368                         /* For sli4 the abort_tag is the XRI,
10369                          * so the abort routine puts the iotag  of the iocb
10370                          * being aborted in the context field of the abort
10371                          * IOCB.
10372                          */
10373                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
10374
10375                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10376                                 "0327 Cannot abort els iocb %p "
10377                                 "with tag %x context %x, abort status %x, "
10378                                 "abort code %x\n",
10379                                 abort_iocb, abort_iotag, abort_context,
10380                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
10381
10382                 spin_unlock_irq(&phba->hbalock);
10383         }
10384         lpfc_sli_release_iocbq(phba, cmdiocb);
10385         return;
10386 }
10387
10388 /**
10389  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
10390  * @phba: Pointer to HBA context object.
10391  * @cmdiocb: Pointer to driver command iocb object.
10392  * @rspiocb: Pointer to driver response iocb object.
10393  *
10394  * The function is called from SLI ring event handler with no
10395  * lock held. This function is the completion handler for ELS commands
10396  * which are aborted. The function frees memory resources used for
10397  * the aborted ELS commands.
10398  **/
10399 static void
10400 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10401                      struct lpfc_iocbq *rspiocb)
10402 {
10403         IOCB_t *irsp = &rspiocb->iocb;
10404
10405         /* ELS cmd tag <ulpIoTag> completes */
10406         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10407                         "0139 Ignoring ELS cmd tag x%x completion Data: "
10408                         "x%x x%x x%x\n",
10409                         irsp->ulpIoTag, irsp->ulpStatus,
10410                         irsp->un.ulpWord[4], irsp->ulpTimeout);
10411         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10412                 lpfc_ct_free_iocb(phba, cmdiocb);
10413         else
10414                 lpfc_els_free_iocb(phba, cmdiocb);
10415         return;
10416 }
10417
10418 /**
10419  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
10420  * @phba: Pointer to HBA context object.
10421  * @pring: Pointer to driver SLI ring object.
10422  * @cmdiocb: Pointer to driver command iocb object.
10423  *
10424  * This function issues an abort iocb for the provided command iocb down to
10425  * the port. Other than the case the outstanding command iocb is an abort
10426  * request, this function issues abort out unconditionally. This function is
10427  * called with hbalock held. The function returns 0 when it fails due to
10428  * memory allocation failure or when the command iocb is an abort request.
10429  **/
10430 static int
10431 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10432                            struct lpfc_iocbq *cmdiocb)
10433 {
10434         struct lpfc_vport *vport = cmdiocb->vport;
10435         struct lpfc_iocbq *abtsiocbp;
10436         IOCB_t *icmd = NULL;
10437         IOCB_t *iabt = NULL;
10438         int retval;
10439         unsigned long iflags;
10440
10441         lockdep_assert_held(&phba->hbalock);
10442
10443         /*
10444          * There are certain command types we don't want to abort.  And we
10445          * don't want to abort commands that are already in the process of
10446          * being aborted.
10447          */
10448         icmd = &cmdiocb->iocb;
10449         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10450             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10451             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10452                 return 0;
10453
10454         /* issue ABTS for this IOCB based on iotag */
10455         abtsiocbp = __lpfc_sli_get_iocbq(phba);
10456         if (abtsiocbp == NULL)
10457                 return 0;
10458
10459         /* This signals the response to set the correct status
10460          * before calling the completion handler
10461          */
10462         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10463
10464         iabt = &abtsiocbp->iocb;
10465         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10466         iabt->un.acxri.abortContextTag = icmd->ulpContext;
10467         if (phba->sli_rev == LPFC_SLI_REV4) {
10468                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
10469                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10470         }
10471         else
10472                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
10473         iabt->ulpLe = 1;
10474         iabt->ulpClass = icmd->ulpClass;
10475
10476         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10477         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
10478         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10479                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
10480         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10481                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
10482
10483         if (phba->link_state >= LPFC_LINK_UP)
10484                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10485         else
10486                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10487
10488         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10489         abtsiocbp->vport = vport;
10490
10491         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10492                          "0339 Abort xri x%x, original iotag x%x, "
10493                          "abort cmd iotag x%x\n",
10494                          iabt->un.acxri.abortIoTag,
10495                          iabt->un.acxri.abortContextTag,
10496                          abtsiocbp->iotag);
10497
10498         if (phba->sli_rev == LPFC_SLI_REV4) {
10499                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10500                 if (unlikely(pring == NULL))
10501                         return 0;
10502                 /* Note: both hbalock and ring_lock need to be set here */
10503                 spin_lock_irqsave(&pring->ring_lock, iflags);
10504                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10505                         abtsiocbp, 0);
10506                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10507         } else {
10508                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10509                         abtsiocbp, 0);
10510         }
10511
10512         if (retval)
10513                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
10514
10515         /*
10516          * Caller to this routine should check for IOCB_ERROR
10517          * and handle it properly.  This routine no longer removes
10518          * iocb off txcmplq and call compl in case of IOCB_ERROR.
10519          */
10520         return retval;
10521 }
10522
10523 /**
10524  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10525  * @phba: Pointer to HBA context object.
10526  * @pring: Pointer to driver SLI ring object.
10527  * @cmdiocb: Pointer to driver command iocb object.
10528  *
10529  * This function issues an abort iocb for the provided command iocb. In case
10530  * of unloading, the abort iocb will not be issued to commands on the ELS
10531  * ring. Instead, the callback function shall be changed to those commands
10532  * so that nothing happens when them finishes. This function is called with
10533  * hbalock held. The function returns 0 when the command iocb is an abort
10534  * request.
10535  **/
10536 int
10537 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10538                            struct lpfc_iocbq *cmdiocb)
10539 {
10540         struct lpfc_vport *vport = cmdiocb->vport;
10541         int retval = IOCB_ERROR;
10542         IOCB_t *icmd = NULL;
10543
10544         lockdep_assert_held(&phba->hbalock);
10545
10546         /*
10547          * There are certain command types we don't want to abort.  And we
10548          * don't want to abort commands that are already in the process of
10549          * being aborted.
10550          */
10551         icmd = &cmdiocb->iocb;
10552         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10553             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10554             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10555                 return 0;
10556
10557         /*
10558          * If we're unloading, don't abort iocb on the ELS ring, but change
10559          * the callback so that nothing happens when it finishes.
10560          */
10561         if ((vport->load_flag & FC_UNLOADING) &&
10562             (pring->ringno == LPFC_ELS_RING)) {
10563                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10564                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10565                 else
10566                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10567                 goto abort_iotag_exit;
10568         }
10569
10570         /* Now, we try to issue the abort to the cmdiocb out */
10571         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10572
10573 abort_iotag_exit:
10574         /*
10575          * Caller to this routine should check for IOCB_ERROR
10576          * and handle it properly.  This routine no longer removes
10577          * iocb off txcmplq and call compl in case of IOCB_ERROR.
10578          */
10579         return retval;
10580 }
10581
10582 /**
10583  * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10584  * @phba: Pointer to HBA context object.
10585  * @pring: Pointer to driver SLI ring object.
10586  * @cmdiocb: Pointer to driver command iocb object.
10587  *
10588  * This function issues an abort iocb for the provided command iocb down to
10589  * the port. Other than the case the outstanding command iocb is an abort
10590  * request, this function issues abort out unconditionally. This function is
10591  * called with hbalock held. The function returns 0 when it fails due to
10592  * memory allocation failure or when the command iocb is an abort request.
10593  **/
10594 static int
10595 lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10596                         struct lpfc_iocbq *cmdiocb)
10597 {
10598         struct lpfc_vport *vport = cmdiocb->vport;
10599         struct lpfc_iocbq *abtsiocbp;
10600         union lpfc_wqe *abts_wqe;
10601         int retval;
10602
10603         /*
10604          * There are certain command types we don't want to abort.  And we
10605          * don't want to abort commands that are already in the process of
10606          * being aborted.
10607          */
10608         if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10609             cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10610             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10611                 return 0;
10612
10613         /* issue ABTS for this io based on iotag */
10614         abtsiocbp = __lpfc_sli_get_iocbq(phba);
10615         if (abtsiocbp == NULL)
10616                 return 0;
10617
10618         /* This signals the response to set the correct status
10619          * before calling the completion handler
10620          */
10621         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10622
10623         /* Complete prepping the abort wqe and issue to the FW. */
10624         abts_wqe = &abtsiocbp->wqe;
10625         bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10626         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10627
10628         /* Explicitly set reserved fields to zero.*/
10629         abts_wqe->abort_cmd.rsrvd4 = 0;
10630         abts_wqe->abort_cmd.rsrvd5 = 0;
10631
10632         /* WQE Common - word 6.  Context is XRI tag.  Set 0. */
10633         bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10634         bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10635
10636         /* word 7 */
10637         bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10638         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10639         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10640                cmdiocb->iocb.ulpClass);
10641
10642         /* word 8 - tell the FW to abort the IO associated with this
10643          * outstanding exchange ID.
10644          */
10645         abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10646
10647         /* word 9 - this is the iotag for the abts_wqe completion. */
10648         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10649                abtsiocbp->iotag);
10650
10651         /* word 10 */
10652         bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10653         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10654         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10655
10656         /* word 11 */
10657         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10658         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10659         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10660
10661         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10662         abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10663         abtsiocbp->vport = vport;
10664         abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
10665         retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10666         if (retval == IOCB_ERROR) {
10667                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10668                                  "6147 Failed abts issue_wqe with status x%x "
10669                                  "for oxid x%x\n",
10670                                  retval, cmdiocb->sli4_xritag);
10671                 lpfc_sli_release_iocbq(phba, abtsiocbp);
10672                 return retval;
10673         }
10674
10675         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10676                          "6148 Drv Abort NVME Request Issued for "
10677                          "ox_id x%x on reqtag x%x\n",
10678                          cmdiocb->sli4_xritag,
10679                          abtsiocbp->iotag);
10680
10681         return retval;
10682 }
10683
10684 /**
10685  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10686  * @phba: pointer to lpfc HBA data structure.
10687  *
10688  * This routine will abort all pending and outstanding iocbs to an HBA.
10689  **/
10690 void
10691 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10692 {
10693         struct lpfc_sli *psli = &phba->sli;
10694         struct lpfc_sli_ring *pring;
10695         struct lpfc_queue *qp = NULL;
10696         int i;
10697
10698         if (phba->sli_rev != LPFC_SLI_REV4) {
10699                 for (i = 0; i < psli->num_rings; i++) {
10700                         pring = &psli->sli3_ring[i];
10701                         lpfc_sli_abort_iocb_ring(phba, pring);
10702                 }
10703                 return;
10704         }
10705         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10706                 pring = qp->pring;
10707                 if (!pring)
10708                         continue;
10709                 lpfc_sli_abort_iocb_ring(phba, pring);
10710         }
10711 }
10712
10713 /**
10714  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
10715  * @iocbq: Pointer to driver iocb object.
10716  * @vport: Pointer to driver virtual port object.
10717  * @tgt_id: SCSI ID of the target.
10718  * @lun_id: LUN ID of the scsi device.
10719  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10720  *
10721  * This function acts as an iocb filter for functions which abort or count
10722  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10723  * 0 if the filtering criteria is met for the given iocb and will return
10724  * 1 if the filtering criteria is not met.
10725  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10726  * given iocb is for the SCSI device specified by vport, tgt_id and
10727  * lun_id parameter.
10728  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
10729  * given iocb is for the SCSI target specified by vport and tgt_id
10730  * parameters.
10731  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10732  * given iocb is for the SCSI host associated with the given vport.
10733  * This function is called with no locks held.
10734  **/
10735 static int
10736 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10737                            uint16_t tgt_id, uint64_t lun_id,
10738                            lpfc_ctx_cmd ctx_cmd)
10739 {
10740         struct lpfc_scsi_buf *lpfc_cmd;
10741         int rc = 1;
10742
10743         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
10744                 return rc;
10745
10746         if (iocbq->vport != vport)
10747                 return rc;
10748
10749         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10750
10751         if (lpfc_cmd->pCmd == NULL)
10752                 return rc;
10753
10754         switch (ctx_cmd) {
10755         case LPFC_CTX_LUN:
10756                 if ((lpfc_cmd->rdata->pnode) &&
10757                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10758                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
10759                         rc = 0;
10760                 break;
10761         case LPFC_CTX_TGT:
10762                 if ((lpfc_cmd->rdata->pnode) &&
10763                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
10764                         rc = 0;
10765                 break;
10766         case LPFC_CTX_HOST:
10767                 rc = 0;
10768                 break;
10769         default:
10770                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
10771                         __func__, ctx_cmd);
10772                 break;
10773         }
10774
10775         return rc;
10776 }
10777
10778 /**
10779  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
10780  * @vport: Pointer to virtual port.
10781  * @tgt_id: SCSI ID of the target.
10782  * @lun_id: LUN ID of the scsi device.
10783  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10784  *
10785  * This function returns number of FCP commands pending for the vport.
10786  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10787  * commands pending on the vport associated with SCSI device specified
10788  * by tgt_id and lun_id parameters.
10789  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10790  * commands pending on the vport associated with SCSI target specified
10791  * by tgt_id parameter.
10792  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10793  * commands pending on the vport.
10794  * This function returns the number of iocbs which satisfy the filter.
10795  * This function is called without any lock held.
10796  **/
10797 int
10798 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10799                   lpfc_ctx_cmd ctx_cmd)
10800 {
10801         struct lpfc_hba *phba = vport->phba;
10802         struct lpfc_iocbq *iocbq;
10803         int sum, i;
10804
10805         spin_lock_irq(&phba->hbalock);
10806         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10807                 iocbq = phba->sli.iocbq_lookup[i];
10808
10809                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10810                                                 ctx_cmd) == 0)
10811                         sum++;
10812         }
10813         spin_unlock_irq(&phba->hbalock);
10814
10815         return sum;
10816 }
10817
10818 /**
10819  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
10820  * @phba: Pointer to HBA context object
10821  * @cmdiocb: Pointer to command iocb object.
10822  * @rspiocb: Pointer to response iocb object.
10823  *
10824  * This function is called when an aborted FCP iocb completes. This
10825  * function is called by the ring event handler with no lock held.
10826  * This function frees the iocb.
10827  **/
10828 void
10829 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10830                         struct lpfc_iocbq *rspiocb)
10831 {
10832         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10833                         "3096 ABORT_XRI_CN completing on rpi x%x "
10834                         "original iotag x%x, abort cmd iotag x%x "
10835                         "status 0x%x, reason 0x%x\n",
10836                         cmdiocb->iocb.un.acxri.abortContextTag,
10837                         cmdiocb->iocb.un.acxri.abortIoTag,
10838                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10839                         rspiocb->iocb.un.ulpWord[4]);
10840         lpfc_sli_release_iocbq(phba, cmdiocb);
10841         return;
10842 }
10843
10844 /**
10845  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
10846  * @vport: Pointer to virtual port.
10847  * @pring: Pointer to driver SLI ring object.
10848  * @tgt_id: SCSI ID of the target.
10849  * @lun_id: LUN ID of the scsi device.
10850  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10851  *
10852  * This function sends an abort command for every SCSI command
10853  * associated with the given virtual port pending on the ring
10854  * filtered by lpfc_sli_validate_fcp_iocb function.
10855  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10856  * FCP iocbs associated with lun specified by tgt_id and lun_id
10857  * parameters
10858  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10859  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10860  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10861  * FCP iocbs associated with virtual port.
10862  * This function returns number of iocbs it failed to abort.
10863  * This function is called with no locks held.
10864  **/
10865 int
10866 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10867                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
10868 {
10869         struct lpfc_hba *phba = vport->phba;
10870         struct lpfc_iocbq *iocbq;
10871         struct lpfc_iocbq *abtsiocb;
10872         IOCB_t *cmd = NULL;
10873         int errcnt = 0, ret_val = 0;
10874         int i;
10875
10876         for (i = 1; i <= phba->sli.last_iotag; i++) {
10877                 iocbq = phba->sli.iocbq_lookup[i];
10878
10879                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10880                                                abort_cmd) != 0)
10881                         continue;
10882
10883                 /*
10884                  * If the iocbq is already being aborted, don't take a second
10885                  * action, but do count it.
10886                  */
10887                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10888                         continue;
10889
10890                 /* issue ABTS for this IOCB based on iotag */
10891                 abtsiocb = lpfc_sli_get_iocbq(phba);
10892                 if (abtsiocb == NULL) {
10893                         errcnt++;
10894                         continue;
10895                 }
10896
10897                 /* indicate the IO is being aborted by the driver. */
10898                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10899
10900                 cmd = &iocbq->iocb;
10901                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10902                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
10903                 if (phba->sli_rev == LPFC_SLI_REV4)
10904                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10905                 else
10906                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
10907                 abtsiocb->iocb.ulpLe = 1;
10908                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
10909                 abtsiocb->vport = vport;
10910
10911                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10912                 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
10913                 if (iocbq->iocb_flag & LPFC_IO_FCP)
10914                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
10915                 if (iocbq->iocb_flag & LPFC_IO_FOF)
10916                         abtsiocb->iocb_flag |= LPFC_IO_FOF;
10917
10918                 if (lpfc_is_link_up(phba))
10919                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10920                 else
10921                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10922
10923                 /* Setup callback routine and issue the command. */
10924                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10925                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10926                                               abtsiocb, 0);
10927                 if (ret_val == IOCB_ERROR) {
10928                         lpfc_sli_release_iocbq(phba, abtsiocb);
10929                         errcnt++;
10930                         continue;
10931                 }
10932         }
10933
10934         return errcnt;
10935 }
10936
10937 /**
10938  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10939  * @vport: Pointer to virtual port.
10940  * @pring: Pointer to driver SLI ring object.
10941  * @tgt_id: SCSI ID of the target.
10942  * @lun_id: LUN ID of the scsi device.
10943  * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10944  *
10945  * This function sends an abort command for every SCSI command
10946  * associated with the given virtual port pending on the ring
10947  * filtered by lpfc_sli_validate_fcp_iocb function.
10948  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10949  * FCP iocbs associated with lun specified by tgt_id and lun_id
10950  * parameters
10951  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10952  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10953  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10954  * FCP iocbs associated with virtual port.
10955  * This function returns number of iocbs it aborted .
10956  * This function is called with no locks held right after a taskmgmt
10957  * command is sent.
10958  **/
10959 int
10960 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10961                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10962 {
10963         struct lpfc_hba *phba = vport->phba;
10964         struct lpfc_scsi_buf *lpfc_cmd;
10965         struct lpfc_iocbq *abtsiocbq;
10966         struct lpfc_nodelist *ndlp;
10967         struct lpfc_iocbq *iocbq;
10968         IOCB_t *icmd;
10969         int sum, i, ret_val;
10970         unsigned long iflags;
10971         struct lpfc_sli_ring *pring_s4;
10972
10973         spin_lock_irq(&phba->hbalock);
10974
10975         /* all I/Os are in process of being flushed */
10976         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10977                 spin_unlock_irq(&phba->hbalock);
10978                 return 0;
10979         }
10980         sum = 0;
10981
10982         for (i = 1; i <= phba->sli.last_iotag; i++) {
10983                 iocbq = phba->sli.iocbq_lookup[i];
10984
10985                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10986                                                cmd) != 0)
10987                         continue;
10988
10989                 /*
10990                  * If the iocbq is already being aborted, don't take a second
10991                  * action, but do count it.
10992                  */
10993                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10994                         continue;
10995
10996                 /* issue ABTS for this IOCB based on iotag */
10997                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10998                 if (abtsiocbq == NULL)
10999                         continue;
11000
11001                 icmd = &iocbq->iocb;
11002                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11003                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11004                 if (phba->sli_rev == LPFC_SLI_REV4)
11005                         abtsiocbq->iocb.un.acxri.abortIoTag =
11006                                                          iocbq->sli4_xritag;
11007                 else
11008                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11009                 abtsiocbq->iocb.ulpLe = 1;
11010                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11011                 abtsiocbq->vport = vport;
11012
11013                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11014                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11015                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11016                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11017                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11018                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11019
11020                 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11021                 ndlp = lpfc_cmd->rdata->pnode;
11022
11023                 if (lpfc_is_link_up(phba) &&
11024                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11025                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11026                 else
11027                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11028
11029                 /* Setup callback routine and issue the command. */
11030                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11031
11032                 /*
11033                  * Indicate the IO is being aborted by the driver and set
11034                  * the caller's flag into the aborted IO.
11035                  */
11036                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11037
11038                 if (phba->sli_rev == LPFC_SLI_REV4) {
11039                         pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11040                         if (pring_s4 == NULL)
11041                                 continue;
11042                         /* Note: both hbalock and ring_lock must be set here */
11043                         spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11044                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11045                                                         abtsiocbq, 0);
11046                         spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11047                 } else {
11048                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11049                                                         abtsiocbq, 0);
11050                 }
11051
11052
11053                 if (ret_val == IOCB_ERROR)
11054                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
11055                 else
11056                         sum++;
11057         }
11058         spin_unlock_irq(&phba->hbalock);
11059         return sum;
11060 }
11061
11062 /**
11063  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11064  * @phba: Pointer to HBA context object.
11065  * @cmdiocbq: Pointer to command iocb.
11066  * @rspiocbq: Pointer to response iocb.
11067  *
11068  * This function is the completion handler for iocbs issued using
11069  * lpfc_sli_issue_iocb_wait function. This function is called by the
11070  * ring event handler function without any lock held. This function
11071  * can be called from both worker thread context and interrupt
11072  * context. This function also can be called from other thread which
11073  * cleans up the SLI layer objects.
11074  * This function copy the contents of the response iocb to the
11075  * response iocb memory object provided by the caller of
11076  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11077  * sleeps for the iocb completion.
11078  **/
11079 static void
11080 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11081                         struct lpfc_iocbq *cmdiocbq,
11082                         struct lpfc_iocbq *rspiocbq)
11083 {
11084         wait_queue_head_t *pdone_q;
11085         unsigned long iflags;
11086         struct lpfc_scsi_buf *lpfc_cmd;
11087
11088         spin_lock_irqsave(&phba->hbalock, iflags);
11089         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11090
11091                 /*
11092                  * A time out has occurred for the iocb.  If a time out
11093                  * completion handler has been supplied, call it.  Otherwise,
11094                  * just free the iocbq.
11095                  */
11096
11097                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11098                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11099                 cmdiocbq->wait_iocb_cmpl = NULL;
11100                 if (cmdiocbq->iocb_cmpl)
11101                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11102                 else
11103                         lpfc_sli_release_iocbq(phba, cmdiocbq);
11104                 return;
11105         }
11106
11107         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11108         if (cmdiocbq->context2 && rspiocbq)
11109                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11110                        &rspiocbq->iocb, sizeof(IOCB_t));
11111
11112         /* Set the exchange busy flag for task management commands */
11113         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11114                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11115                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11116                         cur_iocbq);
11117                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11118         }
11119
11120         pdone_q = cmdiocbq->context_un.wait_queue;
11121         if (pdone_q)
11122                 wake_up(pdone_q);
11123         spin_unlock_irqrestore(&phba->hbalock, iflags);
11124         return;
11125 }
11126
11127 /**
11128  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11129  * @phba: Pointer to HBA context object..
11130  * @piocbq: Pointer to command iocb.
11131  * @flag: Flag to test.
11132  *
11133  * This routine grabs the hbalock and then test the iocb_flag to
11134  * see if the passed in flag is set.
11135  * Returns:
11136  * 1 if flag is set.
11137  * 0 if flag is not set.
11138  **/
11139 static int
11140 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11141                  struct lpfc_iocbq *piocbq, uint32_t flag)
11142 {
11143         unsigned long iflags;
11144         int ret;
11145
11146         spin_lock_irqsave(&phba->hbalock, iflags);
11147         ret = piocbq->iocb_flag & flag;
11148         spin_unlock_irqrestore(&phba->hbalock, iflags);
11149         return ret;
11150
11151 }
11152
11153 /**
11154  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11155  * @phba: Pointer to HBA context object..
11156  * @pring: Pointer to sli ring.
11157  * @piocb: Pointer to command iocb.
11158  * @prspiocbq: Pointer to response iocb.
11159  * @timeout: Timeout in number of seconds.
11160  *
11161  * This function issues the iocb to firmware and waits for the
11162  * iocb to complete. The iocb_cmpl field of the shall be used
11163  * to handle iocbs which time out. If the field is NULL, the
11164  * function shall free the iocbq structure.  If more clean up is
11165  * needed, the caller is expected to provide a completion function
11166  * that will provide the needed clean up.  If the iocb command is
11167  * not completed within timeout seconds, the function will either
11168  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11169  * completion function set in the iocb_cmpl field and then return
11170  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
11171  * resources if this function returns IOCB_TIMEDOUT.
11172  * The function waits for the iocb completion using an
11173  * non-interruptible wait.
11174  * This function will sleep while waiting for iocb completion.
11175  * So, this function should not be called from any context which
11176  * does not allow sleeping. Due to the same reason, this function
11177  * cannot be called with interrupt disabled.
11178  * This function assumes that the iocb completions occur while
11179  * this function sleep. So, this function cannot be called from
11180  * the thread which process iocb completion for this ring.
11181  * This function clears the iocb_flag of the iocb object before
11182  * issuing the iocb and the iocb completion handler sets this
11183  * flag and wakes this thread when the iocb completes.
11184  * The contents of the response iocb will be copied to prspiocbq
11185  * by the completion handler when the command completes.
11186  * This function returns IOCB_SUCCESS when success.
11187  * This function is called with no lock held.
11188  **/
11189 int
11190 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11191                          uint32_t ring_number,
11192                          struct lpfc_iocbq *piocb,
11193                          struct lpfc_iocbq *prspiocbq,
11194                          uint32_t timeout)
11195 {
11196         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11197         long timeleft, timeout_req = 0;
11198         int retval = IOCB_SUCCESS;
11199         uint32_t creg_val;
11200         struct lpfc_iocbq *iocb;
11201         int txq_cnt = 0;
11202         int txcmplq_cnt = 0;
11203         struct lpfc_sli_ring *pring;
11204         unsigned long iflags;
11205         bool iocb_completed = true;
11206
11207         if (phba->sli_rev >= LPFC_SLI_REV4)
11208                 pring = lpfc_sli4_calc_ring(phba, piocb);
11209         else
11210                 pring = &phba->sli.sli3_ring[ring_number];
11211         /*
11212          * If the caller has provided a response iocbq buffer, then context2
11213          * is NULL or its an error.
11214          */
11215         if (prspiocbq) {
11216                 if (piocb->context2)
11217                         return IOCB_ERROR;
11218                 piocb->context2 = prspiocbq;
11219         }
11220
11221         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11222         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11223         piocb->context_un.wait_queue = &done_q;
11224         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11225
11226         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11227                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11228                         return IOCB_ERROR;
11229                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11230                 writel(creg_val, phba->HCregaddr);
11231                 readl(phba->HCregaddr); /* flush */
11232         }
11233
11234         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11235                                      SLI_IOCB_RET_IOCB);
11236         if (retval == IOCB_SUCCESS) {
11237                 timeout_req = msecs_to_jiffies(timeout * 1000);
11238                 timeleft = wait_event_timeout(done_q,
11239                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11240                                 timeout_req);
11241                 spin_lock_irqsave(&phba->hbalock, iflags);
11242                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11243
11244                         /*
11245                          * IOCB timed out.  Inform the wake iocb wait
11246                          * completion function and set local status
11247                          */
11248
11249                         iocb_completed = false;
11250                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11251                 }
11252                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11253                 if (iocb_completed) {
11254                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11255                                         "0331 IOCB wake signaled\n");
11256                         /* Note: we are not indicating if the IOCB has a success
11257                          * status or not - that's for the caller to check.
11258                          * IOCB_SUCCESS means just that the command was sent and
11259                          * completed. Not that it completed successfully.
11260                          * */
11261                 } else if (timeleft == 0) {
11262                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11263                                         "0338 IOCB wait timeout error - no "
11264                                         "wake response Data x%x\n", timeout);
11265                         retval = IOCB_TIMEDOUT;
11266                 } else {
11267                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11268                                         "0330 IOCB wake NOT set, "
11269                                         "Data x%x x%lx\n",
11270                                         timeout, (timeleft / jiffies));
11271                         retval = IOCB_TIMEDOUT;
11272                 }
11273         } else if (retval == IOCB_BUSY) {
11274                 if (phba->cfg_log_verbose & LOG_SLI) {
11275                         list_for_each_entry(iocb, &pring->txq, list) {
11276                                 txq_cnt++;
11277                         }
11278                         list_for_each_entry(iocb, &pring->txcmplq, list) {
11279                                 txcmplq_cnt++;
11280                         }
11281                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11282                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11283                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11284                 }
11285                 return retval;
11286         } else {
11287                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11288                                 "0332 IOCB wait issue failed, Data x%x\n",
11289                                 retval);
11290                 retval = IOCB_ERROR;
11291         }
11292
11293         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11294                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11295                         return IOCB_ERROR;
11296                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11297                 writel(creg_val, phba->HCregaddr);
11298                 readl(phba->HCregaddr); /* flush */
11299         }
11300
11301         if (prspiocbq)
11302                 piocb->context2 = NULL;
11303
11304         piocb->context_un.wait_queue = NULL;
11305         piocb->iocb_cmpl = NULL;
11306         return retval;
11307 }
11308
11309 /**
11310  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
11311  * @phba: Pointer to HBA context object.
11312  * @pmboxq: Pointer to driver mailbox object.
11313  * @timeout: Timeout in number of seconds.
11314  *
11315  * This function issues the mailbox to firmware and waits for the
11316  * mailbox command to complete. If the mailbox command is not
11317  * completed within timeout seconds, it returns MBX_TIMEOUT.
11318  * The function waits for the mailbox completion using an
11319  * interruptible wait. If the thread is woken up due to a
11320  * signal, MBX_TIMEOUT error is returned to the caller. Caller
11321  * should not free the mailbox resources, if this function returns
11322  * MBX_TIMEOUT.
11323  * This function will sleep while waiting for mailbox completion.
11324  * So, this function should not be called from any context which
11325  * does not allow sleeping. Due to the same reason, this function
11326  * cannot be called with interrupt disabled.
11327  * This function assumes that the mailbox completion occurs while
11328  * this function sleep. So, this function cannot be called from
11329  * the worker thread which processes mailbox completion.
11330  * This function is called in the context of HBA management
11331  * applications.
11332  * This function returns MBX_SUCCESS when successful.
11333  * This function is called with no lock held.
11334  **/
11335 int
11336 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
11337                          uint32_t timeout)
11338 {
11339         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11340         MAILBOX_t *mb = NULL;
11341         int retval;
11342         unsigned long flag;
11343
11344         /* The caller might set context1 for extended buffer */
11345         if (pmboxq->context1)
11346                 mb = (MAILBOX_t *)pmboxq->context1;
11347
11348         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
11349         /* setup wake call as IOCB callback */
11350         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11351         /* setup context field to pass wait_queue pointer to wake function  */
11352         pmboxq->context1 = &done_q;
11353
11354         /* now issue the command */
11355         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
11356         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
11357                 wait_event_interruptible_timeout(done_q,
11358                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
11359                                 msecs_to_jiffies(timeout * 1000));
11360
11361                 spin_lock_irqsave(&phba->hbalock, flag);
11362                 /* restore the possible extended buffer for free resource */
11363                 pmboxq->context1 = (uint8_t *)mb;
11364                 /*
11365                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
11366                  * else do not free the resources.
11367                  */
11368                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
11369                         retval = MBX_SUCCESS;
11370                 } else {
11371                         retval = MBX_TIMEOUT;
11372                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11373                 }
11374                 spin_unlock_irqrestore(&phba->hbalock, flag);
11375         } else {
11376                 /* restore the possible extended buffer for free resource */
11377                 pmboxq->context1 = (uint8_t *)mb;
11378         }
11379
11380         return retval;
11381 }
11382
11383 /**
11384  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
11385  * @phba: Pointer to HBA context.
11386  *
11387  * This function is called to shutdown the driver's mailbox sub-system.
11388  * It first marks the mailbox sub-system is in a block state to prevent
11389  * the asynchronous mailbox command from issued off the pending mailbox
11390  * command queue. If the mailbox command sub-system shutdown is due to
11391  * HBA error conditions such as EEH or ERATT, this routine shall invoke
11392  * the mailbox sub-system flush routine to forcefully bring down the
11393  * mailbox sub-system. Otherwise, if it is due to normal condition (such
11394  * as with offline or HBA function reset), this routine will wait for the
11395  * outstanding mailbox command to complete before invoking the mailbox
11396  * sub-system flush routine to gracefully bring down mailbox sub-system.
11397  **/
11398 void
11399 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
11400 {
11401         struct lpfc_sli *psli = &phba->sli;
11402         unsigned long timeout;
11403
11404         if (mbx_action == LPFC_MBX_NO_WAIT) {
11405                 /* delay 100ms for port state */
11406                 msleep(100);
11407                 lpfc_sli_mbox_sys_flush(phba);
11408                 return;
11409         }
11410         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
11411
11412         spin_lock_irq(&phba->hbalock);
11413         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11414
11415         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
11416                 /* Determine how long we might wait for the active mailbox
11417                  * command to be gracefully completed by firmware.
11418                  */
11419                 if (phba->sli.mbox_active)
11420                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11421                                                 phba->sli.mbox_active) *
11422                                                 1000) + jiffies;
11423                 spin_unlock_irq(&phba->hbalock);
11424
11425                 while (phba->sli.mbox_active) {
11426                         /* Check active mailbox complete status every 2ms */
11427                         msleep(2);
11428                         if (time_after(jiffies, timeout))
11429                                 /* Timeout, let the mailbox flush routine to
11430                                  * forcefully release active mailbox command
11431                                  */
11432                                 break;
11433                 }
11434         } else
11435                 spin_unlock_irq(&phba->hbalock);
11436
11437         lpfc_sli_mbox_sys_flush(phba);
11438 }
11439
11440 /**
11441  * lpfc_sli_eratt_read - read sli-3 error attention events
11442  * @phba: Pointer to HBA context.
11443  *
11444  * This function is called to read the SLI3 device error attention registers
11445  * for possible error attention events. The caller must hold the hostlock
11446  * with spin_lock_irq().
11447  *
11448  * This function returns 1 when there is Error Attention in the Host Attention
11449  * Register and returns 0 otherwise.
11450  **/
11451 static int
11452 lpfc_sli_eratt_read(struct lpfc_hba *phba)
11453 {
11454         uint32_t ha_copy;
11455
11456         /* Read chip Host Attention (HA) register */
11457         if (lpfc_readl(phba->HAregaddr, &ha_copy))
11458                 goto unplug_err;
11459
11460         if (ha_copy & HA_ERATT) {
11461                 /* Read host status register to retrieve error event */
11462                 if (lpfc_sli_read_hs(phba))
11463                         goto unplug_err;
11464
11465                 /* Check if there is a deferred error condition is active */
11466                 if ((HS_FFER1 & phba->work_hs) &&
11467                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11468                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
11469                         phba->hba_flag |= DEFER_ERATT;
11470                         /* Clear all interrupt enable conditions */
11471                         writel(0, phba->HCregaddr);
11472                         readl(phba->HCregaddr);
11473                 }
11474
11475                 /* Set the driver HA work bitmap */
11476                 phba->work_ha |= HA_ERATT;
11477                 /* Indicate polling handles this ERATT */
11478                 phba->hba_flag |= HBA_ERATT_HANDLED;
11479                 return 1;
11480         }
11481         return 0;
11482
11483 unplug_err:
11484         /* Set the driver HS work bitmap */
11485         phba->work_hs |= UNPLUG_ERR;
11486         /* Set the driver HA work bitmap */
11487         phba->work_ha |= HA_ERATT;
11488         /* Indicate polling handles this ERATT */
11489         phba->hba_flag |= HBA_ERATT_HANDLED;
11490         return 1;
11491 }
11492
11493 /**
11494  * lpfc_sli4_eratt_read - read sli-4 error attention events
11495  * @phba: Pointer to HBA context.
11496  *
11497  * This function is called to read the SLI4 device error attention registers
11498  * for possible error attention events. The caller must hold the hostlock
11499  * with spin_lock_irq().
11500  *
11501  * This function returns 1 when there is Error Attention in the Host Attention
11502  * Register and returns 0 otherwise.
11503  **/
11504 static int
11505 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11506 {
11507         uint32_t uerr_sta_hi, uerr_sta_lo;
11508         uint32_t if_type, portsmphr;
11509         struct lpfc_register portstat_reg;
11510
11511         /*
11512          * For now, use the SLI4 device internal unrecoverable error
11513          * registers for error attention. This can be changed later.
11514          */
11515         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11516         switch (if_type) {
11517         case LPFC_SLI_INTF_IF_TYPE_0:
11518                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11519                         &uerr_sta_lo) ||
11520                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11521                         &uerr_sta_hi)) {
11522                         phba->work_hs |= UNPLUG_ERR;
11523                         phba->work_ha |= HA_ERATT;
11524                         phba->hba_flag |= HBA_ERATT_HANDLED;
11525                         return 1;
11526                 }
11527                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11528                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11529                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11530                                         "1423 HBA Unrecoverable error: "
11531                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11532                                         "ue_mask_lo_reg=0x%x, "
11533                                         "ue_mask_hi_reg=0x%x\n",
11534                                         uerr_sta_lo, uerr_sta_hi,
11535                                         phba->sli4_hba.ue_mask_lo,
11536                                         phba->sli4_hba.ue_mask_hi);
11537                         phba->work_status[0] = uerr_sta_lo;
11538                         phba->work_status[1] = uerr_sta_hi;
11539                         phba->work_ha |= HA_ERATT;
11540                         phba->hba_flag |= HBA_ERATT_HANDLED;
11541                         return 1;
11542                 }
11543                 break;
11544         case LPFC_SLI_INTF_IF_TYPE_2:
11545                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11546                         &portstat_reg.word0) ||
11547                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11548                         &portsmphr)){
11549                         phba->work_hs |= UNPLUG_ERR;
11550                         phba->work_ha |= HA_ERATT;
11551                         phba->hba_flag |= HBA_ERATT_HANDLED;
11552                         return 1;
11553                 }
11554                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11555                         phba->work_status[0] =
11556                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11557                         phba->work_status[1] =
11558                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11559                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11560                                         "2885 Port Status Event: "
11561                                         "port status reg 0x%x, "
11562                                         "port smphr reg 0x%x, "
11563                                         "error 1=0x%x, error 2=0x%x\n",
11564                                         portstat_reg.word0,
11565                                         portsmphr,
11566                                         phba->work_status[0],
11567                                         phba->work_status[1]);
11568                         phba->work_ha |= HA_ERATT;
11569                         phba->hba_flag |= HBA_ERATT_HANDLED;
11570                         return 1;
11571                 }
11572                 break;
11573         case LPFC_SLI_INTF_IF_TYPE_1:
11574         default:
11575                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11576                                 "2886 HBA Error Attention on unsupported "
11577                                 "if type %d.", if_type);
11578                 return 1;
11579         }
11580
11581         return 0;
11582 }
11583
11584 /**
11585  * lpfc_sli_check_eratt - check error attention events
11586  * @phba: Pointer to HBA context.
11587  *
11588  * This function is called from timer soft interrupt context to check HBA's
11589  * error attention register bit for error attention events.
11590  *
11591  * This function returns 1 when there is Error Attention in the Host Attention
11592  * Register and returns 0 otherwise.
11593  **/
11594 int
11595 lpfc_sli_check_eratt(struct lpfc_hba *phba)
11596 {
11597         uint32_t ha_copy;
11598
11599         /* If somebody is waiting to handle an eratt, don't process it
11600          * here. The brdkill function will do this.
11601          */
11602         if (phba->link_flag & LS_IGNORE_ERATT)
11603                 return 0;
11604
11605         /* Check if interrupt handler handles this ERATT */
11606         spin_lock_irq(&phba->hbalock);
11607         if (phba->hba_flag & HBA_ERATT_HANDLED) {
11608                 /* Interrupt handler has handled ERATT */
11609                 spin_unlock_irq(&phba->hbalock);
11610                 return 0;
11611         }
11612
11613         /*
11614          * If there is deferred error attention, do not check for error
11615          * attention
11616          */
11617         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11618                 spin_unlock_irq(&phba->hbalock);
11619                 return 0;
11620         }
11621
11622         /* If PCI channel is offline, don't process it */
11623         if (unlikely(pci_channel_offline(phba->pcidev))) {
11624                 spin_unlock_irq(&phba->hbalock);
11625                 return 0;
11626         }
11627
11628         switch (phba->sli_rev) {
11629         case LPFC_SLI_REV2:
11630         case LPFC_SLI_REV3:
11631                 /* Read chip Host Attention (HA) register */
11632                 ha_copy = lpfc_sli_eratt_read(phba);
11633                 break;
11634         case LPFC_SLI_REV4:
11635                 /* Read device Uncoverable Error (UERR) registers */
11636                 ha_copy = lpfc_sli4_eratt_read(phba);
11637                 break;
11638         default:
11639                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11640                                 "0299 Invalid SLI revision (%d)\n",
11641                                 phba->sli_rev);
11642                 ha_copy = 0;
11643                 break;
11644         }
11645         spin_unlock_irq(&phba->hbalock);
11646
11647         return ha_copy;
11648 }
11649
11650 /**
11651  * lpfc_intr_state_check - Check device state for interrupt handling
11652  * @phba: Pointer to HBA context.
11653  *
11654  * This inline routine checks whether a device or its PCI slot is in a state
11655  * that the interrupt should be handled.
11656  *
11657  * This function returns 0 if the device or the PCI slot is in a state that
11658  * interrupt should be handled, otherwise -EIO.
11659  */
11660 static inline int
11661 lpfc_intr_state_check(struct lpfc_hba *phba)
11662 {
11663         /* If the pci channel is offline, ignore all the interrupts */
11664         if (unlikely(pci_channel_offline(phba->pcidev)))
11665                 return -EIO;
11666
11667         /* Update device level interrupt statistics */
11668         phba->sli.slistat.sli_intr++;
11669
11670         /* Ignore all interrupts during initialization. */
11671         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11672                 return -EIO;
11673
11674         return 0;
11675 }
11676
11677 /**
11678  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
11679  * @irq: Interrupt number.
11680  * @dev_id: The device context pointer.
11681  *
11682  * This function is directly called from the PCI layer as an interrupt
11683  * service routine when device with SLI-3 interface spec is enabled with
11684  * MSI-X multi-message interrupt mode and there are slow-path events in
11685  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11686  * interrupt mode, this function is called as part of the device-level
11687  * interrupt handler. When the PCI slot is in error recovery or the HBA
11688  * is undergoing initialization, the interrupt handler will not process
11689  * the interrupt. The link attention and ELS ring attention events are
11690  * handled by the worker thread. The interrupt handler signals the worker
11691  * thread and returns for these events. This function is called without
11692  * any lock held. It gets the hbalock to access and update SLI data
11693  * structures.
11694  *
11695  * This function returns IRQ_HANDLED when interrupt is handled else it
11696  * returns IRQ_NONE.
11697  **/
11698 irqreturn_t
11699 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
11700 {
11701         struct lpfc_hba  *phba;
11702         uint32_t ha_copy, hc_copy;
11703         uint32_t work_ha_copy;
11704         unsigned long status;
11705         unsigned long iflag;
11706         uint32_t control;
11707
11708         MAILBOX_t *mbox, *pmbox;
11709         struct lpfc_vport *vport;
11710         struct lpfc_nodelist *ndlp;
11711         struct lpfc_dmabuf *mp;
11712         LPFC_MBOXQ_t *pmb;
11713         int rc;
11714
11715         /*
11716          * Get the driver's phba structure from the dev_id and
11717          * assume the HBA is not interrupting.
11718          */
11719         phba = (struct lpfc_hba *)dev_id;
11720
11721         if (unlikely(!phba))
11722                 return IRQ_NONE;
11723
11724         /*
11725          * Stuff needs to be attented to when this function is invoked as an
11726          * individual interrupt handler in MSI-X multi-message interrupt mode
11727          */
11728         if (phba->intr_type == MSIX) {
11729                 /* Check device state for handling interrupt */
11730                 if (lpfc_intr_state_check(phba))
11731                         return IRQ_NONE;
11732                 /* Need to read HA REG for slow-path events */
11733                 spin_lock_irqsave(&phba->hbalock, iflag);
11734                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11735                         goto unplug_error;
11736                 /* If somebody is waiting to handle an eratt don't process it
11737                  * here. The brdkill function will do this.
11738                  */
11739                 if (phba->link_flag & LS_IGNORE_ERATT)
11740                         ha_copy &= ~HA_ERATT;
11741                 /* Check the need for handling ERATT in interrupt handler */
11742                 if (ha_copy & HA_ERATT) {
11743                         if (phba->hba_flag & HBA_ERATT_HANDLED)
11744                                 /* ERATT polling has handled ERATT */
11745                                 ha_copy &= ~HA_ERATT;
11746                         else
11747                                 /* Indicate interrupt handler handles ERATT */
11748                                 phba->hba_flag |= HBA_ERATT_HANDLED;
11749                 }
11750
11751                 /*
11752                  * If there is deferred error attention, do not check for any
11753                  * interrupt.
11754                  */
11755                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11756                         spin_unlock_irqrestore(&phba->hbalock, iflag);
11757                         return IRQ_NONE;
11758                 }
11759
11760                 /* Clear up only attention source related to slow-path */
11761                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11762                         goto unplug_error;
11763
11764                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11765                         HC_LAINT_ENA | HC_ERINT_ENA),
11766                         phba->HCregaddr);
11767                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11768                         phba->HAregaddr);
11769                 writel(hc_copy, phba->HCregaddr);
11770                 readl(phba->HAregaddr); /* flush */
11771                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11772         } else
11773                 ha_copy = phba->ha_copy;
11774
11775         work_ha_copy = ha_copy & phba->work_ha_mask;
11776
11777         if (work_ha_copy) {
11778                 if (work_ha_copy & HA_LATT) {
11779                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11780                                 /*
11781                                  * Turn off Link Attention interrupts
11782                                  * until CLEAR_LA done
11783                                  */
11784                                 spin_lock_irqsave(&phba->hbalock, iflag);
11785                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
11786                                 if (lpfc_readl(phba->HCregaddr, &control))
11787                                         goto unplug_error;
11788                                 control &= ~HC_LAINT_ENA;
11789                                 writel(control, phba->HCregaddr);
11790                                 readl(phba->HCregaddr); /* flush */
11791                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11792                         }
11793                         else
11794                                 work_ha_copy &= ~HA_LATT;
11795                 }
11796
11797                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
11798                         /*
11799                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11800                          * the only slow ring.
11801                          */
11802                         status = (work_ha_copy &
11803                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
11804                         status >>= (4*LPFC_ELS_RING);
11805                         if (status & HA_RXMASK) {
11806                                 spin_lock_irqsave(&phba->hbalock, iflag);
11807                                 if (lpfc_readl(phba->HCregaddr, &control))
11808                                         goto unplug_error;
11809
11810                                 lpfc_debugfs_slow_ring_trc(phba,
11811                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
11812                                 control, status,
11813                                 (uint32_t)phba->sli.slistat.sli_intr);
11814
11815                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
11816                                         lpfc_debugfs_slow_ring_trc(phba,
11817                                                 "ISR Disable ring:"
11818                                                 "pwork:x%x hawork:x%x wait:x%x",
11819                                                 phba->work_ha, work_ha_copy,
11820                                                 (uint32_t)((unsigned long)
11821                                                 &phba->work_waitq));
11822
11823                                         control &=
11824                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
11825                                         writel(control, phba->HCregaddr);
11826                                         readl(phba->HCregaddr); /* flush */
11827                                 }
11828                                 else {
11829                                         lpfc_debugfs_slow_ring_trc(phba,
11830                                                 "ISR slow ring:   pwork:"
11831                                                 "x%x hawork:x%x wait:x%x",
11832                                                 phba->work_ha, work_ha_copy,
11833                                                 (uint32_t)((unsigned long)
11834                                                 &phba->work_waitq));
11835                                 }
11836                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11837                         }
11838                 }
11839                 spin_lock_irqsave(&phba->hbalock, iflag);
11840                 if (work_ha_copy & HA_ERATT) {
11841                         if (lpfc_sli_read_hs(phba))
11842                                 goto unplug_error;
11843                         /*
11844                          * Check if there is a deferred error condition
11845                          * is active
11846                          */
11847                         if ((HS_FFER1 & phba->work_hs) &&
11848                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11849                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
11850                                   phba->work_hs)) {
11851                                 phba->hba_flag |= DEFER_ERATT;
11852                                 /* Clear all interrupt enable conditions */
11853                                 writel(0, phba->HCregaddr);
11854                                 readl(phba->HCregaddr);
11855                         }
11856                 }
11857
11858                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
11859                         pmb = phba->sli.mbox_active;
11860                         pmbox = &pmb->u.mb;
11861                         mbox = phba->mbox;
11862                         vport = pmb->vport;
11863
11864                         /* First check out the status word */
11865                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11866                         if (pmbox->mbxOwner != OWN_HOST) {
11867                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11868                                 /*
11869                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
11870                                  * mbxStatus <status>
11871                                  */
11872                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11873                                                 LOG_SLI,
11874                                                 "(%d):0304 Stray Mailbox "
11875                                                 "Interrupt mbxCommand x%x "
11876                                                 "mbxStatus x%x\n",
11877                                                 (vport ? vport->vpi : 0),
11878                                                 pmbox->mbxCommand,
11879                                                 pmbox->mbxStatus);
11880                                 /* clear mailbox attention bit */
11881                                 work_ha_copy &= ~HA_MBATT;
11882                         } else {
11883                                 phba->sli.mbox_active = NULL;
11884                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11885                                 phba->last_completion_time = jiffies;
11886                                 del_timer(&phba->sli.mbox_tmo);
11887                                 if (pmb->mbox_cmpl) {
11888                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
11889                                                         MAILBOX_CMD_SIZE);
11890                                         if (pmb->out_ext_byte_len &&
11891                                                 pmb->context2)
11892                                                 lpfc_sli_pcimem_bcopy(
11893                                                 phba->mbox_ext,
11894                                                 pmb->context2,
11895                                                 pmb->out_ext_byte_len);
11896                                 }
11897                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11898                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11899
11900                                         lpfc_debugfs_disc_trc(vport,
11901                                                 LPFC_DISC_TRC_MBOX_VPORT,
11902                                                 "MBOX dflt rpi: : "
11903                                                 "status:x%x rpi:x%x",
11904                                                 (uint32_t)pmbox->mbxStatus,
11905                                                 pmbox->un.varWords[0], 0);
11906
11907                                         if (!pmbox->mbxStatus) {
11908                                                 mp = (struct lpfc_dmabuf *)
11909                                                         (pmb->context1);
11910                                                 ndlp = (struct lpfc_nodelist *)
11911                                                         pmb->context2;
11912
11913                                                 /* Reg_LOGIN of dflt RPI was
11914                                                  * successful. new lets get
11915                                                  * rid of the RPI using the
11916                                                  * same mbox buffer.
11917                                                  */
11918                                                 lpfc_unreg_login(phba,
11919                                                         vport->vpi,
11920                                                         pmbox->un.varWords[0],
11921                                                         pmb);
11922                                                 pmb->mbox_cmpl =
11923                                                         lpfc_mbx_cmpl_dflt_rpi;
11924                                                 pmb->context1 = mp;
11925                                                 pmb->context2 = ndlp;
11926                                                 pmb->vport = vport;
11927                                                 rc = lpfc_sli_issue_mbox(phba,
11928                                                                 pmb,
11929                                                                 MBX_NOWAIT);
11930                                                 if (rc != MBX_BUSY)
11931                                                         lpfc_printf_log(phba,
11932                                                         KERN_ERR,
11933                                                         LOG_MBOX | LOG_SLI,
11934                                                         "0350 rc should have"
11935                                                         "been MBX_BUSY\n");
11936                                                 if (rc != MBX_NOT_FINISHED)
11937                                                         goto send_current_mbox;
11938                                         }
11939                                 }
11940                                 spin_lock_irqsave(
11941                                                 &phba->pport->work_port_lock,
11942                                                 iflag);
11943                                 phba->pport->work_port_events &=
11944                                         ~WORKER_MBOX_TMO;
11945                                 spin_unlock_irqrestore(
11946                                                 &phba->pport->work_port_lock,
11947                                                 iflag);
11948                                 lpfc_mbox_cmpl_put(phba, pmb);
11949                         }
11950                 } else
11951                         spin_unlock_irqrestore(&phba->hbalock, iflag);
11952
11953                 if ((work_ha_copy & HA_MBATT) &&
11954                     (phba->sli.mbox_active == NULL)) {
11955 send_current_mbox:
11956                         /* Process next mailbox command if there is one */
11957                         do {
11958                                 rc = lpfc_sli_issue_mbox(phba, NULL,
11959                                                          MBX_NOWAIT);
11960                         } while (rc == MBX_NOT_FINISHED);
11961                         if (rc != MBX_SUCCESS)
11962                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11963                                                 LOG_SLI, "0349 rc should be "
11964                                                 "MBX_SUCCESS\n");
11965                 }
11966
11967                 spin_lock_irqsave(&phba->hbalock, iflag);
11968                 phba->work_ha |= work_ha_copy;
11969                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11970                 lpfc_worker_wake_up(phba);
11971         }
11972         return IRQ_HANDLED;
11973 unplug_error:
11974         spin_unlock_irqrestore(&phba->hbalock, iflag);
11975         return IRQ_HANDLED;
11976
11977 } /* lpfc_sli_sp_intr_handler */
11978
11979 /**
11980  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
11981  * @irq: Interrupt number.
11982  * @dev_id: The device context pointer.
11983  *
11984  * This function is directly called from the PCI layer as an interrupt
11985  * service routine when device with SLI-3 interface spec is enabled with
11986  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11987  * ring event in the HBA. However, when the device is enabled with either
11988  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11989  * device-level interrupt handler. When the PCI slot is in error recovery
11990  * or the HBA is undergoing initialization, the interrupt handler will not
11991  * process the interrupt. The SCSI FCP fast-path ring event are handled in
11992  * the intrrupt context. This function is called without any lock held.
11993  * It gets the hbalock to access and update SLI data structures.
11994  *
11995  * This function returns IRQ_HANDLED when interrupt is handled else it
11996  * returns IRQ_NONE.
11997  **/
11998 irqreturn_t
11999 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
12000 {
12001         struct lpfc_hba  *phba;
12002         uint32_t ha_copy;
12003         unsigned long status;
12004         unsigned long iflag;
12005         struct lpfc_sli_ring *pring;
12006
12007         /* Get the driver's phba structure from the dev_id and
12008          * assume the HBA is not interrupting.
12009          */
12010         phba = (struct lpfc_hba *) dev_id;
12011
12012         if (unlikely(!phba))
12013                 return IRQ_NONE;
12014
12015         /*
12016          * Stuff needs to be attented to when this function is invoked as an
12017          * individual interrupt handler in MSI-X multi-message interrupt mode
12018          */
12019         if (phba->intr_type == MSIX) {
12020                 /* Check device state for handling interrupt */
12021                 if (lpfc_intr_state_check(phba))
12022                         return IRQ_NONE;
12023                 /* Need to read HA REG for FCP ring and other ring events */
12024                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12025                         return IRQ_HANDLED;
12026                 /* Clear up only attention source related to fast-path */
12027                 spin_lock_irqsave(&phba->hbalock, iflag);
12028                 /*
12029                  * If there is deferred error attention, do not check for
12030                  * any interrupt.
12031                  */
12032                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12033                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12034                         return IRQ_NONE;
12035                 }
12036                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12037                         phba->HAregaddr);
12038                 readl(phba->HAregaddr); /* flush */
12039                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12040         } else
12041                 ha_copy = phba->ha_copy;
12042
12043         /*
12044          * Process all events on FCP ring. Take the optimized path for FCP IO.
12045          */
12046         ha_copy &= ~(phba->work_ha_mask);
12047
12048         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12049         status >>= (4*LPFC_FCP_RING);
12050         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12051         if (status & HA_RXMASK)
12052                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
12053
12054         if (phba->cfg_multi_ring_support == 2) {
12055                 /*
12056                  * Process all events on extra ring. Take the optimized path
12057                  * for extra ring IO.
12058                  */
12059                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12060                 status >>= (4*LPFC_EXTRA_RING);
12061                 if (status & HA_RXMASK) {
12062                         lpfc_sli_handle_fast_ring_event(phba,
12063                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12064                                         status);
12065                 }
12066         }
12067         return IRQ_HANDLED;
12068 }  /* lpfc_sli_fp_intr_handler */
12069
12070 /**
12071  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12072  * @irq: Interrupt number.
12073  * @dev_id: The device context pointer.
12074  *
12075  * This function is the HBA device-level interrupt handler to device with
12076  * SLI-3 interface spec, called from the PCI layer when either MSI or
12077  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12078  * requires driver attention. This function invokes the slow-path interrupt
12079  * attention handling function and fast-path interrupt attention handling
12080  * function in turn to process the relevant HBA attention events. This
12081  * function is called without any lock held. It gets the hbalock to access
12082  * and update SLI data structures.
12083  *
12084  * This function returns IRQ_HANDLED when interrupt is handled, else it
12085  * returns IRQ_NONE.
12086  **/
12087 irqreturn_t
12088 lpfc_sli_intr_handler(int irq, void *dev_id)
12089 {
12090         struct lpfc_hba  *phba;
12091         irqreturn_t sp_irq_rc, fp_irq_rc;
12092         unsigned long status1, status2;
12093         uint32_t hc_copy;
12094
12095         /*
12096          * Get the driver's phba structure from the dev_id and
12097          * assume the HBA is not interrupting.
12098          */
12099         phba = (struct lpfc_hba *) dev_id;
12100
12101         if (unlikely(!phba))
12102                 return IRQ_NONE;
12103
12104         /* Check device state for handling interrupt */
12105         if (lpfc_intr_state_check(phba))
12106                 return IRQ_NONE;
12107
12108         spin_lock(&phba->hbalock);
12109         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12110                 spin_unlock(&phba->hbalock);
12111                 return IRQ_HANDLED;
12112         }
12113
12114         if (unlikely(!phba->ha_copy)) {
12115                 spin_unlock(&phba->hbalock);
12116                 return IRQ_NONE;
12117         } else if (phba->ha_copy & HA_ERATT) {
12118                 if (phba->hba_flag & HBA_ERATT_HANDLED)
12119                         /* ERATT polling has handled ERATT */
12120                         phba->ha_copy &= ~HA_ERATT;
12121                 else
12122                         /* Indicate interrupt handler handles ERATT */
12123                         phba->hba_flag |= HBA_ERATT_HANDLED;
12124         }
12125
12126         /*
12127          * If there is deferred error attention, do not check for any interrupt.
12128          */
12129         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12130                 spin_unlock(&phba->hbalock);
12131                 return IRQ_NONE;
12132         }
12133
12134         /* Clear attention sources except link and error attentions */
12135         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12136                 spin_unlock(&phba->hbalock);
12137                 return IRQ_HANDLED;
12138         }
12139         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12140                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12141                 phba->HCregaddr);
12142         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12143         writel(hc_copy, phba->HCregaddr);
12144         readl(phba->HAregaddr); /* flush */
12145         spin_unlock(&phba->hbalock);
12146
12147         /*
12148          * Invokes slow-path host attention interrupt handling as appropriate.
12149          */
12150
12151         /* status of events with mailbox and link attention */
12152         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12153
12154         /* status of events with ELS ring */
12155         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
12156         status2 >>= (4*LPFC_ELS_RING);
12157
12158         if (status1 || (status2 & HA_RXMASK))
12159                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12160         else
12161                 sp_irq_rc = IRQ_NONE;
12162
12163         /*
12164          * Invoke fast-path host attention interrupt handling as appropriate.
12165          */
12166
12167         /* status of events with FCP ring */
12168         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12169         status1 >>= (4*LPFC_FCP_RING);
12170
12171         /* status of events with extra ring */
12172         if (phba->cfg_multi_ring_support == 2) {
12173                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12174                 status2 >>= (4*LPFC_EXTRA_RING);
12175         } else
12176                 status2 = 0;
12177
12178         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12179                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12180         else
12181                 fp_irq_rc = IRQ_NONE;
12182
12183         /* Return device-level interrupt handling status */
12184         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12185 }  /* lpfc_sli_intr_handler */
12186
12187 /**
12188  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12189  * @phba: pointer to lpfc hba data structure.
12190  *
12191  * This routine is invoked by the worker thread to process all the pending
12192  * SLI4 FCP abort XRI events.
12193  **/
12194 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12195 {
12196         struct lpfc_cq_event *cq_event;
12197
12198         /* First, declare the fcp xri abort event has been handled */
12199         spin_lock_irq(&phba->hbalock);
12200         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12201         spin_unlock_irq(&phba->hbalock);
12202         /* Now, handle all the fcp xri abort events */
12203         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12204                 /* Get the first event from the head of the event queue */
12205                 spin_lock_irq(&phba->hbalock);
12206                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12207                                  cq_event, struct lpfc_cq_event, list);
12208                 spin_unlock_irq(&phba->hbalock);
12209                 /* Notify aborted XRI for FCP work queue */
12210                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12211                 /* Free the event processed back to the free pool */
12212                 lpfc_sli4_cq_event_release(phba, cq_event);
12213         }
12214 }
12215
12216 /**
12217  * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12218  * @phba: pointer to lpfc hba data structure.
12219  *
12220  * This routine is invoked by the worker thread to process all the pending
12221  * SLI4 NVME abort XRI events.
12222  **/
12223 void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12224 {
12225         struct lpfc_cq_event *cq_event;
12226
12227         /* First, declare the fcp xri abort event has been handled */
12228         spin_lock_irq(&phba->hbalock);
12229         phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12230         spin_unlock_irq(&phba->hbalock);
12231         /* Now, handle all the fcp xri abort events */
12232         while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12233                 /* Get the first event from the head of the event queue */
12234                 spin_lock_irq(&phba->hbalock);
12235                 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12236                                  cq_event, struct lpfc_cq_event, list);
12237                 spin_unlock_irq(&phba->hbalock);
12238                 /* Notify aborted XRI for NVME work queue */
12239                 if (phba->nvmet_support) {
12240                         lpfc_sli4_nvmet_xri_aborted(phba,
12241                                                     &cq_event->cqe.wcqe_axri);
12242                 } else {
12243                         lpfc_sli4_nvme_xri_aborted(phba,
12244                                                    &cq_event->cqe.wcqe_axri);
12245                 }
12246                 /* Free the event processed back to the free pool */
12247                 lpfc_sli4_cq_event_release(phba, cq_event);
12248         }
12249 }
12250
12251 /**
12252  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12253  * @phba: pointer to lpfc hba data structure.
12254  *
12255  * This routine is invoked by the worker thread to process all the pending
12256  * SLI4 els abort xri events.
12257  **/
12258 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12259 {
12260         struct lpfc_cq_event *cq_event;
12261
12262         /* First, declare the els xri abort event has been handled */
12263         spin_lock_irq(&phba->hbalock);
12264         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12265         spin_unlock_irq(&phba->hbalock);
12266         /* Now, handle all the els xri abort events */
12267         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12268                 /* Get the first event from the head of the event queue */
12269                 spin_lock_irq(&phba->hbalock);
12270                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12271                                  cq_event, struct lpfc_cq_event, list);
12272                 spin_unlock_irq(&phba->hbalock);
12273                 /* Notify aborted XRI for ELS work queue */
12274                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12275                 /* Free the event processed back to the free pool */
12276                 lpfc_sli4_cq_event_release(phba, cq_event);
12277         }
12278 }
12279
12280 /**
12281  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12282  * @phba: pointer to lpfc hba data structure
12283  * @pIocbIn: pointer to the rspiocbq
12284  * @pIocbOut: pointer to the cmdiocbq
12285  * @wcqe: pointer to the complete wcqe
12286  *
12287  * This routine transfers the fields of a command iocbq to a response iocbq
12288  * by copying all the IOCB fields from command iocbq and transferring the
12289  * completion status information from the complete wcqe.
12290  **/
12291 static void
12292 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12293                               struct lpfc_iocbq *pIocbIn,
12294                               struct lpfc_iocbq *pIocbOut,
12295                               struct lpfc_wcqe_complete *wcqe)
12296 {
12297         int numBdes, i;
12298         unsigned long iflags;
12299         uint32_t status, max_response;
12300         struct lpfc_dmabuf *dmabuf;
12301         struct ulp_bde64 *bpl, bde;
12302         size_t offset = offsetof(struct lpfc_iocbq, iocb);
12303
12304         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12305                sizeof(struct lpfc_iocbq) - offset);
12306         /* Map WCQE parameters into irspiocb parameters */
12307         status = bf_get(lpfc_wcqe_c_status, wcqe);
12308         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12309         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12310                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12311                         pIocbIn->iocb.un.fcpi.fcpi_parm =
12312                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
12313                                         wcqe->total_data_placed;
12314                 else
12315                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12316         else {
12317                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12318                 switch (pIocbOut->iocb.ulpCommand) {
12319                 case CMD_ELS_REQUEST64_CR:
12320                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12321                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
12322                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12323                         max_response = bde.tus.f.bdeSize;
12324                         break;
12325                 case CMD_GEN_REQUEST64_CR:
12326                         max_response = 0;
12327                         if (!pIocbOut->context3)
12328                                 break;
12329                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12330                                         sizeof(struct ulp_bde64);
12331                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12332                         bpl = (struct ulp_bde64 *)dmabuf->virt;
12333                         for (i = 0; i < numBdes; i++) {
12334                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12335                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12336                                         max_response += bde.tus.f.bdeSize;
12337                         }
12338                         break;
12339                 default:
12340                         max_response = wcqe->total_data_placed;
12341                         break;
12342                 }
12343                 if (max_response < wcqe->total_data_placed)
12344                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12345                 else
12346                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12347                                 wcqe->total_data_placed;
12348         }
12349
12350         /* Convert BG errors for completion status */
12351         if (status == CQE_STATUS_DI_ERROR) {
12352                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12353
12354                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12355                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12356                 else
12357                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12358
12359                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12360                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12361                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12362                                 BGS_GUARD_ERR_MASK;
12363                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12364                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12365                                 BGS_APPTAG_ERR_MASK;
12366                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12367                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12368                                 BGS_REFTAG_ERR_MASK;
12369
12370                 /* Check to see if there was any good data before the error */
12371                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12372                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12373                                 BGS_HI_WATER_MARK_PRESENT_MASK;
12374                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
12375                                 wcqe->total_data_placed;
12376                 }
12377
12378                 /*
12379                 * Set ALL the error bits to indicate we don't know what
12380                 * type of error it is.
12381                 */
12382                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12383                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12384                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12385                                 BGS_GUARD_ERR_MASK);
12386         }
12387
12388         /* Pick up HBA exchange busy condition */
12389         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12390                 spin_lock_irqsave(&phba->hbalock, iflags);
12391                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12392                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12393         }
12394 }
12395
12396 /**
12397  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12398  * @phba: Pointer to HBA context object.
12399  * @wcqe: Pointer to work-queue completion queue entry.
12400  *
12401  * This routine handles an ELS work-queue completion event and construct
12402  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12403  * discovery engine to handle.
12404  *
12405  * Return: Pointer to the receive IOCBQ, NULL otherwise.
12406  **/
12407 static struct lpfc_iocbq *
12408 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12409                                struct lpfc_iocbq *irspiocbq)
12410 {
12411         struct lpfc_sli_ring *pring;
12412         struct lpfc_iocbq *cmdiocbq;
12413         struct lpfc_wcqe_complete *wcqe;
12414         unsigned long iflags;
12415
12416         pring = lpfc_phba_elsring(phba);
12417
12418         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
12419         spin_lock_irqsave(&pring->ring_lock, iflags);
12420         pring->stats.iocb_event++;
12421         /* Look up the ELS command IOCB and create pseudo response IOCB */
12422         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12423                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12424         /* Put the iocb back on the txcmplq */
12425         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12426         spin_unlock_irqrestore(&pring->ring_lock, iflags);
12427
12428         if (unlikely(!cmdiocbq)) {
12429                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12430                                 "0386 ELS complete with no corresponding "
12431                                 "cmdiocb: iotag (%d)\n",
12432                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12433                 lpfc_sli_release_iocbq(phba, irspiocbq);
12434                 return NULL;
12435         }
12436
12437         /* Fake the irspiocbq and copy necessary response information */
12438         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
12439
12440         return irspiocbq;
12441 }
12442
12443 /**
12444  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12445  * @phba: Pointer to HBA context object.
12446  * @cqe: Pointer to mailbox completion queue entry.
12447  *
12448  * This routine process a mailbox completion queue entry with asynchrous
12449  * event.
12450  *
12451  * Return: true if work posted to worker thread, otherwise false.
12452  **/
12453 static bool
12454 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12455 {
12456         struct lpfc_cq_event *cq_event;
12457         unsigned long iflags;
12458
12459         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12460                         "0392 Async Event: word0:x%x, word1:x%x, "
12461                         "word2:x%x, word3:x%x\n", mcqe->word0,
12462                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12463
12464         /* Allocate a new internal CQ_EVENT entry */
12465         cq_event = lpfc_sli4_cq_event_alloc(phba);
12466         if (!cq_event) {
12467                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12468                                 "0394 Failed to allocate CQ_EVENT entry\n");
12469                 return false;
12470         }
12471
12472         /* Move the CQE into an asynchronous event entry */
12473         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12474         spin_lock_irqsave(&phba->hbalock, iflags);
12475         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12476         /* Set the async event flag */
12477         phba->hba_flag |= ASYNC_EVENT;
12478         spin_unlock_irqrestore(&phba->hbalock, iflags);
12479
12480         return true;
12481 }
12482
12483 /**
12484  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12485  * @phba: Pointer to HBA context object.
12486  * @cqe: Pointer to mailbox completion queue entry.
12487  *
12488  * This routine process a mailbox completion queue entry with mailbox
12489  * completion event.
12490  *
12491  * Return: true if work posted to worker thread, otherwise false.
12492  **/
12493 static bool
12494 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12495 {
12496         uint32_t mcqe_status;
12497         MAILBOX_t *mbox, *pmbox;
12498         struct lpfc_mqe *mqe;
12499         struct lpfc_vport *vport;
12500         struct lpfc_nodelist *ndlp;
12501         struct lpfc_dmabuf *mp;
12502         unsigned long iflags;
12503         LPFC_MBOXQ_t *pmb;
12504         bool workposted = false;
12505         int rc;
12506
12507         /* If not a mailbox complete MCQE, out by checking mailbox consume */
12508         if (!bf_get(lpfc_trailer_completed, mcqe))
12509                 goto out_no_mqe_complete;
12510
12511         /* Get the reference to the active mbox command */
12512         spin_lock_irqsave(&phba->hbalock, iflags);
12513         pmb = phba->sli.mbox_active;
12514         if (unlikely(!pmb)) {
12515                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12516                                 "1832 No pending MBOX command to handle\n");
12517                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12518                 goto out_no_mqe_complete;
12519         }
12520         spin_unlock_irqrestore(&phba->hbalock, iflags);
12521         mqe = &pmb->u.mqe;
12522         pmbox = (MAILBOX_t *)&pmb->u.mqe;
12523         mbox = phba->mbox;
12524         vport = pmb->vport;
12525
12526         /* Reset heartbeat timer */
12527         phba->last_completion_time = jiffies;
12528         del_timer(&phba->sli.mbox_tmo);
12529
12530         /* Move mbox data to caller's mailbox region, do endian swapping */
12531         if (pmb->mbox_cmpl && mbox)
12532                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
12533
12534         /*
12535          * For mcqe errors, conditionally move a modified error code to
12536          * the mbox so that the error will not be missed.
12537          */
12538         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12539         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12540                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12541                         bf_set(lpfc_mqe_status, mqe,
12542                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
12543         }
12544         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12545                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12546                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12547                                       "MBOX dflt rpi: status:x%x rpi:x%x",
12548                                       mcqe_status,
12549                                       pmbox->un.varWords[0], 0);
12550                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12551                         mp = (struct lpfc_dmabuf *)(pmb->context1);
12552                         ndlp = (struct lpfc_nodelist *)pmb->context2;
12553                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
12554                          * RID of the PPI using the same mbox buffer.
12555                          */
12556                         lpfc_unreg_login(phba, vport->vpi,
12557                                          pmbox->un.varWords[0], pmb);
12558                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12559                         pmb->context1 = mp;
12560                         pmb->context2 = ndlp;
12561                         pmb->vport = vport;
12562                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12563                         if (rc != MBX_BUSY)
12564                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12565                                                 LOG_SLI, "0385 rc should "
12566                                                 "have been MBX_BUSY\n");
12567                         if (rc != MBX_NOT_FINISHED)
12568                                 goto send_current_mbox;
12569                 }
12570         }
12571         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12572         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12573         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12574
12575         /* There is mailbox completion work to do */
12576         spin_lock_irqsave(&phba->hbalock, iflags);
12577         __lpfc_mbox_cmpl_put(phba, pmb);
12578         phba->work_ha |= HA_MBATT;
12579         spin_unlock_irqrestore(&phba->hbalock, iflags);
12580         workposted = true;
12581
12582 send_current_mbox:
12583         spin_lock_irqsave(&phba->hbalock, iflags);
12584         /* Release the mailbox command posting token */
12585         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12586         /* Setting active mailbox pointer need to be in sync to flag clear */
12587         phba->sli.mbox_active = NULL;
12588         spin_unlock_irqrestore(&phba->hbalock, iflags);
12589         /* Wake up worker thread to post the next pending mailbox command */
12590         lpfc_worker_wake_up(phba);
12591 out_no_mqe_complete:
12592         if (bf_get(lpfc_trailer_consumed, mcqe))
12593                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12594         return workposted;
12595 }
12596
12597 /**
12598  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12599  * @phba: Pointer to HBA context object.
12600  * @cqe: Pointer to mailbox completion queue entry.
12601  *
12602  * This routine process a mailbox completion queue entry, it invokes the
12603  * proper mailbox complete handling or asynchrous event handling routine
12604  * according to the MCQE's async bit.
12605  *
12606  * Return: true if work posted to worker thread, otherwise false.
12607  **/
12608 static bool
12609 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12610 {
12611         struct lpfc_mcqe mcqe;
12612         bool workposted;
12613
12614         /* Copy the mailbox MCQE and convert endian order as needed */
12615         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12616
12617         /* Invoke the proper event handling routine */
12618         if (!bf_get(lpfc_trailer_async, &mcqe))
12619                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12620         else
12621                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12622         return workposted;
12623 }
12624
12625 /**
12626  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12627  * @phba: Pointer to HBA context object.
12628  * @cq: Pointer to associated CQ
12629  * @wcqe: Pointer to work-queue completion queue entry.
12630  *
12631  * This routine handles an ELS work-queue completion event.
12632  *
12633  * Return: true if work posted to worker thread, otherwise false.
12634  **/
12635 static bool
12636 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12637                              struct lpfc_wcqe_complete *wcqe)
12638 {
12639         struct lpfc_iocbq *irspiocbq;
12640         unsigned long iflags;
12641         struct lpfc_sli_ring *pring = cq->pring;
12642         int txq_cnt = 0;
12643         int txcmplq_cnt = 0;
12644         int fcp_txcmplq_cnt = 0;
12645
12646         /* Get an irspiocbq for later ELS response processing use */
12647         irspiocbq = lpfc_sli_get_iocbq(phba);
12648         if (!irspiocbq) {
12649                 if (!list_empty(&pring->txq))
12650                         txq_cnt++;
12651                 if (!list_empty(&pring->txcmplq))
12652                         txcmplq_cnt++;
12653                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12654                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12655                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
12656                         txq_cnt, phba->iocb_cnt,
12657                         fcp_txcmplq_cnt,
12658                         txcmplq_cnt);
12659                 return false;
12660         }
12661
12662         /* Save off the slow-path queue event for work thread to process */
12663         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
12664         spin_lock_irqsave(&phba->hbalock, iflags);
12665         list_add_tail(&irspiocbq->cq_event.list,
12666                       &phba->sli4_hba.sp_queue_event);
12667         phba->hba_flag |= HBA_SP_QUEUE_EVT;
12668         spin_unlock_irqrestore(&phba->hbalock, iflags);
12669
12670         return true;
12671 }
12672
12673 /**
12674  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12675  * @phba: Pointer to HBA context object.
12676  * @wcqe: Pointer to work-queue completion queue entry.
12677  *
12678  * This routine handles slow-path WQ entry consumed event by invoking the
12679  * proper WQ release routine to the slow-path WQ.
12680  **/
12681 static void
12682 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12683                              struct lpfc_wcqe_release *wcqe)
12684 {
12685         /* sanity check on queue memory */
12686         if (unlikely(!phba->sli4_hba.els_wq))
12687                 return;
12688         /* Check for the slow-path ELS work queue */
12689         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12690                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12691                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12692         else
12693                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12694                                 "2579 Slow-path wqe consume event carries "
12695                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12696                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12697                                 phba->sli4_hba.els_wq->queue_id);
12698 }
12699
12700 /**
12701  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12702  * @phba: Pointer to HBA context object.
12703  * @cq: Pointer to a WQ completion queue.
12704  * @wcqe: Pointer to work-queue completion queue entry.
12705  *
12706  * This routine handles an XRI abort event.
12707  *
12708  * Return: true if work posted to worker thread, otherwise false.
12709  **/
12710 static bool
12711 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12712                                    struct lpfc_queue *cq,
12713                                    struct sli4_wcqe_xri_aborted *wcqe)
12714 {
12715         bool workposted = false;
12716         struct lpfc_cq_event *cq_event;
12717         unsigned long iflags;
12718
12719         /* Allocate a new internal CQ_EVENT entry */
12720         cq_event = lpfc_sli4_cq_event_alloc(phba);
12721         if (!cq_event) {
12722                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12723                                 "0602 Failed to allocate CQ_EVENT entry\n");
12724                 return false;
12725         }
12726
12727         /* Move the CQE into the proper xri abort event list */
12728         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12729         switch (cq->subtype) {
12730         case LPFC_FCP:
12731                 spin_lock_irqsave(&phba->hbalock, iflags);
12732                 list_add_tail(&cq_event->list,
12733                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12734                 /* Set the fcp xri abort event flag */
12735                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12736                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12737                 workposted = true;
12738                 break;
12739         case LPFC_ELS:
12740                 spin_lock_irqsave(&phba->hbalock, iflags);
12741                 list_add_tail(&cq_event->list,
12742                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12743                 /* Set the els xri abort event flag */
12744                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12745                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12746                 workposted = true;
12747                 break;
12748         case LPFC_NVME:
12749                 spin_lock_irqsave(&phba->hbalock, iflags);
12750                 list_add_tail(&cq_event->list,
12751                               &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12752                 /* Set the nvme xri abort event flag */
12753                 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12754                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12755                 workposted = true;
12756                 break;
12757         default:
12758                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12759                                 "0603 Invalid CQ subtype %d: "
12760                                 "%08x %08x %08x %08x\n",
12761                                 cq->subtype, wcqe->word0, wcqe->parameter,
12762                                 wcqe->word2, wcqe->word3);
12763                 lpfc_sli4_cq_event_release(phba, cq_event);
12764                 workposted = false;
12765                 break;
12766         }
12767         return workposted;
12768 }
12769
12770 /**
12771  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12772  * @phba: Pointer to HBA context object.
12773  * @rcqe: Pointer to receive-queue completion queue entry.
12774  *
12775  * This routine process a receive-queue completion queue entry.
12776  *
12777  * Return: true if work posted to worker thread, otherwise false.
12778  **/
12779 static bool
12780 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12781 {
12782         bool workposted = false;
12783         struct fc_frame_header *fc_hdr;
12784         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12785         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12786         struct hbq_dmabuf *dma_buf;
12787         uint32_t status, rq_id;
12788         unsigned long iflags;
12789
12790         /* sanity check on queue memory */
12791         if (unlikely(!hrq) || unlikely(!drq))
12792                 return workposted;
12793
12794         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12795                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12796         else
12797                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12798         if (rq_id != hrq->queue_id)
12799                 goto out;
12800
12801         status = bf_get(lpfc_rcqe_status, rcqe);
12802         switch (status) {
12803         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12804                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12805                                 "2537 Receive Frame Truncated!!\n");
12806                 hrq->RQ_buf_trunc++;
12807         case FC_STATUS_RQ_SUCCESS:
12808                 lpfc_sli4_rq_release(hrq, drq);
12809                 spin_lock_irqsave(&phba->hbalock, iflags);
12810                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12811                 if (!dma_buf) {
12812                         hrq->RQ_no_buf_found++;
12813                         spin_unlock_irqrestore(&phba->hbalock, iflags);
12814                         goto out;
12815                 }
12816                 hrq->RQ_rcv_buf++;
12817                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12818
12819                 /* If a NVME LS event (type 0x28), treat it as Fast path */
12820                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12821
12822                 /* save off the frame for the word thread to process */
12823                 list_add_tail(&dma_buf->cq_event.list,
12824                               &phba->sli4_hba.sp_queue_event);
12825                 /* Frame received */
12826                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12827                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12828                 workposted = true;
12829                 break;
12830         case FC_STATUS_INSUFF_BUF_NEED_BUF:
12831         case FC_STATUS_INSUFF_BUF_FRM_DISC:
12832                 hrq->RQ_no_posted_buf++;
12833                 /* Post more buffers if possible */
12834                 spin_lock_irqsave(&phba->hbalock, iflags);
12835                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12836                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12837                 workposted = true;
12838                 break;
12839         }
12840 out:
12841         return workposted;
12842 }
12843
12844 /**
12845  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12846  * @phba: Pointer to HBA context object.
12847  * @cq: Pointer to the completion queue.
12848  * @wcqe: Pointer to a completion queue entry.
12849  *
12850  * This routine process a slow-path work-queue or receive queue completion queue
12851  * entry.
12852  *
12853  * Return: true if work posted to worker thread, otherwise false.
12854  **/
12855 static bool
12856 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12857                          struct lpfc_cqe *cqe)
12858 {
12859         struct lpfc_cqe cqevt;
12860         bool workposted = false;
12861
12862         /* Copy the work queue CQE and convert endian order if needed */
12863         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
12864
12865         /* Check and process for different type of WCQE and dispatch */
12866         switch (bf_get(lpfc_cqe_code, &cqevt)) {
12867         case CQE_CODE_COMPL_WQE:
12868                 /* Process the WQ/RQ complete event */
12869                 phba->last_completion_time = jiffies;
12870                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
12871                                 (struct lpfc_wcqe_complete *)&cqevt);
12872                 break;
12873         case CQE_CODE_RELEASE_WQE:
12874                 /* Process the WQ release event */
12875                 lpfc_sli4_sp_handle_rel_wcqe(phba,
12876                                 (struct lpfc_wcqe_release *)&cqevt);
12877                 break;
12878         case CQE_CODE_XRI_ABORTED:
12879                 /* Process the WQ XRI abort event */
12880                 phba->last_completion_time = jiffies;
12881                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12882                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
12883                 break;
12884         case CQE_CODE_RECEIVE:
12885         case CQE_CODE_RECEIVE_V1:
12886                 /* Process the RQ event */
12887                 phba->last_completion_time = jiffies;
12888                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
12889                                 (struct lpfc_rcqe *)&cqevt);
12890                 break;
12891         default:
12892                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12893                                 "0388 Not a valid WCQE code: x%x\n",
12894                                 bf_get(lpfc_cqe_code, &cqevt));
12895                 break;
12896         }
12897         return workposted;
12898 }
12899
12900 /**
12901  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12902  * @phba: Pointer to HBA context object.
12903  * @eqe: Pointer to fast-path event queue entry.
12904  *
12905  * This routine process a event queue entry from the slow-path event queue.
12906  * It will check the MajorCode and MinorCode to determine this is for a
12907  * completion event on a completion queue, if not, an error shall be logged
12908  * and just return. Otherwise, it will get to the corresponding completion
12909  * queue and process all the entries on that completion queue, rearm the
12910  * completion queue, and then return.
12911  *
12912  **/
12913 static void
12914 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12915         struct lpfc_queue *speq)
12916 {
12917         struct lpfc_queue *cq = NULL, *childq;
12918         struct lpfc_cqe *cqe;
12919         bool workposted = false;
12920         int ecount = 0;
12921         uint16_t cqid;
12922
12923         /* Get the reference to the corresponding CQ */
12924         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12925
12926         list_for_each_entry(childq, &speq->child_list, list) {
12927                 if (childq->queue_id == cqid) {
12928                         cq = childq;
12929                         break;
12930                 }
12931         }
12932         if (unlikely(!cq)) {
12933                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12934                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12935                                         "0365 Slow-path CQ identifier "
12936                                         "(%d) does not exist\n", cqid);
12937                 return;
12938         }
12939
12940         /* Save EQ associated with this CQ */
12941         cq->assoc_qp = speq;
12942
12943         /* Process all the entries to the CQ */
12944         switch (cq->type) {
12945         case LPFC_MCQ:
12946                 while ((cqe = lpfc_sli4_cq_get(cq))) {
12947                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
12948                         if (!(++ecount % cq->entry_repost))
12949                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12950                         cq->CQ_mbox++;
12951                 }
12952                 break;
12953         case LPFC_WCQ:
12954                 while ((cqe = lpfc_sli4_cq_get(cq))) {
12955                         if ((cq->subtype == LPFC_FCP) ||
12956                             (cq->subtype == LPFC_NVME))
12957                                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
12958                                                                        cqe);
12959                         else
12960                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12961                                                                       cqe);
12962                         if (!(++ecount % cq->entry_repost))
12963                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12964                 }
12965
12966                 /* Track the max number of CQEs processed in 1 EQ */
12967                 if (ecount > cq->CQ_max_cqe)
12968                         cq->CQ_max_cqe = ecount;
12969                 break;
12970         default:
12971                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12972                                 "0370 Invalid completion queue type (%d)\n",
12973                                 cq->type);
12974                 return;
12975         }
12976
12977         /* Catch the no cq entry condition, log an error */
12978         if (unlikely(ecount == 0))
12979                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12980                                 "0371 No entry from the CQ: identifier "
12981                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12982
12983         /* In any case, flash and re-arm the RCQ */
12984         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12985
12986         /* wake up worker thread if there are works to be done */
12987         if (workposted)
12988                 lpfc_worker_wake_up(phba);
12989 }
12990
12991 /**
12992  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
12993  * @phba: Pointer to HBA context object.
12994  * @cq: Pointer to associated CQ
12995  * @wcqe: Pointer to work-queue completion queue entry.
12996  *
12997  * This routine process a fast-path work queue completion entry from fast-path
12998  * event queue for FCP command response completion.
12999  **/
13000 static void
13001 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13002                              struct lpfc_wcqe_complete *wcqe)
13003 {
13004         struct lpfc_sli_ring *pring = cq->pring;
13005         struct lpfc_iocbq *cmdiocbq;
13006         struct lpfc_iocbq irspiocbq;
13007         unsigned long iflags;
13008
13009         /* Check for response status */
13010         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13011                 /* If resource errors reported from HBA, reduce queue
13012                  * depth of the SCSI device.
13013                  */
13014                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13015                      IOSTAT_LOCAL_REJECT)) &&
13016                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
13017                      IOERR_NO_RESOURCES))
13018                         phba->lpfc_rampdown_queue_depth(phba);
13019
13020                 /* Log the error status */
13021                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13022                                 "0373 FCP complete error: status=x%x, "
13023                                 "hw_status=x%x, total_data_specified=%d, "
13024                                 "parameter=x%x, word3=x%x\n",
13025                                 bf_get(lpfc_wcqe_c_status, wcqe),
13026                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13027                                 wcqe->total_data_placed, wcqe->parameter,
13028                                 wcqe->word3);
13029         }
13030
13031         /* Look up the FCP command IOCB and create pseudo response IOCB */
13032         spin_lock_irqsave(&pring->ring_lock, iflags);
13033         pring->stats.iocb_event++;
13034         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13035                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13036         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13037         if (unlikely(!cmdiocbq)) {
13038                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13039                                 "0374 FCP complete with no corresponding "
13040                                 "cmdiocb: iotag (%d)\n",
13041                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13042                 return;
13043         }
13044
13045         if (cq->assoc_qp)
13046                 cmdiocbq->isr_timestamp =
13047                         cq->assoc_qp->isr_timestamp;
13048
13049         if (cmdiocbq->iocb_cmpl == NULL) {
13050                 if (cmdiocbq->wqe_cmpl) {
13051                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13052                                 spin_lock_irqsave(&phba->hbalock, iflags);
13053                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13054                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13055                         }
13056
13057                         /* Pass the cmd_iocb and the wcqe to the upper layer */
13058                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13059                         return;
13060                 }
13061                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13062                                 "0375 FCP cmdiocb not callback function "
13063                                 "iotag: (%d)\n",
13064                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13065                 return;
13066         }
13067
13068         /* Fake the irspiocb and copy necessary response information */
13069         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13070
13071         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13072                 spin_lock_irqsave(&phba->hbalock, iflags);
13073                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13074                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13075         }
13076
13077         /* Pass the cmd_iocb and the rsp state to the upper layer */
13078         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13079 }
13080
13081 /**
13082  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13083  * @phba: Pointer to HBA context object.
13084  * @cq: Pointer to completion queue.
13085  * @wcqe: Pointer to work-queue completion queue entry.
13086  *
13087  * This routine handles an fast-path WQ entry consumed event by invoking the
13088  * proper WQ release routine to the slow-path WQ.
13089  **/
13090 static void
13091 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13092                              struct lpfc_wcqe_release *wcqe)
13093 {
13094         struct lpfc_queue *childwq;
13095         bool wqid_matched = false;
13096         uint16_t hba_wqid;
13097
13098         /* Check for fast-path FCP work queue release */
13099         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13100         list_for_each_entry(childwq, &cq->child_list, list) {
13101                 if (childwq->queue_id == hba_wqid) {
13102                         lpfc_sli4_wq_release(childwq,
13103                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13104                         wqid_matched = true;
13105                         break;
13106                 }
13107         }
13108         /* Report warning log message if no match found */
13109         if (wqid_matched != true)
13110                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13111                                 "2580 Fast-path wqe consume event carries "
13112                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13113 }
13114
13115 /**
13116  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13117  * @phba: Pointer to HBA context object.
13118  * @rcqe: Pointer to receive-queue completion queue entry.
13119  *
13120  * This routine process a receive-queue completion queue entry.
13121  *
13122  * Return: true if work posted to worker thread, otherwise false.
13123  **/
13124 static bool
13125 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13126                             struct lpfc_rcqe *rcqe)
13127 {
13128         bool workposted = false;
13129         struct lpfc_queue *hrq;
13130         struct lpfc_queue *drq;
13131         struct rqb_dmabuf *dma_buf;
13132         struct fc_frame_header *fc_hdr;
13133         uint32_t status, rq_id;
13134         unsigned long iflags;
13135         uint32_t fctl, idx;
13136
13137         if ((phba->nvmet_support == 0) ||
13138             (phba->sli4_hba.nvmet_cqset == NULL))
13139                 return workposted;
13140
13141         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13142         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13143         drq = phba->sli4_hba.nvmet_mrq_data[idx];
13144
13145         /* sanity check on queue memory */
13146         if (unlikely(!hrq) || unlikely(!drq))
13147                 return workposted;
13148
13149         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13150                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13151         else
13152                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13153
13154         if ((phba->nvmet_support == 0) ||
13155             (rq_id != hrq->queue_id))
13156                 return workposted;
13157
13158         status = bf_get(lpfc_rcqe_status, rcqe);
13159         switch (status) {
13160         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13161                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13162                                 "6126 Receive Frame Truncated!!\n");
13163                 hrq->RQ_buf_trunc++;
13164                 break;
13165         case FC_STATUS_RQ_SUCCESS:
13166                 lpfc_sli4_rq_release(hrq, drq);
13167                 spin_lock_irqsave(&phba->hbalock, iflags);
13168                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13169                 if (!dma_buf) {
13170                         hrq->RQ_no_buf_found++;
13171                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13172                         goto out;
13173                 }
13174                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13175                 hrq->RQ_rcv_buf++;
13176                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13177
13178                 /* Just some basic sanity checks on FCP Command frame */
13179                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13180                 fc_hdr->fh_f_ctl[1] << 8 |
13181                 fc_hdr->fh_f_ctl[2]);
13182                 if (((fctl &
13183                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13184                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13185                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13186                         goto drop;
13187
13188                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13189                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length,  rcqe);
13190                         lpfc_nvmet_unsol_fcp_event(
13191                                 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13192                                 cq->assoc_qp->isr_timestamp);
13193                         return false;
13194                 }
13195 drop:
13196                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13197                 break;
13198         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13199         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13200                 hrq->RQ_no_posted_buf++;
13201                 /* Post more buffers if possible */
13202                 spin_lock_irqsave(&phba->hbalock, iflags);
13203                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13204                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13205                 workposted = true;
13206                 break;
13207         }
13208 out:
13209         return workposted;
13210 }
13211
13212 /**
13213  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13214  * @cq: Pointer to the completion queue.
13215  * @eqe: Pointer to fast-path completion queue entry.
13216  *
13217  * This routine process a fast-path work queue completion entry from fast-path
13218  * event queue for FCP command response completion.
13219  **/
13220 static int
13221 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13222                          struct lpfc_cqe *cqe)
13223 {
13224         struct lpfc_wcqe_release wcqe;
13225         bool workposted = false;
13226
13227         /* Copy the work queue CQE and convert endian order if needed */
13228         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13229
13230         /* Check and process for different type of WCQE and dispatch */
13231         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13232         case CQE_CODE_COMPL_WQE:
13233         case CQE_CODE_NVME_ERSP:
13234                 cq->CQ_wq++;
13235                 /* Process the WQ complete event */
13236                 phba->last_completion_time = jiffies;
13237                 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13238                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13239                                 (struct lpfc_wcqe_complete *)&wcqe);
13240                 if (cq->subtype == LPFC_NVME_LS)
13241                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13242                                 (struct lpfc_wcqe_complete *)&wcqe);
13243                 break;
13244         case CQE_CODE_RELEASE_WQE:
13245                 cq->CQ_release_wqe++;
13246                 /* Process the WQ release event */
13247                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13248                                 (struct lpfc_wcqe_release *)&wcqe);
13249                 break;
13250         case CQE_CODE_XRI_ABORTED:
13251                 cq->CQ_xri_aborted++;
13252                 /* Process the WQ XRI abort event */
13253                 phba->last_completion_time = jiffies;
13254                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13255                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
13256                 break;
13257         case CQE_CODE_RECEIVE_V1:
13258         case CQE_CODE_RECEIVE:
13259                 phba->last_completion_time = jiffies;
13260                 if (cq->subtype == LPFC_NVMET) {
13261                         workposted = lpfc_sli4_nvmet_handle_rcqe(
13262                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
13263                 }
13264                 break;
13265         default:
13266                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13267                                 "0144 Not a valid CQE code: x%x\n",
13268                                 bf_get(lpfc_wcqe_c_code, &wcqe));
13269                 break;
13270         }
13271         return workposted;
13272 }
13273
13274 /**
13275  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
13276  * @phba: Pointer to HBA context object.
13277  * @eqe: Pointer to fast-path event queue entry.
13278  *
13279  * This routine process a event queue entry from the fast-path event queue.
13280  * It will check the MajorCode and MinorCode to determine this is for a
13281  * completion event on a completion queue, if not, an error shall be logged
13282  * and just return. Otherwise, it will get to the corresponding completion
13283  * queue and process all the entries on the completion queue, rearm the
13284  * completion queue, and then return.
13285  **/
13286 static void
13287 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13288                         uint32_t qidx)
13289 {
13290         struct lpfc_queue *cq = NULL;
13291         struct lpfc_cqe *cqe;
13292         bool workposted = false;
13293         uint16_t cqid, id;
13294         int ecount = 0;
13295
13296         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13297                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13298                                 "0366 Not a valid completion "
13299                                 "event: majorcode=x%x, minorcode=x%x\n",
13300                                 bf_get_le32(lpfc_eqe_major_code, eqe),
13301                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
13302                 return;
13303         }
13304
13305         /* Get the reference to the corresponding CQ */
13306         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13307
13308         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13309                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13310                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13311                         /* Process NVMET unsol rcv */
13312                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13313                         goto  process_cq;
13314                 }
13315         }
13316
13317         if (phba->sli4_hba.nvme_cq_map &&
13318             (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
13319                 /* Process NVME / NVMET command completion */
13320                 cq = phba->sli4_hba.nvme_cq[qidx];
13321                 goto  process_cq;
13322         }
13323
13324         if (phba->sli4_hba.fcp_cq_map &&
13325             (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13326                 /* Process FCP command completion */
13327                 cq = phba->sli4_hba.fcp_cq[qidx];
13328                 goto  process_cq;
13329         }
13330
13331         if (phba->sli4_hba.nvmels_cq &&
13332             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13333                 /* Process NVME unsol rcv */
13334                 cq = phba->sli4_hba.nvmels_cq;
13335         }
13336
13337         /* Otherwise this is a Slow path event */
13338         if (cq == NULL) {
13339                 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13340                 return;
13341         }
13342
13343 process_cq:
13344         if (unlikely(cqid != cq->queue_id)) {
13345                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13346                                 "0368 Miss-matched fast-path completion "
13347                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13348                                 cqid, cq->queue_id);
13349                 return;
13350         }
13351
13352         /* Save EQ associated with this CQ */
13353         cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13354
13355         /* Process all the entries to the CQ */
13356         while ((cqe = lpfc_sli4_cq_get(cq))) {
13357                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13358                 if (!(++ecount % cq->entry_repost))
13359                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13360         }
13361
13362         /* Track the max number of CQEs processed in 1 EQ */
13363         if (ecount > cq->CQ_max_cqe)
13364                 cq->CQ_max_cqe = ecount;
13365
13366         /* Catch the no cq entry condition */
13367         if (unlikely(ecount == 0))
13368                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13369                                 "0369 No entry from fast-path completion "
13370                                 "queue fcpcqid=%d\n", cq->queue_id);
13371
13372         /* In any case, flash and re-arm the CQ */
13373         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13374
13375         /* wake up worker thread if there are works to be done */
13376         if (workposted)
13377                 lpfc_worker_wake_up(phba);
13378 }
13379
13380 static void
13381 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13382 {
13383         struct lpfc_eqe *eqe;
13384
13385         /* walk all the EQ entries and drop on the floor */
13386         while ((eqe = lpfc_sli4_eq_get(eq)))
13387                 ;
13388
13389         /* Clear and re-arm the EQ */
13390         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13391 }
13392
13393
13394 /**
13395  * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13396  *                           entry
13397  * @phba: Pointer to HBA context object.
13398  * @eqe: Pointer to fast-path event queue entry.
13399  *
13400  * This routine process a event queue entry from the Flash Optimized Fabric
13401  * event queue.  It will check the MajorCode and MinorCode to determine this
13402  * is for a completion event on a completion queue, if not, an error shall be
13403  * logged and just return. Otherwise, it will get to the corresponding
13404  * completion queue and process all the entries on the completion queue, rearm
13405  * the completion queue, and then return.
13406  **/
13407 static void
13408 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13409 {
13410         struct lpfc_queue *cq;
13411         struct lpfc_cqe *cqe;
13412         bool workposted = false;
13413         uint16_t cqid;
13414         int ecount = 0;
13415
13416         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13417                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13418                                 "9147 Not a valid completion "
13419                                 "event: majorcode=x%x, minorcode=x%x\n",
13420                                 bf_get_le32(lpfc_eqe_major_code, eqe),
13421                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
13422                 return;
13423         }
13424
13425         /* Get the reference to the corresponding CQ */
13426         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13427
13428         /* Next check for OAS */
13429         cq = phba->sli4_hba.oas_cq;
13430         if (unlikely(!cq)) {
13431                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13432                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13433                                         "9148 OAS completion queue "
13434                                         "does not exist\n");
13435                 return;
13436         }
13437
13438         if (unlikely(cqid != cq->queue_id)) {
13439                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13440                                 "9149 Miss-matched fast-path compl "
13441                                 "queue id: eqcqid=%d, fcpcqid=%d\n",
13442                                 cqid, cq->queue_id);
13443                 return;
13444         }
13445
13446         /* Process all the entries to the OAS CQ */
13447         while ((cqe = lpfc_sli4_cq_get(cq))) {
13448                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13449                 if (!(++ecount % cq->entry_repost))
13450                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13451         }
13452
13453         /* Track the max number of CQEs processed in 1 EQ */
13454         if (ecount > cq->CQ_max_cqe)
13455                 cq->CQ_max_cqe = ecount;
13456
13457         /* Catch the no cq entry condition */
13458         if (unlikely(ecount == 0))
13459                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13460                                 "9153 No entry from fast-path completion "
13461                                 "queue fcpcqid=%d\n", cq->queue_id);
13462
13463         /* In any case, flash and re-arm the CQ */
13464         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13465
13466         /* wake up worker thread if there are works to be done */
13467         if (workposted)
13468                 lpfc_worker_wake_up(phba);
13469 }
13470
13471 /**
13472  * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13473  * @irq: Interrupt number.
13474  * @dev_id: The device context pointer.
13475  *
13476  * This function is directly called from the PCI layer as an interrupt
13477  * service routine when device with SLI-4 interface spec is enabled with
13478  * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13479  * IOCB ring event in the HBA. However, when the device is enabled with either
13480  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13481  * device-level interrupt handler. When the PCI slot is in error recovery
13482  * or the HBA is undergoing initialization, the interrupt handler will not
13483  * process the interrupt. The Flash Optimized Fabric ring event are handled in
13484  * the intrrupt context. This function is called without any lock held.
13485  * It gets the hbalock to access and update SLI data structures. Note that,
13486  * the EQ to CQ are one-to-one map such that the EQ index is
13487  * equal to that of CQ index.
13488  *
13489  * This function returns IRQ_HANDLED when interrupt is handled else it
13490  * returns IRQ_NONE.
13491  **/
13492 irqreturn_t
13493 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13494 {
13495         struct lpfc_hba *phba;
13496         struct lpfc_hba_eq_hdl *hba_eq_hdl;
13497         struct lpfc_queue *eq;
13498         struct lpfc_eqe *eqe;
13499         unsigned long iflag;
13500         int ecount = 0;
13501
13502         /* Get the driver's phba structure from the dev_id */
13503         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13504         phba = hba_eq_hdl->phba;
13505
13506         if (unlikely(!phba))
13507                 return IRQ_NONE;
13508
13509         /* Get to the EQ struct associated with this vector */
13510         eq = phba->sli4_hba.fof_eq;
13511         if (unlikely(!eq))
13512                 return IRQ_NONE;
13513
13514         /* Check device state for handling interrupt */
13515         if (unlikely(lpfc_intr_state_check(phba))) {
13516                 eq->EQ_badstate++;
13517                 /* Check again for link_state with lock held */
13518                 spin_lock_irqsave(&phba->hbalock, iflag);
13519                 if (phba->link_state < LPFC_LINK_DOWN)
13520                         /* Flush, clear interrupt, and rearm the EQ */
13521                         lpfc_sli4_eq_flush(phba, eq);
13522                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13523                 return IRQ_NONE;
13524         }
13525
13526         /*
13527          * Process all the event on FCP fast-path EQ
13528          */
13529         while ((eqe = lpfc_sli4_eq_get(eq))) {
13530                 lpfc_sli4_fof_handle_eqe(phba, eqe);
13531                 if (!(++ecount % eq->entry_repost))
13532                         lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13533                 eq->EQ_processed++;
13534         }
13535
13536         /* Track the max number of EQEs processed in 1 intr */
13537         if (ecount > eq->EQ_max_eqe)
13538                 eq->EQ_max_eqe = ecount;
13539
13540
13541         if (unlikely(ecount == 0)) {
13542                 eq->EQ_no_entry++;
13543
13544                 if (phba->intr_type == MSIX)
13545                         /* MSI-X treated interrupt served as no EQ share INT */
13546                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13547                                         "9145 MSI-X interrupt with no EQE\n");
13548                 else {
13549                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13550                                         "9146 ISR interrupt with no EQE\n");
13551                         /* Non MSI-X treated on interrupt as EQ share INT */
13552                         return IRQ_NONE;
13553                 }
13554         }
13555         /* Always clear and re-arm the fast-path EQ */
13556         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13557         return IRQ_HANDLED;
13558 }
13559
13560 /**
13561  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
13562  * @irq: Interrupt number.
13563  * @dev_id: The device context pointer.
13564  *
13565  * This function is directly called from the PCI layer as an interrupt
13566  * service routine when device with SLI-4 interface spec is enabled with
13567  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13568  * ring event in the HBA. However, when the device is enabled with either
13569  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13570  * device-level interrupt handler. When the PCI slot is in error recovery
13571  * or the HBA is undergoing initialization, the interrupt handler will not
13572  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13573  * the intrrupt context. This function is called without any lock held.
13574  * It gets the hbalock to access and update SLI data structures. Note that,
13575  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13576  * equal to that of FCP CQ index.
13577  *
13578  * The link attention and ELS ring attention events are handled
13579  * by the worker thread. The interrupt handler signals the worker thread
13580  * and returns for these events. This function is called without any lock
13581  * held. It gets the hbalock to access and update SLI data structures.
13582  *
13583  * This function returns IRQ_HANDLED when interrupt is handled else it
13584  * returns IRQ_NONE.
13585  **/
13586 irqreturn_t
13587 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
13588 {
13589         struct lpfc_hba *phba;
13590         struct lpfc_hba_eq_hdl *hba_eq_hdl;
13591         struct lpfc_queue *fpeq;
13592         struct lpfc_eqe *eqe;
13593         unsigned long iflag;
13594         int ecount = 0;
13595         int hba_eqidx;
13596
13597         /* Get the driver's phba structure from the dev_id */
13598         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13599         phba = hba_eq_hdl->phba;
13600         hba_eqidx = hba_eq_hdl->idx;
13601
13602         if (unlikely(!phba))
13603                 return IRQ_NONE;
13604         if (unlikely(!phba->sli4_hba.hba_eq))
13605                 return IRQ_NONE;
13606
13607         /* Get to the EQ struct associated with this vector */
13608         fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
13609         if (unlikely(!fpeq))
13610                 return IRQ_NONE;
13611
13612 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13613         if (phba->ktime_on)
13614                 fpeq->isr_timestamp = ktime_get_ns();
13615 #endif
13616
13617         if (lpfc_fcp_look_ahead) {
13618                 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
13619                         lpfc_sli4_eq_clr_intr(fpeq);
13620                 else {
13621                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13622                         return IRQ_NONE;
13623                 }
13624         }
13625
13626         /* Check device state for handling interrupt */
13627         if (unlikely(lpfc_intr_state_check(phba))) {
13628                 fpeq->EQ_badstate++;
13629                 /* Check again for link_state with lock held */
13630                 spin_lock_irqsave(&phba->hbalock, iflag);
13631                 if (phba->link_state < LPFC_LINK_DOWN)
13632                         /* Flush, clear interrupt, and rearm the EQ */
13633                         lpfc_sli4_eq_flush(phba, fpeq);
13634                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13635                 if (lpfc_fcp_look_ahead)
13636                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13637                 return IRQ_NONE;
13638         }
13639
13640         /*
13641          * Process all the event on FCP fast-path EQ
13642          */
13643         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
13644                 if (eqe == NULL)
13645                         break;
13646
13647                 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13648                 if (!(++ecount % fpeq->entry_repost))
13649                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
13650                 fpeq->EQ_processed++;
13651         }
13652
13653         /* Track the max number of EQEs processed in 1 intr */
13654         if (ecount > fpeq->EQ_max_eqe)
13655                 fpeq->EQ_max_eqe = ecount;
13656
13657         /* Always clear and re-arm the fast-path EQ */
13658         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13659
13660         if (unlikely(ecount == 0)) {
13661                 fpeq->EQ_no_entry++;
13662
13663                 if (lpfc_fcp_look_ahead) {
13664                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13665                         return IRQ_NONE;
13666                 }
13667
13668                 if (phba->intr_type == MSIX)
13669                         /* MSI-X treated interrupt served as no EQ share INT */
13670                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13671                                         "0358 MSI-X interrupt with no EQE\n");
13672                 else
13673                         /* Non MSI-X treated on interrupt as EQ share INT */
13674                         return IRQ_NONE;
13675         }
13676
13677         if (lpfc_fcp_look_ahead)
13678                 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13679
13680         return IRQ_HANDLED;
13681 } /* lpfc_sli4_fp_intr_handler */
13682
13683 /**
13684  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13685  * @irq: Interrupt number.
13686  * @dev_id: The device context pointer.
13687  *
13688  * This function is the device-level interrupt handler to device with SLI-4
13689  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13690  * interrupt mode is enabled and there is an event in the HBA which requires
13691  * driver attention. This function invokes the slow-path interrupt attention
13692  * handling function and fast-path interrupt attention handling function in
13693  * turn to process the relevant HBA attention events. This function is called
13694  * without any lock held. It gets the hbalock to access and update SLI data
13695  * structures.
13696  *
13697  * This function returns IRQ_HANDLED when interrupt is handled, else it
13698  * returns IRQ_NONE.
13699  **/
13700 irqreturn_t
13701 lpfc_sli4_intr_handler(int irq, void *dev_id)
13702 {
13703         struct lpfc_hba  *phba;
13704         irqreturn_t hba_irq_rc;
13705         bool hba_handled = false;
13706         int qidx;
13707
13708         /* Get the driver's phba structure from the dev_id */
13709         phba = (struct lpfc_hba *)dev_id;
13710
13711         if (unlikely(!phba))
13712                 return IRQ_NONE;
13713
13714         /*
13715          * Invoke fast-path host attention interrupt handling as appropriate.
13716          */
13717         for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
13718                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
13719                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
13720                 if (hba_irq_rc == IRQ_HANDLED)
13721                         hba_handled |= true;
13722         }
13723
13724         if (phba->cfg_fof) {
13725                 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
13726                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
13727                 if (hba_irq_rc == IRQ_HANDLED)
13728                         hba_handled |= true;
13729         }
13730
13731         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
13732 } /* lpfc_sli4_intr_handler */
13733
13734 /**
13735  * lpfc_sli4_queue_free - free a queue structure and associated memory
13736  * @queue: The queue structure to free.
13737  *
13738  * This function frees a queue structure and the DMAable memory used for
13739  * the host resident queue. This function must be called after destroying the
13740  * queue on the HBA.
13741  **/
13742 void
13743 lpfc_sli4_queue_free(struct lpfc_queue *queue)
13744 {
13745         struct lpfc_dmabuf *dmabuf;
13746
13747         if (!queue)
13748                 return;
13749
13750         while (!list_empty(&queue->page_list)) {
13751                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13752                                  list);
13753                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
13754                                   dmabuf->virt, dmabuf->phys);
13755                 kfree(dmabuf);
13756         }
13757         if (queue->rqbp) {
13758                 lpfc_free_rq_buffer(queue->phba, queue);
13759                 kfree(queue->rqbp);
13760         }
13761         kfree(queue->pring);
13762         kfree(queue);
13763         return;
13764 }
13765
13766 /**
13767  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13768  * @phba: The HBA that this queue is being created on.
13769  * @entry_size: The size of each queue entry for this queue.
13770  * @entry count: The number of entries that this queue will handle.
13771  *
13772  * This function allocates a queue structure and the DMAable memory used for
13773  * the host resident queue. This function must be called before creating the
13774  * queue on the HBA.
13775  **/
13776 struct lpfc_queue *
13777 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13778                       uint32_t entry_count)
13779 {
13780         struct lpfc_queue *queue;
13781         struct lpfc_dmabuf *dmabuf;
13782         int x, total_qe_count;
13783         void *dma_pointer;
13784         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13785
13786         if (!phba->sli4_hba.pc_sli4_params.supported)
13787                 hw_page_size = SLI4_PAGE_SIZE;
13788
13789         queue = kzalloc(sizeof(struct lpfc_queue) +
13790                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13791         if (!queue)
13792                 return NULL;
13793         queue->page_count = (ALIGN(entry_size * entry_count,
13794                         hw_page_size))/hw_page_size;
13795
13796         /* If needed, Adjust page count to match the max the adapter supports */
13797         if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13798                 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13799
13800         INIT_LIST_HEAD(&queue->list);
13801         INIT_LIST_HEAD(&queue->wq_list);
13802         INIT_LIST_HEAD(&queue->page_list);
13803         INIT_LIST_HEAD(&queue->child_list);
13804         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13805                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13806                 if (!dmabuf)
13807                         goto out_fail;
13808                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13809                                                    hw_page_size, &dmabuf->phys,
13810                                                    GFP_KERNEL);
13811                 if (!dmabuf->virt) {
13812                         kfree(dmabuf);
13813                         goto out_fail;
13814                 }
13815                 dmabuf->buffer_tag = x;
13816                 list_add_tail(&dmabuf->list, &queue->page_list);
13817                 /* initialize queue's entry array */
13818                 dma_pointer = dmabuf->virt;
13819                 for (; total_qe_count < entry_count &&
13820                      dma_pointer < (hw_page_size + dmabuf->virt);
13821                      total_qe_count++, dma_pointer += entry_size) {
13822                         queue->qe[total_qe_count].address = dma_pointer;
13823                 }
13824         }
13825         queue->entry_size = entry_size;
13826         queue->entry_count = entry_count;
13827
13828         /*
13829          * entry_repost is calculated based on the number of entries in the
13830          * queue. This works out except for RQs. If buffers are NOT initially
13831          * posted for every RQE, entry_repost should be adjusted accordingly.
13832          */
13833         queue->entry_repost = (entry_count >> 3);
13834         if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13835                 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
13836         queue->phba = phba;
13837
13838         return queue;
13839 out_fail:
13840         lpfc_sli4_queue_free(queue);
13841         return NULL;
13842 }
13843
13844 /**
13845  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13846  * @phba: HBA structure that indicates port to create a queue on.
13847  * @pci_barset: PCI BAR set flag.
13848  *
13849  * This function shall perform iomap of the specified PCI BAR address to host
13850  * memory address if not already done so and return it. The returned host
13851  * memory address can be NULL.
13852  */
13853 static void __iomem *
13854 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13855 {
13856         if (!phba->pcidev)
13857                 return NULL;
13858
13859         switch (pci_barset) {
13860         case WQ_PCI_BAR_0_AND_1:
13861                 return phba->pci_bar0_memmap_p;
13862         case WQ_PCI_BAR_2_AND_3:
13863                 return phba->pci_bar2_memmap_p;
13864         case WQ_PCI_BAR_4_AND_5:
13865                 return phba->pci_bar4_memmap_p;
13866         default:
13867                 break;
13868         }
13869         return NULL;
13870 }
13871
13872 /**
13873  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
13874  * @phba: HBA structure that indicates port to create a queue on.
13875  * @startq: The starting FCP EQ to modify
13876  *
13877  * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13878  * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
13879  * updated in one mailbox command.
13880  *
13881  * The @phba struct is used to send mailbox command to HBA. The @startq
13882  * is used to get the starting FCP EQ to change.
13883  * This function is asynchronous and will wait for the mailbox
13884  * command to finish before continuing.
13885  *
13886  * On success this function will return a zero. If unable to allocate enough
13887  * memory this function will return -ENOMEM. If the queue create mailbox command
13888  * fails this function will return -ENXIO.
13889  **/
13890 int
13891 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
13892 {
13893         struct lpfc_mbx_modify_eq_delay *eq_delay;
13894         LPFC_MBOXQ_t *mbox;
13895         struct lpfc_queue *eq;
13896         int cnt, rc, length, status = 0;
13897         uint32_t shdr_status, shdr_add_status;
13898         uint32_t result;
13899         int qidx;
13900         union lpfc_sli4_cfg_shdr *shdr;
13901         uint16_t dmult;
13902
13903         if (startq >= phba->io_channel_irqs)
13904                 return 0;
13905
13906         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13907         if (!mbox)
13908                 return -ENOMEM;
13909         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13910                   sizeof(struct lpfc_sli4_cfg_mhdr));
13911         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13912                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13913                          length, LPFC_SLI4_MBX_EMBED);
13914         eq_delay = &mbox->u.mqe.un.eq_delay;
13915
13916         /* Calculate delay multiper from maximum interrupt per second */
13917         result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13918         if (result > LPFC_DMULT_CONST || result == 0)
13919                 dmult = 0;
13920         else
13921                 dmult = LPFC_DMULT_CONST/result - 1;
13922
13923         cnt = 0;
13924         for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13925                 eq = phba->sli4_hba.hba_eq[qidx];
13926                 if (!eq)
13927                         continue;
13928                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13929                 eq_delay->u.request.eq[cnt].phase = 0;
13930                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13931                 cnt++;
13932                 if (cnt >= LPFC_MAX_EQ_DELAY_EQID_CNT)
13933                         break;
13934         }
13935         eq_delay->u.request.num_eq = cnt;
13936
13937         mbox->vport = phba->pport;
13938         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13939         mbox->context1 = NULL;
13940         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13941         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13942         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13943         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13944         if (shdr_status || shdr_add_status || rc) {
13945                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13946                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
13947                                 "status x%x add_status x%x, mbx status x%x\n",
13948                                 shdr_status, shdr_add_status, rc);
13949                 status = -ENXIO;
13950         }
13951         mempool_free(mbox, phba->mbox_mem_pool);
13952         return status;
13953 }
13954
13955 /**
13956  * lpfc_eq_create - Create an Event Queue on the HBA
13957  * @phba: HBA structure that indicates port to create a queue on.
13958  * @eq: The queue structure to use to create the event queue.
13959  * @imax: The maximum interrupt per second limit.
13960  *
13961  * This function creates an event queue, as detailed in @eq, on a port,
13962  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13963  *
13964  * The @phba struct is used to send mailbox command to HBA. The @eq struct
13965  * is used to get the entry count and entry size that are necessary to
13966  * determine the number of pages to allocate and use for this queue. This
13967  * function will send the EQ_CREATE mailbox command to the HBA to setup the
13968  * event queue. This function is asynchronous and will wait for the mailbox
13969  * command to finish before continuing.
13970  *
13971  * On success this function will return a zero. If unable to allocate enough
13972  * memory this function will return -ENOMEM. If the queue create mailbox command
13973  * fails this function will return -ENXIO.
13974  **/
13975 int
13976 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
13977 {
13978         struct lpfc_mbx_eq_create *eq_create;
13979         LPFC_MBOXQ_t *mbox;
13980         int rc, length, status = 0;
13981         struct lpfc_dmabuf *dmabuf;
13982         uint32_t shdr_status, shdr_add_status;
13983         union lpfc_sli4_cfg_shdr *shdr;
13984         uint16_t dmult;
13985         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13986
13987         /* sanity check on queue memory */
13988         if (!eq)
13989                 return -ENODEV;
13990         if (!phba->sli4_hba.pc_sli4_params.supported)
13991                 hw_page_size = SLI4_PAGE_SIZE;
13992
13993         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13994         if (!mbox)
13995                 return -ENOMEM;
13996         length = (sizeof(struct lpfc_mbx_eq_create) -
13997                   sizeof(struct lpfc_sli4_cfg_mhdr));
13998         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13999                          LPFC_MBOX_OPCODE_EQ_CREATE,
14000                          length, LPFC_SLI4_MBX_EMBED);
14001         eq_create = &mbox->u.mqe.un.eq_create;
14002         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14003                eq->page_count);
14004         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14005                LPFC_EQE_SIZE);
14006         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
14007         /* don't setup delay multiplier using EQ_CREATE */
14008         dmult = 0;
14009         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14010                dmult);
14011         switch (eq->entry_count) {
14012         default:
14013                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14014                                 "0360 Unsupported EQ count. (%d)\n",
14015                                 eq->entry_count);
14016                 if (eq->entry_count < 256)
14017                         return -EINVAL;
14018                 /* otherwise default to smallest count (drop through) */
14019         case 256:
14020                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14021                        LPFC_EQ_CNT_256);
14022                 break;
14023         case 512:
14024                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14025                        LPFC_EQ_CNT_512);
14026                 break;
14027         case 1024:
14028                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14029                        LPFC_EQ_CNT_1024);
14030                 break;
14031         case 2048:
14032                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14033                        LPFC_EQ_CNT_2048);
14034                 break;
14035         case 4096:
14036                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14037                        LPFC_EQ_CNT_4096);
14038                 break;
14039         }
14040         list_for_each_entry(dmabuf, &eq->page_list, list) {
14041                 memset(dmabuf->virt, 0, hw_page_size);
14042                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14043                                         putPaddrLow(dmabuf->phys);
14044                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14045                                         putPaddrHigh(dmabuf->phys);
14046         }
14047         mbox->vport = phba->pport;
14048         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14049         mbox->context1 = NULL;
14050         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14051         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14052         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14053         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14054         if (shdr_status || shdr_add_status || rc) {
14055                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14056                                 "2500 EQ_CREATE mailbox failed with "
14057                                 "status x%x add_status x%x, mbx status x%x\n",
14058                                 shdr_status, shdr_add_status, rc);
14059                 status = -ENXIO;
14060         }
14061         eq->type = LPFC_EQ;
14062         eq->subtype = LPFC_NONE;
14063         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14064         if (eq->queue_id == 0xFFFF)
14065                 status = -ENXIO;
14066         eq->host_index = 0;
14067         eq->hba_index = 0;
14068
14069         mempool_free(mbox, phba->mbox_mem_pool);
14070         return status;
14071 }
14072
14073 /**
14074  * lpfc_cq_create - Create a Completion Queue on the HBA
14075  * @phba: HBA structure that indicates port to create a queue on.
14076  * @cq: The queue structure to use to create the completion queue.
14077  * @eq: The event queue to bind this completion queue to.
14078  *
14079  * This function creates a completion queue, as detailed in @wq, on a port,
14080  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14081  *
14082  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14083  * is used to get the entry count and entry size that are necessary to
14084  * determine the number of pages to allocate and use for this queue. The @eq
14085  * is used to indicate which event queue to bind this completion queue to. This
14086  * function will send the CQ_CREATE mailbox command to the HBA to setup the
14087  * completion queue. This function is asynchronous and will wait for the mailbox
14088  * command to finish before continuing.
14089  *
14090  * On success this function will return a zero. If unable to allocate enough
14091  * memory this function will return -ENOMEM. If the queue create mailbox command
14092  * fails this function will return -ENXIO.
14093  **/
14094 int
14095 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14096                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14097 {
14098         struct lpfc_mbx_cq_create *cq_create;
14099         struct lpfc_dmabuf *dmabuf;
14100         LPFC_MBOXQ_t *mbox;
14101         int rc, length, status = 0;
14102         uint32_t shdr_status, shdr_add_status;
14103         union lpfc_sli4_cfg_shdr *shdr;
14104         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14105
14106         /* sanity check on queue memory */
14107         if (!cq || !eq)
14108                 return -ENODEV;
14109         if (!phba->sli4_hba.pc_sli4_params.supported)
14110                 hw_page_size = SLI4_PAGE_SIZE;
14111
14112         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14113         if (!mbox)
14114                 return -ENOMEM;
14115         length = (sizeof(struct lpfc_mbx_cq_create) -
14116                   sizeof(struct lpfc_sli4_cfg_mhdr));
14117         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14118                          LPFC_MBOX_OPCODE_CQ_CREATE,
14119                          length, LPFC_SLI4_MBX_EMBED);
14120         cq_create = &mbox->u.mqe.un.cq_create;
14121         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14122         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14123                     cq->page_count);
14124         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14125         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14126         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14127                phba->sli4_hba.pc_sli4_params.cqv);
14128         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14129                 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14130                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
14131                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14132                        eq->queue_id);
14133         } else {
14134                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14135                        eq->queue_id);
14136         }
14137         switch (cq->entry_count) {
14138         default:
14139                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14140                                 "0361 Unsupported CQ count: "
14141                                 "entry cnt %d sz %d pg cnt %d repost %d\n",
14142                                 cq->entry_count, cq->entry_size,
14143                                 cq->page_count, cq->entry_repost);
14144                 if (cq->entry_count < 256) {
14145                         status = -EINVAL;
14146                         goto out;
14147                 }
14148                 /* otherwise default to smallest count (drop through) */
14149         case 256:
14150                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14151                        LPFC_CQ_CNT_256);
14152                 break;
14153         case 512:
14154                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14155                        LPFC_CQ_CNT_512);
14156                 break;
14157         case 1024:
14158                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14159                        LPFC_CQ_CNT_1024);
14160                 break;
14161         }
14162         list_for_each_entry(dmabuf, &cq->page_list, list) {
14163                 memset(dmabuf->virt, 0, hw_page_size);
14164                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14165                                         putPaddrLow(dmabuf->phys);
14166                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14167                                         putPaddrHigh(dmabuf->phys);
14168         }
14169         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14170
14171         /* The IOCTL status is embedded in the mailbox subheader. */
14172         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14173         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14174         if (shdr_status || shdr_add_status || rc) {
14175                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14176                                 "2501 CQ_CREATE mailbox failed with "
14177                                 "status x%x add_status x%x, mbx status x%x\n",
14178                                 shdr_status, shdr_add_status, rc);
14179                 status = -ENXIO;
14180                 goto out;
14181         }
14182         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14183         if (cq->queue_id == 0xFFFF) {
14184                 status = -ENXIO;
14185                 goto out;
14186         }
14187         /* link the cq onto the parent eq child list */
14188         list_add_tail(&cq->list, &eq->child_list);
14189         /* Set up completion queue's type and subtype */
14190         cq->type = type;
14191         cq->subtype = subtype;
14192         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14193         cq->assoc_qid = eq->queue_id;
14194         cq->host_index = 0;
14195         cq->hba_index = 0;
14196
14197 out:
14198         mempool_free(mbox, phba->mbox_mem_pool);
14199         return status;
14200 }
14201
14202 /**
14203  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14204  * @phba: HBA structure that indicates port to create a queue on.
14205  * @cqp: The queue structure array to use to create the completion queues.
14206  * @eqp: The event queue array to bind these completion queues to.
14207  *
14208  * This function creates a set of  completion queue, s to support MRQ
14209  * as detailed in @cqp, on a port,
14210  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14211  *
14212  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14213  * is used to get the entry count and entry size that are necessary to
14214  * determine the number of pages to allocate and use for this queue. The @eq
14215  * is used to indicate which event queue to bind this completion queue to. This
14216  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14217  * completion queue. This function is asynchronous and will wait for the mailbox
14218  * command to finish before continuing.
14219  *
14220  * On success this function will return a zero. If unable to allocate enough
14221  * memory this function will return -ENOMEM. If the queue create mailbox command
14222  * fails this function will return -ENXIO.
14223  **/
14224 int
14225 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14226                    struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14227 {
14228         struct lpfc_queue *cq;
14229         struct lpfc_queue *eq;
14230         struct lpfc_mbx_cq_create_set *cq_set;
14231         struct lpfc_dmabuf *dmabuf;
14232         LPFC_MBOXQ_t *mbox;
14233         int rc, length, alloclen, status = 0;
14234         int cnt, idx, numcq, page_idx = 0;
14235         uint32_t shdr_status, shdr_add_status;
14236         union lpfc_sli4_cfg_shdr *shdr;
14237         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14238
14239         /* sanity check on queue memory */
14240         numcq = phba->cfg_nvmet_mrq;
14241         if (!cqp || !eqp || !numcq)
14242                 return -ENODEV;
14243         if (!phba->sli4_hba.pc_sli4_params.supported)
14244                 hw_page_size = SLI4_PAGE_SIZE;
14245
14246         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14247         if (!mbox)
14248                 return -ENOMEM;
14249
14250         length = sizeof(struct lpfc_mbx_cq_create_set);
14251         length += ((numcq * cqp[0]->page_count) *
14252                    sizeof(struct dma_address));
14253         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14254                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14255                         LPFC_SLI4_MBX_NEMBED);
14256         if (alloclen < length) {
14257                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14258                                 "3098 Allocated DMA memory size (%d) is "
14259                                 "less than the requested DMA memory size "
14260                                 "(%d)\n", alloclen, length);
14261                 status = -ENOMEM;
14262                 goto out;
14263         }
14264         cq_set = mbox->sge_array->addr[0];
14265         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14266         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14267
14268         for (idx = 0; idx < numcq; idx++) {
14269                 cq = cqp[idx];
14270                 eq = eqp[idx];
14271                 if (!cq || !eq) {
14272                         status = -ENOMEM;
14273                         goto out;
14274                 }
14275
14276                 switch (idx) {
14277                 case 0:
14278                         bf_set(lpfc_mbx_cq_create_set_page_size,
14279                                &cq_set->u.request,
14280                                (hw_page_size / SLI4_PAGE_SIZE));
14281                         bf_set(lpfc_mbx_cq_create_set_num_pages,
14282                                &cq_set->u.request, cq->page_count);
14283                         bf_set(lpfc_mbx_cq_create_set_evt,
14284                                &cq_set->u.request, 1);
14285                         bf_set(lpfc_mbx_cq_create_set_valid,
14286                                &cq_set->u.request, 1);
14287                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
14288                                &cq_set->u.request, 0);
14289                         bf_set(lpfc_mbx_cq_create_set_num_cq,
14290                                &cq_set->u.request, numcq);
14291                         switch (cq->entry_count) {
14292                         default:
14293                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14294                                                 "3118 Bad CQ count. (%d)\n",
14295                                                 cq->entry_count);
14296                                 if (cq->entry_count < 256) {
14297                                         status = -EINVAL;
14298                                         goto out;
14299                                 }
14300                                 /* otherwise default to smallest (drop thru) */
14301                         case 256:
14302                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14303                                        &cq_set->u.request, LPFC_CQ_CNT_256);
14304                                 break;
14305                         case 512:
14306                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14307                                        &cq_set->u.request, LPFC_CQ_CNT_512);
14308                                 break;
14309                         case 1024:
14310                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14311                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
14312                                 break;
14313                         }
14314                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
14315                                &cq_set->u.request, eq->queue_id);
14316                         break;
14317                 case 1:
14318                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
14319                                &cq_set->u.request, eq->queue_id);
14320                         break;
14321                 case 2:
14322                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
14323                                &cq_set->u.request, eq->queue_id);
14324                         break;
14325                 case 3:
14326                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
14327                                &cq_set->u.request, eq->queue_id);
14328                         break;
14329                 case 4:
14330                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
14331                                &cq_set->u.request, eq->queue_id);
14332                         break;
14333                 case 5:
14334                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
14335                                &cq_set->u.request, eq->queue_id);
14336                         break;
14337                 case 6:
14338                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
14339                                &cq_set->u.request, eq->queue_id);
14340                         break;
14341                 case 7:
14342                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
14343                                &cq_set->u.request, eq->queue_id);
14344                         break;
14345                 case 8:
14346                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
14347                                &cq_set->u.request, eq->queue_id);
14348                         break;
14349                 case 9:
14350                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
14351                                &cq_set->u.request, eq->queue_id);
14352                         break;
14353                 case 10:
14354                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
14355                                &cq_set->u.request, eq->queue_id);
14356                         break;
14357                 case 11:
14358                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
14359                                &cq_set->u.request, eq->queue_id);
14360                         break;
14361                 case 12:
14362                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
14363                                &cq_set->u.request, eq->queue_id);
14364                         break;
14365                 case 13:
14366                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
14367                                &cq_set->u.request, eq->queue_id);
14368                         break;
14369                 case 14:
14370                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
14371                                &cq_set->u.request, eq->queue_id);
14372                         break;
14373                 case 15:
14374                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
14375                                &cq_set->u.request, eq->queue_id);
14376                         break;
14377                 }
14378
14379                 /* link the cq onto the parent eq child list */
14380                 list_add_tail(&cq->list, &eq->child_list);
14381                 /* Set up completion queue's type and subtype */
14382                 cq->type = type;
14383                 cq->subtype = subtype;
14384                 cq->assoc_qid = eq->queue_id;
14385                 cq->host_index = 0;
14386                 cq->hba_index = 0;
14387
14388                 rc = 0;
14389                 list_for_each_entry(dmabuf, &cq->page_list, list) {
14390                         memset(dmabuf->virt, 0, hw_page_size);
14391                         cnt = page_idx + dmabuf->buffer_tag;
14392                         cq_set->u.request.page[cnt].addr_lo =
14393                                         putPaddrLow(dmabuf->phys);
14394                         cq_set->u.request.page[cnt].addr_hi =
14395                                         putPaddrHigh(dmabuf->phys);
14396                         rc++;
14397                 }
14398                 page_idx += rc;
14399         }
14400
14401         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14402
14403         /* The IOCTL status is embedded in the mailbox subheader. */
14404         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14405         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14406         if (shdr_status || shdr_add_status || rc) {
14407                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14408                                 "3119 CQ_CREATE_SET mailbox failed with "
14409                                 "status x%x add_status x%x, mbx status x%x\n",
14410                                 shdr_status, shdr_add_status, rc);
14411                 status = -ENXIO;
14412                 goto out;
14413         }
14414         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14415         if (rc == 0xFFFF) {
14416                 status = -ENXIO;
14417                 goto out;
14418         }
14419
14420         for (idx = 0; idx < numcq; idx++) {
14421                 cq = cqp[idx];
14422                 cq->queue_id = rc + idx;
14423         }
14424
14425 out:
14426         lpfc_sli4_mbox_cmd_free(phba, mbox);
14427         return status;
14428 }
14429
14430 /**
14431  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14432  * @phba: HBA structure that indicates port to create a queue on.
14433  * @mq: The queue structure to use to create the mailbox queue.
14434  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14435  * @cq: The completion queue to associate with this cq.
14436  *
14437  * This function provides failback (fb) functionality when the
14438  * mq_create_ext fails on older FW generations.  It's purpose is identical
14439  * to mq_create_ext otherwise.
14440  *
14441  * This routine cannot fail as all attributes were previously accessed and
14442  * initialized in mq_create_ext.
14443  **/
14444 static void
14445 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14446                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14447 {
14448         struct lpfc_mbx_mq_create *mq_create;
14449         struct lpfc_dmabuf *dmabuf;
14450         int length;
14451
14452         length = (sizeof(struct lpfc_mbx_mq_create) -
14453                   sizeof(struct lpfc_sli4_cfg_mhdr));
14454         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14455                          LPFC_MBOX_OPCODE_MQ_CREATE,
14456                          length, LPFC_SLI4_MBX_EMBED);
14457         mq_create = &mbox->u.mqe.un.mq_create;
14458         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14459                mq->page_count);
14460         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14461                cq->queue_id);
14462         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14463         switch (mq->entry_count) {
14464         case 16:
14465                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14466                        LPFC_MQ_RING_SIZE_16);
14467                 break;
14468         case 32:
14469                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14470                        LPFC_MQ_RING_SIZE_32);
14471                 break;
14472         case 64:
14473                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14474                        LPFC_MQ_RING_SIZE_64);
14475                 break;
14476         case 128:
14477                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14478                        LPFC_MQ_RING_SIZE_128);
14479                 break;
14480         }
14481         list_for_each_entry(dmabuf, &mq->page_list, list) {
14482                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14483                         putPaddrLow(dmabuf->phys);
14484                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14485                         putPaddrHigh(dmabuf->phys);
14486         }
14487 }
14488
14489 /**
14490  * lpfc_mq_create - Create a mailbox Queue on the HBA
14491  * @phba: HBA structure that indicates port to create a queue on.
14492  * @mq: The queue structure to use to create the mailbox queue.
14493  * @cq: The completion queue to associate with this cq.
14494  * @subtype: The queue's subtype.
14495  *
14496  * This function creates a mailbox queue, as detailed in @mq, on a port,
14497  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14498  *
14499  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14500  * is used to get the entry count and entry size that are necessary to
14501  * determine the number of pages to allocate and use for this queue. This
14502  * function will send the MQ_CREATE mailbox command to the HBA to setup the
14503  * mailbox queue. This function is asynchronous and will wait for the mailbox
14504  * command to finish before continuing.
14505  *
14506  * On success this function will return a zero. If unable to allocate enough
14507  * memory this function will return -ENOMEM. If the queue create mailbox command
14508  * fails this function will return -ENXIO.
14509  **/
14510 int32_t
14511 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14512                struct lpfc_queue *cq, uint32_t subtype)
14513 {
14514         struct lpfc_mbx_mq_create *mq_create;
14515         struct lpfc_mbx_mq_create_ext *mq_create_ext;
14516         struct lpfc_dmabuf *dmabuf;
14517         LPFC_MBOXQ_t *mbox;
14518         int rc, length, status = 0;
14519         uint32_t shdr_status, shdr_add_status;
14520         union lpfc_sli4_cfg_shdr *shdr;
14521         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14522
14523         /* sanity check on queue memory */
14524         if (!mq || !cq)
14525                 return -ENODEV;
14526         if (!phba->sli4_hba.pc_sli4_params.supported)
14527                 hw_page_size = SLI4_PAGE_SIZE;
14528
14529         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14530         if (!mbox)
14531                 return -ENOMEM;
14532         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14533                   sizeof(struct lpfc_sli4_cfg_mhdr));
14534         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14535                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14536                          length, LPFC_SLI4_MBX_EMBED);
14537
14538         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
14539         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
14540         bf_set(lpfc_mbx_mq_create_ext_num_pages,
14541                &mq_create_ext->u.request, mq->page_count);
14542         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14543                &mq_create_ext->u.request, 1);
14544         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
14545                &mq_create_ext->u.request, 1);
14546         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14547                &mq_create_ext->u.request, 1);
14548         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14549                &mq_create_ext->u.request, 1);
14550         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14551                &mq_create_ext->u.request, 1);
14552         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
14553         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14554                phba->sli4_hba.pc_sli4_params.mqv);
14555         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14556                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14557                        cq->queue_id);
14558         else
14559                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14560                        cq->queue_id);
14561         switch (mq->entry_count) {
14562         default:
14563                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14564                                 "0362 Unsupported MQ count. (%d)\n",
14565                                 mq->entry_count);
14566                 if (mq->entry_count < 16) {
14567                         status = -EINVAL;
14568                         goto out;
14569                 }
14570                 /* otherwise default to smallest count (drop through) */
14571         case 16:
14572                 bf_set(lpfc_mq_context_ring_size,
14573                        &mq_create_ext->u.request.context,
14574                        LPFC_MQ_RING_SIZE_16);
14575                 break;
14576         case 32:
14577                 bf_set(lpfc_mq_context_ring_size,
14578                        &mq_create_ext->u.request.context,
14579                        LPFC_MQ_RING_SIZE_32);
14580                 break;
14581         case 64:
14582                 bf_set(lpfc_mq_context_ring_size,
14583                        &mq_create_ext->u.request.context,
14584                        LPFC_MQ_RING_SIZE_64);
14585                 break;
14586         case 128:
14587                 bf_set(lpfc_mq_context_ring_size,
14588                        &mq_create_ext->u.request.context,
14589                        LPFC_MQ_RING_SIZE_128);
14590                 break;
14591         }
14592         list_for_each_entry(dmabuf, &mq->page_list, list) {
14593                 memset(dmabuf->virt, 0, hw_page_size);
14594                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14595                                         putPaddrLow(dmabuf->phys);
14596                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
14597                                         putPaddrHigh(dmabuf->phys);
14598         }
14599         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14600         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14601                               &mq_create_ext->u.response);
14602         if (rc != MBX_SUCCESS) {
14603                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14604                                 "2795 MQ_CREATE_EXT failed with "
14605                                 "status x%x. Failback to MQ_CREATE.\n",
14606                                 rc);
14607                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14608                 mq_create = &mbox->u.mqe.un.mq_create;
14609                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14610                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14611                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14612                                       &mq_create->u.response);
14613         }
14614
14615         /* The IOCTL status is embedded in the mailbox subheader. */
14616         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14617         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14618         if (shdr_status || shdr_add_status || rc) {
14619                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14620                                 "2502 MQ_CREATE mailbox failed with "
14621                                 "status x%x add_status x%x, mbx status x%x\n",
14622                                 shdr_status, shdr_add_status, rc);
14623                 status = -ENXIO;
14624                 goto out;
14625         }
14626         if (mq->queue_id == 0xFFFF) {
14627                 status = -ENXIO;
14628                 goto out;
14629         }
14630         mq->type = LPFC_MQ;
14631         mq->assoc_qid = cq->queue_id;
14632         mq->subtype = subtype;
14633         mq->host_index = 0;
14634         mq->hba_index = 0;
14635
14636         /* link the mq onto the parent cq child list */
14637         list_add_tail(&mq->list, &cq->child_list);
14638 out:
14639         mempool_free(mbox, phba->mbox_mem_pool);
14640         return status;
14641 }
14642
14643 /**
14644  * lpfc_wq_create - Create a Work Queue on the HBA
14645  * @phba: HBA structure that indicates port to create a queue on.
14646  * @wq: The queue structure to use to create the work queue.
14647  * @cq: The completion queue to bind this work queue to.
14648  * @subtype: The subtype of the work queue indicating its functionality.
14649  *
14650  * This function creates a work queue, as detailed in @wq, on a port, described
14651  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14652  *
14653  * The @phba struct is used to send mailbox command to HBA. The @wq struct
14654  * is used to get the entry count and entry size that are necessary to
14655  * determine the number of pages to allocate and use for this queue. The @cq
14656  * is used to indicate which completion queue to bind this work queue to. This
14657  * function will send the WQ_CREATE mailbox command to the HBA to setup the
14658  * work queue. This function is asynchronous and will wait for the mailbox
14659  * command to finish before continuing.
14660  *
14661  * On success this function will return a zero. If unable to allocate enough
14662  * memory this function will return -ENOMEM. If the queue create mailbox command
14663  * fails this function will return -ENXIO.
14664  **/
14665 int
14666 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14667                struct lpfc_queue *cq, uint32_t subtype)
14668 {
14669         struct lpfc_mbx_wq_create *wq_create;
14670         struct lpfc_dmabuf *dmabuf;
14671         LPFC_MBOXQ_t *mbox;
14672         int rc, length, status = 0;
14673         uint32_t shdr_status, shdr_add_status;
14674         union lpfc_sli4_cfg_shdr *shdr;
14675         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14676         struct dma_address *page;
14677         void __iomem *bar_memmap_p;
14678         uint32_t db_offset;
14679         uint16_t pci_barset;
14680
14681         /* sanity check on queue memory */
14682         if (!wq || !cq)
14683                 return -ENODEV;
14684         if (!phba->sli4_hba.pc_sli4_params.supported)
14685                 hw_page_size = SLI4_PAGE_SIZE;
14686
14687         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14688         if (!mbox)
14689                 return -ENOMEM;
14690         length = (sizeof(struct lpfc_mbx_wq_create) -
14691                   sizeof(struct lpfc_sli4_cfg_mhdr));
14692         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14693                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14694                          length, LPFC_SLI4_MBX_EMBED);
14695         wq_create = &mbox->u.mqe.un.wq_create;
14696         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
14697         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14698                     wq->page_count);
14699         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14700                     cq->queue_id);
14701
14702         /* wqv is the earliest version supported, NOT the latest */
14703         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14704                phba->sli4_hba.pc_sli4_params.wqv);
14705
14706         switch (phba->sli4_hba.pc_sli4_params.wqv) {
14707         case LPFC_Q_CREATE_VERSION_0:
14708                 switch (wq->entry_size) {
14709                 default:
14710                 case 64:
14711                         /* Nothing to do, version 0 ONLY supports 64 byte */
14712                         page = wq_create->u.request.page;
14713                         break;
14714                 case 128:
14715                         if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14716                             LPFC_WQ_SZ128_SUPPORT)) {
14717                                 status = -ERANGE;
14718                                 goto out;
14719                         }
14720                         /* If we get here the HBA MUST also support V1 and
14721                          * we MUST use it
14722                          */
14723                         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14724                                LPFC_Q_CREATE_VERSION_1);
14725
14726                         bf_set(lpfc_mbx_wq_create_wqe_count,
14727                                &wq_create->u.request_1, wq->entry_count);
14728                         bf_set(lpfc_mbx_wq_create_wqe_size,
14729                                &wq_create->u.request_1,
14730                                LPFC_WQ_WQE_SIZE_128);
14731                         bf_set(lpfc_mbx_wq_create_page_size,
14732                                &wq_create->u.request_1,
14733                                LPFC_WQ_PAGE_SIZE_4096);
14734                         page = wq_create->u.request_1.page;
14735                         break;
14736                 }
14737                 break;
14738         case LPFC_Q_CREATE_VERSION_1:
14739                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14740                        wq->entry_count);
14741                 switch (wq->entry_size) {
14742                 default:
14743                 case 64:
14744                         bf_set(lpfc_mbx_wq_create_wqe_size,
14745                                &wq_create->u.request_1,
14746                                LPFC_WQ_WQE_SIZE_64);
14747                         break;
14748                 case 128:
14749                         if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14750                                 LPFC_WQ_SZ128_SUPPORT)) {
14751                                 status = -ERANGE;
14752                                 goto out;
14753                         }
14754                         bf_set(lpfc_mbx_wq_create_wqe_size,
14755                                &wq_create->u.request_1,
14756                                LPFC_WQ_WQE_SIZE_128);
14757                         break;
14758                 }
14759                 bf_set(lpfc_mbx_wq_create_page_size,
14760                        &wq_create->u.request_1,
14761                        LPFC_WQ_PAGE_SIZE_4096);
14762                 page = wq_create->u.request_1.page;
14763                 break;
14764         default:
14765                 status = -ERANGE;
14766                 goto out;
14767         }
14768
14769         list_for_each_entry(dmabuf, &wq->page_list, list) {
14770                 memset(dmabuf->virt, 0, hw_page_size);
14771                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14772                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
14773         }
14774
14775         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14776                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14777
14778         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14779         /* The IOCTL status is embedded in the mailbox subheader. */
14780         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14781         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14782         if (shdr_status || shdr_add_status || rc) {
14783                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14784                                 "2503 WQ_CREATE mailbox failed with "
14785                                 "status x%x add_status x%x, mbx status x%x\n",
14786                                 shdr_status, shdr_add_status, rc);
14787                 status = -ENXIO;
14788                 goto out;
14789         }
14790         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14791         if (wq->queue_id == 0xFFFF) {
14792                 status = -ENXIO;
14793                 goto out;
14794         }
14795         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14796                 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14797                                        &wq_create->u.response);
14798                 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14799                     (wq->db_format != LPFC_DB_RING_FORMAT)) {
14800                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14801                                         "3265 WQ[%d] doorbell format not "
14802                                         "supported: x%x\n", wq->queue_id,
14803                                         wq->db_format);
14804                         status = -EINVAL;
14805                         goto out;
14806                 }
14807                 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14808                                     &wq_create->u.response);
14809                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14810                 if (!bar_memmap_p) {
14811                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14812                                         "3263 WQ[%d] failed to memmap pci "
14813                                         "barset:x%x\n", wq->queue_id,
14814                                         pci_barset);
14815                         status = -ENOMEM;
14816                         goto out;
14817                 }
14818                 db_offset = wq_create->u.response.doorbell_offset;
14819                 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14820                     (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14821                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14822                                         "3252 WQ[%d] doorbell offset not "
14823                                         "supported: x%x\n", wq->queue_id,
14824                                         db_offset);
14825                         status = -EINVAL;
14826                         goto out;
14827                 }
14828                 wq->db_regaddr = bar_memmap_p + db_offset;
14829                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14830                                 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14831                                 "format:x%x\n", wq->queue_id, pci_barset,
14832                                 db_offset, wq->db_format);
14833         } else {
14834                 wq->db_format = LPFC_DB_LIST_FORMAT;
14835                 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14836         }
14837         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14838         if (wq->pring == NULL) {
14839                 status = -ENOMEM;
14840                 goto out;
14841         }
14842         wq->type = LPFC_WQ;
14843         wq->assoc_qid = cq->queue_id;
14844         wq->subtype = subtype;
14845         wq->host_index = 0;
14846         wq->hba_index = 0;
14847         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
14848
14849         /* link the wq onto the parent cq child list */
14850         list_add_tail(&wq->list, &cq->child_list);
14851 out:
14852         mempool_free(mbox, phba->mbox_mem_pool);
14853         return status;
14854 }
14855
14856 /**
14857  * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14858  * @phba: HBA structure that indicates port to create a queue on.
14859  * @rq:   The queue structure to use for the receive queue.
14860  * @qno:  The associated HBQ number
14861  *
14862  *
14863  * For SLI4 we need to adjust the RQ repost value based on
14864  * the number of buffers that are initially posted to the RQ.
14865  */
14866 void
14867 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14868 {
14869         uint32_t cnt;
14870
14871         /* sanity check on queue memory */
14872         if (!rq)
14873                 return;
14874         cnt = lpfc_hbq_defs[qno]->entry_count;
14875
14876         /* Recalc repost for RQs based on buffers initially posted */
14877         cnt = (cnt >> 3);
14878         if (cnt < LPFC_QUEUE_MIN_REPOST)
14879                 cnt = LPFC_QUEUE_MIN_REPOST;
14880
14881         rq->entry_repost = cnt;
14882 }
14883
14884 /**
14885  * lpfc_rq_create - Create a Receive Queue on the HBA
14886  * @phba: HBA structure that indicates port to create a queue on.
14887  * @hrq: The queue structure to use to create the header receive queue.
14888  * @drq: The queue structure to use to create the data receive queue.
14889  * @cq: The completion queue to bind this work queue to.
14890  *
14891  * This function creates a receive buffer queue pair , as detailed in @hrq and
14892  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14893  * to the HBA.
14894  *
14895  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14896  * struct is used to get the entry count that is necessary to determine the
14897  * number of pages to use for this queue. The @cq is used to indicate which
14898  * completion queue to bind received buffers that are posted to these queues to.
14899  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14900  * receive queue pair. This function is asynchronous and will wait for the
14901  * mailbox command to finish before continuing.
14902  *
14903  * On success this function will return a zero. If unable to allocate enough
14904  * memory this function will return -ENOMEM. If the queue create mailbox command
14905  * fails this function will return -ENXIO.
14906  **/
14907 int
14908 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14909                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14910 {
14911         struct lpfc_mbx_rq_create *rq_create;
14912         struct lpfc_dmabuf *dmabuf;
14913         LPFC_MBOXQ_t *mbox;
14914         int rc, length, status = 0;
14915         uint32_t shdr_status, shdr_add_status;
14916         union lpfc_sli4_cfg_shdr *shdr;
14917         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14918         void __iomem *bar_memmap_p;
14919         uint32_t db_offset;
14920         uint16_t pci_barset;
14921
14922         /* sanity check on queue memory */
14923         if (!hrq || !drq || !cq)
14924                 return -ENODEV;
14925         if (!phba->sli4_hba.pc_sli4_params.supported)
14926                 hw_page_size = SLI4_PAGE_SIZE;
14927
14928         if (hrq->entry_count != drq->entry_count)
14929                 return -EINVAL;
14930         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14931         if (!mbox)
14932                 return -ENOMEM;
14933         length = (sizeof(struct lpfc_mbx_rq_create) -
14934                   sizeof(struct lpfc_sli4_cfg_mhdr));
14935         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14936                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14937                          length, LPFC_SLI4_MBX_EMBED);
14938         rq_create = &mbox->u.mqe.un.rq_create;
14939         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14940         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14941                phba->sli4_hba.pc_sli4_params.rqv);
14942         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14943                 bf_set(lpfc_rq_context_rqe_count_1,
14944                        &rq_create->u.request.context,
14945                        hrq->entry_count);
14946                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
14947                 bf_set(lpfc_rq_context_rqe_size,
14948                        &rq_create->u.request.context,
14949                        LPFC_RQE_SIZE_8);
14950                 bf_set(lpfc_rq_context_page_size,
14951                        &rq_create->u.request.context,
14952                        LPFC_RQ_PAGE_SIZE_4096);
14953         } else {
14954                 switch (hrq->entry_count) {
14955                 default:
14956                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14957                                         "2535 Unsupported RQ count. (%d)\n",
14958                                         hrq->entry_count);
14959                         if (hrq->entry_count < 512) {
14960                                 status = -EINVAL;
14961                                 goto out;
14962                         }
14963                         /* otherwise default to smallest count (drop through) */
14964                 case 512:
14965                         bf_set(lpfc_rq_context_rqe_count,
14966                                &rq_create->u.request.context,
14967                                LPFC_RQ_RING_SIZE_512);
14968                         break;
14969                 case 1024:
14970                         bf_set(lpfc_rq_context_rqe_count,
14971                                &rq_create->u.request.context,
14972                                LPFC_RQ_RING_SIZE_1024);
14973                         break;
14974                 case 2048:
14975                         bf_set(lpfc_rq_context_rqe_count,
14976                                &rq_create->u.request.context,
14977                                LPFC_RQ_RING_SIZE_2048);
14978                         break;
14979                 case 4096:
14980                         bf_set(lpfc_rq_context_rqe_count,
14981                                &rq_create->u.request.context,
14982                                LPFC_RQ_RING_SIZE_4096);
14983                         break;
14984                 }
14985                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14986                        LPFC_HDR_BUF_SIZE);
14987         }
14988         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14989                cq->queue_id);
14990         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14991                hrq->page_count);
14992         list_for_each_entry(dmabuf, &hrq->page_list, list) {
14993                 memset(dmabuf->virt, 0, hw_page_size);
14994                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14995                                         putPaddrLow(dmabuf->phys);
14996                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14997                                         putPaddrHigh(dmabuf->phys);
14998         }
14999         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15000                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15001
15002         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15003         /* The IOCTL status is embedded in the mailbox subheader. */
15004         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15005         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15006         if (shdr_status || shdr_add_status || rc) {
15007                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15008                                 "2504 RQ_CREATE mailbox failed with "
15009                                 "status x%x add_status x%x, mbx status x%x\n",
15010                                 shdr_status, shdr_add_status, rc);
15011                 status = -ENXIO;
15012                 goto out;
15013         }
15014         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15015         if (hrq->queue_id == 0xFFFF) {
15016                 status = -ENXIO;
15017                 goto out;
15018         }
15019
15020         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15021                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15022                                         &rq_create->u.response);
15023                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15024                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15025                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15026                                         "3262 RQ [%d] doorbell format not "
15027                                         "supported: x%x\n", hrq->queue_id,
15028                                         hrq->db_format);
15029                         status = -EINVAL;
15030                         goto out;
15031                 }
15032
15033                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15034                                     &rq_create->u.response);
15035                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15036                 if (!bar_memmap_p) {
15037                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15038                                         "3269 RQ[%d] failed to memmap pci "
15039                                         "barset:x%x\n", hrq->queue_id,
15040                                         pci_barset);
15041                         status = -ENOMEM;
15042                         goto out;
15043                 }
15044
15045                 db_offset = rq_create->u.response.doorbell_offset;
15046                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15047                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15048                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15049                                         "3270 RQ[%d] doorbell offset not "
15050                                         "supported: x%x\n", hrq->queue_id,
15051                                         db_offset);
15052                         status = -EINVAL;
15053                         goto out;
15054                 }
15055                 hrq->db_regaddr = bar_memmap_p + db_offset;
15056                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15057                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15058                                 "format:x%x\n", hrq->queue_id, pci_barset,
15059                                 db_offset, hrq->db_format);
15060         } else {
15061                 hrq->db_format = LPFC_DB_RING_FORMAT;
15062                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15063         }
15064         hrq->type = LPFC_HRQ;
15065         hrq->assoc_qid = cq->queue_id;
15066         hrq->subtype = subtype;
15067         hrq->host_index = 0;
15068         hrq->hba_index = 0;
15069
15070         /* now create the data queue */
15071         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15072                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15073                          length, LPFC_SLI4_MBX_EMBED);
15074         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15075                phba->sli4_hba.pc_sli4_params.rqv);
15076         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15077                 bf_set(lpfc_rq_context_rqe_count_1,
15078                        &rq_create->u.request.context, hrq->entry_count);
15079                 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
15080                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15081                        LPFC_RQE_SIZE_8);
15082                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15083                        (PAGE_SIZE/SLI4_PAGE_SIZE));
15084         } else {
15085                 switch (drq->entry_count) {
15086                 default:
15087                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15088                                         "2536 Unsupported RQ count. (%d)\n",
15089                                         drq->entry_count);
15090                         if (drq->entry_count < 512) {
15091                                 status = -EINVAL;
15092                                 goto out;
15093                         }
15094                         /* otherwise default to smallest count (drop through) */
15095                 case 512:
15096                         bf_set(lpfc_rq_context_rqe_count,
15097                                &rq_create->u.request.context,
15098                                LPFC_RQ_RING_SIZE_512);
15099                         break;
15100                 case 1024:
15101                         bf_set(lpfc_rq_context_rqe_count,
15102                                &rq_create->u.request.context,
15103                                LPFC_RQ_RING_SIZE_1024);
15104                         break;
15105                 case 2048:
15106                         bf_set(lpfc_rq_context_rqe_count,
15107                                &rq_create->u.request.context,
15108                                LPFC_RQ_RING_SIZE_2048);
15109                         break;
15110                 case 4096:
15111                         bf_set(lpfc_rq_context_rqe_count,
15112                                &rq_create->u.request.context,
15113                                LPFC_RQ_RING_SIZE_4096);
15114                         break;
15115                 }
15116                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15117                        LPFC_DATA_BUF_SIZE);
15118         }
15119         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15120                cq->queue_id);
15121         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15122                drq->page_count);
15123         list_for_each_entry(dmabuf, &drq->page_list, list) {
15124                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15125                                         putPaddrLow(dmabuf->phys);
15126                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15127                                         putPaddrHigh(dmabuf->phys);
15128         }
15129         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15130                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15131         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15132         /* The IOCTL status is embedded in the mailbox subheader. */
15133         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15134         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15135         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15136         if (shdr_status || shdr_add_status || rc) {
15137                 status = -ENXIO;
15138                 goto out;
15139         }
15140         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15141         if (drq->queue_id == 0xFFFF) {
15142                 status = -ENXIO;
15143                 goto out;
15144         }
15145         drq->type = LPFC_DRQ;
15146         drq->assoc_qid = cq->queue_id;
15147         drq->subtype = subtype;
15148         drq->host_index = 0;
15149         drq->hba_index = 0;
15150
15151         /* link the header and data RQs onto the parent cq child list */
15152         list_add_tail(&hrq->list, &cq->child_list);
15153         list_add_tail(&drq->list, &cq->child_list);
15154
15155 out:
15156         mempool_free(mbox, phba->mbox_mem_pool);
15157         return status;
15158 }
15159
15160 /**
15161  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15162  * @phba: HBA structure that indicates port to create a queue on.
15163  * @hrqp: The queue structure array to use to create the header receive queues.
15164  * @drqp: The queue structure array to use to create the data receive queues.
15165  * @cqp: The completion queue array to bind these receive queues to.
15166  *
15167  * This function creates a receive buffer queue pair , as detailed in @hrq and
15168  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15169  * to the HBA.
15170  *
15171  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15172  * struct is used to get the entry count that is necessary to determine the
15173  * number of pages to use for this queue. The @cq is used to indicate which
15174  * completion queue to bind received buffers that are posted to these queues to.
15175  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15176  * receive queue pair. This function is asynchronous and will wait for the
15177  * mailbox command to finish before continuing.
15178  *
15179  * On success this function will return a zero. If unable to allocate enough
15180  * memory this function will return -ENOMEM. If the queue create mailbox command
15181  * fails this function will return -ENXIO.
15182  **/
15183 int
15184 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15185                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15186                 uint32_t subtype)
15187 {
15188         struct lpfc_queue *hrq, *drq, *cq;
15189         struct lpfc_mbx_rq_create_v2 *rq_create;
15190         struct lpfc_dmabuf *dmabuf;
15191         LPFC_MBOXQ_t *mbox;
15192         int rc, length, alloclen, status = 0;
15193         int cnt, idx, numrq, page_idx = 0;
15194         uint32_t shdr_status, shdr_add_status;
15195         union lpfc_sli4_cfg_shdr *shdr;
15196         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15197
15198         numrq = phba->cfg_nvmet_mrq;
15199         /* sanity check on array memory */
15200         if (!hrqp || !drqp || !cqp || !numrq)
15201                 return -ENODEV;
15202         if (!phba->sli4_hba.pc_sli4_params.supported)
15203                 hw_page_size = SLI4_PAGE_SIZE;
15204
15205         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15206         if (!mbox)
15207                 return -ENOMEM;
15208
15209         length = sizeof(struct lpfc_mbx_rq_create_v2);
15210         length += ((2 * numrq * hrqp[0]->page_count) *
15211                    sizeof(struct dma_address));
15212
15213         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15214                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15215                                     LPFC_SLI4_MBX_NEMBED);
15216         if (alloclen < length) {
15217                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15218                                 "3099 Allocated DMA memory size (%d) is "
15219                                 "less than the requested DMA memory size "
15220                                 "(%d)\n", alloclen, length);
15221                 status = -ENOMEM;
15222                 goto out;
15223         }
15224
15225
15226
15227         rq_create = mbox->sge_array->addr[0];
15228         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15229
15230         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15231         cnt = 0;
15232
15233         for (idx = 0; idx < numrq; idx++) {
15234                 hrq = hrqp[idx];
15235                 drq = drqp[idx];
15236                 cq  = cqp[idx];
15237
15238                 /* sanity check on queue memory */
15239                 if (!hrq || !drq || !cq) {
15240                         status = -ENODEV;
15241                         goto out;
15242                 }
15243
15244                 if (hrq->entry_count != drq->entry_count) {
15245                         status = -EINVAL;
15246                         goto out;
15247                 }
15248
15249                 if (idx == 0) {
15250                         bf_set(lpfc_mbx_rq_create_num_pages,
15251                                &rq_create->u.request,
15252                                hrq->page_count);
15253                         bf_set(lpfc_mbx_rq_create_rq_cnt,
15254                                &rq_create->u.request, (numrq * 2));
15255                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15256                                1);
15257                         bf_set(lpfc_rq_context_base_cq,
15258                                &rq_create->u.request.context,
15259                                cq->queue_id);
15260                         bf_set(lpfc_rq_context_data_size,
15261                                &rq_create->u.request.context,
15262                                LPFC_DATA_BUF_SIZE);
15263                         bf_set(lpfc_rq_context_hdr_size,
15264                                &rq_create->u.request.context,
15265                                LPFC_HDR_BUF_SIZE);
15266                         bf_set(lpfc_rq_context_rqe_count_1,
15267                                &rq_create->u.request.context,
15268                                hrq->entry_count);
15269                         bf_set(lpfc_rq_context_rqe_size,
15270                                &rq_create->u.request.context,
15271                                LPFC_RQE_SIZE_8);
15272                         bf_set(lpfc_rq_context_page_size,
15273                                &rq_create->u.request.context,
15274                                (PAGE_SIZE/SLI4_PAGE_SIZE));
15275                 }
15276                 rc = 0;
15277                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15278                         memset(dmabuf->virt, 0, hw_page_size);
15279                         cnt = page_idx + dmabuf->buffer_tag;
15280                         rq_create->u.request.page[cnt].addr_lo =
15281                                         putPaddrLow(dmabuf->phys);
15282                         rq_create->u.request.page[cnt].addr_hi =
15283                                         putPaddrHigh(dmabuf->phys);
15284                         rc++;
15285                 }
15286                 page_idx += rc;
15287
15288                 rc = 0;
15289                 list_for_each_entry(dmabuf, &drq->page_list, list) {
15290                         memset(dmabuf->virt, 0, hw_page_size);
15291                         cnt = page_idx + dmabuf->buffer_tag;
15292                         rq_create->u.request.page[cnt].addr_lo =
15293                                         putPaddrLow(dmabuf->phys);
15294                         rq_create->u.request.page[cnt].addr_hi =
15295                                         putPaddrHigh(dmabuf->phys);
15296                         rc++;
15297                 }
15298                 page_idx += rc;
15299
15300                 hrq->db_format = LPFC_DB_RING_FORMAT;
15301                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15302                 hrq->type = LPFC_HRQ;
15303                 hrq->assoc_qid = cq->queue_id;
15304                 hrq->subtype = subtype;
15305                 hrq->host_index = 0;
15306                 hrq->hba_index = 0;
15307
15308                 drq->db_format = LPFC_DB_RING_FORMAT;
15309                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15310                 drq->type = LPFC_DRQ;
15311                 drq->assoc_qid = cq->queue_id;
15312                 drq->subtype = subtype;
15313                 drq->host_index = 0;
15314                 drq->hba_index = 0;
15315
15316                 list_add_tail(&hrq->list, &cq->child_list);
15317                 list_add_tail(&drq->list, &cq->child_list);
15318         }
15319
15320         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15321         /* The IOCTL status is embedded in the mailbox subheader. */
15322         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15323         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15324         if (shdr_status || shdr_add_status || rc) {
15325                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15326                                 "3120 RQ_CREATE mailbox failed with "
15327                                 "status x%x add_status x%x, mbx status x%x\n",
15328                                 shdr_status, shdr_add_status, rc);
15329                 status = -ENXIO;
15330                 goto out;
15331         }
15332         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15333         if (rc == 0xFFFF) {
15334                 status = -ENXIO;
15335                 goto out;
15336         }
15337
15338         /* Initialize all RQs with associated queue id */
15339         for (idx = 0; idx < numrq; idx++) {
15340                 hrq = hrqp[idx];
15341                 hrq->queue_id = rc + (2 * idx);
15342                 drq = drqp[idx];
15343                 drq->queue_id = rc + (2 * idx) + 1;
15344         }
15345
15346 out:
15347         lpfc_sli4_mbox_cmd_free(phba, mbox);
15348         return status;
15349 }
15350
15351 /**
15352  * lpfc_eq_destroy - Destroy an event Queue on the HBA
15353  * @eq: The queue structure associated with the queue to destroy.
15354  *
15355  * This function destroys a queue, as detailed in @eq by sending an mailbox
15356  * command, specific to the type of queue, to the HBA.
15357  *
15358  * The @eq struct is used to get the queue ID of the queue to destroy.
15359  *
15360  * On success this function will return a zero. If the queue destroy mailbox
15361  * command fails this function will return -ENXIO.
15362  **/
15363 int
15364 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15365 {
15366         LPFC_MBOXQ_t *mbox;
15367         int rc, length, status = 0;
15368         uint32_t shdr_status, shdr_add_status;
15369         union lpfc_sli4_cfg_shdr *shdr;
15370
15371         /* sanity check on queue memory */
15372         if (!eq)
15373                 return -ENODEV;
15374         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15375         if (!mbox)
15376                 return -ENOMEM;
15377         length = (sizeof(struct lpfc_mbx_eq_destroy) -
15378                   sizeof(struct lpfc_sli4_cfg_mhdr));
15379         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15380                          LPFC_MBOX_OPCODE_EQ_DESTROY,
15381                          length, LPFC_SLI4_MBX_EMBED);
15382         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15383                eq->queue_id);
15384         mbox->vport = eq->phba->pport;
15385         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15386
15387         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15388         /* The IOCTL status is embedded in the mailbox subheader. */
15389         shdr = (union lpfc_sli4_cfg_shdr *)
15390                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15391         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15392         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15393         if (shdr_status || shdr_add_status || rc) {
15394                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15395                                 "2505 EQ_DESTROY mailbox failed with "
15396                                 "status x%x add_status x%x, mbx status x%x\n",
15397                                 shdr_status, shdr_add_status, rc);
15398                 status = -ENXIO;
15399         }
15400
15401         /* Remove eq from any list */
15402         list_del_init(&eq->list);
15403         mempool_free(mbox, eq->phba->mbox_mem_pool);
15404         return status;
15405 }
15406
15407 /**
15408  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15409  * @cq: The queue structure associated with the queue to destroy.
15410  *
15411  * This function destroys a queue, as detailed in @cq by sending an mailbox
15412  * command, specific to the type of queue, to the HBA.
15413  *
15414  * The @cq struct is used to get the queue ID of the queue to destroy.
15415  *
15416  * On success this function will return a zero. If the queue destroy mailbox
15417  * command fails this function will return -ENXIO.
15418  **/
15419 int
15420 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15421 {
15422         LPFC_MBOXQ_t *mbox;
15423         int rc, length, status = 0;
15424         uint32_t shdr_status, shdr_add_status;
15425         union lpfc_sli4_cfg_shdr *shdr;
15426
15427         /* sanity check on queue memory */
15428         if (!cq)
15429                 return -ENODEV;
15430         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15431         if (!mbox)
15432                 return -ENOMEM;
15433         length = (sizeof(struct lpfc_mbx_cq_destroy) -
15434                   sizeof(struct lpfc_sli4_cfg_mhdr));
15435         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15436                          LPFC_MBOX_OPCODE_CQ_DESTROY,
15437                          length, LPFC_SLI4_MBX_EMBED);
15438         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15439                cq->queue_id);
15440         mbox->vport = cq->phba->pport;
15441         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15442         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15443         /* The IOCTL status is embedded in the mailbox subheader. */
15444         shdr = (union lpfc_sli4_cfg_shdr *)
15445                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15446         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15447         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15448         if (shdr_status || shdr_add_status || rc) {
15449                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15450                                 "2506 CQ_DESTROY mailbox failed with "
15451                                 "status x%x add_status x%x, mbx status x%x\n",
15452                                 shdr_status, shdr_add_status, rc);
15453                 status = -ENXIO;
15454         }
15455         /* Remove cq from any list */
15456         list_del_init(&cq->list);
15457         mempool_free(mbox, cq->phba->mbox_mem_pool);
15458         return status;
15459 }
15460
15461 /**
15462  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15463  * @qm: The queue structure associated with the queue to destroy.
15464  *
15465  * This function destroys a queue, as detailed in @mq by sending an mailbox
15466  * command, specific to the type of queue, to the HBA.
15467  *
15468  * The @mq struct is used to get the queue ID of the queue to destroy.
15469  *
15470  * On success this function will return a zero. If the queue destroy mailbox
15471  * command fails this function will return -ENXIO.
15472  **/
15473 int
15474 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15475 {
15476         LPFC_MBOXQ_t *mbox;
15477         int rc, length, status = 0;
15478         uint32_t shdr_status, shdr_add_status;
15479         union lpfc_sli4_cfg_shdr *shdr;
15480
15481         /* sanity check on queue memory */
15482         if (!mq)
15483                 return -ENODEV;
15484         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15485         if (!mbox)
15486                 return -ENOMEM;
15487         length = (sizeof(struct lpfc_mbx_mq_destroy) -
15488                   sizeof(struct lpfc_sli4_cfg_mhdr));
15489         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15490                          LPFC_MBOX_OPCODE_MQ_DESTROY,
15491                          length, LPFC_SLI4_MBX_EMBED);
15492         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15493                mq->queue_id);
15494         mbox->vport = mq->phba->pport;
15495         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15496         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15497         /* The IOCTL status is embedded in the mailbox subheader. */
15498         shdr = (union lpfc_sli4_cfg_shdr *)
15499                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15500         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15501         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15502         if (shdr_status || shdr_add_status || rc) {
15503                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15504                                 "2507 MQ_DESTROY mailbox failed with "
15505                                 "status x%x add_status x%x, mbx status x%x\n",
15506                                 shdr_status, shdr_add_status, rc);
15507                 status = -ENXIO;
15508         }
15509         /* Remove mq from any list */
15510         list_del_init(&mq->list);
15511         mempool_free(mbox, mq->phba->mbox_mem_pool);
15512         return status;
15513 }
15514
15515 /**
15516  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15517  * @wq: The queue structure associated with the queue to destroy.
15518  *
15519  * This function destroys a queue, as detailed in @wq by sending an mailbox
15520  * command, specific to the type of queue, to the HBA.
15521  *
15522  * The @wq struct is used to get the queue ID of the queue to destroy.
15523  *
15524  * On success this function will return a zero. If the queue destroy mailbox
15525  * command fails this function will return -ENXIO.
15526  **/
15527 int
15528 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15529 {
15530         LPFC_MBOXQ_t *mbox;
15531         int rc, length, status = 0;
15532         uint32_t shdr_status, shdr_add_status;
15533         union lpfc_sli4_cfg_shdr *shdr;
15534
15535         /* sanity check on queue memory */
15536         if (!wq)
15537                 return -ENODEV;
15538         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15539         if (!mbox)
15540                 return -ENOMEM;
15541         length = (sizeof(struct lpfc_mbx_wq_destroy) -
15542                   sizeof(struct lpfc_sli4_cfg_mhdr));
15543         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15544                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15545                          length, LPFC_SLI4_MBX_EMBED);
15546         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15547                wq->queue_id);
15548         mbox->vport = wq->phba->pport;
15549         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15550         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15551         shdr = (union lpfc_sli4_cfg_shdr *)
15552                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15553         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15554         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15555         if (shdr_status || shdr_add_status || rc) {
15556                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15557                                 "2508 WQ_DESTROY mailbox failed with "
15558                                 "status x%x add_status x%x, mbx status x%x\n",
15559                                 shdr_status, shdr_add_status, rc);
15560                 status = -ENXIO;
15561         }
15562         /* Remove wq from any list */
15563         list_del_init(&wq->list);
15564         mempool_free(mbox, wq->phba->mbox_mem_pool);
15565         return status;
15566 }
15567
15568 /**
15569  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15570  * @rq: The queue structure associated with the queue to destroy.
15571  *
15572  * This function destroys a queue, as detailed in @rq by sending an mailbox
15573  * command, specific to the type of queue, to the HBA.
15574  *
15575  * The @rq struct is used to get the queue ID of the queue to destroy.
15576  *
15577  * On success this function will return a zero. If the queue destroy mailbox
15578  * command fails this function will return -ENXIO.
15579  **/
15580 int
15581 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15582                 struct lpfc_queue *drq)
15583 {
15584         LPFC_MBOXQ_t *mbox;
15585         int rc, length, status = 0;
15586         uint32_t shdr_status, shdr_add_status;
15587         union lpfc_sli4_cfg_shdr *shdr;
15588
15589         /* sanity check on queue memory */
15590         if (!hrq || !drq)
15591                 return -ENODEV;
15592         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15593         if (!mbox)
15594                 return -ENOMEM;
15595         length = (sizeof(struct lpfc_mbx_rq_destroy) -
15596                   sizeof(struct lpfc_sli4_cfg_mhdr));
15597         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15598                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15599                          length, LPFC_SLI4_MBX_EMBED);
15600         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15601                hrq->queue_id);
15602         mbox->vport = hrq->phba->pport;
15603         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15604         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15605         /* The IOCTL status is embedded in the mailbox subheader. */
15606         shdr = (union lpfc_sli4_cfg_shdr *)
15607                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15608         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15609         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15610         if (shdr_status || shdr_add_status || rc) {
15611                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15612                                 "2509 RQ_DESTROY mailbox failed with "
15613                                 "status x%x add_status x%x, mbx status x%x\n",
15614                                 shdr_status, shdr_add_status, rc);
15615                 if (rc != MBX_TIMEOUT)
15616                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
15617                 return -ENXIO;
15618         }
15619         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15620                drq->queue_id);
15621         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15622         shdr = (union lpfc_sli4_cfg_shdr *)
15623                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15624         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15625         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15626         if (shdr_status || shdr_add_status || rc) {
15627                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15628                                 "2510 RQ_DESTROY mailbox failed with "
15629                                 "status x%x add_status x%x, mbx status x%x\n",
15630                                 shdr_status, shdr_add_status, rc);
15631                 status = -ENXIO;
15632         }
15633         list_del_init(&hrq->list);
15634         list_del_init(&drq->list);
15635         mempool_free(mbox, hrq->phba->mbox_mem_pool);
15636         return status;
15637 }
15638
15639 /**
15640  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15641  * @phba: The virtual port for which this call being executed.
15642  * @pdma_phys_addr0: Physical address of the 1st SGL page.
15643  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15644  * @xritag: the xritag that ties this io to the SGL pages.
15645  *
15646  * This routine will post the sgl pages for the IO that has the xritag
15647  * that is in the iocbq structure. The xritag is assigned during iocbq
15648  * creation and persists for as long as the driver is loaded.
15649  * if the caller has fewer than 256 scatter gather segments to map then
15650  * pdma_phys_addr1 should be 0.
15651  * If the caller needs to map more than 256 scatter gather segment then
15652  * pdma_phys_addr1 should be a valid physical address.
15653  * physical address for SGLs must be 64 byte aligned.
15654  * If you are going to map 2 SGL's then the first one must have 256 entries
15655  * the second sgl can have between 1 and 256 entries.
15656  *
15657  * Return codes:
15658  *      0 - Success
15659  *      -ENXIO, -ENOMEM - Failure
15660  **/
15661 int
15662 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15663                 dma_addr_t pdma_phys_addr0,
15664                 dma_addr_t pdma_phys_addr1,
15665                 uint16_t xritag)
15666 {
15667         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15668         LPFC_MBOXQ_t *mbox;
15669         int rc;
15670         uint32_t shdr_status, shdr_add_status;
15671         uint32_t mbox_tmo;
15672         union lpfc_sli4_cfg_shdr *shdr;
15673
15674         if (xritag == NO_XRI) {
15675                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15676                                 "0364 Invalid param:\n");
15677                 return -EINVAL;
15678         }
15679
15680         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15681         if (!mbox)
15682                 return -ENOMEM;
15683
15684         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15685                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15686                         sizeof(struct lpfc_mbx_post_sgl_pages) -
15687                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15688
15689         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15690                                 &mbox->u.mqe.un.post_sgl_pages;
15691         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15692         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15693
15694         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15695                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15696         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15697                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15698
15699         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15700                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15701         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15702                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15703         if (!phba->sli4_hba.intr_enable)
15704                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15705         else {
15706                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15707                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15708         }
15709         /* The IOCTL status is embedded in the mailbox subheader. */
15710         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15711         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15712         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15713         if (rc != MBX_TIMEOUT)
15714                 mempool_free(mbox, phba->mbox_mem_pool);
15715         if (shdr_status || shdr_add_status || rc) {
15716                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15717                                 "2511 POST_SGL mailbox failed with "
15718                                 "status x%x add_status x%x, mbx status x%x\n",
15719                                 shdr_status, shdr_add_status, rc);
15720         }
15721         return 0;
15722 }
15723
15724 /**
15725  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
15726  * @phba: pointer to lpfc hba data structure.
15727  *
15728  * This routine is invoked to post rpi header templates to the
15729  * HBA consistent with the SLI-4 interface spec.  This routine
15730  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15731  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15732  *
15733  * Returns
15734  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15735  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
15736  **/
15737 static uint16_t
15738 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15739 {
15740         unsigned long xri;
15741
15742         /*
15743          * Fetch the next logical xri.  Because this index is logical,
15744          * the driver starts at 0 each time.
15745          */
15746         spin_lock_irq(&phba->hbalock);
15747         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15748                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
15749         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15750                 spin_unlock_irq(&phba->hbalock);
15751                 return NO_XRI;
15752         } else {
15753                 set_bit(xri, phba->sli4_hba.xri_bmask);
15754                 phba->sli4_hba.max_cfg_param.xri_used++;
15755         }
15756         spin_unlock_irq(&phba->hbalock);
15757         return xri;
15758 }
15759
15760 /**
15761  * lpfc_sli4_free_xri - Release an xri for reuse.
15762  * @phba: pointer to lpfc hba data structure.
15763  *
15764  * This routine is invoked to release an xri to the pool of
15765  * available rpis maintained by the driver.
15766  **/
15767 static void
15768 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15769 {
15770         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
15771                 phba->sli4_hba.max_cfg_param.xri_used--;
15772         }
15773 }
15774
15775 /**
15776  * lpfc_sli4_free_xri - Release an xri for reuse.
15777  * @phba: pointer to lpfc hba data structure.
15778  *
15779  * This routine is invoked to release an xri to the pool of
15780  * available rpis maintained by the driver.
15781  **/
15782 void
15783 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15784 {
15785         spin_lock_irq(&phba->hbalock);
15786         __lpfc_sli4_free_xri(phba, xri);
15787         spin_unlock_irq(&phba->hbalock);
15788 }
15789
15790 /**
15791  * lpfc_sli4_next_xritag - Get an xritag for the io
15792  * @phba: Pointer to HBA context object.
15793  *
15794  * This function gets an xritag for the iocb. If there is no unused xritag
15795  * it will return 0xffff.
15796  * The function returns the allocated xritag if successful, else returns zero.
15797  * Zero is not a valid xritag.
15798  * The caller is not required to hold any lock.
15799  **/
15800 uint16_t
15801 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15802 {
15803         uint16_t xri_index;
15804
15805         xri_index = lpfc_sli4_alloc_xri(phba);
15806         if (xri_index == NO_XRI)
15807                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15808                                 "2004 Failed to allocate XRI.last XRITAG is %d"
15809                                 " Max XRI is %d, Used XRI is %d\n",
15810                                 xri_index,
15811                                 phba->sli4_hba.max_cfg_param.max_xri,
15812                                 phba->sli4_hba.max_cfg_param.xri_used);
15813         return xri_index;
15814 }
15815
15816 /**
15817  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
15818  * @phba: pointer to lpfc hba data structure.
15819  * @post_sgl_list: pointer to els sgl entry list.
15820  * @count: number of els sgl entries on the list.
15821  *
15822  * This routine is invoked to post a block of driver's sgl pages to the
15823  * HBA using non-embedded mailbox command. No Lock is held. This routine
15824  * is only called when the driver is loading and after all IO has been
15825  * stopped.
15826  **/
15827 static int
15828 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
15829                             struct list_head *post_sgl_list,
15830                             int post_cnt)
15831 {
15832         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
15833         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15834         struct sgl_page_pairs *sgl_pg_pairs;
15835         void *viraddr;
15836         LPFC_MBOXQ_t *mbox;
15837         uint32_t reqlen, alloclen, pg_pairs;
15838         uint32_t mbox_tmo;
15839         uint16_t xritag_start = 0;
15840         int rc = 0;
15841         uint32_t shdr_status, shdr_add_status;
15842         union lpfc_sli4_cfg_shdr *shdr;
15843
15844         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
15845                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
15846         if (reqlen > SLI4_PAGE_SIZE) {
15847                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15848                                 "2559 Block sgl registration required DMA "
15849                                 "size (%d) great than a page\n", reqlen);
15850                 return -ENOMEM;
15851         }
15852
15853         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15854         if (!mbox)
15855                 return -ENOMEM;
15856
15857         /* Allocate DMA memory and set up the non-embedded mailbox command */
15858         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15859                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15860                          LPFC_SLI4_MBX_NEMBED);
15861
15862         if (alloclen < reqlen) {
15863                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15864                                 "0285 Allocated DMA memory size (%d) is "
15865                                 "less than the requested DMA memory "
15866                                 "size (%d)\n", alloclen, reqlen);
15867                 lpfc_sli4_mbox_cmd_free(phba, mbox);
15868                 return -ENOMEM;
15869         }
15870         /* Set up the SGL pages in the non-embedded DMA pages */
15871         viraddr = mbox->sge_array->addr[0];
15872         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15873         sgl_pg_pairs = &sgl->sgl_pg_pairs;
15874
15875         pg_pairs = 0;
15876         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
15877                 /* Set up the sge entry */
15878                 sgl_pg_pairs->sgl_pg0_addr_lo =
15879                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15880                 sgl_pg_pairs->sgl_pg0_addr_hi =
15881                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15882                 sgl_pg_pairs->sgl_pg1_addr_lo =
15883                                 cpu_to_le32(putPaddrLow(0));
15884                 sgl_pg_pairs->sgl_pg1_addr_hi =
15885                                 cpu_to_le32(putPaddrHigh(0));
15886
15887                 /* Keep the first xritag on the list */
15888                 if (pg_pairs == 0)
15889                         xritag_start = sglq_entry->sli4_xritag;
15890                 sgl_pg_pairs++;
15891                 pg_pairs++;
15892         }
15893
15894         /* Complete initialization and perform endian conversion. */
15895         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15896         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
15897         sgl->word0 = cpu_to_le32(sgl->word0);
15898
15899         if (!phba->sli4_hba.intr_enable)
15900                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15901         else {
15902                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15903                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15904         }
15905         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15906         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15907         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15908         if (rc != MBX_TIMEOUT)
15909                 lpfc_sli4_mbox_cmd_free(phba, mbox);
15910         if (shdr_status || shdr_add_status || rc) {
15911                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15912                                 "2513 POST_SGL_BLOCK mailbox command failed "
15913                                 "status x%x add_status x%x mbx status x%x\n",
15914                                 shdr_status, shdr_add_status, rc);
15915                 rc = -ENXIO;
15916         }
15917         return rc;
15918 }
15919
15920 /**
15921  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15922  * @phba: pointer to lpfc hba data structure.
15923  * @sblist: pointer to scsi buffer list.
15924  * @count: number of scsi buffers on the list.
15925  *
15926  * This routine is invoked to post a block of @count scsi sgl pages from a
15927  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15928  * No Lock is held.
15929  *
15930  **/
15931 int
15932 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15933                               struct list_head *sblist,
15934                               int count)
15935 {
15936         struct lpfc_scsi_buf *psb;
15937         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15938         struct sgl_page_pairs *sgl_pg_pairs;
15939         void *viraddr;
15940         LPFC_MBOXQ_t *mbox;
15941         uint32_t reqlen, alloclen, pg_pairs;
15942         uint32_t mbox_tmo;
15943         uint16_t xritag_start = 0;
15944         int rc = 0;
15945         uint32_t shdr_status, shdr_add_status;
15946         dma_addr_t pdma_phys_bpl1;
15947         union lpfc_sli4_cfg_shdr *shdr;
15948
15949         /* Calculate the requested length of the dma memory */
15950         reqlen = count * sizeof(struct sgl_page_pairs) +
15951                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
15952         if (reqlen > SLI4_PAGE_SIZE) {
15953                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15954                                 "0217 Block sgl registration required DMA "
15955                                 "size (%d) great than a page\n", reqlen);
15956                 return -ENOMEM;
15957         }
15958         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15959         if (!mbox) {
15960                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15961                                 "0283 Failed to allocate mbox cmd memory\n");
15962                 return -ENOMEM;
15963         }
15964
15965         /* Allocate DMA memory and set up the non-embedded mailbox command */
15966         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15967                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15968                                 LPFC_SLI4_MBX_NEMBED);
15969
15970         if (alloclen < reqlen) {
15971                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15972                                 "2561 Allocated DMA memory size (%d) is "
15973                                 "less than the requested DMA memory "
15974                                 "size (%d)\n", alloclen, reqlen);
15975                 lpfc_sli4_mbox_cmd_free(phba, mbox);
15976                 return -ENOMEM;
15977         }
15978
15979         /* Get the first SGE entry from the non-embedded DMA memory */
15980         viraddr = mbox->sge_array->addr[0];
15981
15982         /* Set up the SGL pages in the non-embedded DMA pages */
15983         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15984         sgl_pg_pairs = &sgl->sgl_pg_pairs;
15985
15986         pg_pairs = 0;
15987         list_for_each_entry(psb, sblist, list) {
15988                 /* Set up the sge entry */
15989                 sgl_pg_pairs->sgl_pg0_addr_lo =
15990                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
15991                 sgl_pg_pairs->sgl_pg0_addr_hi =
15992                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
15993                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
15994                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
15995                 else
15996                         pdma_phys_bpl1 = 0;
15997                 sgl_pg_pairs->sgl_pg1_addr_lo =
15998                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
15999                 sgl_pg_pairs->sgl_pg1_addr_hi =
16000                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16001                 /* Keep the first xritag on the list */
16002                 if (pg_pairs == 0)
16003                         xritag_start = psb->cur_iocbq.sli4_xritag;
16004                 sgl_pg_pairs++;
16005                 pg_pairs++;
16006         }
16007         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16008         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16009         /* Perform endian conversion if necessary */
16010         sgl->word0 = cpu_to_le32(sgl->word0);
16011
16012         if (!phba->sli4_hba.intr_enable)
16013                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16014         else {
16015                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16016                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16017         }
16018         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16019         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16020         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16021         if (rc != MBX_TIMEOUT)
16022                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16023         if (shdr_status || shdr_add_status || rc) {
16024                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16025                                 "2564 POST_SGL_BLOCK mailbox command failed "
16026                                 "status x%x add_status x%x mbx status x%x\n",
16027                                 shdr_status, shdr_add_status, rc);
16028                 rc = -ENXIO;
16029         }
16030         return rc;
16031 }
16032
16033 static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
16034 static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
16035
16036 /**
16037  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16038  * @phba: pointer to lpfc_hba struct that the frame was received on
16039  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16040  *
16041  * This function checks the fields in the @fc_hdr to see if the FC frame is a
16042  * valid type of frame that the LPFC driver will handle. This function will
16043  * return a zero if the frame is a valid frame or a non zero value when the
16044  * frame does not pass the check.
16045  **/
16046 static int
16047 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16048 {
16049         /*  make rctl_names static to save stack space */
16050         struct fc_vft_header *fc_vft_hdr;
16051         uint32_t *header = (uint32_t *) fc_hdr;
16052
16053         switch (fc_hdr->fh_r_ctl) {
16054         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
16055         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
16056         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
16057         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
16058         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
16059         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
16060         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
16061         case FC_RCTL_DD_CMD_STATUS:     /* command status */
16062         case FC_RCTL_ELS_REQ:   /* extended link services request */
16063         case FC_RCTL_ELS_REP:   /* extended link services reply */
16064         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
16065         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
16066         case FC_RCTL_BA_NOP:    /* basic link service NOP */
16067         case FC_RCTL_BA_ABTS:   /* basic link service abort */
16068         case FC_RCTL_BA_RMC:    /* remove connection */
16069         case FC_RCTL_BA_ACC:    /* basic accept */
16070         case FC_RCTL_BA_RJT:    /* basic reject */
16071         case FC_RCTL_BA_PRMT:
16072         case FC_RCTL_ACK_1:     /* acknowledge_1 */
16073         case FC_RCTL_ACK_0:     /* acknowledge_0 */
16074         case FC_RCTL_P_RJT:     /* port reject */
16075         case FC_RCTL_F_RJT:     /* fabric reject */
16076         case FC_RCTL_P_BSY:     /* port busy */
16077         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
16078         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
16079         case FC_RCTL_LCR:       /* link credit reset */
16080         case FC_RCTL_END:       /* end */
16081                 break;
16082         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
16083                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16084                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16085                 return lpfc_fc_frame_check(phba, fc_hdr);
16086         default:
16087                 goto drop;
16088         }
16089         switch (fc_hdr->fh_type) {
16090         case FC_TYPE_BLS:
16091         case FC_TYPE_ELS:
16092         case FC_TYPE_FCP:
16093         case FC_TYPE_CT:
16094         case FC_TYPE_NVME:
16095                 break;
16096         case FC_TYPE_IP:
16097         case FC_TYPE_ILS:
16098         default:
16099                 goto drop;
16100         }
16101
16102         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16103                         "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16104                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16105                         lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16106                         lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
16107                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16108                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16109                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16110                         be32_to_cpu(header[6]));
16111         return 0;
16112 drop:
16113         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16114                         "2539 Dropped frame rctl:%s type:%s\n",
16115                         lpfc_rctl_names[fc_hdr->fh_r_ctl],
16116                         lpfc_type_names[fc_hdr->fh_type]);
16117         return 1;
16118 }
16119
16120 /**
16121  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16122  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16123  *
16124  * This function processes the FC header to retrieve the VFI from the VF
16125  * header, if one exists. This function will return the VFI if one exists
16126  * or 0 if no VSAN Header exists.
16127  **/
16128 static uint32_t
16129 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16130 {
16131         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16132
16133         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16134                 return 0;
16135         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16136 }
16137
16138 /**
16139  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16140  * @phba: Pointer to the HBA structure to search for the vport on
16141  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16142  * @fcfi: The FC Fabric ID that the frame came from
16143  *
16144  * This function searches the @phba for a vport that matches the content of the
16145  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16146  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16147  * returns the matching vport pointer or NULL if unable to match frame to a
16148  * vport.
16149  **/
16150 static struct lpfc_vport *
16151 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16152                        uint16_t fcfi, uint32_t did)
16153 {
16154         struct lpfc_vport **vports;
16155         struct lpfc_vport *vport = NULL;
16156         int i;
16157
16158         if (did == Fabric_DID)
16159                 return phba->pport;
16160         if ((phba->pport->fc_flag & FC_PT2PT) &&
16161                 !(phba->link_state == LPFC_HBA_READY))
16162                 return phba->pport;
16163
16164         vports = lpfc_create_vport_work_array(phba);
16165         if (vports != NULL) {
16166                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16167                         if (phba->fcf.fcfi == fcfi &&
16168                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16169                             vports[i]->fc_myDID == did) {
16170                                 vport = vports[i];
16171                                 break;
16172                         }
16173                 }
16174         }
16175         lpfc_destroy_vport_work_array(phba, vports);
16176         return vport;
16177 }
16178
16179 /**
16180  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16181  * @vport: The vport to work on.
16182  *
16183  * This function updates the receive sequence time stamp for this vport. The
16184  * receive sequence time stamp indicates the time that the last frame of the
16185  * the sequence that has been idle for the longest amount of time was received.
16186  * the driver uses this time stamp to indicate if any received sequences have
16187  * timed out.
16188  **/
16189 static void
16190 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16191 {
16192         struct lpfc_dmabuf *h_buf;
16193         struct hbq_dmabuf *dmabuf = NULL;
16194
16195         /* get the oldest sequence on the rcv list */
16196         h_buf = list_get_first(&vport->rcv_buffer_list,
16197                                struct lpfc_dmabuf, list);
16198         if (!h_buf)
16199                 return;
16200         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16201         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16202 }
16203
16204 /**
16205  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16206  * @vport: The vport that the received sequences were sent to.
16207  *
16208  * This function cleans up all outstanding received sequences. This is called
16209  * by the driver when a link event or user action invalidates all the received
16210  * sequences.
16211  **/
16212 void
16213 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16214 {
16215         struct lpfc_dmabuf *h_buf, *hnext;
16216         struct lpfc_dmabuf *d_buf, *dnext;
16217         struct hbq_dmabuf *dmabuf = NULL;
16218
16219         /* start with the oldest sequence on the rcv list */
16220         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16221                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16222                 list_del_init(&dmabuf->hbuf.list);
16223                 list_for_each_entry_safe(d_buf, dnext,
16224                                          &dmabuf->dbuf.list, list) {
16225                         list_del_init(&d_buf->list);
16226                         lpfc_in_buf_free(vport->phba, d_buf);
16227                 }
16228                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16229         }
16230 }
16231
16232 /**
16233  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16234  * @vport: The vport that the received sequences were sent to.
16235  *
16236  * This function determines whether any received sequences have timed out by
16237  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16238  * indicates that there is at least one timed out sequence this routine will
16239  * go through the received sequences one at a time from most inactive to most
16240  * active to determine which ones need to be cleaned up. Once it has determined
16241  * that a sequence needs to be cleaned up it will simply free up the resources
16242  * without sending an abort.
16243  **/
16244 void
16245 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16246 {
16247         struct lpfc_dmabuf *h_buf, *hnext;
16248         struct lpfc_dmabuf *d_buf, *dnext;
16249         struct hbq_dmabuf *dmabuf = NULL;
16250         unsigned long timeout;
16251         int abort_count = 0;
16252
16253         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16254                    vport->rcv_buffer_time_stamp);
16255         if (list_empty(&vport->rcv_buffer_list) ||
16256             time_before(jiffies, timeout))
16257                 return;
16258         /* start with the oldest sequence on the rcv list */
16259         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16260                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16261                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16262                            dmabuf->time_stamp);
16263                 if (time_before(jiffies, timeout))
16264                         break;
16265                 abort_count++;
16266                 list_del_init(&dmabuf->hbuf.list);
16267                 list_for_each_entry_safe(d_buf, dnext,
16268                                          &dmabuf->dbuf.list, list) {
16269                         list_del_init(&d_buf->list);
16270                         lpfc_in_buf_free(vport->phba, d_buf);
16271                 }
16272                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16273         }
16274         if (abort_count)
16275                 lpfc_update_rcv_time_stamp(vport);
16276 }
16277
16278 /**
16279  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16280  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16281  *
16282  * This function searches through the existing incomplete sequences that have
16283  * been sent to this @vport. If the frame matches one of the incomplete
16284  * sequences then the dbuf in the @dmabuf is added to the list of frames that
16285  * make up that sequence. If no sequence is found that matches this frame then
16286  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16287  * This function returns a pointer to the first dmabuf in the sequence list that
16288  * the frame was linked to.
16289  **/
16290 static struct hbq_dmabuf *
16291 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16292 {
16293         struct fc_frame_header *new_hdr;
16294         struct fc_frame_header *temp_hdr;
16295         struct lpfc_dmabuf *d_buf;
16296         struct lpfc_dmabuf *h_buf;
16297         struct hbq_dmabuf *seq_dmabuf = NULL;
16298         struct hbq_dmabuf *temp_dmabuf = NULL;
16299         uint8_t found = 0;
16300
16301         INIT_LIST_HEAD(&dmabuf->dbuf.list);
16302         dmabuf->time_stamp = jiffies;
16303         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16304
16305         /* Use the hdr_buf to find the sequence that this frame belongs to */
16306         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16307                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16308                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16309                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16310                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16311                         continue;
16312                 /* found a pending sequence that matches this frame */
16313                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16314                 break;
16315         }
16316         if (!seq_dmabuf) {
16317                 /*
16318                  * This indicates first frame received for this sequence.
16319                  * Queue the buffer on the vport's rcv_buffer_list.
16320                  */
16321                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16322                 lpfc_update_rcv_time_stamp(vport);
16323                 return dmabuf;
16324         }
16325         temp_hdr = seq_dmabuf->hbuf.virt;
16326         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16327                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16328                 list_del_init(&seq_dmabuf->hbuf.list);
16329                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16330                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16331                 lpfc_update_rcv_time_stamp(vport);
16332                 return dmabuf;
16333         }
16334         /* move this sequence to the tail to indicate a young sequence */
16335         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16336         seq_dmabuf->time_stamp = jiffies;
16337         lpfc_update_rcv_time_stamp(vport);
16338         if (list_empty(&seq_dmabuf->dbuf.list)) {
16339                 temp_hdr = dmabuf->hbuf.virt;
16340                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16341                 return seq_dmabuf;
16342         }
16343         /* find the correct place in the sequence to insert this frame */
16344         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16345         while (!found) {
16346                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16347                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16348                 /*
16349                  * If the frame's sequence count is greater than the frame on
16350                  * the list then insert the frame right after this frame
16351                  */
16352                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16353                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16354                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
16355                         found = 1;
16356                         break;
16357                 }
16358
16359                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16360                         break;
16361                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
16362         }
16363
16364         if (found)
16365                 return seq_dmabuf;
16366         return NULL;
16367 }
16368
16369 /**
16370  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16371  * @vport: pointer to a vitural port
16372  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16373  *
16374  * This function tries to abort from the partially assembed sequence, described
16375  * by the information from basic abbort @dmabuf. It checks to see whether such
16376  * partially assembled sequence held by the driver. If so, it shall free up all
16377  * the frames from the partially assembled sequence.
16378  *
16379  * Return
16380  * true  -- if there is matching partially assembled sequence present and all
16381  *          the frames freed with the sequence;
16382  * false -- if there is no matching partially assembled sequence present so
16383  *          nothing got aborted in the lower layer driver
16384  **/
16385 static bool
16386 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16387                             struct hbq_dmabuf *dmabuf)
16388 {
16389         struct fc_frame_header *new_hdr;
16390         struct fc_frame_header *temp_hdr;
16391         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16392         struct hbq_dmabuf *seq_dmabuf = NULL;
16393
16394         /* Use the hdr_buf to find the sequence that matches this frame */
16395         INIT_LIST_HEAD(&dmabuf->dbuf.list);
16396         INIT_LIST_HEAD(&dmabuf->hbuf.list);
16397         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16398         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16399                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16400                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16401                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16402                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16403                         continue;
16404                 /* found a pending sequence that matches this frame */
16405                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16406                 break;
16407         }
16408
16409         /* Free up all the frames from the partially assembled sequence */
16410         if (seq_dmabuf) {
16411                 list_for_each_entry_safe(d_buf, n_buf,
16412                                          &seq_dmabuf->dbuf.list, list) {
16413                         list_del_init(&d_buf->list);
16414                         lpfc_in_buf_free(vport->phba, d_buf);
16415                 }
16416                 return true;
16417         }
16418         return false;
16419 }
16420
16421 /**
16422  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16423  * @vport: pointer to a vitural port
16424  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16425  *
16426  * This function tries to abort from the assembed sequence from upper level
16427  * protocol, described by the information from basic abbort @dmabuf. It
16428  * checks to see whether such pending context exists at upper level protocol.
16429  * If so, it shall clean up the pending context.
16430  *
16431  * Return
16432  * true  -- if there is matching pending context of the sequence cleaned
16433  *          at ulp;
16434  * false -- if there is no matching pending context of the sequence present
16435  *          at ulp.
16436  **/
16437 static bool
16438 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16439 {
16440         struct lpfc_hba *phba = vport->phba;
16441         int handled;
16442
16443         /* Accepting abort at ulp with SLI4 only */
16444         if (phba->sli_rev < LPFC_SLI_REV4)
16445                 return false;
16446
16447         /* Register all caring upper level protocols to attend abort */
16448         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16449         if (handled)
16450                 return true;
16451
16452         return false;
16453 }
16454
16455 /**
16456  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
16457  * @phba: Pointer to HBA context object.
16458  * @cmd_iocbq: pointer to the command iocbq structure.
16459  * @rsp_iocbq: pointer to the response iocbq structure.
16460  *
16461  * This function handles the sequence abort response iocb command complete
16462  * event. It properly releases the memory allocated to the sequence abort
16463  * accept iocb.
16464  **/
16465 static void
16466 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
16467                              struct lpfc_iocbq *cmd_iocbq,
16468                              struct lpfc_iocbq *rsp_iocbq)
16469 {
16470         struct lpfc_nodelist *ndlp;
16471
16472         if (cmd_iocbq) {
16473                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16474                 lpfc_nlp_put(ndlp);
16475                 lpfc_nlp_not_used(ndlp);
16476                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
16477         }
16478
16479         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16480         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16481                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16482                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
16483                         rsp_iocbq->iocb.ulpStatus,
16484                         rsp_iocbq->iocb.un.ulpWord[4]);
16485 }
16486
16487 /**
16488  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16489  * @phba: Pointer to HBA context object.
16490  * @xri: xri id in transaction.
16491  *
16492  * This function validates the xri maps to the known range of XRIs allocated an
16493  * used by the driver.
16494  **/
16495 uint16_t
16496 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16497                       uint16_t xri)
16498 {
16499         uint16_t i;
16500
16501         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16502                 if (xri == phba->sli4_hba.xri_ids[i])
16503                         return i;
16504         }
16505         return NO_XRI;
16506 }
16507
16508 /**
16509  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
16510  * @phba: Pointer to HBA context object.
16511  * @fc_hdr: pointer to a FC frame header.
16512  *
16513  * This function sends a basic response to a previous unsol sequence abort
16514  * event after aborting the sequence handling.
16515  **/
16516 static void
16517 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16518                         struct fc_frame_header *fc_hdr, bool aborted)
16519 {
16520         struct lpfc_hba *phba = vport->phba;
16521         struct lpfc_iocbq *ctiocb = NULL;
16522         struct lpfc_nodelist *ndlp;
16523         uint16_t oxid, rxid, xri, lxri;
16524         uint32_t sid, fctl;
16525         IOCB_t *icmd;
16526         int rc;
16527
16528         if (!lpfc_is_link_up(phba))
16529                 return;
16530
16531         sid = sli4_sid_from_fc_hdr(fc_hdr);
16532         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
16533         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
16534
16535         ndlp = lpfc_findnode_did(vport, sid);
16536         if (!ndlp) {
16537                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
16538                 if (!ndlp) {
16539                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16540                                          "1268 Failed to allocate ndlp for "
16541                                          "oxid:x%x SID:x%x\n", oxid, sid);
16542                         return;
16543                 }
16544                 lpfc_nlp_init(vport, ndlp, sid);
16545                 /* Put ndlp onto pport node list */
16546                 lpfc_enqueue_node(vport, ndlp);
16547         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16548                 /* re-setup ndlp without removing from node list */
16549                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16550                 if (!ndlp) {
16551                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16552                                          "3275 Failed to active ndlp found "
16553                                          "for oxid:x%x SID:x%x\n", oxid, sid);
16554                         return;
16555                 }
16556         }
16557
16558         /* Allocate buffer for rsp iocb */
16559         ctiocb = lpfc_sli_get_iocbq(phba);
16560         if (!ctiocb)
16561                 return;
16562
16563         /* Extract the F_CTL field from FC_HDR */
16564         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16565
16566         icmd = &ctiocb->iocb;
16567         icmd->un.xseq64.bdl.bdeSize = 0;
16568         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
16569         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16570         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16571         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16572
16573         /* Fill in the rest of iocb fields */
16574         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16575         icmd->ulpBdeCount = 0;
16576         icmd->ulpLe = 1;
16577         icmd->ulpClass = CLASS3;
16578         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
16579         ctiocb->context1 = lpfc_nlp_get(ndlp);
16580
16581         ctiocb->iocb_cmpl = NULL;
16582         ctiocb->vport = phba->pport;
16583         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
16584         ctiocb->sli4_lxritag = NO_XRI;
16585         ctiocb->sli4_xritag = NO_XRI;
16586
16587         if (fctl & FC_FC_EX_CTX)
16588                 /* Exchange responder sent the abort so we
16589                  * own the oxid.
16590                  */
16591                 xri = oxid;
16592         else
16593                 xri = rxid;
16594         lxri = lpfc_sli4_xri_inrange(phba, xri);
16595         if (lxri != NO_XRI)
16596                 lpfc_set_rrq_active(phba, ndlp, lxri,
16597                         (xri == oxid) ? rxid : oxid, 0);
16598         /* For BA_ABTS from exchange responder, if the logical xri with
16599          * the oxid maps to the FCP XRI range, the port no longer has
16600          * that exchange context, send a BLS_RJT. Override the IOCB for
16601          * a BA_RJT.
16602          */
16603         if ((fctl & FC_FC_EX_CTX) &&
16604             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
16605                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16606                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16607                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16608                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16609         }
16610
16611         /* If BA_ABTS failed to abort a partially assembled receive sequence,
16612          * the driver no longer has that exchange, send a BLS_RJT. Override
16613          * the IOCB for a BA_RJT.
16614          */
16615         if (aborted == false) {
16616                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16617                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16618                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16619                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16620         }
16621
16622         if (fctl & FC_FC_EX_CTX) {
16623                 /* ABTS sent by responder to CT exchange, construction
16624                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16625                  * field and RX_ID from ABTS for RX_ID field.
16626                  */
16627                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
16628         } else {
16629                 /* ABTS sent by initiator to CT exchange, construction
16630                  * of BA_ACC will need to allocate a new XRI as for the
16631                  * XRI_TAG field.
16632                  */
16633                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
16634         }
16635         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
16636         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
16637
16638         /* Xmit CT abts response on exchange <xid> */
16639         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16640                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16641                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
16642
16643         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16644         if (rc == IOCB_ERROR) {
16645                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16646                                  "2925 Failed to issue CT ABTS RSP x%x on "
16647                                  "xri x%x, Data x%x\n",
16648                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16649                                  phba->link_state);
16650                 lpfc_nlp_put(ndlp);
16651                 ctiocb->context1 = NULL;
16652                 lpfc_sli_release_iocbq(phba, ctiocb);
16653         }
16654 }
16655
16656 /**
16657  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16658  * @vport: Pointer to the vport on which this sequence was received
16659  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16660  *
16661  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16662  * receive sequence is only partially assembed by the driver, it shall abort
16663  * the partially assembled frames for the sequence. Otherwise, if the
16664  * unsolicited receive sequence has been completely assembled and passed to
16665  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16666  * unsolicited sequence has been aborted. After that, it will issue a basic
16667  * accept to accept the abort.
16668  **/
16669 static void
16670 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16671                              struct hbq_dmabuf *dmabuf)
16672 {
16673         struct lpfc_hba *phba = vport->phba;
16674         struct fc_frame_header fc_hdr;
16675         uint32_t fctl;
16676         bool aborted;
16677
16678         /* Make a copy of fc_hdr before the dmabuf being released */
16679         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
16680         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
16681
16682         if (fctl & FC_FC_EX_CTX) {
16683                 /* ABTS by responder to exchange, no cleanup needed */
16684                 aborted = true;
16685         } else {
16686                 /* ABTS by initiator to exchange, need to do cleanup */
16687                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16688                 if (aborted == false)
16689                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
16690         }
16691         lpfc_in_buf_free(phba, &dmabuf->dbuf);
16692
16693         /* Respond with BA_ACC or BA_RJT accordingly */
16694         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
16695 }
16696
16697 /**
16698  * lpfc_seq_complete - Indicates if a sequence is complete
16699  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16700  *
16701  * This function checks the sequence, starting with the frame described by
16702  * @dmabuf, to see if all the frames associated with this sequence are present.
16703  * the frames associated with this sequence are linked to the @dmabuf using the
16704  * dbuf list. This function looks for two major things. 1) That the first frame
16705  * has a sequence count of zero. 2) There is a frame with last frame of sequence
16706  * set. 3) That there are no holes in the sequence count. The function will
16707  * return 1 when the sequence is complete, otherwise it will return 0.
16708  **/
16709 static int
16710 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16711 {
16712         struct fc_frame_header *hdr;
16713         struct lpfc_dmabuf *d_buf;
16714         struct hbq_dmabuf *seq_dmabuf;
16715         uint32_t fctl;
16716         int seq_count = 0;
16717
16718         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16719         /* make sure first fame of sequence has a sequence count of zero */
16720         if (hdr->fh_seq_cnt != seq_count)
16721                 return 0;
16722         fctl = (hdr->fh_f_ctl[0] << 16 |
16723                 hdr->fh_f_ctl[1] << 8 |
16724                 hdr->fh_f_ctl[2]);
16725         /* If last frame of sequence we can return success. */
16726         if (fctl & FC_FC_END_SEQ)
16727                 return 1;
16728         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16729                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16730                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16731                 /* If there is a hole in the sequence count then fail. */
16732                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
16733                         return 0;
16734                 fctl = (hdr->fh_f_ctl[0] << 16 |
16735                         hdr->fh_f_ctl[1] << 8 |
16736                         hdr->fh_f_ctl[2]);
16737                 /* If last frame of sequence we can return success. */
16738                 if (fctl & FC_FC_END_SEQ)
16739                         return 1;
16740         }
16741         return 0;
16742 }
16743
16744 /**
16745  * lpfc_prep_seq - Prep sequence for ULP processing
16746  * @vport: Pointer to the vport on which this sequence was received
16747  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16748  *
16749  * This function takes a sequence, described by a list of frames, and creates
16750  * a list of iocbq structures to describe the sequence. This iocbq list will be
16751  * used to issue to the generic unsolicited sequence handler. This routine
16752  * returns a pointer to the first iocbq in the list. If the function is unable
16753  * to allocate an iocbq then it throw out the received frames that were not
16754  * able to be described and return a pointer to the first iocbq. If unable to
16755  * allocate any iocbqs (including the first) this function will return NULL.
16756  **/
16757 static struct lpfc_iocbq *
16758 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16759 {
16760         struct hbq_dmabuf *hbq_buf;
16761         struct lpfc_dmabuf *d_buf, *n_buf;
16762         struct lpfc_iocbq *first_iocbq, *iocbq;
16763         struct fc_frame_header *fc_hdr;
16764         uint32_t sid;
16765         uint32_t len, tot_len;
16766         struct ulp_bde64 *pbde;
16767
16768         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16769         /* remove from receive buffer list */
16770         list_del_init(&seq_dmabuf->hbuf.list);
16771         lpfc_update_rcv_time_stamp(vport);
16772         /* get the Remote Port's SID */
16773         sid = sli4_sid_from_fc_hdr(fc_hdr);
16774         tot_len = 0;
16775         /* Get an iocbq struct to fill in. */
16776         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16777         if (first_iocbq) {
16778                 /* Initialize the first IOCB. */
16779                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
16780                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
16781                 first_iocbq->vport = vport;
16782
16783                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16784                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16785                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16786                         first_iocbq->iocb.un.rcvels.parmRo =
16787                                 sli4_did_from_fc_hdr(fc_hdr);
16788                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16789                 } else
16790                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
16791                 first_iocbq->iocb.ulpContext = NO_XRI;
16792                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16793                         be16_to_cpu(fc_hdr->fh_ox_id);
16794                 /* iocbq is prepped for internal consumption.  Physical vpi. */
16795                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16796                         vport->phba->vpi_ids[vport->vpi];
16797                 /* put the first buffer into the first IOCBq */
16798                 tot_len = bf_get(lpfc_rcqe_length,
16799                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16800
16801                 first_iocbq->context2 = &seq_dmabuf->dbuf;
16802                 first_iocbq->context3 = NULL;
16803                 first_iocbq->iocb.ulpBdeCount = 1;
16804                 if (tot_len > LPFC_DATA_BUF_SIZE)
16805                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16806                                                         LPFC_DATA_BUF_SIZE;
16807                 else
16808                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16809
16810                 first_iocbq->iocb.un.rcvels.remoteID = sid;
16811
16812                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16813         }
16814         iocbq = first_iocbq;
16815         /*
16816          * Each IOCBq can have two Buffers assigned, so go through the list
16817          * of buffers for this sequence and save two buffers in each IOCBq
16818          */
16819         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16820                 if (!iocbq) {
16821                         lpfc_in_buf_free(vport->phba, d_buf);
16822                         continue;
16823                 }
16824                 if (!iocbq->context3) {
16825                         iocbq->context3 = d_buf;
16826                         iocbq->iocb.ulpBdeCount++;
16827                         /* We need to get the size out of the right CQE */
16828                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16829                         len = bf_get(lpfc_rcqe_length,
16830                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
16831                         pbde = (struct ulp_bde64 *)
16832                                         &iocbq->iocb.unsli3.sli3Words[4];
16833                         if (len > LPFC_DATA_BUF_SIZE)
16834                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16835                         else
16836                                 pbde->tus.f.bdeSize = len;
16837
16838                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16839                         tot_len += len;
16840                 } else {
16841                         iocbq = lpfc_sli_get_iocbq(vport->phba);
16842                         if (!iocbq) {
16843                                 if (first_iocbq) {
16844                                         first_iocbq->iocb.ulpStatus =
16845                                                         IOSTAT_FCP_RSP_ERROR;
16846                                         first_iocbq->iocb.un.ulpWord[4] =
16847                                                         IOERR_NO_RESOURCES;
16848                                 }
16849                                 lpfc_in_buf_free(vport->phba, d_buf);
16850                                 continue;
16851                         }
16852                         /* We need to get the size out of the right CQE */
16853                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16854                         len = bf_get(lpfc_rcqe_length,
16855                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
16856                         iocbq->context2 = d_buf;
16857                         iocbq->context3 = NULL;
16858                         iocbq->iocb.ulpBdeCount = 1;
16859                         if (len > LPFC_DATA_BUF_SIZE)
16860                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16861                                                         LPFC_DATA_BUF_SIZE;
16862                         else
16863                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16864
16865                         tot_len += len;
16866                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16867
16868                         iocbq->iocb.un.rcvels.remoteID = sid;
16869                         list_add_tail(&iocbq->list, &first_iocbq->list);
16870                 }
16871         }
16872         return first_iocbq;
16873 }
16874
16875 static void
16876 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16877                           struct hbq_dmabuf *seq_dmabuf)
16878 {
16879         struct fc_frame_header *fc_hdr;
16880         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16881         struct lpfc_hba *phba = vport->phba;
16882
16883         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16884         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16885         if (!iocbq) {
16886                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16887                                 "2707 Ring %d handler: Failed to allocate "
16888                                 "iocb Rctl x%x Type x%x received\n",
16889                                 LPFC_ELS_RING,
16890                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16891                 return;
16892         }
16893         if (!lpfc_complete_unsol_iocb(phba,
16894                                       phba->sli4_hba.els_wq->pring,
16895                                       iocbq, fc_hdr->fh_r_ctl,
16896                                       fc_hdr->fh_type))
16897                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16898                                 "2540 Ring %d handler: unexpected Rctl "
16899                                 "x%x Type x%x received\n",
16900                                 LPFC_ELS_RING,
16901                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16902
16903         /* Free iocb created in lpfc_prep_seq */
16904         list_for_each_entry_safe(curr_iocb, next_iocb,
16905                 &iocbq->list, list) {
16906                 list_del_init(&curr_iocb->list);
16907                 lpfc_sli_release_iocbq(phba, curr_iocb);
16908         }
16909         lpfc_sli_release_iocbq(phba, iocbq);
16910 }
16911
16912 /**
16913  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16914  * @phba: Pointer to HBA context object.
16915  *
16916  * This function is called with no lock held. This function processes all
16917  * the received buffers and gives it to upper layers when a received buffer
16918  * indicates that it is the final frame in the sequence. The interrupt
16919  * service routine processes received buffers at interrupt contexts.
16920  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16921  * appropriate receive function when the final frame in a sequence is received.
16922  **/
16923 void
16924 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16925                                  struct hbq_dmabuf *dmabuf)
16926 {
16927         struct hbq_dmabuf *seq_dmabuf;
16928         struct fc_frame_header *fc_hdr;
16929         struct lpfc_vport *vport;
16930         uint32_t fcfi;
16931         uint32_t did;
16932
16933         /* Process each received buffer */
16934         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16935
16936         /* check to see if this a valid type of frame */
16937         if (lpfc_fc_frame_check(phba, fc_hdr)) {
16938                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16939                 return;
16940         }
16941
16942         if ((bf_get(lpfc_cqe_code,
16943                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16944                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16945                               &dmabuf->cq_event.cqe.rcqe_cmpl);
16946         else
16947                 fcfi = bf_get(lpfc_rcqe_fcf_id,
16948                               &dmabuf->cq_event.cqe.rcqe_cmpl);
16949
16950         /* d_id this frame is directed to */
16951         did = sli4_did_from_fc_hdr(fc_hdr);
16952
16953         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
16954         if (!vport) {
16955                 /* throw out the frame */
16956                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16957                 return;
16958         }
16959
16960         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16961         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16962                 (did != Fabric_DID)) {
16963                 /*
16964                  * Throw out the frame if we are not pt2pt.
16965                  * The pt2pt protocol allows for discovery frames
16966                  * to be received without a registered VPI.
16967                  */
16968                 if (!(vport->fc_flag & FC_PT2PT) ||
16969                         (phba->link_state == LPFC_HBA_READY)) {
16970                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
16971                         return;
16972                 }
16973         }
16974
16975         /* Handle the basic abort sequence (BA_ABTS) event */
16976         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16977                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16978                 return;
16979         }
16980
16981         /* Link this frame */
16982         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
16983         if (!seq_dmabuf) {
16984                 /* unable to add frame to vport - throw it out */
16985                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16986                 return;
16987         }
16988         /* If not last frame in sequence continue processing frames. */
16989         if (!lpfc_seq_complete(seq_dmabuf))
16990                 return;
16991
16992         /* Send the complete sequence to the upper layer protocol */
16993         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
16994 }
16995
16996 /**
16997  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
16998  * @phba: pointer to lpfc hba data structure.
16999  *
17000  * This routine is invoked to post rpi header templates to the
17001  * HBA consistent with the SLI-4 interface spec.  This routine
17002  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17003  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17004  *
17005  * This routine does not require any locks.  It's usage is expected
17006  * to be driver load or reset recovery when the driver is
17007  * sequential.
17008  *
17009  * Return codes
17010  *      0 - successful
17011  *      -EIO - The mailbox failed to complete successfully.
17012  *      When this error occurs, the driver is not guaranteed
17013  *      to have any rpi regions posted to the device and
17014  *      must either attempt to repost the regions or take a
17015  *      fatal error.
17016  **/
17017 int
17018 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17019 {
17020         struct lpfc_rpi_hdr *rpi_page;
17021         uint32_t rc = 0;
17022         uint16_t lrpi = 0;
17023
17024         /* SLI4 ports that support extents do not require RPI headers. */
17025         if (!phba->sli4_hba.rpi_hdrs_in_use)
17026                 goto exit;
17027         if (phba->sli4_hba.extents_in_use)
17028                 return -EIO;
17029
17030         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
17031                 /*
17032                  * Assign the rpi headers a physical rpi only if the driver
17033                  * has not initialized those resources.  A port reset only
17034                  * needs the headers posted.
17035                  */
17036                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17037                     LPFC_RPI_RSRC_RDY)
17038                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17039
17040                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17041                 if (rc != MBX_SUCCESS) {
17042                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17043                                         "2008 Error %d posting all rpi "
17044                                         "headers\n", rc);
17045                         rc = -EIO;
17046                         break;
17047                 }
17048         }
17049
17050  exit:
17051         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17052                LPFC_RPI_RSRC_RDY);
17053         return rc;
17054 }
17055
17056 /**
17057  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17058  * @phba: pointer to lpfc hba data structure.
17059  * @rpi_page:  pointer to the rpi memory region.
17060  *
17061  * This routine is invoked to post a single rpi header to the
17062  * HBA consistent with the SLI-4 interface spec.  This memory region
17063  * maps up to 64 rpi context regions.
17064  *
17065  * Return codes
17066  *      0 - successful
17067  *      -ENOMEM - No available memory
17068  *      -EIO - The mailbox failed to complete successfully.
17069  **/
17070 int
17071 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17072 {
17073         LPFC_MBOXQ_t *mboxq;
17074         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17075         uint32_t rc = 0;
17076         uint32_t shdr_status, shdr_add_status;
17077         union lpfc_sli4_cfg_shdr *shdr;
17078
17079         /* SLI4 ports that support extents do not require RPI headers. */
17080         if (!phba->sli4_hba.rpi_hdrs_in_use)
17081                 return rc;
17082         if (phba->sli4_hba.extents_in_use)
17083                 return -EIO;
17084
17085         /* The port is notified of the header region via a mailbox command. */
17086         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17087         if (!mboxq) {
17088                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17089                                 "2001 Unable to allocate memory for issuing "
17090                                 "SLI_CONFIG_SPECIAL mailbox command\n");
17091                 return -ENOMEM;
17092         }
17093
17094         /* Post all rpi memory regions to the port. */
17095         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
17096         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17097                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17098                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
17099                          sizeof(struct lpfc_sli4_cfg_mhdr),
17100                          LPFC_SLI4_MBX_EMBED);
17101
17102
17103         /* Post the physical rpi to the port for this rpi header. */
17104         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17105                rpi_page->start_rpi);
17106         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17107                hdr_tmpl, rpi_page->page_count);
17108
17109         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17110         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
17111         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17112         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17113         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17114         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17115         if (rc != MBX_TIMEOUT)
17116                 mempool_free(mboxq, phba->mbox_mem_pool);
17117         if (shdr_status || shdr_add_status || rc) {
17118                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17119                                 "2514 POST_RPI_HDR mailbox failed with "
17120                                 "status x%x add_status x%x, mbx status x%x\n",
17121                                 shdr_status, shdr_add_status, rc);
17122                 rc = -ENXIO;
17123         }
17124         return rc;
17125 }
17126
17127 /**
17128  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17129  * @phba: pointer to lpfc hba data structure.
17130  *
17131  * This routine is invoked to post rpi header templates to the
17132  * HBA consistent with the SLI-4 interface spec.  This routine
17133  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17134  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17135  *
17136  * Returns
17137  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17138  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17139  **/
17140 int
17141 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17142 {
17143         unsigned long rpi;
17144         uint16_t max_rpi, rpi_limit;
17145         uint16_t rpi_remaining, lrpi = 0;
17146         struct lpfc_rpi_hdr *rpi_hdr;
17147         unsigned long iflag;
17148
17149         /*
17150          * Fetch the next logical rpi.  Because this index is logical,
17151          * the  driver starts at 0 each time.
17152          */
17153         spin_lock_irqsave(&phba->hbalock, iflag);
17154         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17155         rpi_limit = phba->sli4_hba.next_rpi;
17156
17157         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17158         if (rpi >= rpi_limit)
17159                 rpi = LPFC_RPI_ALLOC_ERROR;
17160         else {
17161                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17162                 phba->sli4_hba.max_cfg_param.rpi_used++;
17163                 phba->sli4_hba.rpi_count++;
17164         }
17165         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17166                         "0001 rpi:%x max:%x lim:%x\n",
17167                         (int) rpi, max_rpi, rpi_limit);
17168
17169         /*
17170          * Don't try to allocate more rpi header regions if the device limit
17171          * has been exhausted.
17172          */
17173         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17174             (phba->sli4_hba.rpi_count >= max_rpi)) {
17175                 spin_unlock_irqrestore(&phba->hbalock, iflag);
17176                 return rpi;
17177         }
17178
17179         /*
17180          * RPI header postings are not required for SLI4 ports capable of
17181          * extents.
17182          */
17183         if (!phba->sli4_hba.rpi_hdrs_in_use) {
17184                 spin_unlock_irqrestore(&phba->hbalock, iflag);
17185                 return rpi;
17186         }
17187
17188         /*
17189          * If the driver is running low on rpi resources, allocate another
17190          * page now.  Note that the next_rpi value is used because
17191          * it represents how many are actually in use whereas max_rpi notes
17192          * how many are supported max by the device.
17193          */
17194         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
17195         spin_unlock_irqrestore(&phba->hbalock, iflag);
17196         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17197                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17198                 if (!rpi_hdr) {
17199                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17200                                         "2002 Error Could not grow rpi "
17201                                         "count\n");
17202                 } else {
17203                         lrpi = rpi_hdr->start_rpi;
17204                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17205                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17206                 }
17207         }
17208
17209         return rpi;
17210 }
17211
17212 /**
17213  * lpfc_sli4_free_rpi - Release an rpi for reuse.
17214  * @phba: pointer to lpfc hba data structure.
17215  *
17216  * This routine is invoked to release an rpi to the pool of
17217  * available rpis maintained by the driver.
17218  **/
17219 static void
17220 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17221 {
17222         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17223                 phba->sli4_hba.rpi_count--;
17224                 phba->sli4_hba.max_cfg_param.rpi_used--;
17225         }
17226 }
17227
17228 /**
17229  * lpfc_sli4_free_rpi - Release an rpi for reuse.
17230  * @phba: pointer to lpfc hba data structure.
17231  *
17232  * This routine is invoked to release an rpi to the pool of
17233  * available rpis maintained by the driver.
17234  **/
17235 void
17236 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17237 {
17238         spin_lock_irq(&phba->hbalock);
17239         __lpfc_sli4_free_rpi(phba, rpi);
17240         spin_unlock_irq(&phba->hbalock);
17241 }
17242
17243 /**
17244  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17245  * @phba: pointer to lpfc hba data structure.
17246  *
17247  * This routine is invoked to remove the memory region that
17248  * provided rpi via a bitmask.
17249  **/
17250 void
17251 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17252 {
17253         kfree(phba->sli4_hba.rpi_bmask);
17254         kfree(phba->sli4_hba.rpi_ids);
17255         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
17256 }
17257
17258 /**
17259  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17260  * @phba: pointer to lpfc hba data structure.
17261  *
17262  * This routine is invoked to remove the memory region that
17263  * provided rpi via a bitmask.
17264  **/
17265 int
17266 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17267         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
17268 {
17269         LPFC_MBOXQ_t *mboxq;
17270         struct lpfc_hba *phba = ndlp->phba;
17271         int rc;
17272
17273         /* The port is notified of the header region via a mailbox command. */
17274         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17275         if (!mboxq)
17276                 return -ENOMEM;
17277
17278         /* Post all rpi memory regions to the port. */
17279         lpfc_resume_rpi(mboxq, ndlp);
17280         if (cmpl) {
17281                 mboxq->mbox_cmpl = cmpl;
17282                 mboxq->context1 = arg;
17283                 mboxq->context2 = ndlp;
17284         } else
17285                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17286         mboxq->vport = ndlp->vport;
17287         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17288         if (rc == MBX_NOT_FINISHED) {
17289                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17290                                 "2010 Resume RPI Mailbox failed "
17291                                 "status %d, mbxStatus x%x\n", rc,
17292                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17293                 mempool_free(mboxq, phba->mbox_mem_pool);
17294                 return -EIO;
17295         }
17296         return 0;
17297 }
17298
17299 /**
17300  * lpfc_sli4_init_vpi - Initialize a vpi with the port
17301  * @vport: Pointer to the vport for which the vpi is being initialized
17302  *
17303  * This routine is invoked to activate a vpi with the port.
17304  *
17305  * Returns:
17306  *    0 success
17307  *    -Evalue otherwise
17308  **/
17309 int
17310 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
17311 {
17312         LPFC_MBOXQ_t *mboxq;
17313         int rc = 0;
17314         int retval = MBX_SUCCESS;
17315         uint32_t mbox_tmo;
17316         struct lpfc_hba *phba = vport->phba;
17317         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17318         if (!mboxq)
17319                 return -ENOMEM;
17320         lpfc_init_vpi(phba, mboxq, vport->vpi);
17321         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
17322         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
17323         if (rc != MBX_SUCCESS) {
17324                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
17325                                 "2022 INIT VPI Mailbox failed "
17326                                 "status %d, mbxStatus x%x\n", rc,
17327                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17328                 retval = -EIO;
17329         }
17330         if (rc != MBX_TIMEOUT)
17331                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
17332
17333         return retval;
17334 }
17335
17336 /**
17337  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17338  * @phba: pointer to lpfc hba data structure.
17339  * @mboxq: Pointer to mailbox object.
17340  *
17341  * This routine is invoked to manually add a single FCF record. The caller
17342  * must pass a completely initialized FCF_Record.  This routine takes
17343  * care of the nonembedded mailbox operations.
17344  **/
17345 static void
17346 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17347 {
17348         void *virt_addr;
17349         union lpfc_sli4_cfg_shdr *shdr;
17350         uint32_t shdr_status, shdr_add_status;
17351
17352         virt_addr = mboxq->sge_array->addr[0];
17353         /* The IOCTL status is embedded in the mailbox subheader. */
17354         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17355         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17356         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17357
17358         if ((shdr_status || shdr_add_status) &&
17359                 (shdr_status != STATUS_FCF_IN_USE))
17360                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17361                         "2558 ADD_FCF_RECORD mailbox failed with "
17362                         "status x%x add_status x%x\n",
17363                         shdr_status, shdr_add_status);
17364
17365         lpfc_sli4_mbox_cmd_free(phba, mboxq);
17366 }
17367
17368 /**
17369  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17370  * @phba: pointer to lpfc hba data structure.
17371  * @fcf_record:  pointer to the initialized fcf record to add.
17372  *
17373  * This routine is invoked to manually add a single FCF record. The caller
17374  * must pass a completely initialized FCF_Record.  This routine takes
17375  * care of the nonembedded mailbox operations.
17376  **/
17377 int
17378 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17379 {
17380         int rc = 0;
17381         LPFC_MBOXQ_t *mboxq;
17382         uint8_t *bytep;
17383         void *virt_addr;
17384         struct lpfc_mbx_sge sge;
17385         uint32_t alloc_len, req_len;
17386         uint32_t fcfindex;
17387
17388         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17389         if (!mboxq) {
17390                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17391                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17392                 return -ENOMEM;
17393         }
17394
17395         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17396                   sizeof(uint32_t);
17397
17398         /* Allocate DMA memory and set up the non-embedded mailbox command */
17399         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17400                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17401                                      req_len, LPFC_SLI4_MBX_NEMBED);
17402         if (alloc_len < req_len) {
17403                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17404                         "2523 Allocated DMA memory size (x%x) is "
17405                         "less than the requested DMA memory "
17406                         "size (x%x)\n", alloc_len, req_len);
17407                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17408                 return -ENOMEM;
17409         }
17410
17411         /*
17412          * Get the first SGE entry from the non-embedded DMA memory.  This
17413          * routine only uses a single SGE.
17414          */
17415         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
17416         virt_addr = mboxq->sge_array->addr[0];
17417         /*
17418          * Configure the FCF record for FCFI 0.  This is the driver's
17419          * hardcoded default and gets used in nonFIP mode.
17420          */
17421         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17422         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17423         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17424
17425         /*
17426          * Copy the fcf_index and the FCF Record Data. The data starts after
17427          * the FCoE header plus word10. The data copy needs to be endian
17428          * correct.
17429          */
17430         bytep += sizeof(uint32_t);
17431         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17432         mboxq->vport = phba->pport;
17433         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17434         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17435         if (rc == MBX_NOT_FINISHED) {
17436                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17437                         "2515 ADD_FCF_RECORD mailbox failed with "
17438                         "status 0x%x\n", rc);
17439                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17440                 rc = -EIO;
17441         } else
17442                 rc = 0;
17443
17444         return rc;
17445 }
17446
17447 /**
17448  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17449  * @phba: pointer to lpfc hba data structure.
17450  * @fcf_record:  pointer to the fcf record to write the default data.
17451  * @fcf_index: FCF table entry index.
17452  *
17453  * This routine is invoked to build the driver's default FCF record.  The
17454  * values used are hardcoded.  This routine handles memory initialization.
17455  *
17456  **/
17457 void
17458 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17459                                 struct fcf_record *fcf_record,
17460                                 uint16_t fcf_index)
17461 {
17462         memset(fcf_record, 0, sizeof(struct fcf_record));
17463         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17464         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17465         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17466         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17467         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17468         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17469         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17470         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17471         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17472         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17473         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17474         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17475         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
17476         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
17477         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17478         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17479                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17480         /* Set the VLAN bit map */
17481         if (phba->valid_vlan) {
17482                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17483                         = 1 << (phba->vlan_id % 8);
17484         }
17485 }
17486
17487 /**
17488  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
17489  * @phba: pointer to lpfc hba data structure.
17490  * @fcf_index: FCF table entry offset.
17491  *
17492  * This routine is invoked to scan the entire FCF table by reading FCF
17493  * record and processing it one at a time starting from the @fcf_index
17494  * for initial FCF discovery or fast FCF failover rediscovery.
17495  *
17496  * Return 0 if the mailbox command is submitted successfully, none 0
17497  * otherwise.
17498  **/
17499 int
17500 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17501 {
17502         int rc = 0, error;
17503         LPFC_MBOXQ_t *mboxq;
17504
17505         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
17506         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
17507         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17508         if (!mboxq) {
17509                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17510                                 "2000 Failed to allocate mbox for "
17511                                 "READ_FCF cmd\n");
17512                 error = -ENOMEM;
17513                 goto fail_fcf_scan;
17514         }
17515         /* Construct the read FCF record mailbox command */
17516         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17517         if (rc) {
17518                 error = -EINVAL;
17519                 goto fail_fcf_scan;
17520         }
17521         /* Issue the mailbox command asynchronously */
17522         mboxq->vport = phba->pport;
17523         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
17524
17525         spin_lock_irq(&phba->hbalock);
17526         phba->hba_flag |= FCF_TS_INPROG;
17527         spin_unlock_irq(&phba->hbalock);
17528
17529         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17530         if (rc == MBX_NOT_FINISHED)
17531                 error = -EIO;
17532         else {
17533                 /* Reset eligible FCF count for new scan */
17534                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
17535                         phba->fcf.eligible_fcf_cnt = 0;
17536                 error = 0;
17537         }
17538 fail_fcf_scan:
17539         if (error) {
17540                 if (mboxq)
17541                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
17542                 /* FCF scan failed, clear FCF_TS_INPROG flag */
17543                 spin_lock_irq(&phba->hbalock);
17544                 phba->hba_flag &= ~FCF_TS_INPROG;
17545                 spin_unlock_irq(&phba->hbalock);
17546         }
17547         return error;
17548 }
17549
17550 /**
17551  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
17552  * @phba: pointer to lpfc hba data structure.
17553  * @fcf_index: FCF table entry offset.
17554  *
17555  * This routine is invoked to read an FCF record indicated by @fcf_index
17556  * and to use it for FLOGI roundrobin FCF failover.
17557  *
17558  * Return 0 if the mailbox command is submitted successfully, none 0
17559  * otherwise.
17560  **/
17561 int
17562 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17563 {
17564         int rc = 0, error;
17565         LPFC_MBOXQ_t *mboxq;
17566
17567         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17568         if (!mboxq) {
17569                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17570                                 "2763 Failed to allocate mbox for "
17571                                 "READ_FCF cmd\n");
17572                 error = -ENOMEM;
17573                 goto fail_fcf_read;
17574         }
17575         /* Construct the read FCF record mailbox command */
17576         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17577         if (rc) {
17578                 error = -EINVAL;
17579                 goto fail_fcf_read;
17580         }
17581         /* Issue the mailbox command asynchronously */
17582         mboxq->vport = phba->pport;
17583         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17584         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17585         if (rc == MBX_NOT_FINISHED)
17586                 error = -EIO;
17587         else
17588                 error = 0;
17589
17590 fail_fcf_read:
17591         if (error && mboxq)
17592                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17593         return error;
17594 }
17595
17596 /**
17597  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17598  * @phba: pointer to lpfc hba data structure.
17599  * @fcf_index: FCF table entry offset.
17600  *
17601  * This routine is invoked to read an FCF record indicated by @fcf_index to
17602  * determine whether it's eligible for FLOGI roundrobin failover list.
17603  *
17604  * Return 0 if the mailbox command is submitted successfully, none 0
17605  * otherwise.
17606  **/
17607 int
17608 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17609 {
17610         int rc = 0, error;
17611         LPFC_MBOXQ_t *mboxq;
17612
17613         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17614         if (!mboxq) {
17615                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17616                                 "2758 Failed to allocate mbox for "
17617                                 "READ_FCF cmd\n");
17618                                 error = -ENOMEM;
17619                                 goto fail_fcf_read;
17620         }
17621         /* Construct the read FCF record mailbox command */
17622         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17623         if (rc) {
17624                 error = -EINVAL;
17625                 goto fail_fcf_read;
17626         }
17627         /* Issue the mailbox command asynchronously */
17628         mboxq->vport = phba->pport;
17629         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17630         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17631         if (rc == MBX_NOT_FINISHED)
17632                 error = -EIO;
17633         else
17634                 error = 0;
17635
17636 fail_fcf_read:
17637         if (error && mboxq)
17638                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17639         return error;
17640 }
17641
17642 /**
17643  * lpfc_check_next_fcf_pri_level
17644  * phba pointer to the lpfc_hba struct for this port.
17645  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17646  * routine when the rr_bmask is empty. The FCF indecies are put into the
17647  * rr_bmask based on their priority level. Starting from the highest priority
17648  * to the lowest. The most likely FCF candidate will be in the highest
17649  * priority group. When this routine is called it searches the fcf_pri list for
17650  * next lowest priority group and repopulates the rr_bmask with only those
17651  * fcf_indexes.
17652  * returns:
17653  * 1=success 0=failure
17654  **/
17655 static int
17656 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17657 {
17658         uint16_t next_fcf_pri;
17659         uint16_t last_index;
17660         struct lpfc_fcf_pri *fcf_pri;
17661         int rc;
17662         int ret = 0;
17663
17664         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17665                         LPFC_SLI4_FCF_TBL_INDX_MAX);
17666         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17667                         "3060 Last IDX %d\n", last_index);
17668
17669         /* Verify the priority list has 2 or more entries */
17670         spin_lock_irq(&phba->hbalock);
17671         if (list_empty(&phba->fcf.fcf_pri_list) ||
17672             list_is_singular(&phba->fcf.fcf_pri_list)) {
17673                 spin_unlock_irq(&phba->hbalock);
17674                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17675                         "3061 Last IDX %d\n", last_index);
17676                 return 0; /* Empty rr list */
17677         }
17678         spin_unlock_irq(&phba->hbalock);
17679
17680         next_fcf_pri = 0;
17681         /*
17682          * Clear the rr_bmask and set all of the bits that are at this
17683          * priority.
17684          */
17685         memset(phba->fcf.fcf_rr_bmask, 0,
17686                         sizeof(*phba->fcf.fcf_rr_bmask));
17687         spin_lock_irq(&phba->hbalock);
17688         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17689                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17690                         continue;
17691                 /*
17692                  * the 1st priority that has not FLOGI failed
17693                  * will be the highest.
17694                  */
17695                 if (!next_fcf_pri)
17696                         next_fcf_pri = fcf_pri->fcf_rec.priority;
17697                 spin_unlock_irq(&phba->hbalock);
17698                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17699                         rc = lpfc_sli4_fcf_rr_index_set(phba,
17700                                                 fcf_pri->fcf_rec.fcf_index);
17701                         if (rc)
17702                                 return 0;
17703                 }
17704                 spin_lock_irq(&phba->hbalock);
17705         }
17706         /*
17707          * if next_fcf_pri was not set above and the list is not empty then
17708          * we have failed flogis on all of them. So reset flogi failed
17709          * and start at the beginning.
17710          */
17711         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17712                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17713                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17714                         /*
17715                          * the 1st priority that has not FLOGI failed
17716                          * will be the highest.
17717                          */
17718                         if (!next_fcf_pri)
17719                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
17720                         spin_unlock_irq(&phba->hbalock);
17721                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17722                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
17723                                                 fcf_pri->fcf_rec.fcf_index);
17724                                 if (rc)
17725                                         return 0;
17726                         }
17727                         spin_lock_irq(&phba->hbalock);
17728                 }
17729         } else
17730                 ret = 1;
17731         spin_unlock_irq(&phba->hbalock);
17732
17733         return ret;
17734 }
17735 /**
17736  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17737  * @phba: pointer to lpfc hba data structure.
17738  *
17739  * This routine is to get the next eligible FCF record index in a round
17740  * robin fashion. If the next eligible FCF record index equals to the
17741  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
17742  * shall be returned, otherwise, the next eligible FCF record's index
17743  * shall be returned.
17744  **/
17745 uint16_t
17746 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17747 {
17748         uint16_t next_fcf_index;
17749
17750 initial_priority:
17751         /* Search start from next bit of currently registered FCF index */
17752         next_fcf_index = phba->fcf.current_rec.fcf_indx;
17753
17754 next_priority:
17755         /* Determine the next fcf index to check */
17756         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
17757         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17758                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
17759                                        next_fcf_index);
17760
17761         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
17762         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17763                 /*
17764                  * If we have wrapped then we need to clear the bits that
17765                  * have been tested so that we can detect when we should
17766                  * change the priority level.
17767                  */
17768                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17769                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
17770         }
17771
17772
17773         /* Check roundrobin failover list empty condition */
17774         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17775                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17776                 /*
17777                  * If next fcf index is not found check if there are lower
17778                  * Priority level fcf's in the fcf_priority list.
17779                  * Set up the rr_bmask with all of the avaiable fcf bits
17780                  * at that level and continue the selection process.
17781                  */
17782                 if (lpfc_check_next_fcf_pri_level(phba))
17783                         goto initial_priority;
17784                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17785                                 "2844 No roundrobin failover FCF available\n");
17786                 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17787                         return LPFC_FCOE_FCF_NEXT_NONE;
17788                 else {
17789                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17790                                 "3063 Only FCF available idx %d, flag %x\n",
17791                                 next_fcf_index,
17792                         phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17793                         return next_fcf_index;
17794                 }
17795         }
17796
17797         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17798                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
17799                 LPFC_FCF_FLOGI_FAILED) {
17800                 if (list_is_singular(&phba->fcf.fcf_pri_list))
17801                         return LPFC_FCOE_FCF_NEXT_NONE;
17802
17803                 goto next_priority;
17804         }
17805
17806         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17807                         "2845 Get next roundrobin failover FCF (x%x)\n",
17808                         next_fcf_index);
17809
17810         return next_fcf_index;
17811 }
17812
17813 /**
17814  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17815  * @phba: pointer to lpfc hba data structure.
17816  *
17817  * This routine sets the FCF record index in to the eligible bmask for
17818  * roundrobin failover search. It checks to make sure that the index
17819  * does not go beyond the range of the driver allocated bmask dimension
17820  * before setting the bit.
17821  *
17822  * Returns 0 if the index bit successfully set, otherwise, it returns
17823  * -EINVAL.
17824  **/
17825 int
17826 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17827 {
17828         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17829                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17830                                 "2610 FCF (x%x) reached driver's book "
17831                                 "keeping dimension:x%x\n",
17832                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17833                 return -EINVAL;
17834         }
17835         /* Set the eligible FCF record index bmask */
17836         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17837
17838         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17839                         "2790 Set FCF (x%x) to roundrobin FCF failover "
17840                         "bmask\n", fcf_index);
17841
17842         return 0;
17843 }
17844
17845 /**
17846  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
17847  * @phba: pointer to lpfc hba data structure.
17848  *
17849  * This routine clears the FCF record index from the eligible bmask for
17850  * roundrobin failover search. It checks to make sure that the index
17851  * does not go beyond the range of the driver allocated bmask dimension
17852  * before clearing the bit.
17853  **/
17854 void
17855 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17856 {
17857         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
17858         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17859                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17860                                 "2762 FCF (x%x) reached driver's book "
17861                                 "keeping dimension:x%x\n",
17862                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17863                 return;
17864         }
17865         /* Clear the eligible FCF record index bmask */
17866         spin_lock_irq(&phba->hbalock);
17867         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17868                                  list) {
17869                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17870                         list_del_init(&fcf_pri->list);
17871                         break;
17872                 }
17873         }
17874         spin_unlock_irq(&phba->hbalock);
17875         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17876
17877         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17878                         "2791 Clear FCF (x%x) from roundrobin failover "
17879                         "bmask\n", fcf_index);
17880 }
17881
17882 /**
17883  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17884  * @phba: pointer to lpfc hba data structure.
17885  *
17886  * This routine is the completion routine for the rediscover FCF table mailbox
17887  * command. If the mailbox command returned failure, it will try to stop the
17888  * FCF rediscover wait timer.
17889  **/
17890 static void
17891 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17892 {
17893         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17894         uint32_t shdr_status, shdr_add_status;
17895
17896         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17897
17898         shdr_status = bf_get(lpfc_mbox_hdr_status,
17899                              &redisc_fcf->header.cfg_shdr.response);
17900         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17901                              &redisc_fcf->header.cfg_shdr.response);
17902         if (shdr_status || shdr_add_status) {
17903                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17904                                 "2746 Requesting for FCF rediscovery failed "
17905                                 "status x%x add_status x%x\n",
17906                                 shdr_status, shdr_add_status);
17907                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
17908                         spin_lock_irq(&phba->hbalock);
17909                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
17910                         spin_unlock_irq(&phba->hbalock);
17911                         /*
17912                          * CVL event triggered FCF rediscover request failed,
17913                          * last resort to re-try current registered FCF entry.
17914                          */
17915                         lpfc_retry_pport_discovery(phba);
17916                 } else {
17917                         spin_lock_irq(&phba->hbalock);
17918                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
17919                         spin_unlock_irq(&phba->hbalock);
17920                         /*
17921                          * DEAD FCF event triggered FCF rediscover request
17922                          * failed, last resort to fail over as a link down
17923                          * to FCF registration.
17924                          */
17925                         lpfc_sli4_fcf_dead_failthrough(phba);
17926                 }
17927         } else {
17928                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17929                                 "2775 Start FCF rediscover quiescent timer\n");
17930                 /*
17931                  * Start FCF rediscovery wait timer for pending FCF
17932                  * before rescan FCF record table.
17933                  */
17934                 lpfc_fcf_redisc_wait_start_timer(phba);
17935         }
17936
17937         mempool_free(mbox, phba->mbox_mem_pool);
17938 }
17939
17940 /**
17941  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
17942  * @phba: pointer to lpfc hba data structure.
17943  *
17944  * This routine is invoked to request for rediscovery of the entire FCF table
17945  * by the port.
17946  **/
17947 int
17948 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17949 {
17950         LPFC_MBOXQ_t *mbox;
17951         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17952         int rc, length;
17953
17954         /* Cancel retry delay timers to all vports before FCF rediscover */
17955         lpfc_cancel_all_vport_retry_delay_timer(phba);
17956
17957         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17958         if (!mbox) {
17959                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17960                                 "2745 Failed to allocate mbox for "
17961                                 "requesting FCF rediscover.\n");
17962                 return -ENOMEM;
17963         }
17964
17965         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17966                   sizeof(struct lpfc_sli4_cfg_mhdr));
17967         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17968                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17969                          length, LPFC_SLI4_MBX_EMBED);
17970
17971         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17972         /* Set count to 0 for invalidating the entire FCF database */
17973         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17974
17975         /* Issue the mailbox command asynchronously */
17976         mbox->vport = phba->pport;
17977         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17978         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17979
17980         if (rc == MBX_NOT_FINISHED) {
17981                 mempool_free(mbox, phba->mbox_mem_pool);
17982                 return -EIO;
17983         }
17984         return 0;
17985 }
17986
17987 /**
17988  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
17989  * @phba: pointer to lpfc hba data structure.
17990  *
17991  * This function is the failover routine as a last resort to the FCF DEAD
17992  * event when driver failed to perform fast FCF failover.
17993  **/
17994 void
17995 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
17996 {
17997         uint32_t link_state;
17998
17999         /*
18000          * Last resort as FCF DEAD event failover will treat this as
18001          * a link down, but save the link state because we don't want
18002          * it to be changed to Link Down unless it is already down.
18003          */
18004         link_state = phba->link_state;
18005         lpfc_linkdown(phba);
18006         phba->link_state = link_state;
18007
18008         /* Unregister FCF if no devices connected to it */
18009         lpfc_unregister_unused_fcf(phba);
18010 }
18011
18012 /**
18013  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
18014  * @phba: pointer to lpfc hba data structure.
18015  * @rgn23_data: pointer to configure region 23 data.
18016  *
18017  * This function gets SLI3 port configure region 23 data through memory dump
18018  * mailbox command. When it successfully retrieves data, the size of the data
18019  * will be returned, otherwise, 0 will be returned.
18020  **/
18021 static uint32_t
18022 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18023 {
18024         LPFC_MBOXQ_t *pmb = NULL;
18025         MAILBOX_t *mb;
18026         uint32_t offset = 0;
18027         int rc;
18028
18029         if (!rgn23_data)
18030                 return 0;
18031
18032         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18033         if (!pmb) {
18034                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18035                                 "2600 failed to allocate mailbox memory\n");
18036                 return 0;
18037         }
18038         mb = &pmb->u.mb;
18039
18040         do {
18041                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18042                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18043
18044                 if (rc != MBX_SUCCESS) {
18045                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
18046                                         "2601 failed to read config "
18047                                         "region 23, rc 0x%x Status 0x%x\n",
18048                                         rc, mb->mbxStatus);
18049                         mb->un.varDmp.word_cnt = 0;
18050                 }
18051                 /*
18052                  * dump mem may return a zero when finished or we got a
18053                  * mailbox error, either way we are done.
18054                  */
18055                 if (mb->un.varDmp.word_cnt == 0)
18056                         break;
18057                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18058                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18059
18060                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18061                                        rgn23_data + offset,
18062                                        mb->un.varDmp.word_cnt);
18063                 offset += mb->un.varDmp.word_cnt;
18064         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18065
18066         mempool_free(pmb, phba->mbox_mem_pool);
18067         return offset;
18068 }
18069
18070 /**
18071  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18072  * @phba: pointer to lpfc hba data structure.
18073  * @rgn23_data: pointer to configure region 23 data.
18074  *
18075  * This function gets SLI4 port configure region 23 data through memory dump
18076  * mailbox command. When it successfully retrieves data, the size of the data
18077  * will be returned, otherwise, 0 will be returned.
18078  **/
18079 static uint32_t
18080 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18081 {
18082         LPFC_MBOXQ_t *mboxq = NULL;
18083         struct lpfc_dmabuf *mp = NULL;
18084         struct lpfc_mqe *mqe;
18085         uint32_t data_length = 0;
18086         int rc;
18087
18088         if (!rgn23_data)
18089                 return 0;
18090
18091         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18092         if (!mboxq) {
18093                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18094                                 "3105 failed to allocate mailbox memory\n");
18095                 return 0;
18096         }
18097
18098         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18099                 goto out;
18100         mqe = &mboxq->u.mqe;
18101         mp = (struct lpfc_dmabuf *) mboxq->context1;
18102         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18103         if (rc)
18104                 goto out;
18105         data_length = mqe->un.mb_words[5];
18106         if (data_length == 0)
18107                 goto out;
18108         if (data_length > DMP_RGN23_SIZE) {
18109                 data_length = 0;
18110                 goto out;
18111         }
18112         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18113 out:
18114         mempool_free(mboxq, phba->mbox_mem_pool);
18115         if (mp) {
18116                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18117                 kfree(mp);
18118         }
18119         return data_length;
18120 }
18121
18122 /**
18123  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18124  * @phba: pointer to lpfc hba data structure.
18125  *
18126  * This function read region 23 and parse TLV for port status to
18127  * decide if the user disaled the port. If the TLV indicates the
18128  * port is disabled, the hba_flag is set accordingly.
18129  **/
18130 void
18131 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18132 {
18133         uint8_t *rgn23_data = NULL;
18134         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18135         uint32_t offset = 0;
18136
18137         /* Get adapter Region 23 data */
18138         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18139         if (!rgn23_data)
18140                 goto out;
18141
18142         if (phba->sli_rev < LPFC_SLI_REV4)
18143                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18144         else {
18145                 if_type = bf_get(lpfc_sli_intf_if_type,
18146                                  &phba->sli4_hba.sli_intf);
18147                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18148                         goto out;
18149                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18150         }
18151
18152         if (!data_size)
18153                 goto out;
18154
18155         /* Check the region signature first */
18156         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18157                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18158                         "2619 Config region 23 has bad signature\n");
18159                         goto out;
18160         }
18161         offset += 4;
18162
18163         /* Check the data structure version */
18164         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18165                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18166                         "2620 Config region 23 has bad version\n");
18167                 goto out;
18168         }
18169         offset += 4;
18170
18171         /* Parse TLV entries in the region */
18172         while (offset < data_size) {
18173                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18174                         break;
18175                 /*
18176                  * If the TLV is not driver specific TLV or driver id is
18177                  * not linux driver id, skip the record.
18178                  */
18179                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18180                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18181                     (rgn23_data[offset + 3] != 0)) {
18182                         offset += rgn23_data[offset + 1] * 4 + 4;
18183                         continue;
18184                 }
18185
18186                 /* Driver found a driver specific TLV in the config region */
18187                 sub_tlv_len = rgn23_data[offset + 1] * 4;
18188                 offset += 4;
18189                 tlv_offset = 0;
18190
18191                 /*
18192                  * Search for configured port state sub-TLV.
18193                  */
18194                 while ((offset < data_size) &&
18195                         (tlv_offset < sub_tlv_len)) {
18196                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18197                                 offset += 4;
18198                                 tlv_offset += 4;
18199                                 break;
18200                         }
18201                         if (rgn23_data[offset] != PORT_STE_TYPE) {
18202                                 offset += rgn23_data[offset + 1] * 4 + 4;
18203                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18204                                 continue;
18205                         }
18206
18207                         /* This HBA contains PORT_STE configured */
18208                         if (!rgn23_data[offset + 2])
18209                                 phba->hba_flag |= LINK_DISABLED;
18210
18211                         goto out;
18212                 }
18213         }
18214
18215 out:
18216         kfree(rgn23_data);
18217         return;
18218 }
18219
18220 /**
18221  * lpfc_wr_object - write an object to the firmware
18222  * @phba: HBA structure that indicates port to create a queue on.
18223  * @dmabuf_list: list of dmabufs to write to the port.
18224  * @size: the total byte value of the objects to write to the port.
18225  * @offset: the current offset to be used to start the transfer.
18226  *
18227  * This routine will create a wr_object mailbox command to send to the port.
18228  * the mailbox command will be constructed using the dma buffers described in
18229  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18230  * BDEs that the imbedded mailbox can support. The @offset variable will be
18231  * used to indicate the starting offset of the transfer and will also return
18232  * the offset after the write object mailbox has completed. @size is used to
18233  * determine the end of the object and whether the eof bit should be set.
18234  *
18235  * Return 0 is successful and offset will contain the the new offset to use
18236  * for the next write.
18237  * Return negative value for error cases.
18238  **/
18239 int
18240 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18241                uint32_t size, uint32_t *offset)
18242 {
18243         struct lpfc_mbx_wr_object *wr_object;
18244         LPFC_MBOXQ_t *mbox;
18245         int rc = 0, i = 0;
18246         uint32_t shdr_status, shdr_add_status;
18247         uint32_t mbox_tmo;
18248         union lpfc_sli4_cfg_shdr *shdr;
18249         struct lpfc_dmabuf *dmabuf;
18250         uint32_t written = 0;
18251
18252         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18253         if (!mbox)
18254                 return -ENOMEM;
18255
18256         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18257                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
18258                         sizeof(struct lpfc_mbx_wr_object) -
18259                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18260
18261         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18262         wr_object->u.request.write_offset = *offset;
18263         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18264         wr_object->u.request.object_name[0] =
18265                 cpu_to_le32(wr_object->u.request.object_name[0]);
18266         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18267         list_for_each_entry(dmabuf, dmabuf_list, list) {
18268                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18269                         break;
18270                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18271                 wr_object->u.request.bde[i].addrHigh =
18272                         putPaddrHigh(dmabuf->phys);
18273                 if (written + SLI4_PAGE_SIZE >= size) {
18274                         wr_object->u.request.bde[i].tus.f.bdeSize =
18275                                 (size - written);
18276                         written += (size - written);
18277                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18278                 } else {
18279                         wr_object->u.request.bde[i].tus.f.bdeSize =
18280                                 SLI4_PAGE_SIZE;
18281                         written += SLI4_PAGE_SIZE;
18282                 }
18283                 i++;
18284         }
18285         wr_object->u.request.bde_count = i;
18286         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18287         if (!phba->sli4_hba.intr_enable)
18288                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18289         else {
18290                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18291                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18292         }
18293         /* The IOCTL status is embedded in the mailbox subheader. */
18294         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18295         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18296         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18297         if (rc != MBX_TIMEOUT)
18298                 mempool_free(mbox, phba->mbox_mem_pool);
18299         if (shdr_status || shdr_add_status || rc) {
18300                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18301                                 "3025 Write Object mailbox failed with "
18302                                 "status x%x add_status x%x, mbx status x%x\n",
18303                                 shdr_status, shdr_add_status, rc);
18304                 rc = -ENXIO;
18305         } else
18306                 *offset += wr_object->u.response.actual_write_length;
18307         return rc;
18308 }
18309
18310 /**
18311  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18312  * @vport: pointer to vport data structure.
18313  *
18314  * This function iterate through the mailboxq and clean up all REG_LOGIN
18315  * and REG_VPI mailbox commands associated with the vport. This function
18316  * is called when driver want to restart discovery of the vport due to
18317  * a Clear Virtual Link event.
18318  **/
18319 void
18320 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18321 {
18322         struct lpfc_hba *phba = vport->phba;
18323         LPFC_MBOXQ_t *mb, *nextmb;
18324         struct lpfc_dmabuf *mp;
18325         struct lpfc_nodelist *ndlp;
18326         struct lpfc_nodelist *act_mbx_ndlp = NULL;
18327         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
18328         LIST_HEAD(mbox_cmd_list);
18329         uint8_t restart_loop;
18330
18331         /* Clean up internally queued mailbox commands with the vport */
18332         spin_lock_irq(&phba->hbalock);
18333         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18334                 if (mb->vport != vport)
18335                         continue;
18336
18337                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18338                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
18339                         continue;
18340
18341                 list_del(&mb->list);
18342                 list_add_tail(&mb->list, &mbox_cmd_list);
18343         }
18344         /* Clean up active mailbox command with the vport */
18345         mb = phba->sli.mbox_active;
18346         if (mb && (mb->vport == vport)) {
18347                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18348                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
18349                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18350                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18351                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18352                         /* Put reference count for delayed processing */
18353                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18354                         /* Unregister the RPI when mailbox complete */
18355                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18356                 }
18357         }
18358         /* Cleanup any mailbox completions which are not yet processed */
18359         do {
18360                 restart_loop = 0;
18361                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18362                         /*
18363                          * If this mailox is already processed or it is
18364                          * for another vport ignore it.
18365                          */
18366                         if ((mb->vport != vport) ||
18367                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18368                                 continue;
18369
18370                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18371                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18372                                 continue;
18373
18374                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18375                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18376                                 ndlp = (struct lpfc_nodelist *)mb->context2;
18377                                 /* Unregister the RPI when mailbox complete */
18378                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18379                                 restart_loop = 1;
18380                                 spin_unlock_irq(&phba->hbalock);
18381                                 spin_lock(shost->host_lock);
18382                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18383                                 spin_unlock(shost->host_lock);
18384                                 spin_lock_irq(&phba->hbalock);
18385                                 break;
18386                         }
18387                 }
18388         } while (restart_loop);
18389
18390         spin_unlock_irq(&phba->hbalock);
18391
18392         /* Release the cleaned-up mailbox commands */
18393         while (!list_empty(&mbox_cmd_list)) {
18394                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
18395                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18396                         mp = (struct lpfc_dmabuf *) (mb->context1);
18397                         if (mp) {
18398                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18399                                 kfree(mp);
18400                         }
18401                         ndlp = (struct lpfc_nodelist *) mb->context2;
18402                         mb->context2 = NULL;
18403                         if (ndlp) {
18404                                 spin_lock(shost->host_lock);
18405                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18406                                 spin_unlock(shost->host_lock);
18407                                 lpfc_nlp_put(ndlp);
18408                         }
18409                 }
18410                 mempool_free(mb, phba->mbox_mem_pool);
18411         }
18412
18413         /* Release the ndlp with the cleaned-up active mailbox command */
18414         if (act_mbx_ndlp) {
18415                 spin_lock(shost->host_lock);
18416                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18417                 spin_unlock(shost->host_lock);
18418                 lpfc_nlp_put(act_mbx_ndlp);
18419         }
18420 }
18421
18422 /**
18423  * lpfc_drain_txq - Drain the txq
18424  * @phba: Pointer to HBA context object.
18425  *
18426  * This function attempt to submit IOCBs on the txq
18427  * to the adapter.  For SLI4 adapters, the txq contains
18428  * ELS IOCBs that have been deferred because the there
18429  * are no SGLs.  This congestion can occur with large
18430  * vport counts during node discovery.
18431  **/
18432
18433 uint32_t
18434 lpfc_drain_txq(struct lpfc_hba *phba)
18435 {
18436         LIST_HEAD(completions);
18437         struct lpfc_sli_ring *pring;
18438         struct lpfc_iocbq *piocbq = NULL;
18439         unsigned long iflags = 0;
18440         char *fail_msg = NULL;
18441         struct lpfc_sglq *sglq;
18442         union lpfc_wqe128 wqe128;
18443         union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
18444         uint32_t txq_cnt = 0;
18445
18446         pring = lpfc_phba_elsring(phba);
18447
18448         spin_lock_irqsave(&pring->ring_lock, iflags);
18449         list_for_each_entry(piocbq, &pring->txq, list) {
18450                 txq_cnt++;
18451         }
18452
18453         if (txq_cnt > pring->txq_max)
18454                 pring->txq_max = txq_cnt;
18455
18456         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18457
18458         while (!list_empty(&pring->txq)) {
18459                 spin_lock_irqsave(&pring->ring_lock, iflags);
18460
18461                 piocbq = lpfc_sli_ringtx_get(phba, pring);
18462                 if (!piocbq) {
18463                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18464                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18465                                 "2823 txq empty and txq_cnt is %d\n ",
18466                                 txq_cnt);
18467                         break;
18468                 }
18469                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
18470                 if (!sglq) {
18471                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
18472                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18473                         break;
18474                 }
18475                 txq_cnt--;
18476
18477                 /* The xri and iocb resources secured,
18478                  * attempt to issue request
18479                  */
18480                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
18481                 piocbq->sli4_xritag = sglq->sli4_xritag;
18482                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18483                         fail_msg = "to convert bpl to sgl";
18484                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
18485                         fail_msg = "to convert iocb to wqe";
18486                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
18487                         fail_msg = " - Wq is full";
18488                 else
18489                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18490
18491                 if (fail_msg) {
18492                         /* Failed means we can't issue and need to cancel */
18493                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18494                                         "2822 IOCB failed %s iotag 0x%x "
18495                                         "xri 0x%x\n",
18496                                         fail_msg,
18497                                         piocbq->iotag, piocbq->sli4_xritag);
18498                         list_add_tail(&piocbq->list, &completions);
18499                 }
18500                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18501         }
18502
18503         /* Cancel all the IOCBs that cannot be issued */
18504         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18505                                 IOERR_SLI_ABORTED);
18506
18507         return txq_cnt;
18508 }
18509
18510 /**
18511  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18512  * @phba: Pointer to HBA context object.
18513  * @pwqe: Pointer to command WQE.
18514  * @sglq: Pointer to the scatter gather queue object.
18515  *
18516  * This routine converts the bpl or bde that is in the WQE
18517  * to a sgl list for the sli4 hardware. The physical address
18518  * of the bpl/bde is converted back to a virtual address.
18519  * If the WQE contains a BPL then the list of BDE's is
18520  * converted to sli4_sge's. If the WQE contains a single
18521  * BDE then it is converted to a single sli_sge.
18522  * The WQE is still in cpu endianness so the contents of
18523  * the bpl can be used without byte swapping.
18524  *
18525  * Returns valid XRI = Success, NO_XRI = Failure.
18526  */
18527 static uint16_t
18528 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18529                  struct lpfc_sglq *sglq)
18530 {
18531         uint16_t xritag = NO_XRI;
18532         struct ulp_bde64 *bpl = NULL;
18533         struct ulp_bde64 bde;
18534         struct sli4_sge *sgl  = NULL;
18535         struct lpfc_dmabuf *dmabuf;
18536         union lpfc_wqe *wqe;
18537         int numBdes = 0;
18538         int i = 0;
18539         uint32_t offset = 0; /* accumulated offset in the sg request list */
18540         int inbound = 0; /* number of sg reply entries inbound from firmware */
18541         uint32_t cmd;
18542
18543         if (!pwqeq || !sglq)
18544                 return xritag;
18545
18546         sgl  = (struct sli4_sge *)sglq->sgl;
18547         wqe = &pwqeq->wqe;
18548         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18549
18550         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18551         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18552                 return sglq->sli4_xritag;
18553         numBdes = pwqeq->rsvd2;
18554         if (numBdes) {
18555                 /* The addrHigh and addrLow fields within the WQE
18556                  * have not been byteswapped yet so there is no
18557                  * need to swap them back.
18558                  */
18559                 if (pwqeq->context3)
18560                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18561                 else
18562                         return xritag;
18563
18564                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
18565                 if (!bpl)
18566                         return xritag;
18567
18568                 for (i = 0; i < numBdes; i++) {
18569                         /* Should already be byte swapped. */
18570                         sgl->addr_hi = bpl->addrHigh;
18571                         sgl->addr_lo = bpl->addrLow;
18572
18573                         sgl->word2 = le32_to_cpu(sgl->word2);
18574                         if ((i+1) == numBdes)
18575                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
18576                         else
18577                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
18578                         /* swap the size field back to the cpu so we
18579                          * can assign it to the sgl.
18580                          */
18581                         bde.tus.w = le32_to_cpu(bpl->tus.w);
18582                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18583                         /* The offsets in the sgl need to be accumulated
18584                          * separately for the request and reply lists.
18585                          * The request is always first, the reply follows.
18586                          */
18587                         switch (cmd) {
18588                         case CMD_GEN_REQUEST64_WQE:
18589                                 /* add up the reply sg entries */
18590                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18591                                         inbound++;
18592                                 /* first inbound? reset the offset */
18593                                 if (inbound == 1)
18594                                         offset = 0;
18595                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18596                                 bf_set(lpfc_sli4_sge_type, sgl,
18597                                         LPFC_SGE_TYPE_DATA);
18598                                 offset += bde.tus.f.bdeSize;
18599                                 break;
18600                         case CMD_FCP_TRSP64_WQE:
18601                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18602                                 bf_set(lpfc_sli4_sge_type, sgl,
18603                                         LPFC_SGE_TYPE_DATA);
18604                                 break;
18605                         case CMD_FCP_TSEND64_WQE:
18606                         case CMD_FCP_TRECEIVE64_WQE:
18607                                 bf_set(lpfc_sli4_sge_type, sgl,
18608                                         bpl->tus.f.bdeFlags);
18609                                 if (i < 3)
18610                                         offset = 0;
18611                                 else
18612                                         offset += bde.tus.f.bdeSize;
18613                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18614                                 break;
18615                         }
18616                         sgl->word2 = cpu_to_le32(sgl->word2);
18617                         bpl++;
18618                         sgl++;
18619                 }
18620         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18621                 /* The addrHigh and addrLow fields of the BDE have not
18622                  * been byteswapped yet so they need to be swapped
18623                  * before putting them in the sgl.
18624                  */
18625                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18626                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18627                 sgl->word2 = le32_to_cpu(sgl->word2);
18628                 bf_set(lpfc_sli4_sge_last, sgl, 1);
18629                 sgl->word2 = cpu_to_le32(sgl->word2);
18630                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18631         }
18632         return sglq->sli4_xritag;
18633 }
18634
18635 /**
18636  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18637  * @phba: Pointer to HBA context object.
18638  * @ring_number: Base sli ring number
18639  * @pwqe: Pointer to command WQE.
18640  **/
18641 int
18642 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18643                     struct lpfc_iocbq *pwqe)
18644 {
18645         union lpfc_wqe *wqe = &pwqe->wqe;
18646         struct lpfc_nvmet_rcv_ctx *ctxp;
18647         struct lpfc_queue *wq;
18648         struct lpfc_sglq *sglq;
18649         struct lpfc_sli_ring *pring;
18650         unsigned long iflags;
18651
18652         /* NVME_LS and NVME_LS ABTS requests. */
18653         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18654                 pring =  phba->sli4_hba.nvmels_wq->pring;
18655                 spin_lock_irqsave(&pring->ring_lock, iflags);
18656                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18657                 if (!sglq) {
18658                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18659                         return WQE_BUSY;
18660                 }
18661                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18662                 pwqe->sli4_xritag = sglq->sli4_xritag;
18663                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18664                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18665                         return WQE_ERROR;
18666                 }
18667                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18668                        pwqe->sli4_xritag);
18669                 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18670                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18671                         return WQE_ERROR;
18672                 }
18673                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18674                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18675                 return 0;
18676         }
18677
18678         /* NVME_FCREQ and NVME_ABTS requests */
18679         if (pwqe->iocb_flag & LPFC_IO_NVME) {
18680                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18681                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18682
18683                 spin_lock_irqsave(&pring->ring_lock, iflags);
18684                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18685                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18686                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18687                 if (lpfc_sli4_wq_put(wq, wqe)) {
18688                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18689                         return WQE_ERROR;
18690                 }
18691                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18692                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18693                 return 0;
18694         }
18695
18696         /* NVMET requests */
18697         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18698                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18699                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18700
18701                 spin_lock_irqsave(&pring->ring_lock, iflags);
18702                 ctxp = pwqe->context2;
18703                 sglq = ctxp->rqb_buffer->sglq;
18704                 if (pwqe->sli4_xritag ==  NO_XRI) {
18705                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
18706                         pwqe->sli4_xritag = sglq->sli4_xritag;
18707                 }
18708                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18709                        pwqe->sli4_xritag);
18710                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18711                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18712                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18713                 if (lpfc_sli4_wq_put(wq, wqe)) {
18714                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18715                         return WQE_ERROR;
18716                 }
18717                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18718                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18719                 return 0;
18720         }
18721         return WQE_ERROR;
18722 }