Merge remote-tracking branch 'asoc/fix/rt286' into asoc-linus
[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) ? phba->pport->port_state : 0,
4208                         psli->sli_flag);
4209
4210         /* perform board reset */
4211         phba->fc_eventTag = 0;
4212         phba->link_events = 0;
4213         if (phba->pport) {
4214                 phba->pport->fc_myDID = 0;
4215                 phba->pport->fc_prevDID = 0;
4216         }
4217
4218         /* Turn off parity checking and serr during the physical reset */
4219         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4220         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4221                               (cfg_value &
4222                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4223
4224         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4225
4226         /* Now toggle INITFF bit in the Host Control Register */
4227         writel(HC_INITFF, phba->HCregaddr);
4228         mdelay(1);
4229         readl(phba->HCregaddr); /* flush */
4230         writel(0, phba->HCregaddr);
4231         readl(phba->HCregaddr); /* flush */
4232
4233         /* Restore PCI cmd register */
4234         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4235
4236         /* Initialize relevant SLI info */
4237         for (i = 0; i < psli->num_rings; i++) {
4238                 pring = &psli->sli3_ring[i];
4239                 pring->flag = 0;
4240                 pring->sli.sli3.rspidx = 0;
4241                 pring->sli.sli3.next_cmdidx  = 0;
4242                 pring->sli.sli3.local_getidx = 0;
4243                 pring->sli.sli3.cmdidx = 0;
4244                 pring->missbufcnt = 0;
4245         }
4246
4247         phba->link_state = LPFC_WARM_START;
4248         return 0;
4249 }
4250
4251 /**
4252  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4253  * @phba: Pointer to HBA context object.
4254  *
4255  * This function resets a SLI4 HBA. This function disables PCI layer parity
4256  * checking during resets the device. The caller is not required to hold
4257  * any locks.
4258  *
4259  * This function returns 0 always.
4260  **/
4261 int
4262 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4263 {
4264         struct lpfc_sli *psli = &phba->sli;
4265         uint16_t cfg_value;
4266         int rc = 0;
4267
4268         /* Reset HBA */
4269         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4270                         "0295 Reset HBA Data: x%x x%x x%x\n",
4271                         phba->pport->port_state, psli->sli_flag,
4272                         phba->hba_flag);
4273
4274         /* perform board reset */
4275         phba->fc_eventTag = 0;
4276         phba->link_events = 0;
4277         phba->pport->fc_myDID = 0;
4278         phba->pport->fc_prevDID = 0;
4279
4280         spin_lock_irq(&phba->hbalock);
4281         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4282         phba->fcf.fcf_flag = 0;
4283         spin_unlock_irq(&phba->hbalock);
4284
4285         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4286         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4287                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4288                 return rc;
4289         }
4290
4291         /* Now physically reset the device */
4292         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4293                         "0389 Performing PCI function reset!\n");
4294
4295         /* Turn off parity checking and serr during the physical reset */
4296         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4297         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4298                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4299
4300         /* Perform FCoE PCI function reset before freeing queue memory */
4301         rc = lpfc_pci_function_reset(phba);
4302         lpfc_sli4_queue_destroy(phba);
4303
4304         /* Restore PCI cmd register */
4305         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4306
4307         return rc;
4308 }
4309
4310 /**
4311  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4312  * @phba: Pointer to HBA context object.
4313  *
4314  * This function is called in the SLI initialization code path to
4315  * restart the HBA. The caller is not required to hold any lock.
4316  * This function writes MBX_RESTART mailbox command to the SLIM and
4317  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4318  * function to free any pending commands. The function enables
4319  * POST only during the first initialization. The function returns zero.
4320  * The function does not guarantee completion of MBX_RESTART mailbox
4321  * command before the return of this function.
4322  **/
4323 static int
4324 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4325 {
4326         MAILBOX_t *mb;
4327         struct lpfc_sli *psli;
4328         volatile uint32_t word0;
4329         void __iomem *to_slim;
4330         uint32_t hba_aer_enabled;
4331
4332         spin_lock_irq(&phba->hbalock);
4333
4334         /* Take PCIe device Advanced Error Reporting (AER) state */
4335         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4336
4337         psli = &phba->sli;
4338
4339         /* Restart HBA */
4340         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4341                         "0337 Restart HBA Data: x%x x%x\n",
4342                         (phba->pport) ? phba->pport->port_state : 0,
4343                         psli->sli_flag);
4344
4345         word0 = 0;
4346         mb = (MAILBOX_t *) &word0;
4347         mb->mbxCommand = MBX_RESTART;
4348         mb->mbxHc = 1;
4349
4350         lpfc_reset_barrier(phba);
4351
4352         to_slim = phba->MBslimaddr;
4353         writel(*(uint32_t *) mb, to_slim);
4354         readl(to_slim); /* flush */
4355
4356         /* Only skip post after fc_ffinit is completed */
4357         if (phba->pport && phba->pport->port_state)
4358                 word0 = 1;      /* This is really setting up word1 */
4359         else
4360                 word0 = 0;      /* This is really setting up word1 */
4361         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4362         writel(*(uint32_t *) mb, to_slim);
4363         readl(to_slim); /* flush */
4364
4365         lpfc_sli_brdreset(phba);
4366         if (phba->pport)
4367                 phba->pport->stopped = 0;
4368         phba->link_state = LPFC_INIT_START;
4369         phba->hba_flag = 0;
4370         spin_unlock_irq(&phba->hbalock);
4371
4372         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4373         psli->stats_start = get_seconds();
4374
4375         /* Give the INITFF and Post time to settle. */
4376         mdelay(100);
4377
4378         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4379         if (hba_aer_enabled)
4380                 pci_disable_pcie_error_reporting(phba->pcidev);
4381
4382         lpfc_hba_down_post(phba);
4383
4384         return 0;
4385 }
4386
4387 /**
4388  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4389  * @phba: Pointer to HBA context object.
4390  *
4391  * This function is called in the SLI initialization code path to restart
4392  * a SLI4 HBA. The caller is not required to hold any lock.
4393  * At the end of the function, it calls lpfc_hba_down_post function to
4394  * free any pending commands.
4395  **/
4396 static int
4397 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4398 {
4399         struct lpfc_sli *psli = &phba->sli;
4400         uint32_t hba_aer_enabled;
4401         int rc;
4402
4403         /* Restart HBA */
4404         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4405                         "0296 Restart HBA Data: x%x x%x\n",
4406                         phba->pport->port_state, psli->sli_flag);
4407
4408         /* Take PCIe device Advanced Error Reporting (AER) state */
4409         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4410
4411         rc = lpfc_sli4_brdreset(phba);
4412
4413         spin_lock_irq(&phba->hbalock);
4414         phba->pport->stopped = 0;
4415         phba->link_state = LPFC_INIT_START;
4416         phba->hba_flag = 0;
4417         spin_unlock_irq(&phba->hbalock);
4418
4419         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4420         psli->stats_start = get_seconds();
4421
4422         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4423         if (hba_aer_enabled)
4424                 pci_disable_pcie_error_reporting(phba->pcidev);
4425
4426         lpfc_hba_down_post(phba);
4427
4428         return rc;
4429 }
4430
4431 /**
4432  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4433  * @phba: Pointer to HBA context object.
4434  *
4435  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4436  * API jump table function pointer from the lpfc_hba struct.
4437 **/
4438 int
4439 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4440 {
4441         return phba->lpfc_sli_brdrestart(phba);
4442 }
4443
4444 /**
4445  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4446  * @phba: Pointer to HBA context object.
4447  *
4448  * This function is called after a HBA restart to wait for successful
4449  * restart of the HBA. Successful restart of the HBA is indicated by
4450  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4451  * iteration, the function will restart the HBA again. The function returns
4452  * zero if HBA successfully restarted else returns negative error code.
4453  **/
4454 int
4455 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4456 {
4457         uint32_t status, i = 0;
4458
4459         /* Read the HBA Host Status Register */
4460         if (lpfc_readl(phba->HSregaddr, &status))
4461                 return -EIO;
4462
4463         /* Check status register to see what current state is */
4464         i = 0;
4465         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4466
4467                 /* Check every 10ms for 10 retries, then every 100ms for 90
4468                  * retries, then every 1 sec for 50 retires for a total of
4469                  * ~60 seconds before reset the board again and check every
4470                  * 1 sec for 50 retries. The up to 60 seconds before the
4471                  * board ready is required by the Falcon FIPS zeroization
4472                  * complete, and any reset the board in between shall cause
4473                  * restart of zeroization, further delay the board ready.
4474                  */
4475                 if (i++ >= 200) {
4476                         /* Adapter failed to init, timeout, status reg
4477                            <status> */
4478                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4479                                         "0436 Adapter failed to init, "
4480                                         "timeout, status reg x%x, "
4481                                         "FW Data: A8 x%x AC x%x\n", status,
4482                                         readl(phba->MBslimaddr + 0xa8),
4483                                         readl(phba->MBslimaddr + 0xac));
4484                         phba->link_state = LPFC_HBA_ERROR;
4485                         return -ETIMEDOUT;
4486                 }
4487
4488                 /* Check to see if any errors occurred during init */
4489                 if (status & HS_FFERM) {
4490                         /* ERROR: During chipset initialization */
4491                         /* Adapter failed to init, chipset, status reg
4492                            <status> */
4493                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4494                                         "0437 Adapter failed to init, "
4495                                         "chipset, status reg x%x, "
4496                                         "FW Data: A8 x%x AC x%x\n", status,
4497                                         readl(phba->MBslimaddr + 0xa8),
4498                                         readl(phba->MBslimaddr + 0xac));
4499                         phba->link_state = LPFC_HBA_ERROR;
4500                         return -EIO;
4501                 }
4502
4503                 if (i <= 10)
4504                         msleep(10);
4505                 else if (i <= 100)
4506                         msleep(100);
4507                 else
4508                         msleep(1000);
4509
4510                 if (i == 150) {
4511                         /* Do post */
4512                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4513                         lpfc_sli_brdrestart(phba);
4514                 }
4515                 /* Read the HBA Host Status Register */
4516                 if (lpfc_readl(phba->HSregaddr, &status))
4517                         return -EIO;
4518         }
4519
4520         /* Check to see if any errors occurred during init */
4521         if (status & HS_FFERM) {
4522                 /* ERROR: During chipset initialization */
4523                 /* Adapter failed to init, chipset, status reg <status> */
4524                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4525                                 "0438 Adapter failed to init, chipset, "
4526                                 "status reg x%x, "
4527                                 "FW Data: A8 x%x AC x%x\n", status,
4528                                 readl(phba->MBslimaddr + 0xa8),
4529                                 readl(phba->MBslimaddr + 0xac));
4530                 phba->link_state = LPFC_HBA_ERROR;
4531                 return -EIO;
4532         }
4533
4534         /* Clear all interrupt enable conditions */
4535         writel(0, phba->HCregaddr);
4536         readl(phba->HCregaddr); /* flush */
4537
4538         /* setup host attn register */
4539         writel(0xffffffff, phba->HAregaddr);
4540         readl(phba->HAregaddr); /* flush */
4541         return 0;
4542 }
4543
4544 /**
4545  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4546  *
4547  * This function calculates and returns the number of HBQs required to be
4548  * configured.
4549  **/
4550 int
4551 lpfc_sli_hbq_count(void)
4552 {
4553         return ARRAY_SIZE(lpfc_hbq_defs);
4554 }
4555
4556 /**
4557  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4558  *
4559  * This function adds the number of hbq entries in every HBQ to get
4560  * the total number of hbq entries required for the HBA and returns
4561  * the total count.
4562  **/
4563 static int
4564 lpfc_sli_hbq_entry_count(void)
4565 {
4566         int  hbq_count = lpfc_sli_hbq_count();
4567         int  count = 0;
4568         int  i;
4569
4570         for (i = 0; i < hbq_count; ++i)
4571                 count += lpfc_hbq_defs[i]->entry_count;
4572         return count;
4573 }
4574
4575 /**
4576  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4577  *
4578  * This function calculates amount of memory required for all hbq entries
4579  * to be configured and returns the total memory required.
4580  **/
4581 int
4582 lpfc_sli_hbq_size(void)
4583 {
4584         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4585 }
4586
4587 /**
4588  * lpfc_sli_hbq_setup - configure and initialize HBQs
4589  * @phba: Pointer to HBA context object.
4590  *
4591  * This function is called during the SLI initialization to configure
4592  * all the HBQs and post buffers to the HBQ. The caller is not
4593  * required to hold any locks. This function will return zero if successful
4594  * else it will return negative error code.
4595  **/
4596 static int
4597 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4598 {
4599         int  hbq_count = lpfc_sli_hbq_count();
4600         LPFC_MBOXQ_t *pmb;
4601         MAILBOX_t *pmbox;
4602         uint32_t hbqno;
4603         uint32_t hbq_entry_index;
4604
4605                                 /* Get a Mailbox buffer to setup mailbox
4606                                  * commands for HBA initialization
4607                                  */
4608         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4609
4610         if (!pmb)
4611                 return -ENOMEM;
4612
4613         pmbox = &pmb->u.mb;
4614
4615         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4616         phba->link_state = LPFC_INIT_MBX_CMDS;
4617         phba->hbq_in_use = 1;
4618
4619         hbq_entry_index = 0;
4620         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4621                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4622                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4623                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4624                 phba->hbqs[hbqno].entry_count =
4625                         lpfc_hbq_defs[hbqno]->entry_count;
4626                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4627                         hbq_entry_index, pmb);
4628                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4629
4630                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4631                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4632                            mbxStatus <status>, ring <num> */
4633
4634                         lpfc_printf_log(phba, KERN_ERR,
4635                                         LOG_SLI | LOG_VPORT,
4636                                         "1805 Adapter failed to init. "
4637                                         "Data: x%x x%x x%x\n",
4638                                         pmbox->mbxCommand,
4639                                         pmbox->mbxStatus, hbqno);
4640
4641                         phba->link_state = LPFC_HBA_ERROR;
4642                         mempool_free(pmb, phba->mbox_mem_pool);
4643                         return -ENXIO;
4644                 }
4645         }
4646         phba->hbq_count = hbq_count;
4647
4648         mempool_free(pmb, phba->mbox_mem_pool);
4649
4650         /* Initially populate or replenish the HBQs */
4651         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4652                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4653         return 0;
4654 }
4655
4656 /**
4657  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4658  * @phba: Pointer to HBA context object.
4659  *
4660  * This function is called during the SLI initialization to configure
4661  * all the HBQs and post buffers to the HBQ. The caller is not
4662  * required to hold any locks. This function will return zero if successful
4663  * else it will return negative error code.
4664  **/
4665 static int
4666 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4667 {
4668         phba->hbq_in_use = 1;
4669         phba->hbqs[LPFC_ELS_HBQ].entry_count =
4670                 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4671         phba->hbq_count = 1;
4672         lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4673         /* Initially populate or replenish the HBQs */
4674         return 0;
4675 }
4676
4677 /**
4678  * lpfc_sli_config_port - Issue config port mailbox command
4679  * @phba: Pointer to HBA context object.
4680  * @sli_mode: sli mode - 2/3
4681  *
4682  * This function is called by the sli initialization code path
4683  * to issue config_port mailbox command. This function restarts the
4684  * HBA firmware and issues a config_port mailbox command to configure
4685  * the SLI interface in the sli mode specified by sli_mode
4686  * variable. The caller is not required to hold any locks.
4687  * The function returns 0 if successful, else returns negative error
4688  * code.
4689  **/
4690 int
4691 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4692 {
4693         LPFC_MBOXQ_t *pmb;
4694         uint32_t resetcount = 0, rc = 0, done = 0;
4695
4696         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4697         if (!pmb) {
4698                 phba->link_state = LPFC_HBA_ERROR;
4699                 return -ENOMEM;
4700         }
4701
4702         phba->sli_rev = sli_mode;
4703         while (resetcount < 2 && !done) {
4704                 spin_lock_irq(&phba->hbalock);
4705                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4706                 spin_unlock_irq(&phba->hbalock);
4707                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4708                 lpfc_sli_brdrestart(phba);
4709                 rc = lpfc_sli_chipset_init(phba);
4710                 if (rc)
4711                         break;
4712
4713                 spin_lock_irq(&phba->hbalock);
4714                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4715                 spin_unlock_irq(&phba->hbalock);
4716                 resetcount++;
4717
4718                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4719                  * value of 0 means the call was successful.  Any other
4720                  * nonzero value is a failure, but if ERESTART is returned,
4721                  * the driver may reset the HBA and try again.
4722                  */
4723                 rc = lpfc_config_port_prep(phba);
4724                 if (rc == -ERESTART) {
4725                         phba->link_state = LPFC_LINK_UNKNOWN;
4726                         continue;
4727                 } else if (rc)
4728                         break;
4729
4730                 phba->link_state = LPFC_INIT_MBX_CMDS;
4731                 lpfc_config_port(phba, pmb);
4732                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4733                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4734                                         LPFC_SLI3_HBQ_ENABLED |
4735                                         LPFC_SLI3_CRP_ENABLED |
4736                                         LPFC_SLI3_BG_ENABLED |
4737                                         LPFC_SLI3_DSS_ENABLED);
4738                 if (rc != MBX_SUCCESS) {
4739                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4740                                 "0442 Adapter failed to init, mbxCmd x%x "
4741                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4742                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4743                         spin_lock_irq(&phba->hbalock);
4744                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4745                         spin_unlock_irq(&phba->hbalock);
4746                         rc = -ENXIO;
4747                 } else {
4748                         /* Allow asynchronous mailbox command to go through */
4749                         spin_lock_irq(&phba->hbalock);
4750                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4751                         spin_unlock_irq(&phba->hbalock);
4752                         done = 1;
4753
4754                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4755                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
4756                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4757                                         "3110 Port did not grant ASABT\n");
4758                 }
4759         }
4760         if (!done) {
4761                 rc = -EINVAL;
4762                 goto do_prep_failed;
4763         }
4764         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4765                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4766                         rc = -ENXIO;
4767                         goto do_prep_failed;
4768                 }
4769                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4770                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4771                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4772                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4773                                 phba->max_vpi : phba->max_vports;
4774
4775                 } else
4776                         phba->max_vpi = 0;
4777                 phba->fips_level = 0;
4778                 phba->fips_spec_rev = 0;
4779                 if (pmb->u.mb.un.varCfgPort.gdss) {
4780                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4781                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4782                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4783                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4784                                         "2850 Security Crypto Active. FIPS x%d "
4785                                         "(Spec Rev: x%d)",
4786                                         phba->fips_level, phba->fips_spec_rev);
4787                 }
4788                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4789                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4790                                         "2856 Config Port Security Crypto "
4791                                         "Error: x%x ",
4792                                         pmb->u.mb.un.varCfgPort.sec_err);
4793                 }
4794                 if (pmb->u.mb.un.varCfgPort.gerbm)
4795                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4796                 if (pmb->u.mb.un.varCfgPort.gcrp)
4797                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4798
4799                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4800                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4801
4802                 if (phba->cfg_enable_bg) {
4803                         if (pmb->u.mb.un.varCfgPort.gbg)
4804                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4805                         else
4806                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4807                                                 "0443 Adapter did not grant "
4808                                                 "BlockGuard\n");
4809                 }
4810         } else {
4811                 phba->hbq_get = NULL;
4812                 phba->port_gp = phba->mbox->us.s2.port;
4813                 phba->max_vpi = 0;
4814         }
4815 do_prep_failed:
4816         mempool_free(pmb, phba->mbox_mem_pool);
4817         return rc;
4818 }
4819
4820
4821 /**
4822  * lpfc_sli_hba_setup - SLI initialization function
4823  * @phba: Pointer to HBA context object.
4824  *
4825  * This function is the main SLI initialization function. This function
4826  * is called by the HBA initialization code, HBA reset code and HBA
4827  * error attention handler code. Caller is not required to hold any
4828  * locks. This function issues config_port mailbox command to configure
4829  * the SLI, setup iocb rings and HBQ rings. In the end the function
4830  * calls the config_port_post function to issue init_link mailbox
4831  * command and to start the discovery. The function will return zero
4832  * if successful, else it will return negative error code.
4833  **/
4834 int
4835 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4836 {
4837         uint32_t rc;
4838         int  mode = 3, i;
4839         int longs;
4840
4841         switch (phba->cfg_sli_mode) {
4842         case 2:
4843                 if (phba->cfg_enable_npiv) {
4844                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4845                                 "1824 NPIV enabled: Override sli_mode "
4846                                 "parameter (%d) to auto (0).\n",
4847                                 phba->cfg_sli_mode);
4848                         break;
4849                 }
4850                 mode = 2;
4851                 break;
4852         case 0:
4853         case 3:
4854                 break;
4855         default:
4856                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4857                                 "1819 Unrecognized sli_mode parameter: %d.\n",
4858                                 phba->cfg_sli_mode);
4859
4860                 break;
4861         }
4862         phba->fcp_embed_io = 0; /* SLI4 FC support only */
4863
4864         rc = lpfc_sli_config_port(phba, mode);
4865
4866         if (rc && phba->cfg_sli_mode == 3)
4867                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4868                                 "1820 Unable to select SLI-3.  "
4869                                 "Not supported by adapter.\n");
4870         if (rc && mode != 2)
4871                 rc = lpfc_sli_config_port(phba, 2);
4872         else if (rc && mode == 2)
4873                 rc = lpfc_sli_config_port(phba, 3);
4874         if (rc)
4875                 goto lpfc_sli_hba_setup_error;
4876
4877         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4878         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4879                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4880                 if (!rc) {
4881                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4882                                         "2709 This device supports "
4883                                         "Advanced Error Reporting (AER)\n");
4884                         spin_lock_irq(&phba->hbalock);
4885                         phba->hba_flag |= HBA_AER_ENABLED;
4886                         spin_unlock_irq(&phba->hbalock);
4887                 } else {
4888                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4889                                         "2708 This device does not support "
4890                                         "Advanced Error Reporting (AER): %d\n",
4891                                         rc);
4892                         phba->cfg_aer_support = 0;
4893                 }
4894         }
4895
4896         if (phba->sli_rev == 3) {
4897                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4898                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4899         } else {
4900                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4901                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4902                 phba->sli3_options = 0;
4903         }
4904
4905         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4906                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4907                         phba->sli_rev, phba->max_vpi);
4908         rc = lpfc_sli_ring_map(phba);
4909
4910         if (rc)
4911                 goto lpfc_sli_hba_setup_error;
4912
4913         /* Initialize VPIs. */
4914         if (phba->sli_rev == LPFC_SLI_REV3) {
4915                 /*
4916                  * The VPI bitmask and physical ID array are allocated
4917                  * and initialized once only - at driver load.  A port
4918                  * reset doesn't need to reinitialize this memory.
4919                  */
4920                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4921                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4922                         phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4923                                                   GFP_KERNEL);
4924                         if (!phba->vpi_bmask) {
4925                                 rc = -ENOMEM;
4926                                 goto lpfc_sli_hba_setup_error;
4927                         }
4928
4929                         phba->vpi_ids = kzalloc(
4930                                         (phba->max_vpi+1) * sizeof(uint16_t),
4931                                         GFP_KERNEL);
4932                         if (!phba->vpi_ids) {
4933                                 kfree(phba->vpi_bmask);
4934                                 rc = -ENOMEM;
4935                                 goto lpfc_sli_hba_setup_error;
4936                         }
4937                         for (i = 0; i < phba->max_vpi; i++)
4938                                 phba->vpi_ids[i] = i;
4939                 }
4940         }
4941
4942         /* Init HBQs */
4943         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4944                 rc = lpfc_sli_hbq_setup(phba);
4945                 if (rc)
4946                         goto lpfc_sli_hba_setup_error;
4947         }
4948         spin_lock_irq(&phba->hbalock);
4949         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4950         spin_unlock_irq(&phba->hbalock);
4951
4952         rc = lpfc_config_port_post(phba);
4953         if (rc)
4954                 goto lpfc_sli_hba_setup_error;
4955
4956         return rc;
4957
4958 lpfc_sli_hba_setup_error:
4959         phba->link_state = LPFC_HBA_ERROR;
4960         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4961                         "0445 Firmware initialization failed\n");
4962         return rc;
4963 }
4964
4965 /**
4966  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4967  * @phba: Pointer to HBA context object.
4968  * @mboxq: mailbox pointer.
4969  * This function issue a dump mailbox command to read config region
4970  * 23 and parse the records in the region and populate driver
4971  * data structure.
4972  **/
4973 static int
4974 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4975 {
4976         LPFC_MBOXQ_t *mboxq;
4977         struct lpfc_dmabuf *mp;
4978         struct lpfc_mqe *mqe;
4979         uint32_t data_length;
4980         int rc;
4981
4982         /* Program the default value of vlan_id and fc_map */
4983         phba->valid_vlan = 0;
4984         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4985         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4986         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4987
4988         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4989         if (!mboxq)
4990                 return -ENOMEM;
4991
4992         mqe = &mboxq->u.mqe;
4993         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4994                 rc = -ENOMEM;
4995                 goto out_free_mboxq;
4996         }
4997
4998         mp = (struct lpfc_dmabuf *) mboxq->context1;
4999         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5000
5001         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5002                         "(%d):2571 Mailbox cmd x%x Status x%x "
5003                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5004                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5005                         "CQ: x%x x%x x%x x%x\n",
5006                         mboxq->vport ? mboxq->vport->vpi : 0,
5007                         bf_get(lpfc_mqe_command, mqe),
5008                         bf_get(lpfc_mqe_status, mqe),
5009                         mqe->un.mb_words[0], mqe->un.mb_words[1],
5010                         mqe->un.mb_words[2], mqe->un.mb_words[3],
5011                         mqe->un.mb_words[4], mqe->un.mb_words[5],
5012                         mqe->un.mb_words[6], mqe->un.mb_words[7],
5013                         mqe->un.mb_words[8], mqe->un.mb_words[9],
5014                         mqe->un.mb_words[10], mqe->un.mb_words[11],
5015                         mqe->un.mb_words[12], mqe->un.mb_words[13],
5016                         mqe->un.mb_words[14], mqe->un.mb_words[15],
5017                         mqe->un.mb_words[16], mqe->un.mb_words[50],
5018                         mboxq->mcqe.word0,
5019                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5020                         mboxq->mcqe.trailer);
5021
5022         if (rc) {
5023                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5024                 kfree(mp);
5025                 rc = -EIO;
5026                 goto out_free_mboxq;
5027         }
5028         data_length = mqe->un.mb_words[5];
5029         if (data_length > DMP_RGN23_SIZE) {
5030                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5031                 kfree(mp);
5032                 rc = -EIO;
5033                 goto out_free_mboxq;
5034         }
5035
5036         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5037         lpfc_mbuf_free(phba, mp->virt, mp->phys);
5038         kfree(mp);
5039         rc = 0;
5040
5041 out_free_mboxq:
5042         mempool_free(mboxq, phba->mbox_mem_pool);
5043         return rc;
5044 }
5045
5046 /**
5047  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5048  * @phba: pointer to lpfc hba data structure.
5049  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5050  * @vpd: pointer to the memory to hold resulting port vpd data.
5051  * @vpd_size: On input, the number of bytes allocated to @vpd.
5052  *            On output, the number of data bytes in @vpd.
5053  *
5054  * This routine executes a READ_REV SLI4 mailbox command.  In
5055  * addition, this routine gets the port vpd data.
5056  *
5057  * Return codes
5058  *      0 - successful
5059  *      -ENOMEM - could not allocated memory.
5060  **/
5061 static int
5062 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5063                     uint8_t *vpd, uint32_t *vpd_size)
5064 {
5065         int rc = 0;
5066         uint32_t dma_size;
5067         struct lpfc_dmabuf *dmabuf;
5068         struct lpfc_mqe *mqe;
5069
5070         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5071         if (!dmabuf)
5072                 return -ENOMEM;
5073
5074         /*
5075          * Get a DMA buffer for the vpd data resulting from the READ_REV
5076          * mailbox command.
5077          */
5078         dma_size = *vpd_size;
5079         dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5080                                            &dmabuf->phys, GFP_KERNEL);
5081         if (!dmabuf->virt) {
5082                 kfree(dmabuf);
5083                 return -ENOMEM;
5084         }
5085
5086         /*
5087          * The SLI4 implementation of READ_REV conflicts at word1,
5088          * bits 31:16 and SLI4 adds vpd functionality not present
5089          * in SLI3.  This code corrects the conflicts.
5090          */
5091         lpfc_read_rev(phba, mboxq);
5092         mqe = &mboxq->u.mqe;
5093         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5094         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5095         mqe->un.read_rev.word1 &= 0x0000FFFF;
5096         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5097         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5098
5099         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5100         if (rc) {
5101                 dma_free_coherent(&phba->pcidev->dev, dma_size,
5102                                   dmabuf->virt, dmabuf->phys);
5103                 kfree(dmabuf);
5104                 return -EIO;
5105         }
5106
5107         /*
5108          * The available vpd length cannot be bigger than the
5109          * DMA buffer passed to the port.  Catch the less than
5110          * case and update the caller's size.
5111          */
5112         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5113                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5114
5115         memcpy(vpd, dmabuf->virt, *vpd_size);
5116
5117         dma_free_coherent(&phba->pcidev->dev, dma_size,
5118                           dmabuf->virt, dmabuf->phys);
5119         kfree(dmabuf);
5120         return 0;
5121 }
5122
5123 /**
5124  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5125  * @phba: pointer to lpfc hba data structure.
5126  *
5127  * This routine retrieves SLI4 device physical port name this PCI function
5128  * is attached to.
5129  *
5130  * Return codes
5131  *      0 - successful
5132  *      otherwise - failed to retrieve physical port name
5133  **/
5134 static int
5135 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5136 {
5137         LPFC_MBOXQ_t *mboxq;
5138         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5139         struct lpfc_controller_attribute *cntl_attr;
5140         struct lpfc_mbx_get_port_name *get_port_name;
5141         void *virtaddr = NULL;
5142         uint32_t alloclen, reqlen;
5143         uint32_t shdr_status, shdr_add_status;
5144         union lpfc_sli4_cfg_shdr *shdr;
5145         char cport_name = 0;
5146         int rc;
5147
5148         /* We assume nothing at this point */
5149         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5150         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5151
5152         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5153         if (!mboxq)
5154                 return -ENOMEM;
5155         /* obtain link type and link number via READ_CONFIG */
5156         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5157         lpfc_sli4_read_config(phba);
5158         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5159                 goto retrieve_ppname;
5160
5161         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5162         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5163         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5164                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5165                         LPFC_SLI4_MBX_NEMBED);
5166         if (alloclen < reqlen) {
5167                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5168                                 "3084 Allocated DMA memory size (%d) is "
5169                                 "less than the requested DMA memory size "
5170                                 "(%d)\n", alloclen, reqlen);
5171                 rc = -ENOMEM;
5172                 goto out_free_mboxq;
5173         }
5174         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5175         virtaddr = mboxq->sge_array->addr[0];
5176         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5177         shdr = &mbx_cntl_attr->cfg_shdr;
5178         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5179         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5180         if (shdr_status || shdr_add_status || rc) {
5181                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5182                                 "3085 Mailbox x%x (x%x/x%x) failed, "
5183                                 "rc:x%x, status:x%x, add_status:x%x\n",
5184                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5185                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5186                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5187                                 rc, shdr_status, shdr_add_status);
5188                 rc = -ENXIO;
5189                 goto out_free_mboxq;
5190         }
5191         cntl_attr = &mbx_cntl_attr->cntl_attr;
5192         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5193         phba->sli4_hba.lnk_info.lnk_tp =
5194                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5195         phba->sli4_hba.lnk_info.lnk_no =
5196                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5197         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5198                         "3086 lnk_type:%d, lnk_numb:%d\n",
5199                         phba->sli4_hba.lnk_info.lnk_tp,
5200                         phba->sli4_hba.lnk_info.lnk_no);
5201
5202 retrieve_ppname:
5203         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5204                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5205                 sizeof(struct lpfc_mbx_get_port_name) -
5206                 sizeof(struct lpfc_sli4_cfg_mhdr),
5207                 LPFC_SLI4_MBX_EMBED);
5208         get_port_name = &mboxq->u.mqe.un.get_port_name;
5209         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5210         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5211         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5212                 phba->sli4_hba.lnk_info.lnk_tp);
5213         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5214         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5215         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5216         if (shdr_status || shdr_add_status || rc) {
5217                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5218                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5219                                 "rc:x%x, status:x%x, add_status:x%x\n",
5220                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5221                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5222                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5223                                 rc, shdr_status, shdr_add_status);
5224                 rc = -ENXIO;
5225                 goto out_free_mboxq;
5226         }
5227         switch (phba->sli4_hba.lnk_info.lnk_no) {
5228         case LPFC_LINK_NUMBER_0:
5229                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5230                                 &get_port_name->u.response);
5231                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5232                 break;
5233         case LPFC_LINK_NUMBER_1:
5234                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5235                                 &get_port_name->u.response);
5236                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5237                 break;
5238         case LPFC_LINK_NUMBER_2:
5239                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5240                                 &get_port_name->u.response);
5241                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5242                 break;
5243         case LPFC_LINK_NUMBER_3:
5244                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5245                                 &get_port_name->u.response);
5246                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5247                 break;
5248         default:
5249                 break;
5250         }
5251
5252         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5253                 phba->Port[0] = cport_name;
5254                 phba->Port[1] = '\0';
5255                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5256                                 "3091 SLI get port name: %s\n", phba->Port);
5257         }
5258
5259 out_free_mboxq:
5260         if (rc != MBX_TIMEOUT) {
5261                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5262                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5263                 else
5264                         mempool_free(mboxq, phba->mbox_mem_pool);
5265         }
5266         return rc;
5267 }
5268
5269 /**
5270  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5271  * @phba: pointer to lpfc hba data structure.
5272  *
5273  * This routine is called to explicitly arm the SLI4 device's completion and
5274  * event queues
5275  **/
5276 static void
5277 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5278 {
5279         int qidx;
5280
5281         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5282         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
5283         if (phba->sli4_hba.nvmels_cq)
5284                 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5285                                                 LPFC_QUEUE_REARM);
5286
5287         if (phba->sli4_hba.fcp_cq)
5288                 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5289                         lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5290                                                 LPFC_QUEUE_REARM);
5291
5292         if (phba->sli4_hba.nvme_cq)
5293                 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5294                         lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5295                                                 LPFC_QUEUE_REARM);
5296
5297         if (phba->cfg_fof)
5298                 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5299
5300         if (phba->sli4_hba.hba_eq)
5301                 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5302                         lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5303                                                 LPFC_QUEUE_REARM);
5304
5305         if (phba->nvmet_support) {
5306                 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5307                         lpfc_sli4_cq_release(
5308                                 phba->sli4_hba.nvmet_cqset[qidx],
5309                                 LPFC_QUEUE_REARM);
5310                 }
5311         }
5312
5313         if (phba->cfg_fof)
5314                 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
5315 }
5316
5317 /**
5318  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5319  * @phba: Pointer to HBA context object.
5320  * @type: The resource extent type.
5321  * @extnt_count: buffer to hold port available extent count.
5322  * @extnt_size: buffer to hold element count per extent.
5323  *
5324  * This function calls the port and retrievs the number of available
5325  * extents and their size for a particular extent type.
5326  *
5327  * Returns: 0 if successful.  Nonzero otherwise.
5328  **/
5329 int
5330 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5331                                uint16_t *extnt_count, uint16_t *extnt_size)
5332 {
5333         int rc = 0;
5334         uint32_t length;
5335         uint32_t mbox_tmo;
5336         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5337         LPFC_MBOXQ_t *mbox;
5338
5339         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5340         if (!mbox)
5341                 return -ENOMEM;
5342
5343         /* Find out how many extents are available for this resource type */
5344         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5345                   sizeof(struct lpfc_sli4_cfg_mhdr));
5346         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5347                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5348                          length, LPFC_SLI4_MBX_EMBED);
5349
5350         /* Send an extents count of 0 - the GET doesn't use it. */
5351         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5352                                         LPFC_SLI4_MBX_EMBED);
5353         if (unlikely(rc)) {
5354                 rc = -EIO;
5355                 goto err_exit;
5356         }
5357
5358         if (!phba->sli4_hba.intr_enable)
5359                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5360         else {
5361                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5362                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5363         }
5364         if (unlikely(rc)) {
5365                 rc = -EIO;
5366                 goto err_exit;
5367         }
5368
5369         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5370         if (bf_get(lpfc_mbox_hdr_status,
5371                    &rsrc_info->header.cfg_shdr.response)) {
5372                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5373                                 "2930 Failed to get resource extents "
5374                                 "Status 0x%x Add'l Status 0x%x\n",
5375                                 bf_get(lpfc_mbox_hdr_status,
5376                                        &rsrc_info->header.cfg_shdr.response),
5377                                 bf_get(lpfc_mbox_hdr_add_status,
5378                                        &rsrc_info->header.cfg_shdr.response));
5379                 rc = -EIO;
5380                 goto err_exit;
5381         }
5382
5383         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5384                               &rsrc_info->u.rsp);
5385         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5386                              &rsrc_info->u.rsp);
5387
5388         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5389                         "3162 Retrieved extents type-%d from port: count:%d, "
5390                         "size:%d\n", type, *extnt_count, *extnt_size);
5391
5392 err_exit:
5393         mempool_free(mbox, phba->mbox_mem_pool);
5394         return rc;
5395 }
5396
5397 /**
5398  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5399  * @phba: Pointer to HBA context object.
5400  * @type: The extent type to check.
5401  *
5402  * This function reads the current available extents from the port and checks
5403  * if the extent count or extent size has changed since the last access.
5404  * Callers use this routine post port reset to understand if there is a
5405  * extent reprovisioning requirement.
5406  *
5407  * Returns:
5408  *   -Error: error indicates problem.
5409  *   1: Extent count or size has changed.
5410  *   0: No changes.
5411  **/
5412 static int
5413 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5414 {
5415         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5416         uint16_t size_diff, rsrc_ext_size;
5417         int rc = 0;
5418         struct lpfc_rsrc_blks *rsrc_entry;
5419         struct list_head *rsrc_blk_list = NULL;
5420
5421         size_diff = 0;
5422         curr_ext_cnt = 0;
5423         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5424                                             &rsrc_ext_cnt,
5425                                             &rsrc_ext_size);
5426         if (unlikely(rc))
5427                 return -EIO;
5428
5429         switch (type) {
5430         case LPFC_RSC_TYPE_FCOE_RPI:
5431                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5432                 break;
5433         case LPFC_RSC_TYPE_FCOE_VPI:
5434                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5435                 break;
5436         case LPFC_RSC_TYPE_FCOE_XRI:
5437                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5438                 break;
5439         case LPFC_RSC_TYPE_FCOE_VFI:
5440                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5441                 break;
5442         default:
5443                 break;
5444         }
5445
5446         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5447                 curr_ext_cnt++;
5448                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5449                         size_diff++;
5450         }
5451
5452         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5453                 rc = 1;
5454
5455         return rc;
5456 }
5457
5458 /**
5459  * lpfc_sli4_cfg_post_extnts -
5460  * @phba: Pointer to HBA context object.
5461  * @extnt_cnt - number of available extents.
5462  * @type - the extent type (rpi, xri, vfi, vpi).
5463  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5464  * @mbox - pointer to the caller's allocated mailbox structure.
5465  *
5466  * This function executes the extents allocation request.  It also
5467  * takes care of the amount of memory needed to allocate or get the
5468  * allocated extents. It is the caller's responsibility to evaluate
5469  * the response.
5470  *
5471  * Returns:
5472  *   -Error:  Error value describes the condition found.
5473  *   0: if successful
5474  **/
5475 static int
5476 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5477                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5478 {
5479         int rc = 0;
5480         uint32_t req_len;
5481         uint32_t emb_len;
5482         uint32_t alloc_len, mbox_tmo;
5483
5484         /* Calculate the total requested length of the dma memory */
5485         req_len = extnt_cnt * sizeof(uint16_t);
5486
5487         /*
5488          * Calculate the size of an embedded mailbox.  The uint32_t
5489          * accounts for extents-specific word.
5490          */
5491         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5492                 sizeof(uint32_t);
5493
5494         /*
5495          * Presume the allocation and response will fit into an embedded
5496          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5497          */
5498         *emb = LPFC_SLI4_MBX_EMBED;
5499         if (req_len > emb_len) {
5500                 req_len = extnt_cnt * sizeof(uint16_t) +
5501                         sizeof(union lpfc_sli4_cfg_shdr) +
5502                         sizeof(uint32_t);
5503                 *emb = LPFC_SLI4_MBX_NEMBED;
5504         }
5505
5506         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5507                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5508                                      req_len, *emb);
5509         if (alloc_len < req_len) {
5510                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5511                         "2982 Allocated DMA memory size (x%x) is "
5512                         "less than the requested DMA memory "
5513                         "size (x%x)\n", alloc_len, req_len);
5514                 return -ENOMEM;
5515         }
5516         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5517         if (unlikely(rc))
5518                 return -EIO;
5519
5520         if (!phba->sli4_hba.intr_enable)
5521                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5522         else {
5523                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5524                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5525         }
5526
5527         if (unlikely(rc))
5528                 rc = -EIO;
5529         return rc;
5530 }
5531
5532 /**
5533  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5534  * @phba: Pointer to HBA context object.
5535  * @type:  The resource extent type to allocate.
5536  *
5537  * This function allocates the number of elements for the specified
5538  * resource type.
5539  **/
5540 static int
5541 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5542 {
5543         bool emb = false;
5544         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5545         uint16_t rsrc_id, rsrc_start, j, k;
5546         uint16_t *ids;
5547         int i, rc;
5548         unsigned long longs;
5549         unsigned long *bmask;
5550         struct lpfc_rsrc_blks *rsrc_blks;
5551         LPFC_MBOXQ_t *mbox;
5552         uint32_t length;
5553         struct lpfc_id_range *id_array = NULL;
5554         void *virtaddr = NULL;
5555         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5556         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5557         struct list_head *ext_blk_list;
5558
5559         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5560                                             &rsrc_cnt,
5561                                             &rsrc_size);
5562         if (unlikely(rc))
5563                 return -EIO;
5564
5565         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5566                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5567                         "3009 No available Resource Extents "
5568                         "for resource type 0x%x: Count: 0x%x, "
5569                         "Size 0x%x\n", type, rsrc_cnt,
5570                         rsrc_size);
5571                 return -ENOMEM;
5572         }
5573
5574         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5575                         "2903 Post resource extents type-0x%x: "
5576                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5577
5578         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5579         if (!mbox)
5580                 return -ENOMEM;
5581
5582         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5583         if (unlikely(rc)) {
5584                 rc = -EIO;
5585                 goto err_exit;
5586         }
5587
5588         /*
5589          * Figure out where the response is located.  Then get local pointers
5590          * to the response data.  The port does not guarantee to respond to
5591          * all extents counts request so update the local variable with the
5592          * allocated count from the port.
5593          */
5594         if (emb == LPFC_SLI4_MBX_EMBED) {
5595                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5596                 id_array = &rsrc_ext->u.rsp.id[0];
5597                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5598         } else {
5599                 virtaddr = mbox->sge_array->addr[0];
5600                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5601                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5602                 id_array = &n_rsrc->id;
5603         }
5604
5605         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5606         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5607
5608         /*
5609          * Based on the resource size and count, correct the base and max
5610          * resource values.
5611          */
5612         length = sizeof(struct lpfc_rsrc_blks);
5613         switch (type) {
5614         case LPFC_RSC_TYPE_FCOE_RPI:
5615                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5616                                                    sizeof(unsigned long),
5617                                                    GFP_KERNEL);
5618                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5619                         rc = -ENOMEM;
5620                         goto err_exit;
5621                 }
5622                 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5623                                                  sizeof(uint16_t),
5624                                                  GFP_KERNEL);
5625                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5626                         kfree(phba->sli4_hba.rpi_bmask);
5627                         rc = -ENOMEM;
5628                         goto err_exit;
5629                 }
5630
5631                 /*
5632                  * The next_rpi was initialized with the maximum available
5633                  * count but the port may allocate a smaller number.  Catch
5634                  * that case and update the next_rpi.
5635                  */
5636                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5637
5638                 /* Initialize local ptrs for common extent processing later. */
5639                 bmask = phba->sli4_hba.rpi_bmask;
5640                 ids = phba->sli4_hba.rpi_ids;
5641                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5642                 break;
5643         case LPFC_RSC_TYPE_FCOE_VPI:
5644                 phba->vpi_bmask = kzalloc(longs *
5645                                           sizeof(unsigned long),
5646                                           GFP_KERNEL);
5647                 if (unlikely(!phba->vpi_bmask)) {
5648                         rc = -ENOMEM;
5649                         goto err_exit;
5650                 }
5651                 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5652                                          sizeof(uint16_t),
5653                                          GFP_KERNEL);
5654                 if (unlikely(!phba->vpi_ids)) {
5655                         kfree(phba->vpi_bmask);
5656                         rc = -ENOMEM;
5657                         goto err_exit;
5658                 }
5659
5660                 /* Initialize local ptrs for common extent processing later. */
5661                 bmask = phba->vpi_bmask;
5662                 ids = phba->vpi_ids;
5663                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5664                 break;
5665         case LPFC_RSC_TYPE_FCOE_XRI:
5666                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5667                                                    sizeof(unsigned long),
5668                                                    GFP_KERNEL);
5669                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5670                         rc = -ENOMEM;
5671                         goto err_exit;
5672                 }
5673                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5674                 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5675                                                  sizeof(uint16_t),
5676                                                  GFP_KERNEL);
5677                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5678                         kfree(phba->sli4_hba.xri_bmask);
5679                         rc = -ENOMEM;
5680                         goto err_exit;
5681                 }
5682
5683                 /* Initialize local ptrs for common extent processing later. */
5684                 bmask = phba->sli4_hba.xri_bmask;
5685                 ids = phba->sli4_hba.xri_ids;
5686                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5687                 break;
5688         case LPFC_RSC_TYPE_FCOE_VFI:
5689                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5690                                                    sizeof(unsigned long),
5691                                                    GFP_KERNEL);
5692                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5693                         rc = -ENOMEM;
5694                         goto err_exit;
5695                 }
5696                 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5697                                                  sizeof(uint16_t),
5698                                                  GFP_KERNEL);
5699                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5700                         kfree(phba->sli4_hba.vfi_bmask);
5701                         rc = -ENOMEM;
5702                         goto err_exit;
5703                 }
5704
5705                 /* Initialize local ptrs for common extent processing later. */
5706                 bmask = phba->sli4_hba.vfi_bmask;
5707                 ids = phba->sli4_hba.vfi_ids;
5708                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5709                 break;
5710         default:
5711                 /* Unsupported Opcode.  Fail call. */
5712                 id_array = NULL;
5713                 bmask = NULL;
5714                 ids = NULL;
5715                 ext_blk_list = NULL;
5716                 goto err_exit;
5717         }
5718
5719         /*
5720          * Complete initializing the extent configuration with the
5721          * allocated ids assigned to this function.  The bitmask serves
5722          * as an index into the array and manages the available ids.  The
5723          * array just stores the ids communicated to the port via the wqes.
5724          */
5725         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5726                 if ((i % 2) == 0)
5727                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5728                                          &id_array[k]);
5729                 else
5730                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5731                                          &id_array[k]);
5732
5733                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5734                 if (unlikely(!rsrc_blks)) {
5735                         rc = -ENOMEM;
5736                         kfree(bmask);
5737                         kfree(ids);
5738                         goto err_exit;
5739                 }
5740                 rsrc_blks->rsrc_start = rsrc_id;
5741                 rsrc_blks->rsrc_size = rsrc_size;
5742                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5743                 rsrc_start = rsrc_id;
5744                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
5745                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5746                                 lpfc_sli4_get_iocb_cnt(phba);
5747                         phba->sli4_hba.nvme_xri_start =
5748                                 phba->sli4_hba.scsi_xri_start +
5749                                 phba->sli4_hba.scsi_xri_max;
5750                 }
5751
5752                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5753                         ids[j] = rsrc_id;
5754                         rsrc_id++;
5755                         j++;
5756                 }
5757                 /* Entire word processed.  Get next word.*/
5758                 if ((i % 2) == 1)
5759                         k++;
5760         }
5761  err_exit:
5762         lpfc_sli4_mbox_cmd_free(phba, mbox);
5763         return rc;
5764 }
5765
5766
5767
5768 /**
5769  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5770  * @phba: Pointer to HBA context object.
5771  * @type: the extent's type.
5772  *
5773  * This function deallocates all extents of a particular resource type.
5774  * SLI4 does not allow for deallocating a particular extent range.  It
5775  * is the caller's responsibility to release all kernel memory resources.
5776  **/
5777 static int
5778 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5779 {
5780         int rc;
5781         uint32_t length, mbox_tmo = 0;
5782         LPFC_MBOXQ_t *mbox;
5783         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5784         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5785
5786         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5787         if (!mbox)
5788                 return -ENOMEM;
5789
5790         /*
5791          * This function sends an embedded mailbox because it only sends the
5792          * the resource type.  All extents of this type are released by the
5793          * port.
5794          */
5795         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5796                   sizeof(struct lpfc_sli4_cfg_mhdr));
5797         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5798                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5799                          length, LPFC_SLI4_MBX_EMBED);
5800
5801         /* Send an extents count of 0 - the dealloc doesn't use it. */
5802         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5803                                         LPFC_SLI4_MBX_EMBED);
5804         if (unlikely(rc)) {
5805                 rc = -EIO;
5806                 goto out_free_mbox;
5807         }
5808         if (!phba->sli4_hba.intr_enable)
5809                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5810         else {
5811                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5812                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5813         }
5814         if (unlikely(rc)) {
5815                 rc = -EIO;
5816                 goto out_free_mbox;
5817         }
5818
5819         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5820         if (bf_get(lpfc_mbox_hdr_status,
5821                    &dealloc_rsrc->header.cfg_shdr.response)) {
5822                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5823                                 "2919 Failed to release resource extents "
5824                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
5825                                 "Resource memory not released.\n",
5826                                 type,
5827                                 bf_get(lpfc_mbox_hdr_status,
5828                                     &dealloc_rsrc->header.cfg_shdr.response),
5829                                 bf_get(lpfc_mbox_hdr_add_status,
5830                                     &dealloc_rsrc->header.cfg_shdr.response));
5831                 rc = -EIO;
5832                 goto out_free_mbox;
5833         }
5834
5835         /* Release kernel memory resources for the specific type. */
5836         switch (type) {
5837         case LPFC_RSC_TYPE_FCOE_VPI:
5838                 kfree(phba->vpi_bmask);
5839                 kfree(phba->vpi_ids);
5840                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5841                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5842                                     &phba->lpfc_vpi_blk_list, list) {
5843                         list_del_init(&rsrc_blk->list);
5844                         kfree(rsrc_blk);
5845                 }
5846                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5847                 break;
5848         case LPFC_RSC_TYPE_FCOE_XRI:
5849                 kfree(phba->sli4_hba.xri_bmask);
5850                 kfree(phba->sli4_hba.xri_ids);
5851                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5852                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
5853                         list_del_init(&rsrc_blk->list);
5854                         kfree(rsrc_blk);
5855                 }
5856                 break;
5857         case LPFC_RSC_TYPE_FCOE_VFI:
5858                 kfree(phba->sli4_hba.vfi_bmask);
5859                 kfree(phba->sli4_hba.vfi_ids);
5860                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5861                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5862                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5863                         list_del_init(&rsrc_blk->list);
5864                         kfree(rsrc_blk);
5865                 }
5866                 break;
5867         case LPFC_RSC_TYPE_FCOE_RPI:
5868                 /* RPI bitmask and physical id array are cleaned up earlier. */
5869                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5870                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5871                         list_del_init(&rsrc_blk->list);
5872                         kfree(rsrc_blk);
5873                 }
5874                 break;
5875         default:
5876                 break;
5877         }
5878
5879         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5880
5881  out_free_mbox:
5882         mempool_free(mbox, phba->mbox_mem_pool);
5883         return rc;
5884 }
5885
5886 static void
5887 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5888                   uint32_t feature)
5889 {
5890         uint32_t len;
5891
5892         len = sizeof(struct lpfc_mbx_set_feature) -
5893                 sizeof(struct lpfc_sli4_cfg_mhdr);
5894         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5895                          LPFC_MBOX_OPCODE_SET_FEATURES, len,
5896                          LPFC_SLI4_MBX_EMBED);
5897
5898         switch (feature) {
5899         case LPFC_SET_UE_RECOVERY:
5900                 bf_set(lpfc_mbx_set_feature_UER,
5901                        &mbox->u.mqe.un.set_feature, 1);
5902                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5903                 mbox->u.mqe.un.set_feature.param_len = 8;
5904                 break;
5905         case LPFC_SET_MDS_DIAGS:
5906                 bf_set(lpfc_mbx_set_feature_mds,
5907                        &mbox->u.mqe.un.set_feature, 1);
5908                 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5909                        &mbox->u.mqe.un.set_feature, 0);
5910                 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5911                 mbox->u.mqe.un.set_feature.param_len = 8;
5912                 break;
5913         }
5914
5915         return;
5916 }
5917
5918 /**
5919  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5920  * @phba: Pointer to HBA context object.
5921  *
5922  * This function allocates all SLI4 resource identifiers.
5923  **/
5924 int
5925 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5926 {
5927         int i, rc, error = 0;
5928         uint16_t count, base;
5929         unsigned long longs;
5930
5931         if (!phba->sli4_hba.rpi_hdrs_in_use)
5932                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5933         if (phba->sli4_hba.extents_in_use) {
5934                 /*
5935                  * The port supports resource extents. The XRI, VPI, VFI, RPI
5936                  * resource extent count must be read and allocated before
5937                  * provisioning the resource id arrays.
5938                  */
5939                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5940                     LPFC_IDX_RSRC_RDY) {
5941                         /*
5942                          * Extent-based resources are set - the driver could
5943                          * be in a port reset. Figure out if any corrective
5944                          * actions need to be taken.
5945                          */
5946                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5947                                                  LPFC_RSC_TYPE_FCOE_VFI);
5948                         if (rc != 0)
5949                                 error++;
5950                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5951                                                  LPFC_RSC_TYPE_FCOE_VPI);
5952                         if (rc != 0)
5953                                 error++;
5954                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5955                                                  LPFC_RSC_TYPE_FCOE_XRI);
5956                         if (rc != 0)
5957                                 error++;
5958                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5959                                                  LPFC_RSC_TYPE_FCOE_RPI);
5960                         if (rc != 0)
5961                                 error++;
5962
5963                         /*
5964                          * It's possible that the number of resources
5965                          * provided to this port instance changed between
5966                          * resets.  Detect this condition and reallocate
5967                          * resources.  Otherwise, there is no action.
5968                          */
5969                         if (error) {
5970                                 lpfc_printf_log(phba, KERN_INFO,
5971                                                 LOG_MBOX | LOG_INIT,
5972                                                 "2931 Detected extent resource "
5973                                                 "change.  Reallocating all "
5974                                                 "extents.\n");
5975                                 rc = lpfc_sli4_dealloc_extent(phba,
5976                                                  LPFC_RSC_TYPE_FCOE_VFI);
5977                                 rc = lpfc_sli4_dealloc_extent(phba,
5978                                                  LPFC_RSC_TYPE_FCOE_VPI);
5979                                 rc = lpfc_sli4_dealloc_extent(phba,
5980                                                  LPFC_RSC_TYPE_FCOE_XRI);
5981                                 rc = lpfc_sli4_dealloc_extent(phba,
5982                                                  LPFC_RSC_TYPE_FCOE_RPI);
5983                         } else
5984                                 return 0;
5985                 }
5986
5987                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5988                 if (unlikely(rc))
5989                         goto err_exit;
5990
5991                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5992                 if (unlikely(rc))
5993                         goto err_exit;
5994
5995                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5996                 if (unlikely(rc))
5997                         goto err_exit;
5998
5999                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6000                 if (unlikely(rc))
6001                         goto err_exit;
6002                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6003                        LPFC_IDX_RSRC_RDY);
6004                 return rc;
6005         } else {
6006                 /*
6007                  * The port does not support resource extents.  The XRI, VPI,
6008                  * VFI, RPI resource ids were determined from READ_CONFIG.
6009                  * Just allocate the bitmasks and provision the resource id
6010                  * arrays.  If a port reset is active, the resources don't
6011                  * need any action - just exit.
6012                  */
6013                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6014                     LPFC_IDX_RSRC_RDY) {
6015                         lpfc_sli4_dealloc_resource_identifiers(phba);
6016                         lpfc_sli4_remove_rpis(phba);
6017                 }
6018                 /* RPIs. */
6019                 count = phba->sli4_hba.max_cfg_param.max_rpi;
6020                 if (count <= 0) {
6021                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6022                                         "3279 Invalid provisioning of "
6023                                         "rpi:%d\n", count);
6024                         rc = -EINVAL;
6025                         goto err_exit;
6026                 }
6027                 base = phba->sli4_hba.max_cfg_param.rpi_base;
6028                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6029                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6030                                                    sizeof(unsigned long),
6031                                                    GFP_KERNEL);
6032                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6033                         rc = -ENOMEM;
6034                         goto err_exit;
6035                 }
6036                 phba->sli4_hba.rpi_ids = kzalloc(count *
6037                                                  sizeof(uint16_t),
6038                                                  GFP_KERNEL);
6039                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6040                         rc = -ENOMEM;
6041                         goto free_rpi_bmask;
6042                 }
6043
6044                 for (i = 0; i < count; i++)
6045                         phba->sli4_hba.rpi_ids[i] = base + i;
6046
6047                 /* VPIs. */
6048                 count = phba->sli4_hba.max_cfg_param.max_vpi;
6049                 if (count <= 0) {
6050                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6051                                         "3280 Invalid provisioning of "
6052                                         "vpi:%d\n", count);
6053                         rc = -EINVAL;
6054                         goto free_rpi_ids;
6055                 }
6056                 base = phba->sli4_hba.max_cfg_param.vpi_base;
6057                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6058                 phba->vpi_bmask = kzalloc(longs *
6059                                           sizeof(unsigned long),
6060                                           GFP_KERNEL);
6061                 if (unlikely(!phba->vpi_bmask)) {
6062                         rc = -ENOMEM;
6063                         goto free_rpi_ids;
6064                 }
6065                 phba->vpi_ids = kzalloc(count *
6066                                         sizeof(uint16_t),
6067                                         GFP_KERNEL);
6068                 if (unlikely(!phba->vpi_ids)) {
6069                         rc = -ENOMEM;
6070                         goto free_vpi_bmask;
6071                 }
6072
6073                 for (i = 0; i < count; i++)
6074                         phba->vpi_ids[i] = base + i;
6075
6076                 /* XRIs. */
6077                 count = phba->sli4_hba.max_cfg_param.max_xri;
6078                 if (count <= 0) {
6079                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6080                                         "3281 Invalid provisioning of "
6081                                         "xri:%d\n", count);
6082                         rc = -EINVAL;
6083                         goto free_vpi_ids;
6084                 }
6085                 base = phba->sli4_hba.max_cfg_param.xri_base;
6086                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6087                 phba->sli4_hba.xri_bmask = kzalloc(longs *
6088                                                    sizeof(unsigned long),
6089                                                    GFP_KERNEL);
6090                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6091                         rc = -ENOMEM;
6092                         goto free_vpi_ids;
6093                 }
6094                 phba->sli4_hba.max_cfg_param.xri_used = 0;
6095                 phba->sli4_hba.xri_ids = kzalloc(count *
6096                                                  sizeof(uint16_t),
6097                                                  GFP_KERNEL);
6098                 if (unlikely(!phba->sli4_hba.xri_ids)) {
6099                         rc = -ENOMEM;
6100                         goto free_xri_bmask;
6101                 }
6102
6103                 for (i = 0; i < count; i++)
6104                         phba->sli4_hba.xri_ids[i] = base + i;
6105
6106                 /* VFIs. */
6107                 count = phba->sli4_hba.max_cfg_param.max_vfi;
6108                 if (count <= 0) {
6109                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6110                                         "3282 Invalid provisioning of "
6111                                         "vfi:%d\n", count);
6112                         rc = -EINVAL;
6113                         goto free_xri_ids;
6114                 }
6115                 base = phba->sli4_hba.max_cfg_param.vfi_base;
6116                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6117                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6118                                                    sizeof(unsigned long),
6119                                                    GFP_KERNEL);
6120                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6121                         rc = -ENOMEM;
6122                         goto free_xri_ids;
6123                 }
6124                 phba->sli4_hba.vfi_ids = kzalloc(count *
6125                                                  sizeof(uint16_t),
6126                                                  GFP_KERNEL);
6127                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6128                         rc = -ENOMEM;
6129                         goto free_vfi_bmask;
6130                 }
6131
6132                 for (i = 0; i < count; i++)
6133                         phba->sli4_hba.vfi_ids[i] = base + i;
6134
6135                 /*
6136                  * Mark all resources ready.  An HBA reset doesn't need
6137                  * to reset the initialization.
6138                  */
6139                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6140                        LPFC_IDX_RSRC_RDY);
6141                 return 0;
6142         }
6143
6144  free_vfi_bmask:
6145         kfree(phba->sli4_hba.vfi_bmask);
6146         phba->sli4_hba.vfi_bmask = NULL;
6147  free_xri_ids:
6148         kfree(phba->sli4_hba.xri_ids);
6149         phba->sli4_hba.xri_ids = NULL;
6150  free_xri_bmask:
6151         kfree(phba->sli4_hba.xri_bmask);
6152         phba->sli4_hba.xri_bmask = NULL;
6153  free_vpi_ids:
6154         kfree(phba->vpi_ids);
6155         phba->vpi_ids = NULL;
6156  free_vpi_bmask:
6157         kfree(phba->vpi_bmask);
6158         phba->vpi_bmask = NULL;
6159  free_rpi_ids:
6160         kfree(phba->sli4_hba.rpi_ids);
6161         phba->sli4_hba.rpi_ids = NULL;
6162  free_rpi_bmask:
6163         kfree(phba->sli4_hba.rpi_bmask);
6164         phba->sli4_hba.rpi_bmask = NULL;
6165  err_exit:
6166         return rc;
6167 }
6168
6169 /**
6170  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6171  * @phba: Pointer to HBA context object.
6172  *
6173  * This function allocates the number of elements for the specified
6174  * resource type.
6175  **/
6176 int
6177 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6178 {
6179         if (phba->sli4_hba.extents_in_use) {
6180                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6181                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6182                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6183                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6184         } else {
6185                 kfree(phba->vpi_bmask);
6186                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6187                 kfree(phba->vpi_ids);
6188                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6189                 kfree(phba->sli4_hba.xri_bmask);
6190                 kfree(phba->sli4_hba.xri_ids);
6191                 kfree(phba->sli4_hba.vfi_bmask);
6192                 kfree(phba->sli4_hba.vfi_ids);
6193                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6194                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6195         }
6196
6197         return 0;
6198 }
6199
6200 /**
6201  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6202  * @phba: Pointer to HBA context object.
6203  * @type: The resource extent type.
6204  * @extnt_count: buffer to hold port extent count response
6205  * @extnt_size: buffer to hold port extent size response.
6206  *
6207  * This function calls the port to read the host allocated extents
6208  * for a particular type.
6209  **/
6210 int
6211 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6212                                uint16_t *extnt_cnt, uint16_t *extnt_size)
6213 {
6214         bool emb;
6215         int rc = 0;
6216         uint16_t curr_blks = 0;
6217         uint32_t req_len, emb_len;
6218         uint32_t alloc_len, mbox_tmo;
6219         struct list_head *blk_list_head;
6220         struct lpfc_rsrc_blks *rsrc_blk;
6221         LPFC_MBOXQ_t *mbox;
6222         void *virtaddr = NULL;
6223         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6224         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6225         union  lpfc_sli4_cfg_shdr *shdr;
6226
6227         switch (type) {
6228         case LPFC_RSC_TYPE_FCOE_VPI:
6229                 blk_list_head = &phba->lpfc_vpi_blk_list;
6230                 break;
6231         case LPFC_RSC_TYPE_FCOE_XRI:
6232                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6233                 break;
6234         case LPFC_RSC_TYPE_FCOE_VFI:
6235                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6236                 break;
6237         case LPFC_RSC_TYPE_FCOE_RPI:
6238                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6239                 break;
6240         default:
6241                 return -EIO;
6242         }
6243
6244         /* Count the number of extents currently allocatd for this type. */
6245         list_for_each_entry(rsrc_blk, blk_list_head, list) {
6246                 if (curr_blks == 0) {
6247                         /*
6248                          * The GET_ALLOCATED mailbox does not return the size,
6249                          * just the count.  The size should be just the size
6250                          * stored in the current allocated block and all sizes
6251                          * for an extent type are the same so set the return
6252                          * value now.
6253                          */
6254                         *extnt_size = rsrc_blk->rsrc_size;
6255                 }
6256                 curr_blks++;
6257         }
6258
6259         /*
6260          * Calculate the size of an embedded mailbox.  The uint32_t
6261          * accounts for extents-specific word.
6262          */
6263         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6264                 sizeof(uint32_t);
6265
6266         /*
6267          * Presume the allocation and response will fit into an embedded
6268          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6269          */
6270         emb = LPFC_SLI4_MBX_EMBED;
6271         req_len = emb_len;
6272         if (req_len > emb_len) {
6273                 req_len = curr_blks * sizeof(uint16_t) +
6274                         sizeof(union lpfc_sli4_cfg_shdr) +
6275                         sizeof(uint32_t);
6276                 emb = LPFC_SLI4_MBX_NEMBED;
6277         }
6278
6279         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6280         if (!mbox)
6281                 return -ENOMEM;
6282         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6283
6284         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6285                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6286                                      req_len, emb);
6287         if (alloc_len < req_len) {
6288                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6289                         "2983 Allocated DMA memory size (x%x) is "
6290                         "less than the requested DMA memory "
6291                         "size (x%x)\n", alloc_len, req_len);
6292                 rc = -ENOMEM;
6293                 goto err_exit;
6294         }
6295         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6296         if (unlikely(rc)) {
6297                 rc = -EIO;
6298                 goto err_exit;
6299         }
6300
6301         if (!phba->sli4_hba.intr_enable)
6302                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6303         else {
6304                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6305                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6306         }
6307
6308         if (unlikely(rc)) {
6309                 rc = -EIO;
6310                 goto err_exit;
6311         }
6312
6313         /*
6314          * Figure out where the response is located.  Then get local pointers
6315          * to the response data.  The port does not guarantee to respond to
6316          * all extents counts request so update the local variable with the
6317          * allocated count from the port.
6318          */
6319         if (emb == LPFC_SLI4_MBX_EMBED) {
6320                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6321                 shdr = &rsrc_ext->header.cfg_shdr;
6322                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6323         } else {
6324                 virtaddr = mbox->sge_array->addr[0];
6325                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6326                 shdr = &n_rsrc->cfg_shdr;
6327                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6328         }
6329
6330         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6331                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6332                         "2984 Failed to read allocated resources "
6333                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6334                         type,
6335                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
6336                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6337                 rc = -EIO;
6338                 goto err_exit;
6339         }
6340  err_exit:
6341         lpfc_sli4_mbox_cmd_free(phba, mbox);
6342         return rc;
6343 }
6344
6345 /**
6346  * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
6347  * @phba: pointer to lpfc hba data structure.
6348  * @pring: Pointer to driver SLI ring object.
6349  * @sgl_list: linked link of sgl buffers to post
6350  * @cnt: number of linked list buffers
6351  *
6352  * This routine walks the list of buffers that have been allocated and
6353  * repost them to the port by using SGL block post. This is needed after a
6354  * pci_function_reset/warm_start or start. It attempts to construct blocks
6355  * of buffer sgls which contains contiguous xris and uses the non-embedded
6356  * SGL block post mailbox commands to post them to the port. For single
6357  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6358  * mailbox command for posting.
6359  *
6360  * Returns: 0 = success, non-zero failure.
6361  **/
6362 static int
6363 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6364                           struct list_head *sgl_list, int cnt)
6365 {
6366         struct lpfc_sglq *sglq_entry = NULL;
6367         struct lpfc_sglq *sglq_entry_next = NULL;
6368         struct lpfc_sglq *sglq_entry_first = NULL;
6369         int status, total_cnt;
6370         int post_cnt = 0, num_posted = 0, block_cnt = 0;
6371         int last_xritag = NO_XRI;
6372         LIST_HEAD(prep_sgl_list);
6373         LIST_HEAD(blck_sgl_list);
6374         LIST_HEAD(allc_sgl_list);
6375         LIST_HEAD(post_sgl_list);
6376         LIST_HEAD(free_sgl_list);
6377
6378         spin_lock_irq(&phba->hbalock);
6379         spin_lock(&phba->sli4_hba.sgl_list_lock);
6380         list_splice_init(sgl_list, &allc_sgl_list);
6381         spin_unlock(&phba->sli4_hba.sgl_list_lock);
6382         spin_unlock_irq(&phba->hbalock);
6383
6384         total_cnt = cnt;
6385         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6386                                  &allc_sgl_list, list) {
6387                 list_del_init(&sglq_entry->list);
6388                 block_cnt++;
6389                 if ((last_xritag != NO_XRI) &&
6390                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
6391                         /* a hole in xri block, form a sgl posting block */
6392                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
6393                         post_cnt = block_cnt - 1;
6394                         /* prepare list for next posting block */
6395                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6396                         block_cnt = 1;
6397                 } else {
6398                         /* prepare list for next posting block */
6399                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6400                         /* enough sgls for non-embed sgl mbox command */
6401                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6402                                 list_splice_init(&prep_sgl_list,
6403                                                  &blck_sgl_list);
6404                                 post_cnt = block_cnt;
6405                                 block_cnt = 0;
6406                         }
6407                 }
6408                 num_posted++;
6409
6410                 /* keep track of last sgl's xritag */
6411                 last_xritag = sglq_entry->sli4_xritag;
6412
6413                 /* end of repost sgl list condition for buffers */
6414                 if (num_posted == total_cnt) {
6415                         if (post_cnt == 0) {
6416                                 list_splice_init(&prep_sgl_list,
6417                                                  &blck_sgl_list);
6418                                 post_cnt = block_cnt;
6419                         } else if (block_cnt == 1) {
6420                                 status = lpfc_sli4_post_sgl(phba,
6421                                                 sglq_entry->phys, 0,
6422                                                 sglq_entry->sli4_xritag);
6423                                 if (!status) {
6424                                         /* successful, put sgl to posted list */
6425                                         list_add_tail(&sglq_entry->list,
6426                                                       &post_sgl_list);
6427                                 } else {
6428                                         /* Failure, put sgl to free list */
6429                                         lpfc_printf_log(phba, KERN_WARNING,
6430                                                 LOG_SLI,
6431                                                 "3159 Failed to post "
6432                                                 "sgl, xritag:x%x\n",
6433                                                 sglq_entry->sli4_xritag);
6434                                         list_add_tail(&sglq_entry->list,
6435                                                       &free_sgl_list);
6436                                         total_cnt--;
6437                                 }
6438                         }
6439                 }
6440
6441                 /* continue until a nembed page worth of sgls */
6442                 if (post_cnt == 0)
6443                         continue;
6444
6445                 /* post the buffer list sgls as a block */
6446                 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6447                                                  post_cnt);
6448
6449                 if (!status) {
6450                         /* success, put sgl list to posted sgl list */
6451                         list_splice_init(&blck_sgl_list, &post_sgl_list);
6452                 } else {
6453                         /* Failure, put sgl list to free sgl list */
6454                         sglq_entry_first = list_first_entry(&blck_sgl_list,
6455                                                             struct lpfc_sglq,
6456                                                             list);
6457                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6458                                         "3160 Failed to post sgl-list, "
6459                                         "xritag:x%x-x%x\n",
6460                                         sglq_entry_first->sli4_xritag,
6461                                         (sglq_entry_first->sli4_xritag +
6462                                          post_cnt - 1));
6463                         list_splice_init(&blck_sgl_list, &free_sgl_list);
6464                         total_cnt -= post_cnt;
6465                 }
6466
6467                 /* don't reset xirtag due to hole in xri block */
6468                 if (block_cnt == 0)
6469                         last_xritag = NO_XRI;
6470
6471                 /* reset sgl post count for next round of posting */
6472                 post_cnt = 0;
6473         }
6474
6475         /* free the sgls failed to post */
6476         lpfc_free_sgl_list(phba, &free_sgl_list);
6477
6478         /* push sgls posted to the available list */
6479         if (!list_empty(&post_sgl_list)) {
6480                 spin_lock_irq(&phba->hbalock);
6481                 spin_lock(&phba->sli4_hba.sgl_list_lock);
6482                 list_splice_init(&post_sgl_list, sgl_list);
6483                 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6484                 spin_unlock_irq(&phba->hbalock);
6485         } else {
6486                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6487                                 "3161 Failure to post sgl to port.\n");
6488                 return -EIO;
6489         }
6490
6491         /* return the number of XRIs actually posted */
6492         return total_cnt;
6493 }
6494
6495 void
6496 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6497 {
6498         uint32_t len;
6499
6500         len = sizeof(struct lpfc_mbx_set_host_data) -
6501                 sizeof(struct lpfc_sli4_cfg_mhdr);
6502         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6503                          LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6504                          LPFC_SLI4_MBX_EMBED);
6505
6506         mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6507         mbox->u.mqe.un.set_host_data.param_len =
6508                                         LPFC_HOST_OS_DRIVER_VERSION_SIZE;
6509         snprintf(mbox->u.mqe.un.set_host_data.data,
6510                  LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6511                  "Linux %s v"LPFC_DRIVER_VERSION,
6512                  (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6513 }
6514
6515 /**
6516  * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
6517  * @phba: Pointer to HBA context object.
6518  *
6519  * This function is the main SLI4 device initialization PCI function. This
6520  * function is called by the HBA initialization code, HBA reset code and
6521  * HBA error attention handler code. Caller is not required to hold any
6522  * locks.
6523  **/
6524 int
6525 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6526 {
6527         int rc, i;
6528         LPFC_MBOXQ_t *mboxq;
6529         struct lpfc_mqe *mqe;
6530         uint8_t *vpd;
6531         uint32_t vpd_size;
6532         uint32_t ftr_rsp = 0;
6533         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6534         struct lpfc_vport *vport = phba->pport;
6535         struct lpfc_dmabuf *mp;
6536         struct lpfc_rqb *rqbp;
6537
6538         /* Perform a PCI function reset to start from clean */
6539         rc = lpfc_pci_function_reset(phba);
6540         if (unlikely(rc))
6541                 return -ENODEV;
6542
6543         /* Check the HBA Host Status Register for readyness */
6544         rc = lpfc_sli4_post_status_check(phba);
6545         if (unlikely(rc))
6546                 return -ENODEV;
6547         else {
6548                 spin_lock_irq(&phba->hbalock);
6549                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6550                 spin_unlock_irq(&phba->hbalock);
6551         }
6552
6553         /*
6554          * Allocate a single mailbox container for initializing the
6555          * port.
6556          */
6557         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6558         if (!mboxq)
6559                 return -ENOMEM;
6560
6561         /* Issue READ_REV to collect vpd and FW information. */
6562         vpd_size = SLI4_PAGE_SIZE;
6563         vpd = kzalloc(vpd_size, GFP_KERNEL);
6564         if (!vpd) {
6565                 rc = -ENOMEM;
6566                 goto out_free_mbox;
6567         }
6568
6569         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6570         if (unlikely(rc)) {
6571                 kfree(vpd);
6572                 goto out_free_mbox;
6573         }
6574
6575         mqe = &mboxq->u.mqe;
6576         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6577         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6578                 phba->hba_flag |= HBA_FCOE_MODE;
6579                 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6580         } else {
6581                 phba->hba_flag &= ~HBA_FCOE_MODE;
6582         }
6583
6584         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6585                 LPFC_DCBX_CEE_MODE)
6586                 phba->hba_flag |= HBA_FIP_SUPPORT;
6587         else
6588                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6589
6590         phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6591
6592         if (phba->sli_rev != LPFC_SLI_REV4) {
6593                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6594                         "0376 READ_REV Error. SLI Level %d "
6595                         "FCoE enabled %d\n",
6596                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6597                 rc = -EIO;
6598                 kfree(vpd);
6599                 goto out_free_mbox;
6600         }
6601
6602         /*
6603          * Continue initialization with default values even if driver failed
6604          * to read FCoE param config regions, only read parameters if the
6605          * board is FCoE
6606          */
6607         if (phba->hba_flag & HBA_FCOE_MODE &&
6608             lpfc_sli4_read_fcoe_params(phba))
6609                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6610                         "2570 Failed to read FCoE parameters\n");
6611
6612         /*
6613          * Retrieve sli4 device physical port name, failure of doing it
6614          * is considered as non-fatal.
6615          */
6616         rc = lpfc_sli4_retrieve_pport_name(phba);
6617         if (!rc)
6618                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6619                                 "3080 Successful retrieving SLI4 device "
6620                                 "physical port name: %s.\n", phba->Port);
6621
6622         /*
6623          * Evaluate the read rev and vpd data. Populate the driver
6624          * state with the results. If this routine fails, the failure
6625          * is not fatal as the driver will use generic values.
6626          */
6627         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6628         if (unlikely(!rc)) {
6629                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6630                                 "0377 Error %d parsing vpd. "
6631                                 "Using defaults.\n", rc);
6632                 rc = 0;
6633         }
6634         kfree(vpd);
6635
6636         /* Save information as VPD data */
6637         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6638         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6639         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6640         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6641                                          &mqe->un.read_rev);
6642         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6643                                        &mqe->un.read_rev);
6644         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6645                                             &mqe->un.read_rev);
6646         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6647                                            &mqe->un.read_rev);
6648         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6649         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6650         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6651         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6652         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6653         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6654         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6655                         "(%d):0380 READ_REV Status x%x "
6656                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6657                         mboxq->vport ? mboxq->vport->vpi : 0,
6658                         bf_get(lpfc_mqe_status, mqe),
6659                         phba->vpd.rev.opFwName,
6660                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6661                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6662
6663         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
6664         rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6665         if (phba->pport->cfg_lun_queue_depth > rc) {
6666                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6667                                 "3362 LUN queue depth changed from %d to %d\n",
6668                                 phba->pport->cfg_lun_queue_depth, rc);
6669                 phba->pport->cfg_lun_queue_depth = rc;
6670         }
6671
6672         if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6673             LPFC_SLI_INTF_IF_TYPE_0) {
6674                 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6675                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6676                 if (rc == MBX_SUCCESS) {
6677                         phba->hba_flag |= HBA_RECOVERABLE_UE;
6678                         /* Set 1Sec interval to detect UE */
6679                         phba->eratt_poll_interval = 1;
6680                         phba->sli4_hba.ue_to_sr = bf_get(
6681                                         lpfc_mbx_set_feature_UESR,
6682                                         &mboxq->u.mqe.un.set_feature);
6683                         phba->sli4_hba.ue_to_rp = bf_get(
6684                                         lpfc_mbx_set_feature_UERP,
6685                                         &mboxq->u.mqe.un.set_feature);
6686                 }
6687         }
6688
6689         if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6690                 /* Enable MDS Diagnostics only if the SLI Port supports it */
6691                 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6692                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6693                 if (rc != MBX_SUCCESS)
6694                         phba->mds_diags_support = 0;
6695         }
6696
6697         /*
6698          * Discover the port's supported feature set and match it against the
6699          * hosts requests.
6700          */
6701         lpfc_request_features(phba, mboxq);
6702         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6703         if (unlikely(rc)) {
6704                 rc = -EIO;
6705                 goto out_free_mbox;
6706         }
6707
6708         /*
6709          * The port must support FCP initiator mode as this is the
6710          * only mode running in the host.
6711          */
6712         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6713                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6714                                 "0378 No support for fcpi mode.\n");
6715                 ftr_rsp++;
6716         }
6717         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6718                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6719         else
6720                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6721         /*
6722          * If the port cannot support the host's requested features
6723          * then turn off the global config parameters to disable the
6724          * feature in the driver.  This is not a fatal error.
6725          */
6726         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6727         if (phba->cfg_enable_bg) {
6728                 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6729                         phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6730                 else
6731                         ftr_rsp++;
6732         }
6733
6734         if (phba->max_vpi && phba->cfg_enable_npiv &&
6735             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6736                 ftr_rsp++;
6737
6738         if (ftr_rsp) {
6739                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6740                                 "0379 Feature Mismatch Data: x%08x %08x "
6741                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6742                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6743                                 phba->cfg_enable_npiv, phba->max_vpi);
6744                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6745                         phba->cfg_enable_bg = 0;
6746                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6747                         phba->cfg_enable_npiv = 0;
6748         }
6749
6750         /* These SLI3 features are assumed in SLI4 */
6751         spin_lock_irq(&phba->hbalock);
6752         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6753         spin_unlock_irq(&phba->hbalock);
6754
6755         /*
6756          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
6757          * calls depends on these resources to complete port setup.
6758          */
6759         rc = lpfc_sli4_alloc_resource_identifiers(phba);
6760         if (rc) {
6761                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6762                                 "2920 Failed to alloc Resource IDs "
6763                                 "rc = x%x\n", rc);
6764                 goto out_free_mbox;
6765         }
6766
6767         lpfc_set_host_data(phba, mboxq);
6768
6769         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6770         if (rc) {
6771                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6772                                 "2134 Failed to set host os driver version %x",
6773                                 rc);
6774         }
6775
6776         /* Read the port's service parameters. */
6777         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6778         if (rc) {
6779                 phba->link_state = LPFC_HBA_ERROR;
6780                 rc = -ENOMEM;
6781                 goto out_free_mbox;
6782         }
6783
6784         mboxq->vport = vport;
6785         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6786         mp = (struct lpfc_dmabuf *) mboxq->context1;
6787         if (rc == MBX_SUCCESS) {
6788                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6789                 rc = 0;
6790         }
6791
6792         /*
6793          * This memory was allocated by the lpfc_read_sparam routine. Release
6794          * it to the mbuf pool.
6795          */
6796         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6797         kfree(mp);
6798         mboxq->context1 = NULL;
6799         if (unlikely(rc)) {
6800                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6801                                 "0382 READ_SPARAM command failed "
6802                                 "status %d, mbxStatus x%x\n",
6803                                 rc, bf_get(lpfc_mqe_status, mqe));
6804                 phba->link_state = LPFC_HBA_ERROR;
6805                 rc = -EIO;
6806                 goto out_free_mbox;
6807         }
6808
6809         lpfc_update_vport_wwn(vport);
6810
6811         /* Update the fc_host data structures with new wwn. */
6812         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6813         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6814
6815         /* Create all the SLI4 queues */
6816         rc = lpfc_sli4_queue_create(phba);
6817         if (rc) {
6818                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6819                                 "3089 Failed to allocate queues\n");
6820                 rc = -ENODEV;
6821                 goto out_free_mbox;
6822         }
6823         /* Set up all the queues to the device */
6824         rc = lpfc_sli4_queue_setup(phba);
6825         if (unlikely(rc)) {
6826                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6827                                 "0381 Error %d during queue setup.\n ", rc);
6828                 goto out_stop_timers;
6829         }
6830         /* Initialize the driver internal SLI layer lists. */
6831         lpfc_sli4_setup(phba);
6832         lpfc_sli4_queue_init(phba);
6833
6834         /* update host els xri-sgl sizes and mappings */
6835         rc = lpfc_sli4_els_sgl_update(phba);
6836         if (unlikely(rc)) {
6837                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6838                                 "1400 Failed to update xri-sgl size and "
6839                                 "mapping: %d\n", rc);
6840                 goto out_destroy_queue;
6841         }
6842
6843         /* register the els sgl pool to the port */
6844         rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6845                                        phba->sli4_hba.els_xri_cnt);
6846         if (unlikely(rc < 0)) {
6847                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6848                                 "0582 Error %d during els sgl post "
6849                                 "operation\n", rc);
6850                 rc = -ENODEV;
6851                 goto out_destroy_queue;
6852         }
6853         phba->sli4_hba.els_xri_cnt = rc;
6854
6855         if (phba->nvmet_support) {
6856                 /* update host nvmet xri-sgl sizes and mappings */
6857                 rc = lpfc_sli4_nvmet_sgl_update(phba);
6858                 if (unlikely(rc)) {
6859                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6860                                         "6308 Failed to update nvmet-sgl size "
6861                                         "and mapping: %d\n", rc);
6862                         goto out_destroy_queue;
6863                 }
6864
6865                 /* register the nvmet sgl pool to the port */
6866                 rc = lpfc_sli4_repost_sgl_list(
6867                         phba,
6868                         &phba->sli4_hba.lpfc_nvmet_sgl_list,
6869                         phba->sli4_hba.nvmet_xri_cnt);
6870                 if (unlikely(rc < 0)) {
6871                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6872                                         "3117 Error %d during nvmet "
6873                                         "sgl post\n", rc);
6874                         rc = -ENODEV;
6875                         goto out_destroy_queue;
6876                 }
6877                 phba->sli4_hba.nvmet_xri_cnt = rc;
6878                 lpfc_nvmet_create_targetport(phba);
6879         } else {
6880                 /* update host scsi xri-sgl sizes and mappings */
6881                 rc = lpfc_sli4_scsi_sgl_update(phba);
6882                 if (unlikely(rc)) {
6883                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6884                                         "6309 Failed to update scsi-sgl size "
6885                                         "and mapping: %d\n", rc);
6886                         goto out_destroy_queue;
6887                 }
6888
6889                 /* update host nvme xri-sgl sizes and mappings */
6890                 rc = lpfc_sli4_nvme_sgl_update(phba);
6891                 if (unlikely(rc)) {
6892                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6893                                         "6082 Failed to update nvme-sgl size "
6894                                         "and mapping: %d\n", rc);
6895                         goto out_destroy_queue;
6896                 }
6897         }
6898
6899         if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
6900
6901                 /* Post initial buffers to all RQs created */
6902                 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6903                         rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6904                         INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6905                         rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6906                         rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
6907                         rqbp->entry_count = 256;
6908                         rqbp->buffer_count = 0;
6909
6910                         /* Divide by 4 and round down to multiple of 16 */
6911                         rc = (phba->cfg_nvmet_mrq_post >> 2) & 0xfff8;
6912                         phba->sli4_hba.nvmet_mrq_hdr[i]->entry_repost = rc;
6913                         phba->sli4_hba.nvmet_mrq_data[i]->entry_repost = rc;
6914
6915                         lpfc_post_rq_buffer(
6916                                 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6917                                 phba->sli4_hba.nvmet_mrq_data[i],
6918                                 phba->cfg_nvmet_mrq_post);
6919                 }
6920         }
6921
6922         if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6923                 /* register the allocated scsi sgl pool to the port */
6924                 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6925                 if (unlikely(rc)) {
6926                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6927                                         "0383 Error %d during scsi sgl post "
6928                                         "operation\n", rc);
6929                         /* Some Scsi buffers were moved to abort scsi list */
6930                         /* A pci function reset will repost them */
6931                         rc = -ENODEV;
6932                         goto out_destroy_queue;
6933                 }
6934         }
6935
6936         if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6937             (phba->nvmet_support == 0)) {
6938
6939                 /* register the allocated nvme sgl pool to the port */
6940                 rc = lpfc_repost_nvme_sgl_list(phba);
6941                 if (unlikely(rc)) {
6942                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6943                                         "6116 Error %d during nvme sgl post "
6944                                         "operation\n", rc);
6945                         /* Some NVME buffers were moved to abort nvme list */
6946                         /* A pci function reset will repost them */
6947                         rc = -ENODEV;
6948                         goto out_destroy_queue;
6949                 }
6950         }
6951
6952         /* Post the rpi header region to the device. */
6953         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6954         if (unlikely(rc)) {
6955                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6956                                 "0393 Error %d during rpi post operation\n",
6957                                 rc);
6958                 rc = -ENODEV;
6959                 goto out_destroy_queue;
6960         }
6961         lpfc_sli4_node_prep(phba);
6962
6963         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6964                 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
6965                         /*
6966                          * The FC Port needs to register FCFI (index 0)
6967                          */
6968                         lpfc_reg_fcfi(phba, mboxq);
6969                         mboxq->vport = phba->pport;
6970                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6971                         if (rc != MBX_SUCCESS)
6972                                 goto out_unset_queue;
6973                         rc = 0;
6974                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6975                                                 &mboxq->u.mqe.un.reg_fcfi);
6976                 } else {
6977                         /* We are a NVME Target mode with MRQ > 1 */
6978
6979                         /* First register the FCFI */
6980                         lpfc_reg_fcfi_mrq(phba, mboxq, 0);
6981                         mboxq->vport = phba->pport;
6982                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6983                         if (rc != MBX_SUCCESS)
6984                                 goto out_unset_queue;
6985                         rc = 0;
6986                         phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
6987                                                 &mboxq->u.mqe.un.reg_fcfi_mrq);
6988
6989                         /* Next register the MRQs */
6990                         lpfc_reg_fcfi_mrq(phba, mboxq, 1);
6991                         mboxq->vport = phba->pport;
6992                         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6993                         if (rc != MBX_SUCCESS)
6994                                 goto out_unset_queue;
6995                         rc = 0;
6996                 }
6997                 /* Check if the port is configured to be disabled */
6998                 lpfc_sli_read_link_ste(phba);
6999         }
7000
7001         /* Arm the CQs and then EQs on device */
7002         lpfc_sli4_arm_cqeq_intr(phba);
7003
7004         /* Indicate device interrupt mode */
7005         phba->sli4_hba.intr_enable = 1;
7006
7007         /* Allow asynchronous mailbox command to go through */
7008         spin_lock_irq(&phba->hbalock);
7009         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7010         spin_unlock_irq(&phba->hbalock);
7011
7012         /* Post receive buffers to the device */
7013         lpfc_sli4_rb_setup(phba);
7014
7015         /* Reset HBA FCF states after HBA reset */
7016         phba->fcf.fcf_flag = 0;
7017         phba->fcf.current_rec.flag = 0;
7018
7019         /* Start the ELS watchdog timer */
7020         mod_timer(&vport->els_tmofunc,
7021                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7022
7023         /* Start heart beat timer */
7024         mod_timer(&phba->hb_tmofunc,
7025                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7026         phba->hb_outstanding = 0;
7027         phba->last_completion_time = jiffies;
7028
7029         /* Start error attention (ERATT) polling timer */
7030         mod_timer(&phba->eratt_poll,
7031                   jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7032
7033         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7034         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7035                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7036                 if (!rc) {
7037                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7038                                         "2829 This device supports "
7039                                         "Advanced Error Reporting (AER)\n");
7040                         spin_lock_irq(&phba->hbalock);
7041                         phba->hba_flag |= HBA_AER_ENABLED;
7042                         spin_unlock_irq(&phba->hbalock);
7043                 } else {
7044                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7045                                         "2830 This device does not support "
7046                                         "Advanced Error Reporting (AER)\n");
7047                         phba->cfg_aer_support = 0;
7048                 }
7049                 rc = 0;
7050         }
7051
7052         /*
7053          * The port is ready, set the host's link state to LINK_DOWN
7054          * in preparation for link interrupts.
7055          */
7056         spin_lock_irq(&phba->hbalock);
7057         phba->link_state = LPFC_LINK_DOWN;
7058         spin_unlock_irq(&phba->hbalock);
7059         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7060             (phba->hba_flag & LINK_DISABLED)) {
7061                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7062                                 "3103 Adapter Link is disabled.\n");
7063                 lpfc_down_link(phba, mboxq);
7064                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7065                 if (rc != MBX_SUCCESS) {
7066                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7067                                         "3104 Adapter failed to issue "
7068                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7069                         goto out_unset_queue;
7070                 }
7071         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7072                 /* don't perform init_link on SLI4 FC port loopback test */
7073                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7074                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7075                         if (rc)
7076                                 goto out_unset_queue;
7077                 }
7078         }
7079         mempool_free(mboxq, phba->mbox_mem_pool);
7080         return rc;
7081 out_unset_queue:
7082         /* Unset all the queues set up in this routine when error out */
7083         lpfc_sli4_queue_unset(phba);
7084 out_destroy_queue:
7085         lpfc_sli4_queue_destroy(phba);
7086 out_stop_timers:
7087         lpfc_stop_hba_timers(phba);
7088 out_free_mbox:
7089         mempool_free(mboxq, phba->mbox_mem_pool);
7090         return rc;
7091 }
7092
7093 /**
7094  * lpfc_mbox_timeout - Timeout call back function for mbox timer
7095  * @ptr: context object - pointer to hba structure.
7096  *
7097  * This is the callback function for mailbox timer. The mailbox
7098  * timer is armed when a new mailbox command is issued and the timer
7099  * is deleted when the mailbox complete. The function is called by
7100  * the kernel timer code when a mailbox does not complete within
7101  * expected time. This function wakes up the worker thread to
7102  * process the mailbox timeout and returns. All the processing is
7103  * done by the worker thread function lpfc_mbox_timeout_handler.
7104  **/
7105 void
7106 lpfc_mbox_timeout(unsigned long ptr)
7107 {
7108         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
7109         unsigned long iflag;
7110         uint32_t tmo_posted;
7111
7112         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7113         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7114         if (!tmo_posted)
7115                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7116         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7117
7118         if (!tmo_posted)
7119                 lpfc_worker_wake_up(phba);
7120         return;
7121 }
7122
7123 /**
7124  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7125  *                                    are pending
7126  * @phba: Pointer to HBA context object.
7127  *
7128  * This function checks if any mailbox completions are present on the mailbox
7129  * completion queue.
7130  **/
7131 static bool
7132 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7133 {
7134
7135         uint32_t idx;
7136         struct lpfc_queue *mcq;
7137         struct lpfc_mcqe *mcqe;
7138         bool pending_completions = false;
7139
7140         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7141                 return false;
7142
7143         /* Check for completions on mailbox completion queue */
7144
7145         mcq = phba->sli4_hba.mbx_cq;
7146         idx = mcq->hba_index;
7147         while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7148                 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7149                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7150                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7151                         pending_completions = true;
7152                         break;
7153                 }
7154                 idx = (idx + 1) % mcq->entry_count;
7155                 if (mcq->hba_index == idx)
7156                         break;
7157         }
7158         return pending_completions;
7159
7160 }
7161
7162 /**
7163  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7164  *                                            that were missed.
7165  * @phba: Pointer to HBA context object.
7166  *
7167  * For sli4, it is possible to miss an interrupt. As such mbox completions
7168  * maybe missed causing erroneous mailbox timeouts to occur. This function
7169  * checks to see if mbox completions are on the mailbox completion queue
7170  * and will process all the completions associated with the eq for the
7171  * mailbox completion queue.
7172  **/
7173 bool
7174 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7175 {
7176
7177         uint32_t eqidx;
7178         struct lpfc_queue *fpeq = NULL;
7179         struct lpfc_eqe *eqe;
7180         bool mbox_pending;
7181
7182         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7183                 return false;
7184
7185         /* Find the eq associated with the mcq */
7186
7187         if (phba->sli4_hba.hba_eq)
7188                 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
7189                         if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7190                             phba->sli4_hba.mbx_cq->assoc_qid) {
7191                                 fpeq = phba->sli4_hba.hba_eq[eqidx];
7192                                 break;
7193                         }
7194         if (!fpeq)
7195                 return false;
7196
7197         /* Turn off interrupts from this EQ */
7198
7199         lpfc_sli4_eq_clr_intr(fpeq);
7200
7201         /* Check to see if a mbox completion is pending */
7202
7203         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7204
7205         /*
7206          * If a mbox completion is pending, process all the events on EQ
7207          * associated with the mbox completion queue (this could include
7208          * mailbox commands, async events, els commands, receive queue data
7209          * and fcp commands)
7210          */
7211
7212         if (mbox_pending)
7213                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7214                         lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7215                         fpeq->EQ_processed++;
7216                 }
7217
7218         /* Always clear and re-arm the EQ */
7219
7220         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7221
7222         return mbox_pending;
7223
7224 }
7225
7226 /**
7227  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7228  * @phba: Pointer to HBA context object.
7229  *
7230  * This function is called from worker thread when a mailbox command times out.
7231  * The caller is not required to hold any locks. This function will reset the
7232  * HBA and recover all the pending commands.
7233  **/
7234 void
7235 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7236 {
7237         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7238         MAILBOX_t *mb = NULL;
7239
7240         struct lpfc_sli *psli = &phba->sli;
7241
7242         /* If the mailbox completed, process the completion and return */
7243         if (lpfc_sli4_process_missed_mbox_completions(phba))
7244                 return;
7245
7246         if (pmbox != NULL)
7247                 mb = &pmbox->u.mb;
7248         /* Check the pmbox pointer first.  There is a race condition
7249          * between the mbox timeout handler getting executed in the
7250          * worklist and the mailbox actually completing. When this
7251          * race condition occurs, the mbox_active will be NULL.
7252          */
7253         spin_lock_irq(&phba->hbalock);
7254         if (pmbox == NULL) {
7255                 lpfc_printf_log(phba, KERN_WARNING,
7256                                 LOG_MBOX | LOG_SLI,
7257                                 "0353 Active Mailbox cleared - mailbox timeout "
7258                                 "exiting\n");
7259                 spin_unlock_irq(&phba->hbalock);
7260                 return;
7261         }
7262
7263         /* Mbox cmd <mbxCommand> timeout */
7264         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7265                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7266                         mb->mbxCommand,
7267                         phba->pport->port_state,
7268                         phba->sli.sli_flag,
7269                         phba->sli.mbox_active);
7270         spin_unlock_irq(&phba->hbalock);
7271
7272         /* Setting state unknown so lpfc_sli_abort_iocb_ring
7273          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7274          * it to fail all outstanding SCSI IO.
7275          */
7276         spin_lock_irq(&phba->pport->work_port_lock);
7277         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7278         spin_unlock_irq(&phba->pport->work_port_lock);
7279         spin_lock_irq(&phba->hbalock);
7280         phba->link_state = LPFC_LINK_UNKNOWN;
7281         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7282         spin_unlock_irq(&phba->hbalock);
7283
7284         lpfc_sli_abort_fcp_rings(phba);
7285
7286         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7287                         "0345 Resetting board due to mailbox timeout\n");
7288
7289         /* Reset the HBA device */
7290         lpfc_reset_hba(phba);
7291 }
7292
7293 /**
7294  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7295  * @phba: Pointer to HBA context object.
7296  * @pmbox: Pointer to mailbox object.
7297  * @flag: Flag indicating how the mailbox need to be processed.
7298  *
7299  * This function is called by discovery code and HBA management code
7300  * to submit a mailbox command to firmware with SLI-3 interface spec. This
7301  * function gets the hbalock to protect the data structures.
7302  * The mailbox command can be submitted in polling mode, in which case
7303  * this function will wait in a polling loop for the completion of the
7304  * mailbox.
7305  * If the mailbox is submitted in no_wait mode (not polling) the
7306  * function will submit the command and returns immediately without waiting
7307  * for the mailbox completion. The no_wait is supported only when HBA
7308  * is in SLI2/SLI3 mode - interrupts are enabled.
7309  * The SLI interface allows only one mailbox pending at a time. If the
7310  * mailbox is issued in polling mode and there is already a mailbox
7311  * pending, then the function will return an error. If the mailbox is issued
7312  * in NO_WAIT mode and there is a mailbox pending already, the function
7313  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7314  * The sli layer owns the mailbox object until the completion of mailbox
7315  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7316  * return codes the caller owns the mailbox command after the return of
7317  * the function.
7318  **/
7319 static int
7320 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7321                        uint32_t flag)
7322 {
7323         MAILBOX_t *mbx;
7324         struct lpfc_sli *psli = &phba->sli;
7325         uint32_t status, evtctr;
7326         uint32_t ha_copy, hc_copy;
7327         int i;
7328         unsigned long timeout;
7329         unsigned long drvr_flag = 0;
7330         uint32_t word0, ldata;
7331         void __iomem *to_slim;
7332         int processing_queue = 0;
7333
7334         spin_lock_irqsave(&phba->hbalock, drvr_flag);
7335         if (!pmbox) {
7336                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7337                 /* processing mbox queue from intr_handler */
7338                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7339                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7340                         return MBX_SUCCESS;
7341                 }
7342                 processing_queue = 1;
7343                 pmbox = lpfc_mbox_get(phba);
7344                 if (!pmbox) {
7345                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7346                         return MBX_SUCCESS;
7347                 }
7348         }
7349
7350         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7351                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7352                 if(!pmbox->vport) {
7353                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7354                         lpfc_printf_log(phba, KERN_ERR,
7355                                         LOG_MBOX | LOG_VPORT,
7356                                         "1806 Mbox x%x failed. No vport\n",
7357                                         pmbox->u.mb.mbxCommand);
7358                         dump_stack();
7359                         goto out_not_finished;
7360                 }
7361         }
7362
7363         /* If the PCI channel is in offline state, do not post mbox. */
7364         if (unlikely(pci_channel_offline(phba->pcidev))) {
7365                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7366                 goto out_not_finished;
7367         }
7368
7369         /* If HBA has a deferred error attention, fail the iocb. */
7370         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7371                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7372                 goto out_not_finished;
7373         }
7374
7375         psli = &phba->sli;
7376
7377         mbx = &pmbox->u.mb;
7378         status = MBX_SUCCESS;
7379
7380         if (phba->link_state == LPFC_HBA_ERROR) {
7381                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7382
7383                 /* Mbox command <mbxCommand> cannot issue */
7384                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7385                                 "(%d):0311 Mailbox command x%x cannot "
7386                                 "issue Data: x%x x%x\n",
7387                                 pmbox->vport ? pmbox->vport->vpi : 0,
7388                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7389                 goto out_not_finished;
7390         }
7391
7392         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7393                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7394                         !(hc_copy & HC_MBINT_ENA)) {
7395                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7396                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7397                                 "(%d):2528 Mailbox command x%x cannot "
7398                                 "issue Data: x%x x%x\n",
7399                                 pmbox->vport ? pmbox->vport->vpi : 0,
7400                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7401                         goto out_not_finished;
7402                 }
7403         }
7404
7405         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7406                 /* Polling for a mbox command when another one is already active
7407                  * is not allowed in SLI. Also, the driver must have established
7408                  * SLI2 mode to queue and process multiple mbox commands.
7409                  */
7410
7411                 if (flag & MBX_POLL) {
7412                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7413
7414                         /* Mbox command <mbxCommand> cannot issue */
7415                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7416                                         "(%d):2529 Mailbox command x%x "
7417                                         "cannot issue Data: x%x x%x\n",
7418                                         pmbox->vport ? pmbox->vport->vpi : 0,
7419                                         pmbox->u.mb.mbxCommand,
7420                                         psli->sli_flag, flag);
7421                         goto out_not_finished;
7422                 }
7423
7424                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7425                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7426                         /* Mbox command <mbxCommand> cannot issue */
7427                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7428                                         "(%d):2530 Mailbox command x%x "
7429                                         "cannot issue Data: x%x x%x\n",
7430                                         pmbox->vport ? pmbox->vport->vpi : 0,
7431                                         pmbox->u.mb.mbxCommand,
7432                                         psli->sli_flag, flag);
7433                         goto out_not_finished;
7434                 }
7435
7436                 /* Another mailbox command is still being processed, queue this
7437                  * command to be processed later.
7438                  */
7439                 lpfc_mbox_put(phba, pmbox);
7440
7441                 /* Mbox cmd issue - BUSY */
7442                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7443                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
7444                                 "x%x x%x x%x x%x\n",
7445                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7446                                 mbx->mbxCommand, phba->pport->port_state,
7447                                 psli->sli_flag, flag);
7448
7449                 psli->slistat.mbox_busy++;
7450                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7451
7452                 if (pmbox->vport) {
7453                         lpfc_debugfs_disc_trc(pmbox->vport,
7454                                 LPFC_DISC_TRC_MBOX_VPORT,
7455                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
7456                                 (uint32_t)mbx->mbxCommand,
7457                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7458                 }
7459                 else {
7460                         lpfc_debugfs_disc_trc(phba->pport,
7461                                 LPFC_DISC_TRC_MBOX,
7462                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
7463                                 (uint32_t)mbx->mbxCommand,
7464                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7465                 }
7466
7467                 return MBX_BUSY;
7468         }
7469
7470         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7471
7472         /* If we are not polling, we MUST be in SLI2 mode */
7473         if (flag != MBX_POLL) {
7474                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7475                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
7476                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7477                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7478                         /* Mbox command <mbxCommand> cannot issue */
7479                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7480                                         "(%d):2531 Mailbox command x%x "
7481                                         "cannot issue Data: x%x x%x\n",
7482                                         pmbox->vport ? pmbox->vport->vpi : 0,
7483                                         pmbox->u.mb.mbxCommand,
7484                                         psli->sli_flag, flag);
7485                         goto out_not_finished;
7486                 }
7487                 /* timeout active mbox command */
7488                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7489                                            1000);
7490                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7491         }
7492
7493         /* Mailbox cmd <cmd> issue */
7494         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7495                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7496                         "x%x\n",
7497                         pmbox->vport ? pmbox->vport->vpi : 0,
7498                         mbx->mbxCommand, phba->pport->port_state,
7499                         psli->sli_flag, flag);
7500
7501         if (mbx->mbxCommand != MBX_HEARTBEAT) {
7502                 if (pmbox->vport) {
7503                         lpfc_debugfs_disc_trc(pmbox->vport,
7504                                 LPFC_DISC_TRC_MBOX_VPORT,
7505                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7506                                 (uint32_t)mbx->mbxCommand,
7507                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7508                 }
7509                 else {
7510                         lpfc_debugfs_disc_trc(phba->pport,
7511                                 LPFC_DISC_TRC_MBOX,
7512                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
7513                                 (uint32_t)mbx->mbxCommand,
7514                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7515                 }
7516         }
7517
7518         psli->slistat.mbox_cmd++;
7519         evtctr = psli->slistat.mbox_event;
7520
7521         /* next set own bit for the adapter and copy over command word */
7522         mbx->mbxOwner = OWN_CHIP;
7523
7524         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7525                 /* Populate mbox extension offset word. */
7526                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7527                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7528                                 = (uint8_t *)phba->mbox_ext
7529                                   - (uint8_t *)phba->mbox;
7530                 }
7531
7532                 /* Copy the mailbox extension data */
7533                 if (pmbox->in_ext_byte_len && pmbox->context2) {
7534                         lpfc_sli_pcimem_bcopy(pmbox->context2,
7535                                 (uint8_t *)phba->mbox_ext,
7536                                 pmbox->in_ext_byte_len);
7537                 }
7538                 /* Copy command data to host SLIM area */
7539                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7540         } else {
7541                 /* Populate mbox extension offset word. */
7542                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7543                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7544                                 = MAILBOX_HBA_EXT_OFFSET;
7545
7546                 /* Copy the mailbox extension data */
7547                 if (pmbox->in_ext_byte_len && pmbox->context2)
7548                         lpfc_memcpy_to_slim(phba->MBslimaddr +
7549                                 MAILBOX_HBA_EXT_OFFSET,
7550                                 pmbox->context2, pmbox->in_ext_byte_len);
7551
7552                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7553                         /* copy command data into host mbox for cmpl */
7554                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7555                                               MAILBOX_CMD_SIZE);
7556
7557                 /* First copy mbox command data to HBA SLIM, skip past first
7558                    word */
7559                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7560                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7561                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
7562
7563                 /* Next copy over first word, with mbxOwner set */
7564                 ldata = *((uint32_t *)mbx);
7565                 to_slim = phba->MBslimaddr;
7566                 writel(ldata, to_slim);
7567                 readl(to_slim); /* flush */
7568
7569                 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7570                         /* switch over to host mailbox */
7571                         psli->sli_flag |= LPFC_SLI_ACTIVE;
7572         }
7573
7574         wmb();
7575
7576         switch (flag) {
7577         case MBX_NOWAIT:
7578                 /* Set up reference to mailbox command */
7579                 psli->mbox_active = pmbox;
7580                 /* Interrupt board to do it */
7581                 writel(CA_MBATT, phba->CAregaddr);
7582                 readl(phba->CAregaddr); /* flush */
7583                 /* Don't wait for it to finish, just return */
7584                 break;
7585
7586         case MBX_POLL:
7587                 /* Set up null reference to mailbox command */
7588                 psli->mbox_active = NULL;
7589                 /* Interrupt board to do it */
7590                 writel(CA_MBATT, phba->CAregaddr);
7591                 readl(phba->CAregaddr); /* flush */
7592
7593                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7594                         /* First read mbox status word */
7595                         word0 = *((uint32_t *)phba->mbox);
7596                         word0 = le32_to_cpu(word0);
7597                 } else {
7598                         /* First read mbox status word */
7599                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
7600                                 spin_unlock_irqrestore(&phba->hbalock,
7601                                                        drvr_flag);
7602                                 goto out_not_finished;
7603                         }
7604                 }
7605
7606                 /* Read the HBA Host Attention Register */
7607                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7608                         spin_unlock_irqrestore(&phba->hbalock,
7609                                                        drvr_flag);
7610                         goto out_not_finished;
7611                 }
7612                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7613                                                         1000) + jiffies;
7614                 i = 0;
7615                 /* Wait for command to complete */
7616                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7617                        (!(ha_copy & HA_MBATT) &&
7618                         (phba->link_state > LPFC_WARM_START))) {
7619                         if (time_after(jiffies, timeout)) {
7620                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7621                                 spin_unlock_irqrestore(&phba->hbalock,
7622                                                        drvr_flag);
7623                                 goto out_not_finished;
7624                         }
7625
7626                         /* Check if we took a mbox interrupt while we were
7627                            polling */
7628                         if (((word0 & OWN_CHIP) != OWN_CHIP)
7629                             && (evtctr != psli->slistat.mbox_event))
7630                                 break;
7631
7632                         if (i++ > 10) {
7633                                 spin_unlock_irqrestore(&phba->hbalock,
7634                                                        drvr_flag);
7635                                 msleep(1);
7636                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7637                         }
7638
7639                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7640                                 /* First copy command data */
7641                                 word0 = *((uint32_t *)phba->mbox);
7642                                 word0 = le32_to_cpu(word0);
7643                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7644                                         MAILBOX_t *slimmb;
7645                                         uint32_t slimword0;
7646                                         /* Check real SLIM for any errors */
7647                                         slimword0 = readl(phba->MBslimaddr);
7648                                         slimmb = (MAILBOX_t *) & slimword0;
7649                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7650                                             && slimmb->mbxStatus) {
7651                                                 psli->sli_flag &=
7652                                                     ~LPFC_SLI_ACTIVE;
7653                                                 word0 = slimword0;
7654                                         }
7655                                 }
7656                         } else {
7657                                 /* First copy command data */
7658                                 word0 = readl(phba->MBslimaddr);
7659                         }
7660                         /* Read the HBA Host Attention Register */
7661                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7662                                 spin_unlock_irqrestore(&phba->hbalock,
7663                                                        drvr_flag);
7664                                 goto out_not_finished;
7665                         }
7666                 }
7667
7668                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7669                         /* copy results back to user */
7670                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7671                                                 MAILBOX_CMD_SIZE);
7672                         /* Copy the mailbox extension data */
7673                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7674                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7675                                                       pmbox->context2,
7676                                                       pmbox->out_ext_byte_len);
7677                         }
7678                 } else {
7679                         /* First copy command data */
7680                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7681                                                 MAILBOX_CMD_SIZE);
7682                         /* Copy the mailbox extension data */
7683                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7684                                 lpfc_memcpy_from_slim(pmbox->context2,
7685                                         phba->MBslimaddr +
7686                                         MAILBOX_HBA_EXT_OFFSET,
7687                                         pmbox->out_ext_byte_len);
7688                         }
7689                 }
7690
7691                 writel(HA_MBATT, phba->HAregaddr);
7692                 readl(phba->HAregaddr); /* flush */
7693
7694                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7695                 status = mbx->mbxStatus;
7696         }
7697
7698         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7699         return status;
7700
7701 out_not_finished:
7702         if (processing_queue) {
7703                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7704                 lpfc_mbox_cmpl_put(phba, pmbox);
7705         }
7706         return MBX_NOT_FINISHED;
7707 }
7708
7709 /**
7710  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7711  * @phba: Pointer to HBA context object.
7712  *
7713  * The function blocks the posting of SLI4 asynchronous mailbox commands from
7714  * the driver internal pending mailbox queue. It will then try to wait out the
7715  * possible outstanding mailbox command before return.
7716  *
7717  * Returns:
7718  *      0 - the outstanding mailbox command completed; otherwise, the wait for
7719  *      the outstanding mailbox command timed out.
7720  **/
7721 static int
7722 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7723 {
7724         struct lpfc_sli *psli = &phba->sli;
7725         int rc = 0;
7726         unsigned long timeout = 0;
7727
7728         /* Mark the asynchronous mailbox command posting as blocked */
7729         spin_lock_irq(&phba->hbalock);
7730         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7731         /* Determine how long we might wait for the active mailbox
7732          * command to be gracefully completed by firmware.
7733          */
7734         if (phba->sli.mbox_active)
7735                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7736                                                 phba->sli.mbox_active) *
7737                                                 1000) + jiffies;
7738         spin_unlock_irq(&phba->hbalock);
7739
7740         /* Make sure the mailbox is really active */
7741         if (timeout)
7742                 lpfc_sli4_process_missed_mbox_completions(phba);
7743
7744         /* Wait for the outstnading mailbox command to complete */
7745         while (phba->sli.mbox_active) {
7746                 /* Check active mailbox complete status every 2ms */
7747                 msleep(2);
7748                 if (time_after(jiffies, timeout)) {
7749                         /* Timeout, marked the outstanding cmd not complete */
7750                         rc = 1;
7751                         break;
7752                 }
7753         }
7754
7755         /* Can not cleanly block async mailbox command, fails it */
7756         if (rc) {
7757                 spin_lock_irq(&phba->hbalock);
7758                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7759                 spin_unlock_irq(&phba->hbalock);
7760         }
7761         return rc;
7762 }
7763
7764 /**
7765  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7766  * @phba: Pointer to HBA context object.
7767  *
7768  * The function unblocks and resume posting of SLI4 asynchronous mailbox
7769  * commands from the driver internal pending mailbox queue. It makes sure
7770  * that there is no outstanding mailbox command before resuming posting
7771  * asynchronous mailbox commands. If, for any reason, there is outstanding
7772  * mailbox command, it will try to wait it out before resuming asynchronous
7773  * mailbox command posting.
7774  **/
7775 static void
7776 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7777 {
7778         struct lpfc_sli *psli = &phba->sli;
7779
7780         spin_lock_irq(&phba->hbalock);
7781         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7782                 /* Asynchronous mailbox posting is not blocked, do nothing */
7783                 spin_unlock_irq(&phba->hbalock);
7784                 return;
7785         }
7786
7787         /* Outstanding synchronous mailbox command is guaranteed to be done,
7788          * successful or timeout, after timing-out the outstanding mailbox
7789          * command shall always be removed, so just unblock posting async
7790          * mailbox command and resume
7791          */
7792         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7793         spin_unlock_irq(&phba->hbalock);
7794
7795         /* wake up worker thread to post asynchronlous mailbox command */
7796         lpfc_worker_wake_up(phba);
7797 }
7798
7799 /**
7800  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7801  * @phba: Pointer to HBA context object.
7802  * @mboxq: Pointer to mailbox object.
7803  *
7804  * The function waits for the bootstrap mailbox register ready bit from
7805  * port for twice the regular mailbox command timeout value.
7806  *
7807  *      0 - no timeout on waiting for bootstrap mailbox register ready.
7808  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7809  **/
7810 static int
7811 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7812 {
7813         uint32_t db_ready;
7814         unsigned long timeout;
7815         struct lpfc_register bmbx_reg;
7816
7817         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7818                                    * 1000) + jiffies;
7819
7820         do {
7821                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7822                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7823                 if (!db_ready)
7824                         msleep(2);
7825
7826                 if (time_after(jiffies, timeout))
7827                         return MBXERR_ERROR;
7828         } while (!db_ready);
7829
7830         return 0;
7831 }
7832
7833 /**
7834  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7835  * @phba: Pointer to HBA context object.
7836  * @mboxq: Pointer to mailbox object.
7837  *
7838  * The function posts a mailbox to the port.  The mailbox is expected
7839  * to be comletely filled in and ready for the port to operate on it.
7840  * This routine executes a synchronous completion operation on the
7841  * mailbox by polling for its completion.
7842  *
7843  * The caller must not be holding any locks when calling this routine.
7844  *
7845  * Returns:
7846  *      MBX_SUCCESS - mailbox posted successfully
7847  *      Any of the MBX error values.
7848  **/
7849 static int
7850 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7851 {
7852         int rc = MBX_SUCCESS;
7853         unsigned long iflag;
7854         uint32_t mcqe_status;
7855         uint32_t mbx_cmnd;
7856         struct lpfc_sli *psli = &phba->sli;
7857         struct lpfc_mqe *mb = &mboxq->u.mqe;
7858         struct lpfc_bmbx_create *mbox_rgn;
7859         struct dma_address *dma_address;
7860
7861         /*
7862          * Only one mailbox can be active to the bootstrap mailbox region
7863          * at a time and there is no queueing provided.
7864          */
7865         spin_lock_irqsave(&phba->hbalock, iflag);
7866         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7867                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7868                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7869                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7870                                 "cannot issue Data: x%x x%x\n",
7871                                 mboxq->vport ? mboxq->vport->vpi : 0,
7872                                 mboxq->u.mb.mbxCommand,
7873                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7874                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7875                                 psli->sli_flag, MBX_POLL);
7876                 return MBXERR_ERROR;
7877         }
7878         /* The server grabs the token and owns it until release */
7879         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7880         phba->sli.mbox_active = mboxq;
7881         spin_unlock_irqrestore(&phba->hbalock, iflag);
7882
7883         /* wait for bootstrap mbox register for readyness */
7884         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7885         if (rc)
7886                 goto exit;
7887
7888         /*
7889          * Initialize the bootstrap memory region to avoid stale data areas
7890          * in the mailbox post.  Then copy the caller's mailbox contents to
7891          * the bmbx mailbox region.
7892          */
7893         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7894         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7895         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7896                               sizeof(struct lpfc_mqe));
7897
7898         /* Post the high mailbox dma address to the port and wait for ready. */
7899         dma_address = &phba->sli4_hba.bmbx.dma_address;
7900         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7901
7902         /* wait for bootstrap mbox register for hi-address write done */
7903         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7904         if (rc)
7905                 goto exit;
7906
7907         /* Post the low mailbox dma address to the port. */
7908         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7909
7910         /* wait for bootstrap mbox register for low address write done */
7911         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7912         if (rc)
7913                 goto exit;
7914
7915         /*
7916          * Read the CQ to ensure the mailbox has completed.
7917          * If so, update the mailbox status so that the upper layers
7918          * can complete the request normally.
7919          */
7920         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7921                               sizeof(struct lpfc_mqe));
7922         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7923         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7924                               sizeof(struct lpfc_mcqe));
7925         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7926         /*
7927          * When the CQE status indicates a failure and the mailbox status
7928          * indicates success then copy the CQE status into the mailbox status
7929          * (and prefix it with x4000).
7930          */
7931         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7932                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7933                         bf_set(lpfc_mqe_status, mb,
7934                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
7935                 rc = MBXERR_ERROR;
7936         } else
7937                 lpfc_sli4_swap_str(phba, mboxq);
7938
7939         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7940                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7941                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7942                         " x%x x%x CQ: x%x x%x x%x x%x\n",
7943                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7944                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7945                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7946                         bf_get(lpfc_mqe_status, mb),
7947                         mb->un.mb_words[0], mb->un.mb_words[1],
7948                         mb->un.mb_words[2], mb->un.mb_words[3],
7949                         mb->un.mb_words[4], mb->un.mb_words[5],
7950                         mb->un.mb_words[6], mb->un.mb_words[7],
7951                         mb->un.mb_words[8], mb->un.mb_words[9],
7952                         mb->un.mb_words[10], mb->un.mb_words[11],
7953                         mb->un.mb_words[12], mboxq->mcqe.word0,
7954                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
7955                         mboxq->mcqe.trailer);
7956 exit:
7957         /* We are holding the token, no needed for lock when release */
7958         spin_lock_irqsave(&phba->hbalock, iflag);
7959         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7960         phba->sli.mbox_active = NULL;
7961         spin_unlock_irqrestore(&phba->hbalock, iflag);
7962         return rc;
7963 }
7964
7965 /**
7966  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7967  * @phba: Pointer to HBA context object.
7968  * @pmbox: Pointer to mailbox object.
7969  * @flag: Flag indicating how the mailbox need to be processed.
7970  *
7971  * This function is called by discovery code and HBA management code to submit
7972  * a mailbox command to firmware with SLI-4 interface spec.
7973  *
7974  * Return codes the caller owns the mailbox command after the return of the
7975  * function.
7976  **/
7977 static int
7978 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7979                        uint32_t flag)
7980 {
7981         struct lpfc_sli *psli = &phba->sli;
7982         unsigned long iflags;
7983         int rc;
7984
7985         /* dump from issue mailbox command if setup */
7986         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7987
7988         rc = lpfc_mbox_dev_check(phba);
7989         if (unlikely(rc)) {
7990                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7991                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7992                                 "cannot issue Data: x%x x%x\n",
7993                                 mboxq->vport ? mboxq->vport->vpi : 0,
7994                                 mboxq->u.mb.mbxCommand,
7995                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7996                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7997                                 psli->sli_flag, flag);
7998                 goto out_not_finished;
7999         }
8000
8001         /* Detect polling mode and jump to a handler */
8002         if (!phba->sli4_hba.intr_enable) {
8003                 if (flag == MBX_POLL)
8004                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8005                 else
8006                         rc = -EIO;
8007                 if (rc != MBX_SUCCESS)
8008                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8009                                         "(%d):2541 Mailbox command x%x "
8010                                         "(x%x/x%x) failure: "
8011                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8012                                         "Data: x%x x%x\n,",
8013                                         mboxq->vport ? mboxq->vport->vpi : 0,
8014                                         mboxq->u.mb.mbxCommand,
8015                                         lpfc_sli_config_mbox_subsys_get(phba,
8016                                                                         mboxq),
8017                                         lpfc_sli_config_mbox_opcode_get(phba,
8018                                                                         mboxq),
8019                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8020                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8021                                         bf_get(lpfc_mcqe_ext_status,
8022                                                &mboxq->mcqe),
8023                                         psli->sli_flag, flag);
8024                 return rc;
8025         } else if (flag == MBX_POLL) {
8026                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8027                                 "(%d):2542 Try to issue mailbox command "
8028                                 "x%x (x%x/x%x) synchronously ahead of async"
8029                                 "mailbox command queue: x%x x%x\n",
8030                                 mboxq->vport ? mboxq->vport->vpi : 0,
8031                                 mboxq->u.mb.mbxCommand,
8032                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8033                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8034                                 psli->sli_flag, flag);
8035                 /* Try to block the asynchronous mailbox posting */
8036                 rc = lpfc_sli4_async_mbox_block(phba);
8037                 if (!rc) {
8038                         /* Successfully blocked, now issue sync mbox cmd */
8039                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8040                         if (rc != MBX_SUCCESS)
8041                                 lpfc_printf_log(phba, KERN_WARNING,
8042                                         LOG_MBOX | LOG_SLI,
8043                                         "(%d):2597 Sync Mailbox command "
8044                                         "x%x (x%x/x%x) failure: "
8045                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
8046                                         "Data: x%x x%x\n,",
8047                                         mboxq->vport ? mboxq->vport->vpi : 0,
8048                                         mboxq->u.mb.mbxCommand,
8049                                         lpfc_sli_config_mbox_subsys_get(phba,
8050                                                                         mboxq),
8051                                         lpfc_sli_config_mbox_opcode_get(phba,
8052                                                                         mboxq),
8053                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8054                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8055                                         bf_get(lpfc_mcqe_ext_status,
8056                                                &mboxq->mcqe),
8057                                         psli->sli_flag, flag);
8058                         /* Unblock the async mailbox posting afterward */
8059                         lpfc_sli4_async_mbox_unblock(phba);
8060                 }
8061                 return rc;
8062         }
8063
8064         /* Now, interrupt mode asynchrous mailbox command */
8065         rc = lpfc_mbox_cmd_check(phba, mboxq);
8066         if (rc) {
8067                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8068                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
8069                                 "cannot issue Data: x%x x%x\n",
8070                                 mboxq->vport ? mboxq->vport->vpi : 0,
8071                                 mboxq->u.mb.mbxCommand,
8072                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8073                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8074                                 psli->sli_flag, flag);
8075                 goto out_not_finished;
8076         }
8077
8078         /* Put the mailbox command to the driver internal FIFO */
8079         psli->slistat.mbox_busy++;
8080         spin_lock_irqsave(&phba->hbalock, iflags);
8081         lpfc_mbox_put(phba, mboxq);
8082         spin_unlock_irqrestore(&phba->hbalock, iflags);
8083         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8084                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
8085                         "x%x (x%x/x%x) x%x x%x x%x\n",
8086                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8087                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8088                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8089                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8090                         phba->pport->port_state,
8091                         psli->sli_flag, MBX_NOWAIT);
8092         /* Wake up worker thread to transport mailbox command from head */
8093         lpfc_worker_wake_up(phba);
8094
8095         return MBX_BUSY;
8096
8097 out_not_finished:
8098         return MBX_NOT_FINISHED;
8099 }
8100
8101 /**
8102  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8103  * @phba: Pointer to HBA context object.
8104  *
8105  * This function is called by worker thread to send a mailbox command to
8106  * SLI4 HBA firmware.
8107  *
8108  **/
8109 int
8110 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8111 {
8112         struct lpfc_sli *psli = &phba->sli;
8113         LPFC_MBOXQ_t *mboxq;
8114         int rc = MBX_SUCCESS;
8115         unsigned long iflags;
8116         struct lpfc_mqe *mqe;
8117         uint32_t mbx_cmnd;
8118
8119         /* Check interrupt mode before post async mailbox command */
8120         if (unlikely(!phba->sli4_hba.intr_enable))
8121                 return MBX_NOT_FINISHED;
8122
8123         /* Check for mailbox command service token */
8124         spin_lock_irqsave(&phba->hbalock, iflags);
8125         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8126                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8127                 return MBX_NOT_FINISHED;
8128         }
8129         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8130                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8131                 return MBX_NOT_FINISHED;
8132         }
8133         if (unlikely(phba->sli.mbox_active)) {
8134                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8135                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8136                                 "0384 There is pending active mailbox cmd\n");
8137                 return MBX_NOT_FINISHED;
8138         }
8139         /* Take the mailbox command service token */
8140         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8141
8142         /* Get the next mailbox command from head of queue */
8143         mboxq = lpfc_mbox_get(phba);
8144
8145         /* If no more mailbox command waiting for post, we're done */
8146         if (!mboxq) {
8147                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8148                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8149                 return MBX_SUCCESS;
8150         }
8151         phba->sli.mbox_active = mboxq;
8152         spin_unlock_irqrestore(&phba->hbalock, iflags);
8153
8154         /* Check device readiness for posting mailbox command */
8155         rc = lpfc_mbox_dev_check(phba);
8156         if (unlikely(rc))
8157                 /* Driver clean routine will clean up pending mailbox */
8158                 goto out_not_finished;
8159
8160         /* Prepare the mbox command to be posted */
8161         mqe = &mboxq->u.mqe;
8162         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8163
8164         /* Start timer for the mbox_tmo and log some mailbox post messages */
8165         mod_timer(&psli->mbox_tmo, (jiffies +
8166                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8167
8168         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8169                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8170                         "x%x x%x\n",
8171                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8172                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8173                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8174                         phba->pport->port_state, psli->sli_flag);
8175
8176         if (mbx_cmnd != MBX_HEARTBEAT) {
8177                 if (mboxq->vport) {
8178                         lpfc_debugfs_disc_trc(mboxq->vport,
8179                                 LPFC_DISC_TRC_MBOX_VPORT,
8180                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8181                                 mbx_cmnd, mqe->un.mb_words[0],
8182                                 mqe->un.mb_words[1]);
8183                 } else {
8184                         lpfc_debugfs_disc_trc(phba->pport,
8185                                 LPFC_DISC_TRC_MBOX,
8186                                 "MBOX Send: cmd:x%x mb:x%x x%x",
8187                                 mbx_cmnd, mqe->un.mb_words[0],
8188                                 mqe->un.mb_words[1]);
8189                 }
8190         }
8191         psli->slistat.mbox_cmd++;
8192
8193         /* Post the mailbox command to the port */
8194         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8195         if (rc != MBX_SUCCESS) {
8196                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8197                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
8198                                 "cannot issue Data: x%x x%x\n",
8199                                 mboxq->vport ? mboxq->vport->vpi : 0,
8200                                 mboxq->u.mb.mbxCommand,
8201                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8202                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8203                                 psli->sli_flag, MBX_NOWAIT);
8204                 goto out_not_finished;
8205         }
8206
8207         return rc;
8208
8209 out_not_finished:
8210         spin_lock_irqsave(&phba->hbalock, iflags);
8211         if (phba->sli.mbox_active) {
8212                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8213                 __lpfc_mbox_cmpl_put(phba, mboxq);
8214                 /* Release the token */
8215                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8216                 phba->sli.mbox_active = NULL;
8217         }
8218         spin_unlock_irqrestore(&phba->hbalock, iflags);
8219
8220         return MBX_NOT_FINISHED;
8221 }
8222
8223 /**
8224  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8225  * @phba: Pointer to HBA context object.
8226  * @pmbox: Pointer to mailbox object.
8227  * @flag: Flag indicating how the mailbox need to be processed.
8228  *
8229  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8230  * the API jump table function pointer from the lpfc_hba struct.
8231  *
8232  * Return codes the caller owns the mailbox command after the return of the
8233  * function.
8234  **/
8235 int
8236 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8237 {
8238         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8239 }
8240
8241 /**
8242  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8243  * @phba: The hba struct for which this call is being executed.
8244  * @dev_grp: The HBA PCI-Device group number.
8245  *
8246  * This routine sets up the mbox interface API function jump table in @phba
8247  * struct.
8248  * Returns: 0 - success, -ENODEV - failure.
8249  **/
8250 int
8251 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8252 {
8253
8254         switch (dev_grp) {
8255         case LPFC_PCI_DEV_LP:
8256                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8257                 phba->lpfc_sli_handle_slow_ring_event =
8258                                 lpfc_sli_handle_slow_ring_event_s3;
8259                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8260                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8261                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8262                 break;
8263         case LPFC_PCI_DEV_OC:
8264                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8265                 phba->lpfc_sli_handle_slow_ring_event =
8266                                 lpfc_sli_handle_slow_ring_event_s4;
8267                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8268                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8269                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8270                 break;
8271         default:
8272                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8273                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
8274                                 dev_grp);
8275                 return -ENODEV;
8276                 break;
8277         }
8278         return 0;
8279 }
8280
8281 /**
8282  * __lpfc_sli_ringtx_put - Add an iocb to the txq
8283  * @phba: Pointer to HBA context object.
8284  * @pring: Pointer to driver SLI ring object.
8285  * @piocb: Pointer to address of newly added command iocb.
8286  *
8287  * This function is called with hbalock held to add a command
8288  * iocb to the txq when SLI layer cannot submit the command iocb
8289  * to the ring.
8290  **/
8291 void
8292 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8293                     struct lpfc_iocbq *piocb)
8294 {
8295         lockdep_assert_held(&phba->hbalock);
8296         /* Insert the caller's iocb in the txq tail for later processing. */
8297         list_add_tail(&piocb->list, &pring->txq);
8298 }
8299
8300 /**
8301  * lpfc_sli_next_iocb - Get the next iocb in the txq
8302  * @phba: Pointer to HBA context object.
8303  * @pring: Pointer to driver SLI ring object.
8304  * @piocb: Pointer to address of newly added command iocb.
8305  *
8306  * This function is called with hbalock held before a new
8307  * iocb is submitted to the firmware. This function checks
8308  * txq to flush the iocbs in txq to Firmware before
8309  * submitting new iocbs to the Firmware.
8310  * If there are iocbs in the txq which need to be submitted
8311  * to firmware, lpfc_sli_next_iocb returns the first element
8312  * of the txq after dequeuing it from txq.
8313  * If there is no iocb in the txq then the function will return
8314  * *piocb and *piocb is set to NULL. Caller needs to check
8315  * *piocb to find if there are more commands in the txq.
8316  **/
8317 static struct lpfc_iocbq *
8318 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8319                    struct lpfc_iocbq **piocb)
8320 {
8321         struct lpfc_iocbq * nextiocb;
8322
8323         lockdep_assert_held(&phba->hbalock);
8324
8325         nextiocb = lpfc_sli_ringtx_get(phba, pring);
8326         if (!nextiocb) {
8327                 nextiocb = *piocb;
8328                 *piocb = NULL;
8329         }
8330
8331         return nextiocb;
8332 }
8333
8334 /**
8335  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8336  * @phba: Pointer to HBA context object.
8337  * @ring_number: SLI ring number to issue iocb on.
8338  * @piocb: Pointer to command iocb.
8339  * @flag: Flag indicating if this command can be put into txq.
8340  *
8341  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8342  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8343  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8344  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8345  * this function allows only iocbs for posting buffers. This function finds
8346  * next available slot in the command ring and posts the command to the
8347  * available slot and writes the port attention register to request HBA start
8348  * processing new iocb. If there is no slot available in the ring and
8349  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8350  * the function returns IOCB_BUSY.
8351  *
8352  * This function is called with hbalock held. The function will return success
8353  * after it successfully submit the iocb to firmware or after adding to the
8354  * txq.
8355  **/
8356 static int
8357 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8358                     struct lpfc_iocbq *piocb, uint32_t flag)
8359 {
8360         struct lpfc_iocbq *nextiocb;
8361         IOCB_t *iocb;
8362         struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
8363
8364         lockdep_assert_held(&phba->hbalock);
8365
8366         if (piocb->iocb_cmpl && (!piocb->vport) &&
8367            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8368            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8369                 lpfc_printf_log(phba, KERN_ERR,
8370                                 LOG_SLI | LOG_VPORT,
8371                                 "1807 IOCB x%x failed. No vport\n",
8372                                 piocb->iocb.ulpCommand);
8373                 dump_stack();
8374                 return IOCB_ERROR;
8375         }
8376
8377
8378         /* If the PCI channel is in offline state, do not post iocbs. */
8379         if (unlikely(pci_channel_offline(phba->pcidev)))
8380                 return IOCB_ERROR;
8381
8382         /* If HBA has a deferred error attention, fail the iocb. */
8383         if (unlikely(phba->hba_flag & DEFER_ERATT))
8384                 return IOCB_ERROR;
8385
8386         /*
8387          * We should never get an IOCB if we are in a < LINK_DOWN state
8388          */
8389         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8390                 return IOCB_ERROR;
8391
8392         /*
8393          * Check to see if we are blocking IOCB processing because of a
8394          * outstanding event.
8395          */
8396         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8397                 goto iocb_busy;
8398
8399         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8400                 /*
8401                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8402                  * can be issued if the link is not up.
8403                  */
8404                 switch (piocb->iocb.ulpCommand) {
8405                 case CMD_GEN_REQUEST64_CR:
8406                 case CMD_GEN_REQUEST64_CX:
8407                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8408                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8409                                         FC_RCTL_DD_UNSOL_CMD) ||
8410                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8411                                         MENLO_TRANSPORT_TYPE))
8412
8413                                 goto iocb_busy;
8414                         break;
8415                 case CMD_QUE_RING_BUF_CN:
8416                 case CMD_QUE_RING_BUF64_CN:
8417                         /*
8418                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8419                          * completion, iocb_cmpl MUST be 0.
8420                          */
8421                         if (piocb->iocb_cmpl)
8422                                 piocb->iocb_cmpl = NULL;
8423                         /*FALLTHROUGH*/
8424                 case CMD_CREATE_XRI_CR:
8425                 case CMD_CLOSE_XRI_CN:
8426                 case CMD_CLOSE_XRI_CX:
8427                         break;
8428                 default:
8429                         goto iocb_busy;
8430                 }
8431
8432         /*
8433          * For FCP commands, we must be in a state where we can process link
8434          * attention events.
8435          */
8436         } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
8437                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8438                 goto iocb_busy;
8439         }
8440
8441         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8442                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8443                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8444
8445         if (iocb)
8446                 lpfc_sli_update_ring(phba, pring);
8447         else
8448                 lpfc_sli_update_full_ring(phba, pring);
8449
8450         if (!piocb)
8451                 return IOCB_SUCCESS;
8452
8453         goto out_busy;
8454
8455  iocb_busy:
8456         pring->stats.iocb_cmd_delay++;
8457
8458  out_busy:
8459
8460         if (!(flag & SLI_IOCB_RET_IOCB)) {
8461                 __lpfc_sli_ringtx_put(phba, pring, piocb);
8462                 return IOCB_SUCCESS;
8463         }
8464
8465         return IOCB_BUSY;
8466 }
8467
8468 /**
8469  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8470  * @phba: Pointer to HBA context object.
8471  * @piocb: Pointer to command iocb.
8472  * @sglq: Pointer to the scatter gather queue object.
8473  *
8474  * This routine converts the bpl or bde that is in the IOCB
8475  * to a sgl list for the sli4 hardware. The physical address
8476  * of the bpl/bde is converted back to a virtual address.
8477  * If the IOCB contains a BPL then the list of BDE's is
8478  * converted to sli4_sge's. If the IOCB contains a single
8479  * BDE then it is converted to a single sli_sge.
8480  * The IOCB is still in cpu endianess so the contents of
8481  * the bpl can be used without byte swapping.
8482  *
8483  * Returns valid XRI = Success, NO_XRI = Failure.
8484 **/
8485 static uint16_t
8486 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8487                 struct lpfc_sglq *sglq)
8488 {
8489         uint16_t xritag = NO_XRI;
8490         struct ulp_bde64 *bpl = NULL;
8491         struct ulp_bde64 bde;
8492         struct sli4_sge *sgl  = NULL;
8493         struct lpfc_dmabuf *dmabuf;
8494         IOCB_t *icmd;
8495         int numBdes = 0;
8496         int i = 0;
8497         uint32_t offset = 0; /* accumulated offset in the sg request list */
8498         int inbound = 0; /* number of sg reply entries inbound from firmware */
8499
8500         if (!piocbq || !sglq)
8501                 return xritag;
8502
8503         sgl  = (struct sli4_sge *)sglq->sgl;
8504         icmd = &piocbq->iocb;
8505         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8506                 return sglq->sli4_xritag;
8507         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8508                 numBdes = icmd->un.genreq64.bdl.bdeSize /
8509                                 sizeof(struct ulp_bde64);
8510                 /* The addrHigh and addrLow fields within the IOCB
8511                  * have not been byteswapped yet so there is no
8512                  * need to swap them back.
8513                  */
8514                 if (piocbq->context3)
8515                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8516                 else
8517                         return xritag;
8518
8519                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
8520                 if (!bpl)
8521                         return xritag;
8522
8523                 for (i = 0; i < numBdes; i++) {
8524                         /* Should already be byte swapped. */
8525                         sgl->addr_hi = bpl->addrHigh;
8526                         sgl->addr_lo = bpl->addrLow;
8527
8528                         sgl->word2 = le32_to_cpu(sgl->word2);
8529                         if ((i+1) == numBdes)
8530                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
8531                         else
8532                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
8533                         /* swap the size field back to the cpu so we
8534                          * can assign it to the sgl.
8535                          */
8536                         bde.tus.w = le32_to_cpu(bpl->tus.w);
8537                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8538                         /* The offsets in the sgl need to be accumulated
8539                          * separately for the request and reply lists.
8540                          * The request is always first, the reply follows.
8541                          */
8542                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8543                                 /* add up the reply sg entries */
8544                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8545                                         inbound++;
8546                                 /* first inbound? reset the offset */
8547                                 if (inbound == 1)
8548                                         offset = 0;
8549                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8550                                 bf_set(lpfc_sli4_sge_type, sgl,
8551                                         LPFC_SGE_TYPE_DATA);
8552                                 offset += bde.tus.f.bdeSize;
8553                         }
8554                         sgl->word2 = cpu_to_le32(sgl->word2);
8555                         bpl++;
8556                         sgl++;
8557                 }
8558         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8559                         /* The addrHigh and addrLow fields of the BDE have not
8560                          * been byteswapped yet so they need to be swapped
8561                          * before putting them in the sgl.
8562                          */
8563                         sgl->addr_hi =
8564                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8565                         sgl->addr_lo =
8566                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8567                         sgl->word2 = le32_to_cpu(sgl->word2);
8568                         bf_set(lpfc_sli4_sge_last, sgl, 1);
8569                         sgl->word2 = cpu_to_le32(sgl->word2);
8570                         sgl->sge_len =
8571                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8572         }
8573         return sglq->sli4_xritag;
8574 }
8575
8576 /**
8577  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8578  * @phba: Pointer to HBA context object.
8579  * @piocb: Pointer to command iocb.
8580  * @wqe: Pointer to the work queue entry.
8581  *
8582  * This routine converts the iocb command to its Work Queue Entry
8583  * equivalent. The wqe pointer should not have any fields set when
8584  * this routine is called because it will memcpy over them.
8585  * This routine does not set the CQ_ID or the WQEC bits in the
8586  * wqe.
8587  *
8588  * Returns: 0 = Success, IOCB_ERROR = Failure.
8589  **/
8590 static int
8591 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8592                 union lpfc_wqe *wqe)
8593 {
8594         uint32_t xmit_len = 0, total_len = 0;
8595         uint8_t ct = 0;
8596         uint32_t fip;
8597         uint32_t abort_tag;
8598         uint8_t command_type = ELS_COMMAND_NON_FIP;
8599         uint8_t cmnd;
8600         uint16_t xritag;
8601         uint16_t abrt_iotag;
8602         struct lpfc_iocbq *abrtiocbq;
8603         struct ulp_bde64 *bpl = NULL;
8604         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8605         int numBdes, i;
8606         struct ulp_bde64 bde;
8607         struct lpfc_nodelist *ndlp;
8608         uint32_t *pcmd;
8609         uint32_t if_type;
8610
8611         fip = phba->hba_flag & HBA_FIP_SUPPORT;
8612         /* The fcp commands will set command type */
8613         if (iocbq->iocb_flag &  LPFC_IO_FCP)
8614                 command_type = FCP_COMMAND;
8615         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8616                 command_type = ELS_COMMAND_FIP;
8617         else
8618                 command_type = ELS_COMMAND_NON_FIP;
8619
8620         if (phba->fcp_embed_io)
8621                 memset(wqe, 0, sizeof(union lpfc_wqe128));
8622         /* Some of the fields are in the right position already */
8623         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8624         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8625         wqe->generic.wqe_com.word10 = 0;
8626
8627         abort_tag = (uint32_t) iocbq->iotag;
8628         xritag = iocbq->sli4_xritag;
8629         /* words0-2 bpl convert bde */
8630         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8631                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8632                                 sizeof(struct ulp_bde64);
8633                 bpl  = (struct ulp_bde64 *)
8634                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8635                 if (!bpl)
8636                         return IOCB_ERROR;
8637
8638                 /* Should already be byte swapped. */
8639                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
8640                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
8641                 /* swap the size field back to the cpu so we
8642                  * can assign it to the sgl.
8643                  */
8644                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
8645                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8646                 total_len = 0;
8647                 for (i = 0; i < numBdes; i++) {
8648                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
8649                         total_len += bde.tus.f.bdeSize;
8650                 }
8651         } else
8652                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8653
8654         iocbq->iocb.ulpIoTag = iocbq->iotag;
8655         cmnd = iocbq->iocb.ulpCommand;
8656
8657         switch (iocbq->iocb.ulpCommand) {
8658         case CMD_ELS_REQUEST64_CR:
8659                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8660                         ndlp = iocbq->context_un.ndlp;
8661                 else
8662                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
8663                 if (!iocbq->iocb.ulpLe) {
8664                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8665                                 "2007 Only Limited Edition cmd Format"
8666                                 " supported 0x%x\n",
8667                                 iocbq->iocb.ulpCommand);
8668                         return IOCB_ERROR;
8669                 }
8670
8671                 wqe->els_req.payload_len = xmit_len;
8672                 /* Els_reguest64 has a TMO */
8673                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8674                         iocbq->iocb.ulpTimeout);
8675                 /* Need a VF for word 4 set the vf bit*/
8676                 bf_set(els_req64_vf, &wqe->els_req, 0);
8677                 /* And a VFID for word 12 */
8678                 bf_set(els_req64_vfid, &wqe->els_req, 0);
8679                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8680                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8681                        iocbq->iocb.ulpContext);
8682                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8683                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8684                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8685                 if (command_type == ELS_COMMAND_FIP)
8686                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8687                                         >> LPFC_FIP_ELS_ID_SHIFT);
8688                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8689                                         iocbq->context2)->virt);
8690                 if_type = bf_get(lpfc_sli_intf_if_type,
8691                                         &phba->sli4_hba.sli_intf);
8692                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8693                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8694                                 *pcmd == ELS_CMD_SCR ||
8695                                 *pcmd == ELS_CMD_FDISC ||
8696                                 *pcmd == ELS_CMD_LOGO ||
8697                                 *pcmd == ELS_CMD_PLOGI)) {
8698                                 bf_set(els_req64_sp, &wqe->els_req, 1);
8699                                 bf_set(els_req64_sid, &wqe->els_req,
8700                                         iocbq->vport->fc_myDID);
8701                                 if ((*pcmd == ELS_CMD_FLOGI) &&
8702                                         !(phba->fc_topology ==
8703                                                 LPFC_TOPOLOGY_LOOP))
8704                                         bf_set(els_req64_sid, &wqe->els_req, 0);
8705                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8706                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8707                                         phba->vpi_ids[iocbq->vport->vpi]);
8708                         } else if (pcmd && iocbq->context1) {
8709                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8710                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8711                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8712                         }
8713                 }
8714                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8715                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8716                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8717                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8718                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8719                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8720                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8721                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8722                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
8723                 break;
8724         case CMD_XMIT_SEQUENCE64_CX:
8725                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8726                        iocbq->iocb.un.ulpWord[3]);
8727                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8728                        iocbq->iocb.unsli3.rcvsli3.ox_id);
8729                 /* The entire sequence is transmitted for this IOCB */
8730                 xmit_len = total_len;
8731                 cmnd = CMD_XMIT_SEQUENCE64_CR;
8732                 if (phba->link_flag & LS_LOOPBACK_MODE)
8733                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8734         case CMD_XMIT_SEQUENCE64_CR:
8735                 /* word3 iocb=io_tag32 wqe=reserved */
8736                 wqe->xmit_sequence.rsvd3 = 0;
8737                 /* word4 relative_offset memcpy */
8738                 /* word5 r_ctl/df_ctl memcpy */
8739                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8740                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8741                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8742                        LPFC_WQE_IOD_WRITE);
8743                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8744                        LPFC_WQE_LENLOC_WORD12);
8745                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8746                 wqe->xmit_sequence.xmit_len = xmit_len;
8747                 command_type = OTHER_COMMAND;
8748                 break;
8749         case CMD_XMIT_BCAST64_CN:
8750                 /* word3 iocb=iotag32 wqe=seq_payload_len */
8751                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8752                 /* word4 iocb=rsvd wqe=rsvd */
8753                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8754                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8755                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8756                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8757                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8758                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8759                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8760                        LPFC_WQE_LENLOC_WORD3);
8761                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8762                 break;
8763         case CMD_FCP_IWRITE64_CR:
8764                 command_type = FCP_COMMAND_DATA_OUT;
8765                 /* word3 iocb=iotag wqe=payload_offset_len */
8766                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8767                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8768                        xmit_len + sizeof(struct fcp_rsp));
8769                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8770                        0);
8771                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8772                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8773                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8774                        iocbq->iocb.ulpFCP2Rcvy);
8775                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8776                 /* Always open the exchange */
8777                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8778                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8779                        LPFC_WQE_LENLOC_WORD4);
8780                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8781                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8782                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8783                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8784                         bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8785                         if (iocbq->priority) {
8786                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8787                                        (iocbq->priority << 1));
8788                         } else {
8789                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8790                                        (phba->cfg_XLanePriority << 1));
8791                         }
8792                 }
8793                 /* Note, word 10 is already initialized to 0 */
8794
8795                 if (phba->fcp_embed_io) {
8796                         struct lpfc_scsi_buf *lpfc_cmd;
8797                         struct sli4_sge *sgl;
8798                         union lpfc_wqe128 *wqe128;
8799                         struct fcp_cmnd *fcp_cmnd;
8800                         uint32_t *ptr;
8801
8802                         /* 128 byte wqe support here */
8803                         wqe128 = (union lpfc_wqe128 *)wqe;
8804
8805                         lpfc_cmd = iocbq->context1;
8806                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8807                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
8808
8809                         /* Word 0-2 - FCP_CMND */
8810                         wqe128->generic.bde.tus.f.bdeFlags =
8811                                 BUFF_TYPE_BDE_IMMED;
8812                         wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8813                         wqe128->generic.bde.addrHigh = 0;
8814                         wqe128->generic.bde.addrLow =  88;  /* Word 22 */
8815
8816                         bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8817
8818                         /* Word 22-29  FCP CMND Payload */
8819                         ptr = &wqe128->words[22];
8820                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8821                 }
8822                 break;
8823         case CMD_FCP_IREAD64_CR:
8824                 /* word3 iocb=iotag wqe=payload_offset_len */
8825                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8826                 bf_set(payload_offset_len, &wqe->fcp_iread,
8827                        xmit_len + sizeof(struct fcp_rsp));
8828                 bf_set(cmd_buff_len, &wqe->fcp_iread,
8829                        0);
8830                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8831                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8832                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8833                        iocbq->iocb.ulpFCP2Rcvy);
8834                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8835                 /* Always open the exchange */
8836                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8837                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8838                        LPFC_WQE_LENLOC_WORD4);
8839                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8840                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8841                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8842                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8843                         bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8844                         if (iocbq->priority) {
8845                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8846                                        (iocbq->priority << 1));
8847                         } else {
8848                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8849                                        (phba->cfg_XLanePriority << 1));
8850                         }
8851                 }
8852                 /* Note, word 10 is already initialized to 0 */
8853
8854                 if (phba->fcp_embed_io) {
8855                         struct lpfc_scsi_buf *lpfc_cmd;
8856                         struct sli4_sge *sgl;
8857                         union lpfc_wqe128 *wqe128;
8858                         struct fcp_cmnd *fcp_cmnd;
8859                         uint32_t *ptr;
8860
8861                         /* 128 byte wqe support here */
8862                         wqe128 = (union lpfc_wqe128 *)wqe;
8863
8864                         lpfc_cmd = iocbq->context1;
8865                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8866                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
8867
8868                         /* Word 0-2 - FCP_CMND */
8869                         wqe128->generic.bde.tus.f.bdeFlags =
8870                                 BUFF_TYPE_BDE_IMMED;
8871                         wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8872                         wqe128->generic.bde.addrHigh = 0;
8873                         wqe128->generic.bde.addrLow =  88;  /* Word 22 */
8874
8875                         bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8876
8877                         /* Word 22-29  FCP CMND Payload */
8878                         ptr = &wqe128->words[22];
8879                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8880                 }
8881                 break;
8882         case CMD_FCP_ICMND64_CR:
8883                 /* word3 iocb=iotag wqe=payload_offset_len */
8884                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8885                 bf_set(payload_offset_len, &wqe->fcp_icmd,
8886                        xmit_len + sizeof(struct fcp_rsp));
8887                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8888                        0);
8889                 /* word3 iocb=IO_TAG wqe=reserved */
8890                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8891                 /* Always open the exchange */
8892                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8893                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8894                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8895                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8896                        LPFC_WQE_LENLOC_NONE);
8897                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8898                        iocbq->iocb.ulpFCP2Rcvy);
8899                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8900                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8901                         bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8902                         if (iocbq->priority) {
8903                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8904                                        (iocbq->priority << 1));
8905                         } else {
8906                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8907                                        (phba->cfg_XLanePriority << 1));
8908                         }
8909                 }
8910                 /* Note, word 10 is already initialized to 0 */
8911
8912                 if (phba->fcp_embed_io) {
8913                         struct lpfc_scsi_buf *lpfc_cmd;
8914                         struct sli4_sge *sgl;
8915                         union lpfc_wqe128 *wqe128;
8916                         struct fcp_cmnd *fcp_cmnd;
8917                         uint32_t *ptr;
8918
8919                         /* 128 byte wqe support here */
8920                         wqe128 = (union lpfc_wqe128 *)wqe;
8921
8922                         lpfc_cmd = iocbq->context1;
8923                         sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8924                         fcp_cmnd = lpfc_cmd->fcp_cmnd;
8925
8926                         /* Word 0-2 - FCP_CMND */
8927                         wqe128->generic.bde.tus.f.bdeFlags =
8928                                 BUFF_TYPE_BDE_IMMED;
8929                         wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8930                         wqe128->generic.bde.addrHigh = 0;
8931                         wqe128->generic.bde.addrLow =  88;  /* Word 22 */
8932
8933                         bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8934
8935                         /* Word 22-29  FCP CMND Payload */
8936                         ptr = &wqe128->words[22];
8937                         memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8938                 }
8939                 break;
8940         case CMD_GEN_REQUEST64_CR:
8941                 /* For this command calculate the xmit length of the
8942                  * request bde.
8943                  */
8944                 xmit_len = 0;
8945                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8946                         sizeof(struct ulp_bde64);
8947                 for (i = 0; i < numBdes; i++) {
8948                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8949                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8950                                 break;
8951                         xmit_len += bde.tus.f.bdeSize;
8952                 }
8953                 /* word3 iocb=IO_TAG wqe=request_payload_len */
8954                 wqe->gen_req.request_payload_len = xmit_len;
8955                 /* word4 iocb=parameter wqe=relative_offset memcpy */
8956                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8957                 /* word6 context tag copied in memcpy */
8958                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
8959                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8960                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8961                                 "2015 Invalid CT %x command 0x%x\n",
8962                                 ct, iocbq->iocb.ulpCommand);
8963                         return IOCB_ERROR;
8964                 }
8965                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8966                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8967                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8968                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8969                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8970                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8971                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8972                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8973                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
8974                 command_type = OTHER_COMMAND;
8975                 break;
8976         case CMD_XMIT_ELS_RSP64_CX:
8977                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8978                 /* words0-2 BDE memcpy */
8979                 /* word3 iocb=iotag32 wqe=response_payload_len */
8980                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8981                 /* word4 */
8982                 wqe->xmit_els_rsp.word4 = 0;
8983                 /* word5 iocb=rsvd wge=did */
8984                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8985                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
8986
8987                 if_type = bf_get(lpfc_sli_intf_if_type,
8988                                         &phba->sli4_hba.sli_intf);
8989                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8990                         if (iocbq->vport->fc_flag & FC_PT2PT) {
8991                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8992                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8993                                         iocbq->vport->fc_myDID);
8994                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
8995                                         bf_set(wqe_els_did,
8996                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
8997                                 }
8998                         }
8999                 }
9000                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9001                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9002                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9003                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
9004                        iocbq->iocb.unsli3.rcvsli3.ox_id);
9005                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
9006                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9007                                phba->vpi_ids[iocbq->vport->vpi]);
9008                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9009                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9010                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9011                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9012                        LPFC_WQE_LENLOC_WORD3);
9013                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9014                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9015                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9016                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9017                                         iocbq->context2)->virt);
9018                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9019                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9020                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9021                                         iocbq->vport->fc_myDID);
9022                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9023                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9024                                         phba->vpi_ids[phba->pport->vpi]);
9025                 }
9026                 command_type = OTHER_COMMAND;
9027                 break;
9028         case CMD_CLOSE_XRI_CN:
9029         case CMD_ABORT_XRI_CN:
9030         case CMD_ABORT_XRI_CX:
9031                 /* words 0-2 memcpy should be 0 rserved */
9032                 /* port will send abts */
9033                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9034                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9035                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9036                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9037                 } else
9038                         fip = 0;
9039
9040                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9041                         /*
9042                          * The link is down, or the command was ELS_FIP
9043                          * so the fw does not need to send abts
9044                          * on the wire.
9045                          */
9046                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9047                 else
9048                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9049                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9050                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9051                 wqe->abort_cmd.rsrvd5 = 0;
9052                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9053                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9054                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9055                 /*
9056                  * The abort handler will send us CMD_ABORT_XRI_CN or
9057                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9058                  */
9059                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9060                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9061                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9062                        LPFC_WQE_LENLOC_NONE);
9063                 cmnd = CMD_ABORT_XRI_CX;
9064                 command_type = OTHER_COMMAND;
9065                 xritag = 0;
9066                 break;
9067         case CMD_XMIT_BLS_RSP64_CX:
9068                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9069                 /* As BLS ABTS RSP WQE is very different from other WQEs,
9070                  * we re-construct this WQE here based on information in
9071                  * iocbq from scratch.
9072                  */
9073                 memset(wqe, 0, sizeof(union lpfc_wqe));
9074                 /* OX_ID is invariable to who sent ABTS to CT exchange */
9075                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9076                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9077                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9078                     LPFC_ABTS_UNSOL_INT) {
9079                         /* ABTS sent by initiator to CT exchange, the
9080                          * RX_ID field will be filled with the newly
9081                          * allocated responder XRI.
9082                          */
9083                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9084                                iocbq->sli4_xritag);
9085                 } else {
9086                         /* ABTS sent by responder to CT exchange, the
9087                          * RX_ID field will be filled with the responder
9088                          * RX_ID from ABTS.
9089                          */
9090                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9091                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9092                 }
9093                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9094                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9095
9096                 /* Use CT=VPI */
9097                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9098                         ndlp->nlp_DID);
9099                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9100                         iocbq->iocb.ulpContext);
9101                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9102                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9103                         phba->vpi_ids[phba->pport->vpi]);
9104                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9105                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9106                        LPFC_WQE_LENLOC_NONE);
9107                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9108                 command_type = OTHER_COMMAND;
9109                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9110                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9111                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9112                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9113                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9114                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9115                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9116                 }
9117
9118                 break;
9119         case CMD_XRI_ABORTED_CX:
9120         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9121         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9122         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9123         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9124         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9125         default:
9126                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9127                                 "2014 Invalid command 0x%x\n",
9128                                 iocbq->iocb.ulpCommand);
9129                 return IOCB_ERROR;
9130                 break;
9131         }
9132
9133         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9134                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9135         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9136                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9137         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9138                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9139         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9140                               LPFC_IO_DIF_INSERT);
9141         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9142         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9143         wqe->generic.wqe_com.abort_tag = abort_tag;
9144         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9145         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9146         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9147         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9148         return 0;
9149 }
9150
9151 /**
9152  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9153  * @phba: Pointer to HBA context object.
9154  * @ring_number: SLI ring number to issue iocb on.
9155  * @piocb: Pointer to command iocb.
9156  * @flag: Flag indicating if this command can be put into txq.
9157  *
9158  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9159  * an iocb command to an HBA with SLI-4 interface spec.
9160  *
9161  * This function is called with hbalock held. The function will return success
9162  * after it successfully submit the iocb to firmware or after adding to the
9163  * txq.
9164  **/
9165 static int
9166 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9167                          struct lpfc_iocbq *piocb, uint32_t flag)
9168 {
9169         struct lpfc_sglq *sglq;
9170         union lpfc_wqe *wqe;
9171         union lpfc_wqe128 wqe128;
9172         struct lpfc_queue *wq;
9173         struct lpfc_sli_ring *pring;
9174
9175         /* Get the WQ */
9176         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9177             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9178                 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9179                         wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9180                 else
9181                         wq = phba->sli4_hba.oas_wq;
9182         } else {
9183                 wq = phba->sli4_hba.els_wq;
9184         }
9185
9186         /* Get corresponding ring */
9187         pring = wq->pring;
9188
9189         /*
9190          * The WQE can be either 64 or 128 bytes,
9191          * so allocate space on the stack assuming the largest.
9192          */
9193         wqe = (union lpfc_wqe *)&wqe128;
9194
9195         lockdep_assert_held(&phba->hbalock);
9196
9197         if (piocb->sli4_xritag == NO_XRI) {
9198                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9199                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9200                         sglq = NULL;
9201                 else {
9202                         if (!list_empty(&pring->txq)) {
9203                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
9204                                         __lpfc_sli_ringtx_put(phba,
9205                                                 pring, piocb);
9206                                         return IOCB_SUCCESS;
9207                                 } else {
9208                                         return IOCB_BUSY;
9209                                 }
9210                         } else {
9211                                 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9212                                 if (!sglq) {
9213                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
9214                                                 __lpfc_sli_ringtx_put(phba,
9215                                                                 pring,
9216                                                                 piocb);
9217                                                 return IOCB_SUCCESS;
9218                                         } else
9219                                                 return IOCB_BUSY;
9220                                 }
9221                         }
9222                 }
9223         } else if (piocb->iocb_flag &  LPFC_IO_FCP)
9224                 /* These IO's already have an XRI and a mapped sgl. */
9225                 sglq = NULL;
9226         else {
9227                 /*
9228                  * This is a continuation of a commandi,(CX) so this
9229                  * sglq is on the active list
9230                  */
9231                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9232                 if (!sglq)
9233                         return IOCB_ERROR;
9234         }
9235
9236         if (sglq) {
9237                 piocb->sli4_lxritag = sglq->sli4_lxritag;
9238                 piocb->sli4_xritag = sglq->sli4_xritag;
9239                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9240                         return IOCB_ERROR;
9241         }
9242
9243         if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
9244                 return IOCB_ERROR;
9245
9246         if (lpfc_sli4_wq_put(wq, wqe))
9247                 return IOCB_ERROR;
9248         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9249
9250         return 0;
9251 }
9252
9253 /**
9254  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9255  *
9256  * This routine wraps the actual lockless version for issusing IOCB function
9257  * pointer from the lpfc_hba struct.
9258  *
9259  * Return codes:
9260  * IOCB_ERROR - Error
9261  * IOCB_SUCCESS - Success
9262  * IOCB_BUSY - Busy
9263  **/
9264 int
9265 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9266                 struct lpfc_iocbq *piocb, uint32_t flag)
9267 {
9268         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9269 }
9270
9271 /**
9272  * lpfc_sli_api_table_setup - Set up sli api function jump table
9273  * @phba: The hba struct for which this call is being executed.
9274  * @dev_grp: The HBA PCI-Device group number.
9275  *
9276  * This routine sets up the SLI interface API function jump table in @phba
9277  * struct.
9278  * Returns: 0 - success, -ENODEV - failure.
9279  **/
9280 int
9281 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9282 {
9283
9284         switch (dev_grp) {
9285         case LPFC_PCI_DEV_LP:
9286                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9287                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9288                 break;
9289         case LPFC_PCI_DEV_OC:
9290                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9291                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9292                 break;
9293         default:
9294                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9295                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
9296                                 dev_grp);
9297                 return -ENODEV;
9298                 break;
9299         }
9300         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9301         return 0;
9302 }
9303
9304 /**
9305  * lpfc_sli4_calc_ring - Calculates which ring to use
9306  * @phba: Pointer to HBA context object.
9307  * @piocb: Pointer to command iocb.
9308  *
9309  * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9310  * hba_wqidx, thus we need to calculate the corresponding ring.
9311  * Since ABORTS must go on the same WQ of the command they are
9312  * aborting, we use command's hba_wqidx.
9313  */
9314 struct lpfc_sli_ring *
9315 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9316 {
9317         if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9318                 if (!(phba->cfg_fof) ||
9319                     (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9320                         if (unlikely(!phba->sli4_hba.fcp_wq))
9321                                 return NULL;
9322                         /*
9323                          * for abort iocb hba_wqidx should already
9324                          * be setup based on what work queue we used.
9325                          */
9326                         if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9327                                 piocb->hba_wqidx =
9328                                         lpfc_sli4_scmd_to_wqidx_distr(phba,
9329                                                               piocb->context1);
9330                         return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
9331                 } else {
9332                         if (unlikely(!phba->sli4_hba.oas_wq))
9333                                 return NULL;
9334                         piocb->hba_wqidx = 0;
9335                         return phba->sli4_hba.oas_wq->pring;
9336                 }
9337         } else {
9338                 if (unlikely(!phba->sli4_hba.els_wq))
9339                         return NULL;
9340                 piocb->hba_wqidx = 0;
9341                 return phba->sli4_hba.els_wq->pring;
9342         }
9343 }
9344
9345 /**
9346  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9347  * @phba: Pointer to HBA context object.
9348  * @pring: Pointer to driver SLI ring object.
9349  * @piocb: Pointer to command iocb.
9350  * @flag: Flag indicating if this command can be put into txq.
9351  *
9352  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9353  * function. This function gets the hbalock and calls
9354  * __lpfc_sli_issue_iocb function and will return the error returned
9355  * by __lpfc_sli_issue_iocb function. This wrapper is used by
9356  * functions which do not hold hbalock.
9357  **/
9358 int
9359 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9360                     struct lpfc_iocbq *piocb, uint32_t flag)
9361 {
9362         struct lpfc_hba_eq_hdl *hba_eq_hdl;
9363         struct lpfc_sli_ring *pring;
9364         struct lpfc_queue *fpeq;
9365         struct lpfc_eqe *eqe;
9366         unsigned long iflags;
9367         int rc, idx;
9368
9369         if (phba->sli_rev == LPFC_SLI_REV4) {
9370                 pring = lpfc_sli4_calc_ring(phba, piocb);
9371                 if (unlikely(pring == NULL))
9372                         return IOCB_ERROR;
9373
9374                 spin_lock_irqsave(&pring->ring_lock, iflags);
9375                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9376                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9377
9378                 if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
9379                         idx = piocb->hba_wqidx;
9380                         hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
9381
9382                         if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
9383
9384                                 /* Get associated EQ with this index */
9385                                 fpeq = phba->sli4_hba.hba_eq[idx];
9386
9387                                 /* Turn off interrupts from this EQ */
9388                                 lpfc_sli4_eq_clr_intr(fpeq);
9389
9390                                 /*
9391                                  * Process all the events on FCP EQ
9392                                  */
9393                                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9394                                         lpfc_sli4_hba_handle_eqe(phba,
9395                                                 eqe, idx);
9396                                         fpeq->EQ_processed++;
9397                                 }
9398
9399                                 /* Always clear and re-arm the EQ */
9400                                 lpfc_sli4_eq_release(fpeq,
9401                                         LPFC_QUEUE_REARM);
9402                         }
9403                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
9404                 }
9405         } else {
9406                 /* For now, SLI2/3 will still use hbalock */
9407                 spin_lock_irqsave(&phba->hbalock, iflags);
9408                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9409                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9410         }
9411         return rc;
9412 }
9413
9414 /**
9415  * lpfc_extra_ring_setup - Extra ring setup function
9416  * @phba: Pointer to HBA context object.
9417  *
9418  * This function is called while driver attaches with the
9419  * HBA to setup the extra ring. The extra ring is used
9420  * only when driver needs to support target mode functionality
9421  * or IP over FC functionalities.
9422  *
9423  * This function is called with no lock held. SLI3 only.
9424  **/
9425 static int
9426 lpfc_extra_ring_setup( struct lpfc_hba *phba)
9427 {
9428         struct lpfc_sli *psli;
9429         struct lpfc_sli_ring *pring;
9430
9431         psli = &phba->sli;
9432
9433         /* Adjust cmd/rsp ring iocb entries more evenly */
9434
9435         /* Take some away from the FCP ring */
9436         pring = &psli->sli3_ring[LPFC_FCP_RING];
9437         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9438         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9439         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9440         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9441
9442         /* and give them to the extra ring */
9443         pring = &psli->sli3_ring[LPFC_EXTRA_RING];
9444
9445         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9446         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9447         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9448         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9449
9450         /* Setup default profile for this ring */
9451         pring->iotag_max = 4096;
9452         pring->num_mask = 1;
9453         pring->prt[0].profile = 0;      /* Mask 0 */
9454         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9455         pring->prt[0].type = phba->cfg_multi_ring_type;
9456         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9457         return 0;
9458 }
9459
9460 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9461  * @phba: Pointer to HBA context object.
9462  * @iocbq: Pointer to iocb object.
9463  *
9464  * The async_event handler calls this routine when it receives
9465  * an ASYNC_STATUS_CN event from the port.  The port generates
9466  * this event when an Abort Sequence request to an rport fails
9467  * twice in succession.  The abort could be originated by the
9468  * driver or by the port.  The ABTS could have been for an ELS
9469  * or FCP IO.  The port only generates this event when an ABTS
9470  * fails to complete after one retry.
9471  */
9472 static void
9473 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9474                           struct lpfc_iocbq *iocbq)
9475 {
9476         struct lpfc_nodelist *ndlp = NULL;
9477         uint16_t rpi = 0, vpi = 0;
9478         struct lpfc_vport *vport = NULL;
9479
9480         /* The rpi in the ulpContext is vport-sensitive. */
9481         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9482         rpi = iocbq->iocb.ulpContext;
9483
9484         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9485                         "3092 Port generated ABTS async event "
9486                         "on vpi %d rpi %d status 0x%x\n",
9487                         vpi, rpi, iocbq->iocb.ulpStatus);
9488
9489         vport = lpfc_find_vport_by_vpid(phba, vpi);
9490         if (!vport)
9491                 goto err_exit;
9492         ndlp = lpfc_findnode_rpi(vport, rpi);
9493         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9494                 goto err_exit;
9495
9496         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9497                 lpfc_sli_abts_recover_port(vport, ndlp);
9498         return;
9499
9500  err_exit:
9501         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9502                         "3095 Event Context not found, no "
9503                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9504                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9505                         vpi, rpi);
9506 }
9507
9508 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9509  * @phba: pointer to HBA context object.
9510  * @ndlp: nodelist pointer for the impacted rport.
9511  * @axri: pointer to the wcqe containing the failed exchange.
9512  *
9513  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9514  * port.  The port generates this event when an abort exchange request to an
9515  * rport fails twice in succession with no reply.  The abort could be originated
9516  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
9517  */
9518 void
9519 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9520                            struct lpfc_nodelist *ndlp,
9521                            struct sli4_wcqe_xri_aborted *axri)
9522 {
9523         struct lpfc_vport *vport;
9524         uint32_t ext_status = 0;
9525
9526         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9527                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9528                                 "3115 Node Context not found, driver "
9529                                 "ignoring abts err event\n");
9530                 return;
9531         }
9532
9533         vport = ndlp->vport;
9534         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9535                         "3116 Port generated FCP XRI ABORT event on "
9536                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9537                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9538                         bf_get(lpfc_wcqe_xa_xri, axri),
9539                         bf_get(lpfc_wcqe_xa_status, axri),
9540                         axri->parameter);
9541
9542         /*
9543          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
9544          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9545          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9546          */
9547         ext_status = axri->parameter & IOERR_PARAM_MASK;
9548         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9549             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9550                 lpfc_sli_abts_recover_port(vport, ndlp);
9551 }
9552
9553 /**
9554  * lpfc_sli_async_event_handler - ASYNC iocb handler function
9555  * @phba: Pointer to HBA context object.
9556  * @pring: Pointer to driver SLI ring object.
9557  * @iocbq: Pointer to iocb object.
9558  *
9559  * This function is called by the slow ring event handler
9560  * function when there is an ASYNC event iocb in the ring.
9561  * This function is called with no lock held.
9562  * Currently this function handles only temperature related
9563  * ASYNC events. The function decodes the temperature sensor
9564  * event message and posts events for the management applications.
9565  **/
9566 static void
9567 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9568         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9569 {
9570         IOCB_t *icmd;
9571         uint16_t evt_code;
9572         struct temp_event temp_event_data;
9573         struct Scsi_Host *shost;
9574         uint32_t *iocb_w;
9575
9576         icmd = &iocbq->iocb;
9577         evt_code = icmd->un.asyncstat.evt_code;
9578
9579         switch (evt_code) {
9580         case ASYNC_TEMP_WARN:
9581         case ASYNC_TEMP_SAFE:
9582                 temp_event_data.data = (uint32_t) icmd->ulpContext;
9583                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9584                 if (evt_code == ASYNC_TEMP_WARN) {
9585                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9586                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9587                                 "0347 Adapter is very hot, please take "
9588                                 "corrective action. temperature : %d Celsius\n",
9589                                 (uint32_t) icmd->ulpContext);
9590                 } else {
9591                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
9592                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9593                                 "0340 Adapter temperature is OK now. "
9594                                 "temperature : %d Celsius\n",
9595                                 (uint32_t) icmd->ulpContext);
9596                 }
9597
9598                 /* Send temperature change event to applications */
9599                 shost = lpfc_shost_from_vport(phba->pport);
9600                 fc_host_post_vendor_event(shost, fc_get_event_number(),
9601                         sizeof(temp_event_data), (char *) &temp_event_data,
9602                         LPFC_NL_VENDOR_ID);
9603                 break;
9604         case ASYNC_STATUS_CN:
9605                 lpfc_sli_abts_err_handler(phba, iocbq);
9606                 break;
9607         default:
9608                 iocb_w = (uint32_t *) icmd;
9609                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9610                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
9611                         " evt_code 0x%x\n"
9612                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
9613                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
9614                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
9615                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9616                         pring->ringno, icmd->un.asyncstat.evt_code,
9617                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9618                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9619                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9620                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9621
9622                 break;
9623         }
9624 }
9625
9626
9627 /**
9628  * lpfc_sli4_setup - SLI ring setup function
9629  * @phba: Pointer to HBA context object.
9630  *
9631  * lpfc_sli_setup sets up rings of the SLI interface with
9632  * number of iocbs per ring and iotags. This function is
9633  * called while driver attach to the HBA and before the
9634  * interrupts are enabled. So there is no need for locking.
9635  *
9636  * This function always returns 0.
9637  **/
9638 int
9639 lpfc_sli4_setup(struct lpfc_hba *phba)
9640 {
9641         struct lpfc_sli_ring *pring;
9642
9643         pring = phba->sli4_hba.els_wq->pring;
9644         pring->num_mask = LPFC_MAX_RING_MASK;
9645         pring->prt[0].profile = 0;      /* Mask 0 */
9646         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9647         pring->prt[0].type = FC_TYPE_ELS;
9648         pring->prt[0].lpfc_sli_rcv_unsol_event =
9649             lpfc_els_unsol_event;
9650         pring->prt[1].profile = 0;      /* Mask 1 */
9651         pring->prt[1].rctl = FC_RCTL_ELS_REP;
9652         pring->prt[1].type = FC_TYPE_ELS;
9653         pring->prt[1].lpfc_sli_rcv_unsol_event =
9654             lpfc_els_unsol_event;
9655         pring->prt[2].profile = 0;      /* Mask 2 */
9656         /* NameServer Inquiry */
9657         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9658         /* NameServer */
9659         pring->prt[2].type = FC_TYPE_CT;
9660         pring->prt[2].lpfc_sli_rcv_unsol_event =
9661             lpfc_ct_unsol_event;
9662         pring->prt[3].profile = 0;      /* Mask 3 */
9663         /* NameServer response */
9664         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9665         /* NameServer */
9666         pring->prt[3].type = FC_TYPE_CT;
9667         pring->prt[3].lpfc_sli_rcv_unsol_event =
9668             lpfc_ct_unsol_event;
9669         return 0;
9670 }
9671
9672 /**
9673  * lpfc_sli_setup - SLI ring setup function
9674  * @phba: Pointer to HBA context object.
9675  *
9676  * lpfc_sli_setup sets up rings of the SLI interface with
9677  * number of iocbs per ring and iotags. This function is
9678  * called while driver attach to the HBA and before the
9679  * interrupts are enabled. So there is no need for locking.
9680  *
9681  * This function always returns 0. SLI3 only.
9682  **/
9683 int
9684 lpfc_sli_setup(struct lpfc_hba *phba)
9685 {
9686         int i, totiocbsize = 0;
9687         struct lpfc_sli *psli = &phba->sli;
9688         struct lpfc_sli_ring *pring;
9689
9690         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9691         psli->sli_flag = 0;
9692
9693         psli->iocbq_lookup = NULL;
9694         psli->iocbq_lookup_len = 0;
9695         psli->last_iotag = 0;
9696
9697         for (i = 0; i < psli->num_rings; i++) {
9698                 pring = &psli->sli3_ring[i];
9699                 switch (i) {
9700                 case LPFC_FCP_RING:     /* ring 0 - FCP */
9701                         /* numCiocb and numRiocb are used in config_port */
9702                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9703                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9704                         pring->sli.sli3.numCiocb +=
9705                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9706                         pring->sli.sli3.numRiocb +=
9707                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9708                         pring->sli.sli3.numCiocb +=
9709                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9710                         pring->sli.sli3.numRiocb +=
9711                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9712                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9713                                                         SLI3_IOCB_CMD_SIZE :
9714                                                         SLI2_IOCB_CMD_SIZE;
9715                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9716                                                         SLI3_IOCB_RSP_SIZE :
9717                                                         SLI2_IOCB_RSP_SIZE;
9718                         pring->iotag_ctr = 0;
9719                         pring->iotag_max =
9720                             (phba->cfg_hba_queue_depth * 2);
9721                         pring->fast_iotag = pring->iotag_max;
9722                         pring->num_mask = 0;
9723                         break;
9724                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
9725                         /* numCiocb and numRiocb are used in config_port */
9726                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9727                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9728                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9729                                                         SLI3_IOCB_CMD_SIZE :
9730                                                         SLI2_IOCB_CMD_SIZE;
9731                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9732                                                         SLI3_IOCB_RSP_SIZE :
9733                                                         SLI2_IOCB_RSP_SIZE;
9734                         pring->iotag_max = phba->cfg_hba_queue_depth;
9735                         pring->num_mask = 0;
9736                         break;
9737                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
9738                         /* numCiocb and numRiocb are used in config_port */
9739                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9740                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9741                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9742                                                         SLI3_IOCB_CMD_SIZE :
9743                                                         SLI2_IOCB_CMD_SIZE;
9744                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9745                                                         SLI3_IOCB_RSP_SIZE :
9746                                                         SLI2_IOCB_RSP_SIZE;
9747                         pring->fast_iotag = 0;
9748                         pring->iotag_ctr = 0;
9749                         pring->iotag_max = 4096;
9750                         pring->lpfc_sli_rcv_async_status =
9751                                 lpfc_sli_async_event_handler;
9752                         pring->num_mask = LPFC_MAX_RING_MASK;
9753                         pring->prt[0].profile = 0;      /* Mask 0 */
9754                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9755                         pring->prt[0].type = FC_TYPE_ELS;
9756                         pring->prt[0].lpfc_sli_rcv_unsol_event =
9757                             lpfc_els_unsol_event;
9758                         pring->prt[1].profile = 0;      /* Mask 1 */
9759                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
9760                         pring->prt[1].type = FC_TYPE_ELS;
9761                         pring->prt[1].lpfc_sli_rcv_unsol_event =
9762                             lpfc_els_unsol_event;
9763                         pring->prt[2].profile = 0;      /* Mask 2 */
9764                         /* NameServer Inquiry */
9765                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9766                         /* NameServer */
9767                         pring->prt[2].type = FC_TYPE_CT;
9768                         pring->prt[2].lpfc_sli_rcv_unsol_event =
9769                             lpfc_ct_unsol_event;
9770                         pring->prt[3].profile = 0;      /* Mask 3 */
9771                         /* NameServer response */
9772                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9773                         /* NameServer */
9774                         pring->prt[3].type = FC_TYPE_CT;
9775                         pring->prt[3].lpfc_sli_rcv_unsol_event =
9776                             lpfc_ct_unsol_event;
9777                         break;
9778                 }
9779                 totiocbsize += (pring->sli.sli3.numCiocb *
9780                         pring->sli.sli3.sizeCiocb) +
9781                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
9782         }
9783         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
9784                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
9785                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9786                        "SLI2 SLIM Data: x%x x%lx\n",
9787                        phba->brd_no, totiocbsize,
9788                        (unsigned long) MAX_SLIM_IOCB_SIZE);
9789         }
9790         if (phba->cfg_multi_ring_support == 2)
9791                 lpfc_extra_ring_setup(phba);
9792
9793         return 0;
9794 }
9795
9796 /**
9797  * lpfc_sli4_queue_init - Queue initialization function
9798  * @phba: Pointer to HBA context object.
9799  *
9800  * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
9801  * ring. This function also initializes ring indices of each ring.
9802  * This function is called during the initialization of the SLI
9803  * interface of an HBA.
9804  * This function is called with no lock held and always returns
9805  * 1.
9806  **/
9807 void
9808 lpfc_sli4_queue_init(struct lpfc_hba *phba)
9809 {
9810         struct lpfc_sli *psli;
9811         struct lpfc_sli_ring *pring;
9812         int i;
9813
9814         psli = &phba->sli;
9815         spin_lock_irq(&phba->hbalock);
9816         INIT_LIST_HEAD(&psli->mboxq);
9817         INIT_LIST_HEAD(&psli->mboxq_cmpl);
9818         /* Initialize list headers for txq and txcmplq as double linked lists */
9819         for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9820                 pring = phba->sli4_hba.fcp_wq[i]->pring;
9821                 pring->flag = 0;
9822                 pring->ringno = LPFC_FCP_RING;
9823                 INIT_LIST_HEAD(&pring->txq);
9824                 INIT_LIST_HEAD(&pring->txcmplq);
9825                 INIT_LIST_HEAD(&pring->iocb_continueq);
9826                 spin_lock_init(&pring->ring_lock);
9827         }
9828         for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9829                 pring = phba->sli4_hba.nvme_wq[i]->pring;
9830                 pring->flag = 0;
9831                 pring->ringno = LPFC_FCP_RING;
9832                 INIT_LIST_HEAD(&pring->txq);
9833                 INIT_LIST_HEAD(&pring->txcmplq);
9834                 INIT_LIST_HEAD(&pring->iocb_continueq);
9835                 spin_lock_init(&pring->ring_lock);
9836         }
9837         pring = phba->sli4_hba.els_wq->pring;
9838         pring->flag = 0;
9839         pring->ringno = LPFC_ELS_RING;
9840         INIT_LIST_HEAD(&pring->txq);
9841         INIT_LIST_HEAD(&pring->txcmplq);
9842         INIT_LIST_HEAD(&pring->iocb_continueq);
9843         spin_lock_init(&pring->ring_lock);
9844
9845         if (phba->cfg_nvme_io_channel) {
9846                 pring = phba->sli4_hba.nvmels_wq->pring;
9847                 pring->flag = 0;
9848                 pring->ringno = LPFC_ELS_RING;
9849                 INIT_LIST_HEAD(&pring->txq);
9850                 INIT_LIST_HEAD(&pring->txcmplq);
9851                 INIT_LIST_HEAD(&pring->iocb_continueq);
9852                 spin_lock_init(&pring->ring_lock);
9853         }
9854
9855         if (phba->cfg_fof) {
9856                 pring = phba->sli4_hba.oas_wq->pring;
9857                 pring->flag = 0;
9858                 pring->ringno = LPFC_FCP_RING;
9859                 INIT_LIST_HEAD(&pring->txq);
9860                 INIT_LIST_HEAD(&pring->txcmplq);
9861                 INIT_LIST_HEAD(&pring->iocb_continueq);
9862                 spin_lock_init(&pring->ring_lock);
9863         }
9864
9865         spin_unlock_irq(&phba->hbalock);
9866 }
9867
9868 /**
9869  * lpfc_sli_queue_init - Queue initialization function
9870  * @phba: Pointer to HBA context object.
9871  *
9872  * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9873  * ring. This function also initializes ring indices of each ring.
9874  * This function is called during the initialization of the SLI
9875  * interface of an HBA.
9876  * This function is called with no lock held and always returns
9877  * 1.
9878  **/
9879 void
9880 lpfc_sli_queue_init(struct lpfc_hba *phba)
9881 {
9882         struct lpfc_sli *psli;
9883         struct lpfc_sli_ring *pring;
9884         int i;
9885
9886         psli = &phba->sli;
9887         spin_lock_irq(&phba->hbalock);
9888         INIT_LIST_HEAD(&psli->mboxq);
9889         INIT_LIST_HEAD(&psli->mboxq_cmpl);
9890         /* Initialize list headers for txq and txcmplq as double linked lists */
9891         for (i = 0; i < psli->num_rings; i++) {
9892                 pring = &psli->sli3_ring[i];
9893                 pring->ringno = i;
9894                 pring->sli.sli3.next_cmdidx  = 0;
9895                 pring->sli.sli3.local_getidx = 0;
9896                 pring->sli.sli3.cmdidx = 0;
9897                 INIT_LIST_HEAD(&pring->iocb_continueq);
9898                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9899                 INIT_LIST_HEAD(&pring->postbufq);
9900                 pring->flag = 0;
9901                 INIT_LIST_HEAD(&pring->txq);
9902                 INIT_LIST_HEAD(&pring->txcmplq);
9903                 spin_lock_init(&pring->ring_lock);
9904         }
9905         spin_unlock_irq(&phba->hbalock);
9906 }
9907
9908 /**
9909  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9910  * @phba: Pointer to HBA context object.
9911  *
9912  * This routine flushes the mailbox command subsystem. It will unconditionally
9913  * flush all the mailbox commands in the three possible stages in the mailbox
9914  * command sub-system: pending mailbox command queue; the outstanding mailbox
9915  * command; and completed mailbox command queue. It is caller's responsibility
9916  * to make sure that the driver is in the proper state to flush the mailbox
9917  * command sub-system. Namely, the posting of mailbox commands into the
9918  * pending mailbox command queue from the various clients must be stopped;
9919  * either the HBA is in a state that it will never works on the outstanding
9920  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9921  * mailbox command has been completed.
9922  **/
9923 static void
9924 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9925 {
9926         LIST_HEAD(completions);
9927         struct lpfc_sli *psli = &phba->sli;
9928         LPFC_MBOXQ_t *pmb;
9929         unsigned long iflag;
9930
9931         /* Flush all the mailbox commands in the mbox system */
9932         spin_lock_irqsave(&phba->hbalock, iflag);
9933         /* The pending mailbox command queue */
9934         list_splice_init(&phba->sli.mboxq, &completions);
9935         /* The outstanding active mailbox command */
9936         if (psli->mbox_active) {
9937                 list_add_tail(&psli->mbox_active->list, &completions);
9938                 psli->mbox_active = NULL;
9939                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9940         }
9941         /* The completed mailbox command queue */
9942         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9943         spin_unlock_irqrestore(&phba->hbalock, iflag);
9944
9945         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9946         while (!list_empty(&completions)) {
9947                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9948                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9949                 if (pmb->mbox_cmpl)
9950                         pmb->mbox_cmpl(phba, pmb);
9951         }
9952 }
9953
9954 /**
9955  * lpfc_sli_host_down - Vport cleanup function
9956  * @vport: Pointer to virtual port object.
9957  *
9958  * lpfc_sli_host_down is called to clean up the resources
9959  * associated with a vport before destroying virtual
9960  * port data structures.
9961  * This function does following operations:
9962  * - Free discovery resources associated with this virtual
9963  *   port.
9964  * - Free iocbs associated with this virtual port in
9965  *   the txq.
9966  * - Send abort for all iocb commands associated with this
9967  *   vport in txcmplq.
9968  *
9969  * This function is called with no lock held and always returns 1.
9970  **/
9971 int
9972 lpfc_sli_host_down(struct lpfc_vport *vport)
9973 {
9974         LIST_HEAD(completions);
9975         struct lpfc_hba *phba = vport->phba;
9976         struct lpfc_sli *psli = &phba->sli;
9977         struct lpfc_queue *qp = NULL;
9978         struct lpfc_sli_ring *pring;
9979         struct lpfc_iocbq *iocb, *next_iocb;
9980         int i;
9981         unsigned long flags = 0;
9982         uint16_t prev_pring_flag;
9983
9984         lpfc_cleanup_discovery_resources(vport);
9985
9986         spin_lock_irqsave(&phba->hbalock, flags);
9987
9988         /*
9989          * Error everything on the txq since these iocbs
9990          * have not been given to the FW yet.
9991          * Also issue ABTS for everything on the txcmplq
9992          */
9993         if (phba->sli_rev != LPFC_SLI_REV4) {
9994                 for (i = 0; i < psli->num_rings; i++) {
9995                         pring = &psli->sli3_ring[i];
9996                         prev_pring_flag = pring->flag;
9997                         /* Only slow rings */
9998                         if (pring->ringno == LPFC_ELS_RING) {
9999                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10000                                 /* Set the lpfc data pending flag */
10001                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10002                         }
10003                         list_for_each_entry_safe(iocb, next_iocb,
10004                                                  &pring->txq, list) {
10005                                 if (iocb->vport != vport)
10006                                         continue;
10007                                 list_move_tail(&iocb->list, &completions);
10008                         }
10009                         list_for_each_entry_safe(iocb, next_iocb,
10010                                                  &pring->txcmplq, list) {
10011                                 if (iocb->vport != vport)
10012                                         continue;
10013                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10014                         }
10015                         pring->flag = prev_pring_flag;
10016                 }
10017         } else {
10018                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10019                         pring = qp->pring;
10020                         if (!pring)
10021                                 continue;
10022                         if (pring == phba->sli4_hba.els_wq->pring) {
10023                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10024                                 /* Set the lpfc data pending flag */
10025                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10026                         }
10027                         prev_pring_flag = pring->flag;
10028                         spin_lock_irq(&pring->ring_lock);
10029                         list_for_each_entry_safe(iocb, next_iocb,
10030                                                  &pring->txq, list) {
10031                                 if (iocb->vport != vport)
10032                                         continue;
10033                                 list_move_tail(&iocb->list, &completions);
10034                         }
10035                         spin_unlock_irq(&pring->ring_lock);
10036                         list_for_each_entry_safe(iocb, next_iocb,
10037                                                  &pring->txcmplq, list) {
10038                                 if (iocb->vport != vport)
10039                                         continue;
10040                                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10041                         }
10042                         pring->flag = prev_pring_flag;
10043                 }
10044         }
10045         spin_unlock_irqrestore(&phba->hbalock, flags);
10046
10047         /* Cancel all the IOCBs from the completions list */
10048         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10049                               IOERR_SLI_DOWN);
10050         return 1;
10051 }
10052
10053 /**
10054  * lpfc_sli_hba_down - Resource cleanup function for the HBA
10055  * @phba: Pointer to HBA context object.
10056  *
10057  * This function cleans up all iocb, buffers, mailbox commands
10058  * while shutting down the HBA. This function is called with no
10059  * lock held and always returns 1.
10060  * This function does the following to cleanup driver resources:
10061  * - Free discovery resources for each virtual port
10062  * - Cleanup any pending fabric iocbs
10063  * - Iterate through the iocb txq and free each entry
10064  *   in the list.
10065  * - Free up any buffer posted to the HBA
10066  * - Free mailbox commands in the mailbox queue.
10067  **/
10068 int
10069 lpfc_sli_hba_down(struct lpfc_hba *phba)
10070 {
10071         LIST_HEAD(completions);
10072         struct lpfc_sli *psli = &phba->sli;
10073         struct lpfc_queue *qp = NULL;
10074         struct lpfc_sli_ring *pring;
10075         struct lpfc_dmabuf *buf_ptr;
10076         unsigned long flags = 0;
10077         int i;
10078
10079         /* Shutdown the mailbox command sub-system */
10080         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10081
10082         lpfc_hba_down_prep(phba);
10083
10084         lpfc_fabric_abort_hba(phba);
10085
10086         spin_lock_irqsave(&phba->hbalock, flags);
10087
10088         /*
10089          * Error everything on the txq since these iocbs
10090          * have not been given to the FW yet.
10091          */
10092         if (phba->sli_rev != LPFC_SLI_REV4) {
10093                 for (i = 0; i < psli->num_rings; i++) {
10094                         pring = &psli->sli3_ring[i];
10095                         /* Only slow rings */
10096                         if (pring->ringno == LPFC_ELS_RING) {
10097                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10098                                 /* Set the lpfc data pending flag */
10099                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10100                         }
10101                         list_splice_init(&pring->txq, &completions);
10102                 }
10103         } else {
10104                 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10105                         pring = qp->pring;
10106                         if (!pring)
10107                                 continue;
10108                         spin_lock_irq(&pring->ring_lock);
10109                         list_splice_init(&pring->txq, &completions);
10110                         spin_unlock_irq(&pring->ring_lock);
10111                         if (pring == phba->sli4_hba.els_wq->pring) {
10112                                 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10113                                 /* Set the lpfc data pending flag */
10114                                 set_bit(LPFC_DATA_READY, &phba->data_flags);
10115                         }
10116                 }
10117         }
10118         spin_unlock_irqrestore(&phba->hbalock, flags);
10119
10120         /* Cancel all the IOCBs from the completions list */
10121         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10122                               IOERR_SLI_DOWN);
10123
10124         spin_lock_irqsave(&phba->hbalock, flags);
10125         list_splice_init(&phba->elsbuf, &completions);
10126         phba->elsbuf_cnt = 0;
10127         phba->elsbuf_prev_cnt = 0;
10128         spin_unlock_irqrestore(&phba->hbalock, flags);
10129
10130         while (!list_empty(&completions)) {
10131                 list_remove_head(&completions, buf_ptr,
10132                         struct lpfc_dmabuf, list);
10133                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10134                 kfree(buf_ptr);
10135         }
10136
10137         /* Return any active mbox cmds */
10138         del_timer_sync(&psli->mbox_tmo);
10139
10140         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10141         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10142         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10143
10144         return 1;
10145 }
10146
10147 /**
10148  * lpfc_sli_pcimem_bcopy - SLI memory copy function
10149  * @srcp: Source memory pointer.
10150  * @destp: Destination memory pointer.
10151  * @cnt: Number of words required to be copied.
10152  *
10153  * This function is used for copying data between driver memory
10154  * and the SLI memory. This function also changes the endianness
10155  * of each word if native endianness is different from SLI
10156  * endianness. This function can be called with or without
10157  * lock.
10158  **/
10159 void
10160 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10161 {
10162         uint32_t *src = srcp;
10163         uint32_t *dest = destp;
10164         uint32_t ldata;
10165         int i;
10166
10167         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10168                 ldata = *src;
10169                 ldata = le32_to_cpu(ldata);
10170                 *dest = ldata;
10171                 src++;
10172                 dest++;
10173         }
10174 }
10175
10176
10177 /**
10178  * lpfc_sli_bemem_bcopy - SLI memory copy function
10179  * @srcp: Source memory pointer.
10180  * @destp: Destination memory pointer.
10181  * @cnt: Number of words required to be copied.
10182  *
10183  * This function is used for copying data between a data structure
10184  * with big endian representation to local endianness.
10185  * This function can be called with or without lock.
10186  **/
10187 void
10188 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10189 {
10190         uint32_t *src = srcp;
10191         uint32_t *dest = destp;
10192         uint32_t ldata;
10193         int i;
10194
10195         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10196                 ldata = *src;
10197                 ldata = be32_to_cpu(ldata);
10198                 *dest = ldata;
10199                 src++;
10200                 dest++;
10201         }
10202 }
10203
10204 /**
10205  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10206  * @phba: Pointer to HBA context object.
10207  * @pring: Pointer to driver SLI ring object.
10208  * @mp: Pointer to driver buffer object.
10209  *
10210  * This function is called with no lock held.
10211  * It always return zero after adding the buffer to the postbufq
10212  * buffer list.
10213  **/
10214 int
10215 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10216                          struct lpfc_dmabuf *mp)
10217 {
10218         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10219            later */
10220         spin_lock_irq(&phba->hbalock);
10221         list_add_tail(&mp->list, &pring->postbufq);
10222         pring->postbufq_cnt++;
10223         spin_unlock_irq(&phba->hbalock);
10224         return 0;
10225 }
10226
10227 /**
10228  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10229  * @phba: Pointer to HBA context object.
10230  *
10231  * When HBQ is enabled, buffers are searched based on tags. This function
10232  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10233  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10234  * does not conflict with tags of buffer posted for unsolicited events.
10235  * The function returns the allocated tag. The function is called with
10236  * no locks held.
10237  **/
10238 uint32_t
10239 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10240 {
10241         spin_lock_irq(&phba->hbalock);
10242         phba->buffer_tag_count++;
10243         /*
10244          * Always set the QUE_BUFTAG_BIT to distiguish between
10245          * a tag assigned by HBQ.
10246          */
10247         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10248         spin_unlock_irq(&phba->hbalock);
10249         return phba->buffer_tag_count;
10250 }
10251
10252 /**
10253  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10254  * @phba: Pointer to HBA context object.
10255  * @pring: Pointer to driver SLI ring object.
10256  * @tag: Buffer tag.
10257  *
10258  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10259  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10260  * iocb is posted to the response ring with the tag of the buffer.
10261  * This function searches the pring->postbufq list using the tag
10262  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10263  * iocb. If the buffer is found then lpfc_dmabuf object of the
10264  * buffer is returned to the caller else NULL is returned.
10265  * This function is called with no lock held.
10266  **/
10267 struct lpfc_dmabuf *
10268 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10269                         uint32_t tag)
10270 {
10271         struct lpfc_dmabuf *mp, *next_mp;
10272         struct list_head *slp = &pring->postbufq;
10273
10274         /* Search postbufq, from the beginning, looking for a match on tag */
10275         spin_lock_irq(&phba->hbalock);
10276         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10277                 if (mp->buffer_tag == tag) {
10278                         list_del_init(&mp->list);
10279                         pring->postbufq_cnt--;
10280                         spin_unlock_irq(&phba->hbalock);
10281                         return mp;
10282                 }
10283         }
10284
10285         spin_unlock_irq(&phba->hbalock);
10286         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10287                         "0402 Cannot find virtual addr for buffer tag on "
10288                         "ring %d Data x%lx x%p x%p x%x\n",
10289                         pring->ringno, (unsigned long) tag,
10290                         slp->next, slp->prev, pring->postbufq_cnt);
10291
10292         return NULL;
10293 }
10294
10295 /**
10296  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10297  * @phba: Pointer to HBA context object.
10298  * @pring: Pointer to driver SLI ring object.
10299  * @phys: DMA address of the buffer.
10300  *
10301  * This function searches the buffer list using the dma_address
10302  * of unsolicited event to find the driver's lpfc_dmabuf object
10303  * corresponding to the dma_address. The function returns the
10304  * lpfc_dmabuf object if a buffer is found else it returns NULL.
10305  * This function is called by the ct and els unsolicited event
10306  * handlers to get the buffer associated with the unsolicited
10307  * event.
10308  *
10309  * This function is called with no lock held.
10310  **/
10311 struct lpfc_dmabuf *
10312 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10313                          dma_addr_t phys)
10314 {
10315         struct lpfc_dmabuf *mp, *next_mp;
10316         struct list_head *slp = &pring->postbufq;
10317
10318         /* Search postbufq, from the beginning, looking for a match on phys */
10319         spin_lock_irq(&phba->hbalock);
10320         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10321                 if (mp->phys == phys) {
10322                         list_del_init(&mp->list);
10323                         pring->postbufq_cnt--;
10324                         spin_unlock_irq(&phba->hbalock);
10325                         return mp;
10326                 }
10327         }
10328
10329         spin_unlock_irq(&phba->hbalock);
10330         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10331                         "0410 Cannot find virtual addr for mapped buf on "
10332                         "ring %d Data x%llx x%p x%p x%x\n",
10333                         pring->ringno, (unsigned long long)phys,
10334                         slp->next, slp->prev, pring->postbufq_cnt);
10335         return NULL;
10336 }
10337
10338 /**
10339  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
10340  * @phba: Pointer to HBA context object.
10341  * @cmdiocb: Pointer to driver command iocb object.
10342  * @rspiocb: Pointer to driver response iocb object.
10343  *
10344  * This function is the completion handler for the abort iocbs for
10345  * ELS commands. This function is called from the ELS ring event
10346  * handler with no lock held. This function frees memory resources
10347  * associated with the abort iocb.
10348  **/
10349 static void
10350 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10351                         struct lpfc_iocbq *rspiocb)
10352 {
10353         IOCB_t *irsp = &rspiocb->iocb;
10354         uint16_t abort_iotag, abort_context;
10355         struct lpfc_iocbq *abort_iocb = NULL;
10356
10357         if (irsp->ulpStatus) {
10358
10359                 /*
10360                  * Assume that the port already completed and returned, or
10361                  * will return the iocb. Just Log the message.
10362                  */
10363                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10364                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10365
10366                 spin_lock_irq(&phba->hbalock);
10367                 if (phba->sli_rev < LPFC_SLI_REV4) {
10368                         if (abort_iotag != 0 &&
10369                                 abort_iotag <= phba->sli.last_iotag)
10370                                 abort_iocb =
10371                                         phba->sli.iocbq_lookup[abort_iotag];
10372                 } else
10373                         /* For sli4 the abort_tag is the XRI,
10374                          * so the abort routine puts the iotag  of the iocb
10375                          * being aborted in the context field of the abort
10376                          * IOCB.
10377                          */
10378                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
10379
10380                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10381                                 "0327 Cannot abort els iocb %p "
10382                                 "with tag %x context %x, abort status %x, "
10383                                 "abort code %x\n",
10384                                 abort_iocb, abort_iotag, abort_context,
10385                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
10386
10387                 spin_unlock_irq(&phba->hbalock);
10388         }
10389         lpfc_sli_release_iocbq(phba, cmdiocb);
10390         return;
10391 }
10392
10393 /**
10394  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
10395  * @phba: Pointer to HBA context object.
10396  * @cmdiocb: Pointer to driver command iocb object.
10397  * @rspiocb: Pointer to driver response iocb object.
10398  *
10399  * The function is called from SLI ring event handler with no
10400  * lock held. This function is the completion handler for ELS commands
10401  * which are aborted. The function frees memory resources used for
10402  * the aborted ELS commands.
10403  **/
10404 static void
10405 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10406                      struct lpfc_iocbq *rspiocb)
10407 {
10408         IOCB_t *irsp = &rspiocb->iocb;
10409
10410         /* ELS cmd tag <ulpIoTag> completes */
10411         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10412                         "0139 Ignoring ELS cmd tag x%x completion Data: "
10413                         "x%x x%x x%x\n",
10414                         irsp->ulpIoTag, irsp->ulpStatus,
10415                         irsp->un.ulpWord[4], irsp->ulpTimeout);
10416         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10417                 lpfc_ct_free_iocb(phba, cmdiocb);
10418         else
10419                 lpfc_els_free_iocb(phba, cmdiocb);
10420         return;
10421 }
10422
10423 /**
10424  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
10425  * @phba: Pointer to HBA context object.
10426  * @pring: Pointer to driver SLI ring object.
10427  * @cmdiocb: Pointer to driver command iocb object.
10428  *
10429  * This function issues an abort iocb for the provided command iocb down to
10430  * the port. Other than the case the outstanding command iocb is an abort
10431  * request, this function issues abort out unconditionally. This function is
10432  * called with hbalock held. The function returns 0 when it fails due to
10433  * memory allocation failure or when the command iocb is an abort request.
10434  **/
10435 static int
10436 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10437                            struct lpfc_iocbq *cmdiocb)
10438 {
10439         struct lpfc_vport *vport = cmdiocb->vport;
10440         struct lpfc_iocbq *abtsiocbp;
10441         IOCB_t *icmd = NULL;
10442         IOCB_t *iabt = NULL;
10443         int retval;
10444         unsigned long iflags;
10445
10446         lockdep_assert_held(&phba->hbalock);
10447
10448         /*
10449          * There are certain command types we don't want to abort.  And we
10450          * don't want to abort commands that are already in the process of
10451          * being aborted.
10452          */
10453         icmd = &cmdiocb->iocb;
10454         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10455             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10456             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10457                 return 0;
10458
10459         /* issue ABTS for this IOCB based on iotag */
10460         abtsiocbp = __lpfc_sli_get_iocbq(phba);
10461         if (abtsiocbp == NULL)
10462                 return 0;
10463
10464         /* This signals the response to set the correct status
10465          * before calling the completion handler
10466          */
10467         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10468
10469         iabt = &abtsiocbp->iocb;
10470         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10471         iabt->un.acxri.abortContextTag = icmd->ulpContext;
10472         if (phba->sli_rev == LPFC_SLI_REV4) {
10473                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
10474                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10475         }
10476         else
10477                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
10478         iabt->ulpLe = 1;
10479         iabt->ulpClass = icmd->ulpClass;
10480
10481         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10482         abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
10483         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10484                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
10485         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10486                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
10487
10488         if (phba->link_state >= LPFC_LINK_UP)
10489                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10490         else
10491                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10492
10493         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10494         abtsiocbp->vport = vport;
10495
10496         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10497                          "0339 Abort xri x%x, original iotag x%x, "
10498                          "abort cmd iotag x%x\n",
10499                          iabt->un.acxri.abortIoTag,
10500                          iabt->un.acxri.abortContextTag,
10501                          abtsiocbp->iotag);
10502
10503         if (phba->sli_rev == LPFC_SLI_REV4) {
10504                 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10505                 if (unlikely(pring == NULL))
10506                         return 0;
10507                 /* Note: both hbalock and ring_lock need to be set here */
10508                 spin_lock_irqsave(&pring->ring_lock, iflags);
10509                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10510                         abtsiocbp, 0);
10511                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10512         } else {
10513                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10514                         abtsiocbp, 0);
10515         }
10516
10517         if (retval)
10518                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
10519
10520         /*
10521          * Caller to this routine should check for IOCB_ERROR
10522          * and handle it properly.  This routine no longer removes
10523          * iocb off txcmplq and call compl in case of IOCB_ERROR.
10524          */
10525         return retval;
10526 }
10527
10528 /**
10529  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10530  * @phba: Pointer to HBA context object.
10531  * @pring: Pointer to driver SLI ring object.
10532  * @cmdiocb: Pointer to driver command iocb object.
10533  *
10534  * This function issues an abort iocb for the provided command iocb. In case
10535  * of unloading, the abort iocb will not be issued to commands on the ELS
10536  * ring. Instead, the callback function shall be changed to those commands
10537  * so that nothing happens when them finishes. This function is called with
10538  * hbalock held. The function returns 0 when the command iocb is an abort
10539  * request.
10540  **/
10541 int
10542 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10543                            struct lpfc_iocbq *cmdiocb)
10544 {
10545         struct lpfc_vport *vport = cmdiocb->vport;
10546         int retval = IOCB_ERROR;
10547         IOCB_t *icmd = NULL;
10548
10549         lockdep_assert_held(&phba->hbalock);
10550
10551         /*
10552          * There are certain command types we don't want to abort.  And we
10553          * don't want to abort commands that are already in the process of
10554          * being aborted.
10555          */
10556         icmd = &cmdiocb->iocb;
10557         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10558             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10559             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10560                 return 0;
10561
10562         /*
10563          * If we're unloading, don't abort iocb on the ELS ring, but change
10564          * the callback so that nothing happens when it finishes.
10565          */
10566         if ((vport->load_flag & FC_UNLOADING) &&
10567             (pring->ringno == LPFC_ELS_RING)) {
10568                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10569                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10570                 else
10571                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10572                 goto abort_iotag_exit;
10573         }
10574
10575         /* Now, we try to issue the abort to the cmdiocb out */
10576         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10577
10578 abort_iotag_exit:
10579         /*
10580          * Caller to this routine should check for IOCB_ERROR
10581          * and handle it properly.  This routine no longer removes
10582          * iocb off txcmplq and call compl in case of IOCB_ERROR.
10583          */
10584         return retval;
10585 }
10586
10587 /**
10588  * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10589  * @phba: Pointer to HBA context object.
10590  * @pring: Pointer to driver SLI ring object.
10591  * @cmdiocb: Pointer to driver command iocb object.
10592  *
10593  * This function issues an abort iocb for the provided command iocb down to
10594  * the port. Other than the case the outstanding command iocb is an abort
10595  * request, this function issues abort out unconditionally. This function is
10596  * called with hbalock held. The function returns 0 when it fails due to
10597  * memory allocation failure or when the command iocb is an abort request.
10598  **/
10599 static int
10600 lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10601                         struct lpfc_iocbq *cmdiocb)
10602 {
10603         struct lpfc_vport *vport = cmdiocb->vport;
10604         struct lpfc_iocbq *abtsiocbp;
10605         union lpfc_wqe *abts_wqe;
10606         int retval;
10607
10608         /*
10609          * There are certain command types we don't want to abort.  And we
10610          * don't want to abort commands that are already in the process of
10611          * being aborted.
10612          */
10613         if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10614             cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10615             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10616                 return 0;
10617
10618         /* issue ABTS for this io based on iotag */
10619         abtsiocbp = __lpfc_sli_get_iocbq(phba);
10620         if (abtsiocbp == NULL)
10621                 return 0;
10622
10623         /* This signals the response to set the correct status
10624          * before calling the completion handler
10625          */
10626         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10627
10628         /* Complete prepping the abort wqe and issue to the FW. */
10629         abts_wqe = &abtsiocbp->wqe;
10630         bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10631         bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10632
10633         /* Explicitly set reserved fields to zero.*/
10634         abts_wqe->abort_cmd.rsrvd4 = 0;
10635         abts_wqe->abort_cmd.rsrvd5 = 0;
10636
10637         /* WQE Common - word 6.  Context is XRI tag.  Set 0. */
10638         bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10639         bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10640
10641         /* word 7 */
10642         bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10643         bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10644         bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10645                cmdiocb->iocb.ulpClass);
10646
10647         /* word 8 - tell the FW to abort the IO associated with this
10648          * outstanding exchange ID.
10649          */
10650         abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10651
10652         /* word 9 - this is the iotag for the abts_wqe completion. */
10653         bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10654                abtsiocbp->iotag);
10655
10656         /* word 10 */
10657         bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10658         bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10659         bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10660
10661         /* word 11 */
10662         bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10663         bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10664         bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10665
10666         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10667         abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10668         abtsiocbp->vport = vport;
10669         abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
10670         retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10671         if (retval == IOCB_ERROR) {
10672                 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10673                                  "6147 Failed abts issue_wqe with status x%x "
10674                                  "for oxid x%x\n",
10675                                  retval, cmdiocb->sli4_xritag);
10676                 lpfc_sli_release_iocbq(phba, abtsiocbp);
10677                 return retval;
10678         }
10679
10680         lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10681                          "6148 Drv Abort NVME Request Issued for "
10682                          "ox_id x%x on reqtag x%x\n",
10683                          cmdiocb->sli4_xritag,
10684                          abtsiocbp->iotag);
10685
10686         return retval;
10687 }
10688
10689 /**
10690  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10691  * @phba: pointer to lpfc HBA data structure.
10692  *
10693  * This routine will abort all pending and outstanding iocbs to an HBA.
10694  **/
10695 void
10696 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10697 {
10698         struct lpfc_sli *psli = &phba->sli;
10699         struct lpfc_sli_ring *pring;
10700         struct lpfc_queue *qp = NULL;
10701         int i;
10702
10703         if (phba->sli_rev != LPFC_SLI_REV4) {
10704                 for (i = 0; i < psli->num_rings; i++) {
10705                         pring = &psli->sli3_ring[i];
10706                         lpfc_sli_abort_iocb_ring(phba, pring);
10707                 }
10708                 return;
10709         }
10710         list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10711                 pring = qp->pring;
10712                 if (!pring)
10713                         continue;
10714                 lpfc_sli_abort_iocb_ring(phba, pring);
10715         }
10716 }
10717
10718 /**
10719  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
10720  * @iocbq: Pointer to driver iocb object.
10721  * @vport: Pointer to driver virtual port object.
10722  * @tgt_id: SCSI ID of the target.
10723  * @lun_id: LUN ID of the scsi device.
10724  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10725  *
10726  * This function acts as an iocb filter for functions which abort or count
10727  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10728  * 0 if the filtering criteria is met for the given iocb and will return
10729  * 1 if the filtering criteria is not met.
10730  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10731  * given iocb is for the SCSI device specified by vport, tgt_id and
10732  * lun_id parameter.
10733  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
10734  * given iocb is for the SCSI target specified by vport and tgt_id
10735  * parameters.
10736  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10737  * given iocb is for the SCSI host associated with the given vport.
10738  * This function is called with no locks held.
10739  **/
10740 static int
10741 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10742                            uint16_t tgt_id, uint64_t lun_id,
10743                            lpfc_ctx_cmd ctx_cmd)
10744 {
10745         struct lpfc_scsi_buf *lpfc_cmd;
10746         int rc = 1;
10747
10748         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
10749                 return rc;
10750
10751         if (iocbq->vport != vport)
10752                 return rc;
10753
10754         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10755
10756         if (lpfc_cmd->pCmd == NULL)
10757                 return rc;
10758
10759         switch (ctx_cmd) {
10760         case LPFC_CTX_LUN:
10761                 if ((lpfc_cmd->rdata->pnode) &&
10762                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10763                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
10764                         rc = 0;
10765                 break;
10766         case LPFC_CTX_TGT:
10767                 if ((lpfc_cmd->rdata->pnode) &&
10768                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
10769                         rc = 0;
10770                 break;
10771         case LPFC_CTX_HOST:
10772                 rc = 0;
10773                 break;
10774         default:
10775                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
10776                         __func__, ctx_cmd);
10777                 break;
10778         }
10779
10780         return rc;
10781 }
10782
10783 /**
10784  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
10785  * @vport: Pointer to virtual port.
10786  * @tgt_id: SCSI ID of the target.
10787  * @lun_id: LUN ID of the scsi device.
10788  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10789  *
10790  * This function returns number of FCP commands pending for the vport.
10791  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10792  * commands pending on the vport associated with SCSI device specified
10793  * by tgt_id and lun_id parameters.
10794  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10795  * commands pending on the vport associated with SCSI target specified
10796  * by tgt_id parameter.
10797  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10798  * commands pending on the vport.
10799  * This function returns the number of iocbs which satisfy the filter.
10800  * This function is called without any lock held.
10801  **/
10802 int
10803 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10804                   lpfc_ctx_cmd ctx_cmd)
10805 {
10806         struct lpfc_hba *phba = vport->phba;
10807         struct lpfc_iocbq *iocbq;
10808         int sum, i;
10809
10810         spin_lock_irq(&phba->hbalock);
10811         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10812                 iocbq = phba->sli.iocbq_lookup[i];
10813
10814                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10815                                                 ctx_cmd) == 0)
10816                         sum++;
10817         }
10818         spin_unlock_irq(&phba->hbalock);
10819
10820         return sum;
10821 }
10822
10823 /**
10824  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
10825  * @phba: Pointer to HBA context object
10826  * @cmdiocb: Pointer to command iocb object.
10827  * @rspiocb: Pointer to response iocb object.
10828  *
10829  * This function is called when an aborted FCP iocb completes. This
10830  * function is called by the ring event handler with no lock held.
10831  * This function frees the iocb.
10832  **/
10833 void
10834 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10835                         struct lpfc_iocbq *rspiocb)
10836 {
10837         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10838                         "3096 ABORT_XRI_CN completing on rpi x%x "
10839                         "original iotag x%x, abort cmd iotag x%x "
10840                         "status 0x%x, reason 0x%x\n",
10841                         cmdiocb->iocb.un.acxri.abortContextTag,
10842                         cmdiocb->iocb.un.acxri.abortIoTag,
10843                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10844                         rspiocb->iocb.un.ulpWord[4]);
10845         lpfc_sli_release_iocbq(phba, cmdiocb);
10846         return;
10847 }
10848
10849 /**
10850  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
10851  * @vport: Pointer to virtual port.
10852  * @pring: Pointer to driver SLI ring object.
10853  * @tgt_id: SCSI ID of the target.
10854  * @lun_id: LUN ID of the scsi device.
10855  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10856  *
10857  * This function sends an abort command for every SCSI command
10858  * associated with the given virtual port pending on the ring
10859  * filtered by lpfc_sli_validate_fcp_iocb function.
10860  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10861  * FCP iocbs associated with lun specified by tgt_id and lun_id
10862  * parameters
10863  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10864  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10865  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10866  * FCP iocbs associated with virtual port.
10867  * This function returns number of iocbs it failed to abort.
10868  * This function is called with no locks held.
10869  **/
10870 int
10871 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10872                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
10873 {
10874         struct lpfc_hba *phba = vport->phba;
10875         struct lpfc_iocbq *iocbq;
10876         struct lpfc_iocbq *abtsiocb;
10877         IOCB_t *cmd = NULL;
10878         int errcnt = 0, ret_val = 0;
10879         int i;
10880
10881         for (i = 1; i <= phba->sli.last_iotag; i++) {
10882                 iocbq = phba->sli.iocbq_lookup[i];
10883
10884                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10885                                                abort_cmd) != 0)
10886                         continue;
10887
10888                 /*
10889                  * If the iocbq is already being aborted, don't take a second
10890                  * action, but do count it.
10891                  */
10892                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10893                         continue;
10894
10895                 /* issue ABTS for this IOCB based on iotag */
10896                 abtsiocb = lpfc_sli_get_iocbq(phba);
10897                 if (abtsiocb == NULL) {
10898                         errcnt++;
10899                         continue;
10900                 }
10901
10902                 /* indicate the IO is being aborted by the driver. */
10903                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10904
10905                 cmd = &iocbq->iocb;
10906                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10907                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
10908                 if (phba->sli_rev == LPFC_SLI_REV4)
10909                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10910                 else
10911                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
10912                 abtsiocb->iocb.ulpLe = 1;
10913                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
10914                 abtsiocb->vport = vport;
10915
10916                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10917                 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
10918                 if (iocbq->iocb_flag & LPFC_IO_FCP)
10919                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
10920                 if (iocbq->iocb_flag & LPFC_IO_FOF)
10921                         abtsiocb->iocb_flag |= LPFC_IO_FOF;
10922
10923                 if (lpfc_is_link_up(phba))
10924                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10925                 else
10926                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10927
10928                 /* Setup callback routine and issue the command. */
10929                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10930                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10931                                               abtsiocb, 0);
10932                 if (ret_val == IOCB_ERROR) {
10933                         lpfc_sli_release_iocbq(phba, abtsiocb);
10934                         errcnt++;
10935                         continue;
10936                 }
10937         }
10938
10939         return errcnt;
10940 }
10941
10942 /**
10943  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10944  * @vport: Pointer to virtual port.
10945  * @pring: Pointer to driver SLI ring object.
10946  * @tgt_id: SCSI ID of the target.
10947  * @lun_id: LUN ID of the scsi device.
10948  * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10949  *
10950  * This function sends an abort command for every SCSI command
10951  * associated with the given virtual port pending on the ring
10952  * filtered by lpfc_sli_validate_fcp_iocb function.
10953  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10954  * FCP iocbs associated with lun specified by tgt_id and lun_id
10955  * parameters
10956  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10957  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10958  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10959  * FCP iocbs associated with virtual port.
10960  * This function returns number of iocbs it aborted .
10961  * This function is called with no locks held right after a taskmgmt
10962  * command is sent.
10963  **/
10964 int
10965 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10966                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10967 {
10968         struct lpfc_hba *phba = vport->phba;
10969         struct lpfc_scsi_buf *lpfc_cmd;
10970         struct lpfc_iocbq *abtsiocbq;
10971         struct lpfc_nodelist *ndlp;
10972         struct lpfc_iocbq *iocbq;
10973         IOCB_t *icmd;
10974         int sum, i, ret_val;
10975         unsigned long iflags;
10976         struct lpfc_sli_ring *pring_s4;
10977
10978         spin_lock_irq(&phba->hbalock);
10979
10980         /* all I/Os are in process of being flushed */
10981         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10982                 spin_unlock_irq(&phba->hbalock);
10983                 return 0;
10984         }
10985         sum = 0;
10986
10987         for (i = 1; i <= phba->sli.last_iotag; i++) {
10988                 iocbq = phba->sli.iocbq_lookup[i];
10989
10990                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10991                                                cmd) != 0)
10992                         continue;
10993
10994                 /*
10995                  * If the iocbq is already being aborted, don't take a second
10996                  * action, but do count it.
10997                  */
10998                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10999                         continue;
11000
11001                 /* issue ABTS for this IOCB based on iotag */
11002                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11003                 if (abtsiocbq == NULL)
11004                         continue;
11005
11006                 icmd = &iocbq->iocb;
11007                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11008                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11009                 if (phba->sli_rev == LPFC_SLI_REV4)
11010                         abtsiocbq->iocb.un.acxri.abortIoTag =
11011                                                          iocbq->sli4_xritag;
11012                 else
11013                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11014                 abtsiocbq->iocb.ulpLe = 1;
11015                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11016                 abtsiocbq->vport = vport;
11017
11018                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11019                 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11020                 if (iocbq->iocb_flag & LPFC_IO_FCP)
11021                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11022                 if (iocbq->iocb_flag & LPFC_IO_FOF)
11023                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11024
11025                 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11026                 ndlp = lpfc_cmd->rdata->pnode;
11027
11028                 if (lpfc_is_link_up(phba) &&
11029                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11030                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11031                 else
11032                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11033
11034                 /* Setup callback routine and issue the command. */
11035                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11036
11037                 /*
11038                  * Indicate the IO is being aborted by the driver and set
11039                  * the caller's flag into the aborted IO.
11040                  */
11041                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11042
11043                 if (phba->sli_rev == LPFC_SLI_REV4) {
11044                         pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11045                         if (pring_s4 == NULL)
11046                                 continue;
11047                         /* Note: both hbalock and ring_lock must be set here */
11048                         spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11049                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11050                                                         abtsiocbq, 0);
11051                         spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11052                 } else {
11053                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11054                                                         abtsiocbq, 0);
11055                 }
11056
11057
11058                 if (ret_val == IOCB_ERROR)
11059                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
11060                 else
11061                         sum++;
11062         }
11063         spin_unlock_irq(&phba->hbalock);
11064         return sum;
11065 }
11066
11067 /**
11068  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11069  * @phba: Pointer to HBA context object.
11070  * @cmdiocbq: Pointer to command iocb.
11071  * @rspiocbq: Pointer to response iocb.
11072  *
11073  * This function is the completion handler for iocbs issued using
11074  * lpfc_sli_issue_iocb_wait function. This function is called by the
11075  * ring event handler function without any lock held. This function
11076  * can be called from both worker thread context and interrupt
11077  * context. This function also can be called from other thread which
11078  * cleans up the SLI layer objects.
11079  * This function copy the contents of the response iocb to the
11080  * response iocb memory object provided by the caller of
11081  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11082  * sleeps for the iocb completion.
11083  **/
11084 static void
11085 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11086                         struct lpfc_iocbq *cmdiocbq,
11087                         struct lpfc_iocbq *rspiocbq)
11088 {
11089         wait_queue_head_t *pdone_q;
11090         unsigned long iflags;
11091         struct lpfc_scsi_buf *lpfc_cmd;
11092
11093         spin_lock_irqsave(&phba->hbalock, iflags);
11094         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11095
11096                 /*
11097                  * A time out has occurred for the iocb.  If a time out
11098                  * completion handler has been supplied, call it.  Otherwise,
11099                  * just free the iocbq.
11100                  */
11101
11102                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11103                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11104                 cmdiocbq->wait_iocb_cmpl = NULL;
11105                 if (cmdiocbq->iocb_cmpl)
11106                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11107                 else
11108                         lpfc_sli_release_iocbq(phba, cmdiocbq);
11109                 return;
11110         }
11111
11112         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11113         if (cmdiocbq->context2 && rspiocbq)
11114                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11115                        &rspiocbq->iocb, sizeof(IOCB_t));
11116
11117         /* Set the exchange busy flag for task management commands */
11118         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11119                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11120                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11121                         cur_iocbq);
11122                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11123         }
11124
11125         pdone_q = cmdiocbq->context_un.wait_queue;
11126         if (pdone_q)
11127                 wake_up(pdone_q);
11128         spin_unlock_irqrestore(&phba->hbalock, iflags);
11129         return;
11130 }
11131
11132 /**
11133  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11134  * @phba: Pointer to HBA context object..
11135  * @piocbq: Pointer to command iocb.
11136  * @flag: Flag to test.
11137  *
11138  * This routine grabs the hbalock and then test the iocb_flag to
11139  * see if the passed in flag is set.
11140  * Returns:
11141  * 1 if flag is set.
11142  * 0 if flag is not set.
11143  **/
11144 static int
11145 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11146                  struct lpfc_iocbq *piocbq, uint32_t flag)
11147 {
11148         unsigned long iflags;
11149         int ret;
11150
11151         spin_lock_irqsave(&phba->hbalock, iflags);
11152         ret = piocbq->iocb_flag & flag;
11153         spin_unlock_irqrestore(&phba->hbalock, iflags);
11154         return ret;
11155
11156 }
11157
11158 /**
11159  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11160  * @phba: Pointer to HBA context object..
11161  * @pring: Pointer to sli ring.
11162  * @piocb: Pointer to command iocb.
11163  * @prspiocbq: Pointer to response iocb.
11164  * @timeout: Timeout in number of seconds.
11165  *
11166  * This function issues the iocb to firmware and waits for the
11167  * iocb to complete. The iocb_cmpl field of the shall be used
11168  * to handle iocbs which time out. If the field is NULL, the
11169  * function shall free the iocbq structure.  If more clean up is
11170  * needed, the caller is expected to provide a completion function
11171  * that will provide the needed clean up.  If the iocb command is
11172  * not completed within timeout seconds, the function will either
11173  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11174  * completion function set in the iocb_cmpl field and then return
11175  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
11176  * resources if this function returns IOCB_TIMEDOUT.
11177  * The function waits for the iocb completion using an
11178  * non-interruptible wait.
11179  * This function will sleep while waiting for iocb completion.
11180  * So, this function should not be called from any context which
11181  * does not allow sleeping. Due to the same reason, this function
11182  * cannot be called with interrupt disabled.
11183  * This function assumes that the iocb completions occur while
11184  * this function sleep. So, this function cannot be called from
11185  * the thread which process iocb completion for this ring.
11186  * This function clears the iocb_flag of the iocb object before
11187  * issuing the iocb and the iocb completion handler sets this
11188  * flag and wakes this thread when the iocb completes.
11189  * The contents of the response iocb will be copied to prspiocbq
11190  * by the completion handler when the command completes.
11191  * This function returns IOCB_SUCCESS when success.
11192  * This function is called with no lock held.
11193  **/
11194 int
11195 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11196                          uint32_t ring_number,
11197                          struct lpfc_iocbq *piocb,
11198                          struct lpfc_iocbq *prspiocbq,
11199                          uint32_t timeout)
11200 {
11201         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11202         long timeleft, timeout_req = 0;
11203         int retval = IOCB_SUCCESS;
11204         uint32_t creg_val;
11205         struct lpfc_iocbq *iocb;
11206         int txq_cnt = 0;
11207         int txcmplq_cnt = 0;
11208         struct lpfc_sli_ring *pring;
11209         unsigned long iflags;
11210         bool iocb_completed = true;
11211
11212         if (phba->sli_rev >= LPFC_SLI_REV4)
11213                 pring = lpfc_sli4_calc_ring(phba, piocb);
11214         else
11215                 pring = &phba->sli.sli3_ring[ring_number];
11216         /*
11217          * If the caller has provided a response iocbq buffer, then context2
11218          * is NULL or its an error.
11219          */
11220         if (prspiocbq) {
11221                 if (piocb->context2)
11222                         return IOCB_ERROR;
11223                 piocb->context2 = prspiocbq;
11224         }
11225
11226         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11227         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11228         piocb->context_un.wait_queue = &done_q;
11229         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11230
11231         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11232                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11233                         return IOCB_ERROR;
11234                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11235                 writel(creg_val, phba->HCregaddr);
11236                 readl(phba->HCregaddr); /* flush */
11237         }
11238
11239         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11240                                      SLI_IOCB_RET_IOCB);
11241         if (retval == IOCB_SUCCESS) {
11242                 timeout_req = msecs_to_jiffies(timeout * 1000);
11243                 timeleft = wait_event_timeout(done_q,
11244                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11245                                 timeout_req);
11246                 spin_lock_irqsave(&phba->hbalock, iflags);
11247                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11248
11249                         /*
11250                          * IOCB timed out.  Inform the wake iocb wait
11251                          * completion function and set local status
11252                          */
11253
11254                         iocb_completed = false;
11255                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11256                 }
11257                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11258                 if (iocb_completed) {
11259                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11260                                         "0331 IOCB wake signaled\n");
11261                         /* Note: we are not indicating if the IOCB has a success
11262                          * status or not - that's for the caller to check.
11263                          * IOCB_SUCCESS means just that the command was sent and
11264                          * completed. Not that it completed successfully.
11265                          * */
11266                 } else if (timeleft == 0) {
11267                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11268                                         "0338 IOCB wait timeout error - no "
11269                                         "wake response Data x%x\n", timeout);
11270                         retval = IOCB_TIMEDOUT;
11271                 } else {
11272                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11273                                         "0330 IOCB wake NOT set, "
11274                                         "Data x%x x%lx\n",
11275                                         timeout, (timeleft / jiffies));
11276                         retval = IOCB_TIMEDOUT;
11277                 }
11278         } else if (retval == IOCB_BUSY) {
11279                 if (phba->cfg_log_verbose & LOG_SLI) {
11280                         list_for_each_entry(iocb, &pring->txq, list) {
11281                                 txq_cnt++;
11282                         }
11283                         list_for_each_entry(iocb, &pring->txcmplq, list) {
11284                                 txcmplq_cnt++;
11285                         }
11286                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11287                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11288                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11289                 }
11290                 return retval;
11291         } else {
11292                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11293                                 "0332 IOCB wait issue failed, Data x%x\n",
11294                                 retval);
11295                 retval = IOCB_ERROR;
11296         }
11297
11298         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11299                 if (lpfc_readl(phba->HCregaddr, &creg_val))
11300                         return IOCB_ERROR;
11301                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11302                 writel(creg_val, phba->HCregaddr);
11303                 readl(phba->HCregaddr); /* flush */
11304         }
11305
11306         if (prspiocbq)
11307                 piocb->context2 = NULL;
11308
11309         piocb->context_un.wait_queue = NULL;
11310         piocb->iocb_cmpl = NULL;
11311         return retval;
11312 }
11313
11314 /**
11315  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
11316  * @phba: Pointer to HBA context object.
11317  * @pmboxq: Pointer to driver mailbox object.
11318  * @timeout: Timeout in number of seconds.
11319  *
11320  * This function issues the mailbox to firmware and waits for the
11321  * mailbox command to complete. If the mailbox command is not
11322  * completed within timeout seconds, it returns MBX_TIMEOUT.
11323  * The function waits for the mailbox completion using an
11324  * interruptible wait. If the thread is woken up due to a
11325  * signal, MBX_TIMEOUT error is returned to the caller. Caller
11326  * should not free the mailbox resources, if this function returns
11327  * MBX_TIMEOUT.
11328  * This function will sleep while waiting for mailbox completion.
11329  * So, this function should not be called from any context which
11330  * does not allow sleeping. Due to the same reason, this function
11331  * cannot be called with interrupt disabled.
11332  * This function assumes that the mailbox completion occurs while
11333  * this function sleep. So, this function cannot be called from
11334  * the worker thread which processes mailbox completion.
11335  * This function is called in the context of HBA management
11336  * applications.
11337  * This function returns MBX_SUCCESS when successful.
11338  * This function is called with no lock held.
11339  **/
11340 int
11341 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
11342                          uint32_t timeout)
11343 {
11344         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11345         MAILBOX_t *mb = NULL;
11346         int retval;
11347         unsigned long flag;
11348
11349         /* The caller might set context1 for extended buffer */
11350         if (pmboxq->context1)
11351                 mb = (MAILBOX_t *)pmboxq->context1;
11352
11353         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
11354         /* setup wake call as IOCB callback */
11355         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11356         /* setup context field to pass wait_queue pointer to wake function  */
11357         pmboxq->context1 = &done_q;
11358
11359         /* now issue the command */
11360         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
11361         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
11362                 wait_event_interruptible_timeout(done_q,
11363                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
11364                                 msecs_to_jiffies(timeout * 1000));
11365
11366                 spin_lock_irqsave(&phba->hbalock, flag);
11367                 /* restore the possible extended buffer for free resource */
11368                 pmboxq->context1 = (uint8_t *)mb;
11369                 /*
11370                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
11371                  * else do not free the resources.
11372                  */
11373                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
11374                         retval = MBX_SUCCESS;
11375                 } else {
11376                         retval = MBX_TIMEOUT;
11377                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11378                 }
11379                 spin_unlock_irqrestore(&phba->hbalock, flag);
11380         } else {
11381                 /* restore the possible extended buffer for free resource */
11382                 pmboxq->context1 = (uint8_t *)mb;
11383         }
11384
11385         return retval;
11386 }
11387
11388 /**
11389  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
11390  * @phba: Pointer to HBA context.
11391  *
11392  * This function is called to shutdown the driver's mailbox sub-system.
11393  * It first marks the mailbox sub-system is in a block state to prevent
11394  * the asynchronous mailbox command from issued off the pending mailbox
11395  * command queue. If the mailbox command sub-system shutdown is due to
11396  * HBA error conditions such as EEH or ERATT, this routine shall invoke
11397  * the mailbox sub-system flush routine to forcefully bring down the
11398  * mailbox sub-system. Otherwise, if it is due to normal condition (such
11399  * as with offline or HBA function reset), this routine will wait for the
11400  * outstanding mailbox command to complete before invoking the mailbox
11401  * sub-system flush routine to gracefully bring down mailbox sub-system.
11402  **/
11403 void
11404 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
11405 {
11406         struct lpfc_sli *psli = &phba->sli;
11407         unsigned long timeout;
11408
11409         if (mbx_action == LPFC_MBX_NO_WAIT) {
11410                 /* delay 100ms for port state */
11411                 msleep(100);
11412                 lpfc_sli_mbox_sys_flush(phba);
11413                 return;
11414         }
11415         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
11416
11417         spin_lock_irq(&phba->hbalock);
11418         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11419
11420         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
11421                 /* Determine how long we might wait for the active mailbox
11422                  * command to be gracefully completed by firmware.
11423                  */
11424                 if (phba->sli.mbox_active)
11425                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11426                                                 phba->sli.mbox_active) *
11427                                                 1000) + jiffies;
11428                 spin_unlock_irq(&phba->hbalock);
11429
11430                 while (phba->sli.mbox_active) {
11431                         /* Check active mailbox complete status every 2ms */
11432                         msleep(2);
11433                         if (time_after(jiffies, timeout))
11434                                 /* Timeout, let the mailbox flush routine to
11435                                  * forcefully release active mailbox command
11436                                  */
11437                                 break;
11438                 }
11439         } else
11440                 spin_unlock_irq(&phba->hbalock);
11441
11442         lpfc_sli_mbox_sys_flush(phba);
11443 }
11444
11445 /**
11446  * lpfc_sli_eratt_read - read sli-3 error attention events
11447  * @phba: Pointer to HBA context.
11448  *
11449  * This function is called to read the SLI3 device error attention registers
11450  * for possible error attention events. The caller must hold the hostlock
11451  * with spin_lock_irq().
11452  *
11453  * This function returns 1 when there is Error Attention in the Host Attention
11454  * Register and returns 0 otherwise.
11455  **/
11456 static int
11457 lpfc_sli_eratt_read(struct lpfc_hba *phba)
11458 {
11459         uint32_t ha_copy;
11460
11461         /* Read chip Host Attention (HA) register */
11462         if (lpfc_readl(phba->HAregaddr, &ha_copy))
11463                 goto unplug_err;
11464
11465         if (ha_copy & HA_ERATT) {
11466                 /* Read host status register to retrieve error event */
11467                 if (lpfc_sli_read_hs(phba))
11468                         goto unplug_err;
11469
11470                 /* Check if there is a deferred error condition is active */
11471                 if ((HS_FFER1 & phba->work_hs) &&
11472                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11473                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
11474                         phba->hba_flag |= DEFER_ERATT;
11475                         /* Clear all interrupt enable conditions */
11476                         writel(0, phba->HCregaddr);
11477                         readl(phba->HCregaddr);
11478                 }
11479
11480                 /* Set the driver HA work bitmap */
11481                 phba->work_ha |= HA_ERATT;
11482                 /* Indicate polling handles this ERATT */
11483                 phba->hba_flag |= HBA_ERATT_HANDLED;
11484                 return 1;
11485         }
11486         return 0;
11487
11488 unplug_err:
11489         /* Set the driver HS work bitmap */
11490         phba->work_hs |= UNPLUG_ERR;
11491         /* Set the driver HA work bitmap */
11492         phba->work_ha |= HA_ERATT;
11493         /* Indicate polling handles this ERATT */
11494         phba->hba_flag |= HBA_ERATT_HANDLED;
11495         return 1;
11496 }
11497
11498 /**
11499  * lpfc_sli4_eratt_read - read sli-4 error attention events
11500  * @phba: Pointer to HBA context.
11501  *
11502  * This function is called to read the SLI4 device error attention registers
11503  * for possible error attention events. The caller must hold the hostlock
11504  * with spin_lock_irq().
11505  *
11506  * This function returns 1 when there is Error Attention in the Host Attention
11507  * Register and returns 0 otherwise.
11508  **/
11509 static int
11510 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11511 {
11512         uint32_t uerr_sta_hi, uerr_sta_lo;
11513         uint32_t if_type, portsmphr;
11514         struct lpfc_register portstat_reg;
11515
11516         /*
11517          * For now, use the SLI4 device internal unrecoverable error
11518          * registers for error attention. This can be changed later.
11519          */
11520         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11521         switch (if_type) {
11522         case LPFC_SLI_INTF_IF_TYPE_0:
11523                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11524                         &uerr_sta_lo) ||
11525                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11526                         &uerr_sta_hi)) {
11527                         phba->work_hs |= UNPLUG_ERR;
11528                         phba->work_ha |= HA_ERATT;
11529                         phba->hba_flag |= HBA_ERATT_HANDLED;
11530                         return 1;
11531                 }
11532                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11533                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11534                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11535                                         "1423 HBA Unrecoverable error: "
11536                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11537                                         "ue_mask_lo_reg=0x%x, "
11538                                         "ue_mask_hi_reg=0x%x\n",
11539                                         uerr_sta_lo, uerr_sta_hi,
11540                                         phba->sli4_hba.ue_mask_lo,
11541                                         phba->sli4_hba.ue_mask_hi);
11542                         phba->work_status[0] = uerr_sta_lo;
11543                         phba->work_status[1] = uerr_sta_hi;
11544                         phba->work_ha |= HA_ERATT;
11545                         phba->hba_flag |= HBA_ERATT_HANDLED;
11546                         return 1;
11547                 }
11548                 break;
11549         case LPFC_SLI_INTF_IF_TYPE_2:
11550                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11551                         &portstat_reg.word0) ||
11552                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11553                         &portsmphr)){
11554                         phba->work_hs |= UNPLUG_ERR;
11555                         phba->work_ha |= HA_ERATT;
11556                         phba->hba_flag |= HBA_ERATT_HANDLED;
11557                         return 1;
11558                 }
11559                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11560                         phba->work_status[0] =
11561                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11562                         phba->work_status[1] =
11563                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11564                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11565                                         "2885 Port Status Event: "
11566                                         "port status reg 0x%x, "
11567                                         "port smphr reg 0x%x, "
11568                                         "error 1=0x%x, error 2=0x%x\n",
11569                                         portstat_reg.word0,
11570                                         portsmphr,
11571                                         phba->work_status[0],
11572                                         phba->work_status[1]);
11573                         phba->work_ha |= HA_ERATT;
11574                         phba->hba_flag |= HBA_ERATT_HANDLED;
11575                         return 1;
11576                 }
11577                 break;
11578         case LPFC_SLI_INTF_IF_TYPE_1:
11579         default:
11580                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11581                                 "2886 HBA Error Attention on unsupported "
11582                                 "if type %d.", if_type);
11583                 return 1;
11584         }
11585
11586         return 0;
11587 }
11588
11589 /**
11590  * lpfc_sli_check_eratt - check error attention events
11591  * @phba: Pointer to HBA context.
11592  *
11593  * This function is called from timer soft interrupt context to check HBA's
11594  * error attention register bit for error attention events.
11595  *
11596  * This function returns 1 when there is Error Attention in the Host Attention
11597  * Register and returns 0 otherwise.
11598  **/
11599 int
11600 lpfc_sli_check_eratt(struct lpfc_hba *phba)
11601 {
11602         uint32_t ha_copy;
11603
11604         /* If somebody is waiting to handle an eratt, don't process it
11605          * here. The brdkill function will do this.
11606          */
11607         if (phba->link_flag & LS_IGNORE_ERATT)
11608                 return 0;
11609
11610         /* Check if interrupt handler handles this ERATT */
11611         spin_lock_irq(&phba->hbalock);
11612         if (phba->hba_flag & HBA_ERATT_HANDLED) {
11613                 /* Interrupt handler has handled ERATT */
11614                 spin_unlock_irq(&phba->hbalock);
11615                 return 0;
11616         }
11617
11618         /*
11619          * If there is deferred error attention, do not check for error
11620          * attention
11621          */
11622         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11623                 spin_unlock_irq(&phba->hbalock);
11624                 return 0;
11625         }
11626
11627         /* If PCI channel is offline, don't process it */
11628         if (unlikely(pci_channel_offline(phba->pcidev))) {
11629                 spin_unlock_irq(&phba->hbalock);
11630                 return 0;
11631         }
11632
11633         switch (phba->sli_rev) {
11634         case LPFC_SLI_REV2:
11635         case LPFC_SLI_REV3:
11636                 /* Read chip Host Attention (HA) register */
11637                 ha_copy = lpfc_sli_eratt_read(phba);
11638                 break;
11639         case LPFC_SLI_REV4:
11640                 /* Read device Uncoverable Error (UERR) registers */
11641                 ha_copy = lpfc_sli4_eratt_read(phba);
11642                 break;
11643         default:
11644                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11645                                 "0299 Invalid SLI revision (%d)\n",
11646                                 phba->sli_rev);
11647                 ha_copy = 0;
11648                 break;
11649         }
11650         spin_unlock_irq(&phba->hbalock);
11651
11652         return ha_copy;
11653 }
11654
11655 /**
11656  * lpfc_intr_state_check - Check device state for interrupt handling
11657  * @phba: Pointer to HBA context.
11658  *
11659  * This inline routine checks whether a device or its PCI slot is in a state
11660  * that the interrupt should be handled.
11661  *
11662  * This function returns 0 if the device or the PCI slot is in a state that
11663  * interrupt should be handled, otherwise -EIO.
11664  */
11665 static inline int
11666 lpfc_intr_state_check(struct lpfc_hba *phba)
11667 {
11668         /* If the pci channel is offline, ignore all the interrupts */
11669         if (unlikely(pci_channel_offline(phba->pcidev)))
11670                 return -EIO;
11671
11672         /* Update device level interrupt statistics */
11673         phba->sli.slistat.sli_intr++;
11674
11675         /* Ignore all interrupts during initialization. */
11676         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11677                 return -EIO;
11678
11679         return 0;
11680 }
11681
11682 /**
11683  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
11684  * @irq: Interrupt number.
11685  * @dev_id: The device context pointer.
11686  *
11687  * This function is directly called from the PCI layer as an interrupt
11688  * service routine when device with SLI-3 interface spec is enabled with
11689  * MSI-X multi-message interrupt mode and there are slow-path events in
11690  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11691  * interrupt mode, this function is called as part of the device-level
11692  * interrupt handler. When the PCI slot is in error recovery or the HBA
11693  * is undergoing initialization, the interrupt handler will not process
11694  * the interrupt. The link attention and ELS ring attention events are
11695  * handled by the worker thread. The interrupt handler signals the worker
11696  * thread and returns for these events. This function is called without
11697  * any lock held. It gets the hbalock to access and update SLI data
11698  * structures.
11699  *
11700  * This function returns IRQ_HANDLED when interrupt is handled else it
11701  * returns IRQ_NONE.
11702  **/
11703 irqreturn_t
11704 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
11705 {
11706         struct lpfc_hba  *phba;
11707         uint32_t ha_copy, hc_copy;
11708         uint32_t work_ha_copy;
11709         unsigned long status;
11710         unsigned long iflag;
11711         uint32_t control;
11712
11713         MAILBOX_t *mbox, *pmbox;
11714         struct lpfc_vport *vport;
11715         struct lpfc_nodelist *ndlp;
11716         struct lpfc_dmabuf *mp;
11717         LPFC_MBOXQ_t *pmb;
11718         int rc;
11719
11720         /*
11721          * Get the driver's phba structure from the dev_id and
11722          * assume the HBA is not interrupting.
11723          */
11724         phba = (struct lpfc_hba *)dev_id;
11725
11726         if (unlikely(!phba))
11727                 return IRQ_NONE;
11728
11729         /*
11730          * Stuff needs to be attented to when this function is invoked as an
11731          * individual interrupt handler in MSI-X multi-message interrupt mode
11732          */
11733         if (phba->intr_type == MSIX) {
11734                 /* Check device state for handling interrupt */
11735                 if (lpfc_intr_state_check(phba))
11736                         return IRQ_NONE;
11737                 /* Need to read HA REG for slow-path events */
11738                 spin_lock_irqsave(&phba->hbalock, iflag);
11739                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11740                         goto unplug_error;
11741                 /* If somebody is waiting to handle an eratt don't process it
11742                  * here. The brdkill function will do this.
11743                  */
11744                 if (phba->link_flag & LS_IGNORE_ERATT)
11745                         ha_copy &= ~HA_ERATT;
11746                 /* Check the need for handling ERATT in interrupt handler */
11747                 if (ha_copy & HA_ERATT) {
11748                         if (phba->hba_flag & HBA_ERATT_HANDLED)
11749                                 /* ERATT polling has handled ERATT */
11750                                 ha_copy &= ~HA_ERATT;
11751                         else
11752                                 /* Indicate interrupt handler handles ERATT */
11753                                 phba->hba_flag |= HBA_ERATT_HANDLED;
11754                 }
11755
11756                 /*
11757                  * If there is deferred error attention, do not check for any
11758                  * interrupt.
11759                  */
11760                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11761                         spin_unlock_irqrestore(&phba->hbalock, iflag);
11762                         return IRQ_NONE;
11763                 }
11764
11765                 /* Clear up only attention source related to slow-path */
11766                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11767                         goto unplug_error;
11768
11769                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11770                         HC_LAINT_ENA | HC_ERINT_ENA),
11771                         phba->HCregaddr);
11772                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11773                         phba->HAregaddr);
11774                 writel(hc_copy, phba->HCregaddr);
11775                 readl(phba->HAregaddr); /* flush */
11776                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11777         } else
11778                 ha_copy = phba->ha_copy;
11779
11780         work_ha_copy = ha_copy & phba->work_ha_mask;
11781
11782         if (work_ha_copy) {
11783                 if (work_ha_copy & HA_LATT) {
11784                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11785                                 /*
11786                                  * Turn off Link Attention interrupts
11787                                  * until CLEAR_LA done
11788                                  */
11789                                 spin_lock_irqsave(&phba->hbalock, iflag);
11790                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
11791                                 if (lpfc_readl(phba->HCregaddr, &control))
11792                                         goto unplug_error;
11793                                 control &= ~HC_LAINT_ENA;
11794                                 writel(control, phba->HCregaddr);
11795                                 readl(phba->HCregaddr); /* flush */
11796                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11797                         }
11798                         else
11799                                 work_ha_copy &= ~HA_LATT;
11800                 }
11801
11802                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
11803                         /*
11804                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11805                          * the only slow ring.
11806                          */
11807                         status = (work_ha_copy &
11808                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
11809                         status >>= (4*LPFC_ELS_RING);
11810                         if (status & HA_RXMASK) {
11811                                 spin_lock_irqsave(&phba->hbalock, iflag);
11812                                 if (lpfc_readl(phba->HCregaddr, &control))
11813                                         goto unplug_error;
11814
11815                                 lpfc_debugfs_slow_ring_trc(phba,
11816                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
11817                                 control, status,
11818                                 (uint32_t)phba->sli.slistat.sli_intr);
11819
11820                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
11821                                         lpfc_debugfs_slow_ring_trc(phba,
11822                                                 "ISR Disable ring:"
11823                                                 "pwork:x%x hawork:x%x wait:x%x",
11824                                                 phba->work_ha, work_ha_copy,
11825                                                 (uint32_t)((unsigned long)
11826                                                 &phba->work_waitq));
11827
11828                                         control &=
11829                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
11830                                         writel(control, phba->HCregaddr);
11831                                         readl(phba->HCregaddr); /* flush */
11832                                 }
11833                                 else {
11834                                         lpfc_debugfs_slow_ring_trc(phba,
11835                                                 "ISR slow ring:   pwork:"
11836                                                 "x%x hawork:x%x wait:x%x",
11837                                                 phba->work_ha, work_ha_copy,
11838                                                 (uint32_t)((unsigned long)
11839                                                 &phba->work_waitq));
11840                                 }
11841                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11842                         }
11843                 }
11844                 spin_lock_irqsave(&phba->hbalock, iflag);
11845                 if (work_ha_copy & HA_ERATT) {
11846                         if (lpfc_sli_read_hs(phba))
11847                                 goto unplug_error;
11848                         /*
11849                          * Check if there is a deferred error condition
11850                          * is active
11851                          */
11852                         if ((HS_FFER1 & phba->work_hs) &&
11853                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11854                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
11855                                   phba->work_hs)) {
11856                                 phba->hba_flag |= DEFER_ERATT;
11857                                 /* Clear all interrupt enable conditions */
11858                                 writel(0, phba->HCregaddr);
11859                                 readl(phba->HCregaddr);
11860                         }
11861                 }
11862
11863                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
11864                         pmb = phba->sli.mbox_active;
11865                         pmbox = &pmb->u.mb;
11866                         mbox = phba->mbox;
11867                         vport = pmb->vport;
11868
11869                         /* First check out the status word */
11870                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11871                         if (pmbox->mbxOwner != OWN_HOST) {
11872                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11873                                 /*
11874                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
11875                                  * mbxStatus <status>
11876                                  */
11877                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11878                                                 LOG_SLI,
11879                                                 "(%d):0304 Stray Mailbox "
11880                                                 "Interrupt mbxCommand x%x "
11881                                                 "mbxStatus x%x\n",
11882                                                 (vport ? vport->vpi : 0),
11883                                                 pmbox->mbxCommand,
11884                                                 pmbox->mbxStatus);
11885                                 /* clear mailbox attention bit */
11886                                 work_ha_copy &= ~HA_MBATT;
11887                         } else {
11888                                 phba->sli.mbox_active = NULL;
11889                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11890                                 phba->last_completion_time = jiffies;
11891                                 del_timer(&phba->sli.mbox_tmo);
11892                                 if (pmb->mbox_cmpl) {
11893                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
11894                                                         MAILBOX_CMD_SIZE);
11895                                         if (pmb->out_ext_byte_len &&
11896                                                 pmb->context2)
11897                                                 lpfc_sli_pcimem_bcopy(
11898                                                 phba->mbox_ext,
11899                                                 pmb->context2,
11900                                                 pmb->out_ext_byte_len);
11901                                 }
11902                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11903                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11904
11905                                         lpfc_debugfs_disc_trc(vport,
11906                                                 LPFC_DISC_TRC_MBOX_VPORT,
11907                                                 "MBOX dflt rpi: : "
11908                                                 "status:x%x rpi:x%x",
11909                                                 (uint32_t)pmbox->mbxStatus,
11910                                                 pmbox->un.varWords[0], 0);
11911
11912                                         if (!pmbox->mbxStatus) {
11913                                                 mp = (struct lpfc_dmabuf *)
11914                                                         (pmb->context1);
11915                                                 ndlp = (struct lpfc_nodelist *)
11916                                                         pmb->context2;
11917
11918                                                 /* Reg_LOGIN of dflt RPI was
11919                                                  * successful. new lets get
11920                                                  * rid of the RPI using the
11921                                                  * same mbox buffer.
11922                                                  */
11923                                                 lpfc_unreg_login(phba,
11924                                                         vport->vpi,
11925                                                         pmbox->un.varWords[0],
11926                                                         pmb);
11927                                                 pmb->mbox_cmpl =
11928                                                         lpfc_mbx_cmpl_dflt_rpi;
11929                                                 pmb->context1 = mp;
11930                                                 pmb->context2 = ndlp;
11931                                                 pmb->vport = vport;
11932                                                 rc = lpfc_sli_issue_mbox(phba,
11933                                                                 pmb,
11934                                                                 MBX_NOWAIT);
11935                                                 if (rc != MBX_BUSY)
11936                                                         lpfc_printf_log(phba,
11937                                                         KERN_ERR,
11938                                                         LOG_MBOX | LOG_SLI,
11939                                                         "0350 rc should have"
11940                                                         "been MBX_BUSY\n");
11941                                                 if (rc != MBX_NOT_FINISHED)
11942                                                         goto send_current_mbox;
11943                                         }
11944                                 }
11945                                 spin_lock_irqsave(
11946                                                 &phba->pport->work_port_lock,
11947                                                 iflag);
11948                                 phba->pport->work_port_events &=
11949                                         ~WORKER_MBOX_TMO;
11950                                 spin_unlock_irqrestore(
11951                                                 &phba->pport->work_port_lock,
11952                                                 iflag);
11953                                 lpfc_mbox_cmpl_put(phba, pmb);
11954                         }
11955                 } else
11956                         spin_unlock_irqrestore(&phba->hbalock, iflag);
11957
11958                 if ((work_ha_copy & HA_MBATT) &&
11959                     (phba->sli.mbox_active == NULL)) {
11960 send_current_mbox:
11961                         /* Process next mailbox command if there is one */
11962                         do {
11963                                 rc = lpfc_sli_issue_mbox(phba, NULL,
11964                                                          MBX_NOWAIT);
11965                         } while (rc == MBX_NOT_FINISHED);
11966                         if (rc != MBX_SUCCESS)
11967                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11968                                                 LOG_SLI, "0349 rc should be "
11969                                                 "MBX_SUCCESS\n");
11970                 }
11971
11972                 spin_lock_irqsave(&phba->hbalock, iflag);
11973                 phba->work_ha |= work_ha_copy;
11974                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11975                 lpfc_worker_wake_up(phba);
11976         }
11977         return IRQ_HANDLED;
11978 unplug_error:
11979         spin_unlock_irqrestore(&phba->hbalock, iflag);
11980         return IRQ_HANDLED;
11981
11982 } /* lpfc_sli_sp_intr_handler */
11983
11984 /**
11985  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
11986  * @irq: Interrupt number.
11987  * @dev_id: The device context pointer.
11988  *
11989  * This function is directly called from the PCI layer as an interrupt
11990  * service routine when device with SLI-3 interface spec is enabled with
11991  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11992  * ring event in the HBA. However, when the device is enabled with either
11993  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11994  * device-level interrupt handler. When the PCI slot is in error recovery
11995  * or the HBA is undergoing initialization, the interrupt handler will not
11996  * process the interrupt. The SCSI FCP fast-path ring event are handled in
11997  * the intrrupt context. This function is called without any lock held.
11998  * It gets the hbalock to access and update SLI data structures.
11999  *
12000  * This function returns IRQ_HANDLED when interrupt is handled else it
12001  * returns IRQ_NONE.
12002  **/
12003 irqreturn_t
12004 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
12005 {
12006         struct lpfc_hba  *phba;
12007         uint32_t ha_copy;
12008         unsigned long status;
12009         unsigned long iflag;
12010         struct lpfc_sli_ring *pring;
12011
12012         /* Get the driver's phba structure from the dev_id and
12013          * assume the HBA is not interrupting.
12014          */
12015         phba = (struct lpfc_hba *) dev_id;
12016
12017         if (unlikely(!phba))
12018                 return IRQ_NONE;
12019
12020         /*
12021          * Stuff needs to be attented to when this function is invoked as an
12022          * individual interrupt handler in MSI-X multi-message interrupt mode
12023          */
12024         if (phba->intr_type == MSIX) {
12025                 /* Check device state for handling interrupt */
12026                 if (lpfc_intr_state_check(phba))
12027                         return IRQ_NONE;
12028                 /* Need to read HA REG for FCP ring and other ring events */
12029                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12030                         return IRQ_HANDLED;
12031                 /* Clear up only attention source related to fast-path */
12032                 spin_lock_irqsave(&phba->hbalock, iflag);
12033                 /*
12034                  * If there is deferred error attention, do not check for
12035                  * any interrupt.
12036                  */
12037                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12038                         spin_unlock_irqrestore(&phba->hbalock, iflag);
12039                         return IRQ_NONE;
12040                 }
12041                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12042                         phba->HAregaddr);
12043                 readl(phba->HAregaddr); /* flush */
12044                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12045         } else
12046                 ha_copy = phba->ha_copy;
12047
12048         /*
12049          * Process all events on FCP ring. Take the optimized path for FCP IO.
12050          */
12051         ha_copy &= ~(phba->work_ha_mask);
12052
12053         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12054         status >>= (4*LPFC_FCP_RING);
12055         pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12056         if (status & HA_RXMASK)
12057                 lpfc_sli_handle_fast_ring_event(phba, pring, status);
12058
12059         if (phba->cfg_multi_ring_support == 2) {
12060                 /*
12061                  * Process all events on extra ring. Take the optimized path
12062                  * for extra ring IO.
12063                  */
12064                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12065                 status >>= (4*LPFC_EXTRA_RING);
12066                 if (status & HA_RXMASK) {
12067                         lpfc_sli_handle_fast_ring_event(phba,
12068                                         &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12069                                         status);
12070                 }
12071         }
12072         return IRQ_HANDLED;
12073 }  /* lpfc_sli_fp_intr_handler */
12074
12075 /**
12076  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12077  * @irq: Interrupt number.
12078  * @dev_id: The device context pointer.
12079  *
12080  * This function is the HBA device-level interrupt handler to device with
12081  * SLI-3 interface spec, called from the PCI layer when either MSI or
12082  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12083  * requires driver attention. This function invokes the slow-path interrupt
12084  * attention handling function and fast-path interrupt attention handling
12085  * function in turn to process the relevant HBA attention events. This
12086  * function is called without any lock held. It gets the hbalock to access
12087  * and update SLI data structures.
12088  *
12089  * This function returns IRQ_HANDLED when interrupt is handled, else it
12090  * returns IRQ_NONE.
12091  **/
12092 irqreturn_t
12093 lpfc_sli_intr_handler(int irq, void *dev_id)
12094 {
12095         struct lpfc_hba  *phba;
12096         irqreturn_t sp_irq_rc, fp_irq_rc;
12097         unsigned long status1, status2;
12098         uint32_t hc_copy;
12099
12100         /*
12101          * Get the driver's phba structure from the dev_id and
12102          * assume the HBA is not interrupting.
12103          */
12104         phba = (struct lpfc_hba *) dev_id;
12105
12106         if (unlikely(!phba))
12107                 return IRQ_NONE;
12108
12109         /* Check device state for handling interrupt */
12110         if (lpfc_intr_state_check(phba))
12111                 return IRQ_NONE;
12112
12113         spin_lock(&phba->hbalock);
12114         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12115                 spin_unlock(&phba->hbalock);
12116                 return IRQ_HANDLED;
12117         }
12118
12119         if (unlikely(!phba->ha_copy)) {
12120                 spin_unlock(&phba->hbalock);
12121                 return IRQ_NONE;
12122         } else if (phba->ha_copy & HA_ERATT) {
12123                 if (phba->hba_flag & HBA_ERATT_HANDLED)
12124                         /* ERATT polling has handled ERATT */
12125                         phba->ha_copy &= ~HA_ERATT;
12126                 else
12127                         /* Indicate interrupt handler handles ERATT */
12128                         phba->hba_flag |= HBA_ERATT_HANDLED;
12129         }
12130
12131         /*
12132          * If there is deferred error attention, do not check for any interrupt.
12133          */
12134         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12135                 spin_unlock(&phba->hbalock);
12136                 return IRQ_NONE;
12137         }
12138
12139         /* Clear attention sources except link and error attentions */
12140         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12141                 spin_unlock(&phba->hbalock);
12142                 return IRQ_HANDLED;
12143         }
12144         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12145                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12146                 phba->HCregaddr);
12147         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12148         writel(hc_copy, phba->HCregaddr);
12149         readl(phba->HAregaddr); /* flush */
12150         spin_unlock(&phba->hbalock);
12151
12152         /*
12153          * Invokes slow-path host attention interrupt handling as appropriate.
12154          */
12155
12156         /* status of events with mailbox and link attention */
12157         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12158
12159         /* status of events with ELS ring */
12160         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
12161         status2 >>= (4*LPFC_ELS_RING);
12162
12163         if (status1 || (status2 & HA_RXMASK))
12164                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12165         else
12166                 sp_irq_rc = IRQ_NONE;
12167
12168         /*
12169          * Invoke fast-path host attention interrupt handling as appropriate.
12170          */
12171
12172         /* status of events with FCP ring */
12173         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12174         status1 >>= (4*LPFC_FCP_RING);
12175
12176         /* status of events with extra ring */
12177         if (phba->cfg_multi_ring_support == 2) {
12178                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12179                 status2 >>= (4*LPFC_EXTRA_RING);
12180         } else
12181                 status2 = 0;
12182
12183         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12184                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12185         else
12186                 fp_irq_rc = IRQ_NONE;
12187
12188         /* Return device-level interrupt handling status */
12189         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12190 }  /* lpfc_sli_intr_handler */
12191
12192 /**
12193  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12194  * @phba: pointer to lpfc hba data structure.
12195  *
12196  * This routine is invoked by the worker thread to process all the pending
12197  * SLI4 FCP abort XRI events.
12198  **/
12199 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12200 {
12201         struct lpfc_cq_event *cq_event;
12202
12203         /* First, declare the fcp xri abort event has been handled */
12204         spin_lock_irq(&phba->hbalock);
12205         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12206         spin_unlock_irq(&phba->hbalock);
12207         /* Now, handle all the fcp xri abort events */
12208         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12209                 /* Get the first event from the head of the event queue */
12210                 spin_lock_irq(&phba->hbalock);
12211                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12212                                  cq_event, struct lpfc_cq_event, list);
12213                 spin_unlock_irq(&phba->hbalock);
12214                 /* Notify aborted XRI for FCP work queue */
12215                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12216                 /* Free the event processed back to the free pool */
12217                 lpfc_sli4_cq_event_release(phba, cq_event);
12218         }
12219 }
12220
12221 /**
12222  * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12223  * @phba: pointer to lpfc hba data structure.
12224  *
12225  * This routine is invoked by the worker thread to process all the pending
12226  * SLI4 NVME abort XRI events.
12227  **/
12228 void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12229 {
12230         struct lpfc_cq_event *cq_event;
12231
12232         /* First, declare the fcp xri abort event has been handled */
12233         spin_lock_irq(&phba->hbalock);
12234         phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12235         spin_unlock_irq(&phba->hbalock);
12236         /* Now, handle all the fcp xri abort events */
12237         while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12238                 /* Get the first event from the head of the event queue */
12239                 spin_lock_irq(&phba->hbalock);
12240                 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12241                                  cq_event, struct lpfc_cq_event, list);
12242                 spin_unlock_irq(&phba->hbalock);
12243                 /* Notify aborted XRI for NVME work queue */
12244                 if (phba->nvmet_support) {
12245                         lpfc_sli4_nvmet_xri_aborted(phba,
12246                                                     &cq_event->cqe.wcqe_axri);
12247                 } else {
12248                         lpfc_sli4_nvme_xri_aborted(phba,
12249                                                    &cq_event->cqe.wcqe_axri);
12250                 }
12251                 /* Free the event processed back to the free pool */
12252                 lpfc_sli4_cq_event_release(phba, cq_event);
12253         }
12254 }
12255
12256 /**
12257  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12258  * @phba: pointer to lpfc hba data structure.
12259  *
12260  * This routine is invoked by the worker thread to process all the pending
12261  * SLI4 els abort xri events.
12262  **/
12263 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12264 {
12265         struct lpfc_cq_event *cq_event;
12266
12267         /* First, declare the els xri abort event has been handled */
12268         spin_lock_irq(&phba->hbalock);
12269         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12270         spin_unlock_irq(&phba->hbalock);
12271         /* Now, handle all the els xri abort events */
12272         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12273                 /* Get the first event from the head of the event queue */
12274                 spin_lock_irq(&phba->hbalock);
12275                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12276                                  cq_event, struct lpfc_cq_event, list);
12277                 spin_unlock_irq(&phba->hbalock);
12278                 /* Notify aborted XRI for ELS work queue */
12279                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12280                 /* Free the event processed back to the free pool */
12281                 lpfc_sli4_cq_event_release(phba, cq_event);
12282         }
12283 }
12284
12285 /**
12286  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12287  * @phba: pointer to lpfc hba data structure
12288  * @pIocbIn: pointer to the rspiocbq
12289  * @pIocbOut: pointer to the cmdiocbq
12290  * @wcqe: pointer to the complete wcqe
12291  *
12292  * This routine transfers the fields of a command iocbq to a response iocbq
12293  * by copying all the IOCB fields from command iocbq and transferring the
12294  * completion status information from the complete wcqe.
12295  **/
12296 static void
12297 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12298                               struct lpfc_iocbq *pIocbIn,
12299                               struct lpfc_iocbq *pIocbOut,
12300                               struct lpfc_wcqe_complete *wcqe)
12301 {
12302         int numBdes, i;
12303         unsigned long iflags;
12304         uint32_t status, max_response;
12305         struct lpfc_dmabuf *dmabuf;
12306         struct ulp_bde64 *bpl, bde;
12307         size_t offset = offsetof(struct lpfc_iocbq, iocb);
12308
12309         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12310                sizeof(struct lpfc_iocbq) - offset);
12311         /* Map WCQE parameters into irspiocb parameters */
12312         status = bf_get(lpfc_wcqe_c_status, wcqe);
12313         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12314         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12315                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12316                         pIocbIn->iocb.un.fcpi.fcpi_parm =
12317                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
12318                                         wcqe->total_data_placed;
12319                 else
12320                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12321         else {
12322                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12323                 switch (pIocbOut->iocb.ulpCommand) {
12324                 case CMD_ELS_REQUEST64_CR:
12325                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12326                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
12327                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12328                         max_response = bde.tus.f.bdeSize;
12329                         break;
12330                 case CMD_GEN_REQUEST64_CR:
12331                         max_response = 0;
12332                         if (!pIocbOut->context3)
12333                                 break;
12334                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12335                                         sizeof(struct ulp_bde64);
12336                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12337                         bpl = (struct ulp_bde64 *)dmabuf->virt;
12338                         for (i = 0; i < numBdes; i++) {
12339                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12340                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12341                                         max_response += bde.tus.f.bdeSize;
12342                         }
12343                         break;
12344                 default:
12345                         max_response = wcqe->total_data_placed;
12346                         break;
12347                 }
12348                 if (max_response < wcqe->total_data_placed)
12349                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12350                 else
12351                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12352                                 wcqe->total_data_placed;
12353         }
12354
12355         /* Convert BG errors for completion status */
12356         if (status == CQE_STATUS_DI_ERROR) {
12357                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12358
12359                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12360                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12361                 else
12362                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12363
12364                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12365                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12366                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12367                                 BGS_GUARD_ERR_MASK;
12368                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12369                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12370                                 BGS_APPTAG_ERR_MASK;
12371                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12372                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12373                                 BGS_REFTAG_ERR_MASK;
12374
12375                 /* Check to see if there was any good data before the error */
12376                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12377                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12378                                 BGS_HI_WATER_MARK_PRESENT_MASK;
12379                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
12380                                 wcqe->total_data_placed;
12381                 }
12382
12383                 /*
12384                 * Set ALL the error bits to indicate we don't know what
12385                 * type of error it is.
12386                 */
12387                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12388                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12389                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12390                                 BGS_GUARD_ERR_MASK);
12391         }
12392
12393         /* Pick up HBA exchange busy condition */
12394         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12395                 spin_lock_irqsave(&phba->hbalock, iflags);
12396                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12397                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12398         }
12399 }
12400
12401 /**
12402  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12403  * @phba: Pointer to HBA context object.
12404  * @wcqe: Pointer to work-queue completion queue entry.
12405  *
12406  * This routine handles an ELS work-queue completion event and construct
12407  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12408  * discovery engine to handle.
12409  *
12410  * Return: Pointer to the receive IOCBQ, NULL otherwise.
12411  **/
12412 static struct lpfc_iocbq *
12413 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12414                                struct lpfc_iocbq *irspiocbq)
12415 {
12416         struct lpfc_sli_ring *pring;
12417         struct lpfc_iocbq *cmdiocbq;
12418         struct lpfc_wcqe_complete *wcqe;
12419         unsigned long iflags;
12420
12421         pring = lpfc_phba_elsring(phba);
12422
12423         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
12424         spin_lock_irqsave(&pring->ring_lock, iflags);
12425         pring->stats.iocb_event++;
12426         /* Look up the ELS command IOCB and create pseudo response IOCB */
12427         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12428                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12429         /* Put the iocb back on the txcmplq */
12430         lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12431         spin_unlock_irqrestore(&pring->ring_lock, iflags);
12432
12433         if (unlikely(!cmdiocbq)) {
12434                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12435                                 "0386 ELS complete with no corresponding "
12436                                 "cmdiocb: iotag (%d)\n",
12437                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12438                 lpfc_sli_release_iocbq(phba, irspiocbq);
12439                 return NULL;
12440         }
12441
12442         /* Fake the irspiocbq and copy necessary response information */
12443         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
12444
12445         return irspiocbq;
12446 }
12447
12448 /**
12449  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12450  * @phba: Pointer to HBA context object.
12451  * @cqe: Pointer to mailbox completion queue entry.
12452  *
12453  * This routine process a mailbox completion queue entry with asynchrous
12454  * event.
12455  *
12456  * Return: true if work posted to worker thread, otherwise false.
12457  **/
12458 static bool
12459 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12460 {
12461         struct lpfc_cq_event *cq_event;
12462         unsigned long iflags;
12463
12464         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12465                         "0392 Async Event: word0:x%x, word1:x%x, "
12466                         "word2:x%x, word3:x%x\n", mcqe->word0,
12467                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12468
12469         /* Allocate a new internal CQ_EVENT entry */
12470         cq_event = lpfc_sli4_cq_event_alloc(phba);
12471         if (!cq_event) {
12472                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12473                                 "0394 Failed to allocate CQ_EVENT entry\n");
12474                 return false;
12475         }
12476
12477         /* Move the CQE into an asynchronous event entry */
12478         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12479         spin_lock_irqsave(&phba->hbalock, iflags);
12480         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12481         /* Set the async event flag */
12482         phba->hba_flag |= ASYNC_EVENT;
12483         spin_unlock_irqrestore(&phba->hbalock, iflags);
12484
12485         return true;
12486 }
12487
12488 /**
12489  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12490  * @phba: Pointer to HBA context object.
12491  * @cqe: Pointer to mailbox completion queue entry.
12492  *
12493  * This routine process a mailbox completion queue entry with mailbox
12494  * completion event.
12495  *
12496  * Return: true if work posted to worker thread, otherwise false.
12497  **/
12498 static bool
12499 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12500 {
12501         uint32_t mcqe_status;
12502         MAILBOX_t *mbox, *pmbox;
12503         struct lpfc_mqe *mqe;
12504         struct lpfc_vport *vport;
12505         struct lpfc_nodelist *ndlp;
12506         struct lpfc_dmabuf *mp;
12507         unsigned long iflags;
12508         LPFC_MBOXQ_t *pmb;
12509         bool workposted = false;
12510         int rc;
12511
12512         /* If not a mailbox complete MCQE, out by checking mailbox consume */
12513         if (!bf_get(lpfc_trailer_completed, mcqe))
12514                 goto out_no_mqe_complete;
12515
12516         /* Get the reference to the active mbox command */
12517         spin_lock_irqsave(&phba->hbalock, iflags);
12518         pmb = phba->sli.mbox_active;
12519         if (unlikely(!pmb)) {
12520                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12521                                 "1832 No pending MBOX command to handle\n");
12522                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12523                 goto out_no_mqe_complete;
12524         }
12525         spin_unlock_irqrestore(&phba->hbalock, iflags);
12526         mqe = &pmb->u.mqe;
12527         pmbox = (MAILBOX_t *)&pmb->u.mqe;
12528         mbox = phba->mbox;
12529         vport = pmb->vport;
12530
12531         /* Reset heartbeat timer */
12532         phba->last_completion_time = jiffies;
12533         del_timer(&phba->sli.mbox_tmo);
12534
12535         /* Move mbox data to caller's mailbox region, do endian swapping */
12536         if (pmb->mbox_cmpl && mbox)
12537                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
12538
12539         /*
12540          * For mcqe errors, conditionally move a modified error code to
12541          * the mbox so that the error will not be missed.
12542          */
12543         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12544         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12545                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12546                         bf_set(lpfc_mqe_status, mqe,
12547                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
12548         }
12549         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12550                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12551                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12552                                       "MBOX dflt rpi: status:x%x rpi:x%x",
12553                                       mcqe_status,
12554                                       pmbox->un.varWords[0], 0);
12555                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12556                         mp = (struct lpfc_dmabuf *)(pmb->context1);
12557                         ndlp = (struct lpfc_nodelist *)pmb->context2;
12558                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
12559                          * RID of the PPI using the same mbox buffer.
12560                          */
12561                         lpfc_unreg_login(phba, vport->vpi,
12562                                          pmbox->un.varWords[0], pmb);
12563                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12564                         pmb->context1 = mp;
12565                         pmb->context2 = ndlp;
12566                         pmb->vport = vport;
12567                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12568                         if (rc != MBX_BUSY)
12569                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12570                                                 LOG_SLI, "0385 rc should "
12571                                                 "have been MBX_BUSY\n");
12572                         if (rc != MBX_NOT_FINISHED)
12573                                 goto send_current_mbox;
12574                 }
12575         }
12576         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12577         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12578         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12579
12580         /* There is mailbox completion work to do */
12581         spin_lock_irqsave(&phba->hbalock, iflags);
12582         __lpfc_mbox_cmpl_put(phba, pmb);
12583         phba->work_ha |= HA_MBATT;
12584         spin_unlock_irqrestore(&phba->hbalock, iflags);
12585         workposted = true;
12586
12587 send_current_mbox:
12588         spin_lock_irqsave(&phba->hbalock, iflags);
12589         /* Release the mailbox command posting token */
12590         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12591         /* Setting active mailbox pointer need to be in sync to flag clear */
12592         phba->sli.mbox_active = NULL;
12593         spin_unlock_irqrestore(&phba->hbalock, iflags);
12594         /* Wake up worker thread to post the next pending mailbox command */
12595         lpfc_worker_wake_up(phba);
12596 out_no_mqe_complete:
12597         if (bf_get(lpfc_trailer_consumed, mcqe))
12598                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12599         return workposted;
12600 }
12601
12602 /**
12603  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12604  * @phba: Pointer to HBA context object.
12605  * @cqe: Pointer to mailbox completion queue entry.
12606  *
12607  * This routine process a mailbox completion queue entry, it invokes the
12608  * proper mailbox complete handling or asynchrous event handling routine
12609  * according to the MCQE's async bit.
12610  *
12611  * Return: true if work posted to worker thread, otherwise false.
12612  **/
12613 static bool
12614 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12615 {
12616         struct lpfc_mcqe mcqe;
12617         bool workposted;
12618
12619         /* Copy the mailbox MCQE and convert endian order as needed */
12620         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12621
12622         /* Invoke the proper event handling routine */
12623         if (!bf_get(lpfc_trailer_async, &mcqe))
12624                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12625         else
12626                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12627         return workposted;
12628 }
12629
12630 /**
12631  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12632  * @phba: Pointer to HBA context object.
12633  * @cq: Pointer to associated CQ
12634  * @wcqe: Pointer to work-queue completion queue entry.
12635  *
12636  * This routine handles an ELS work-queue completion event.
12637  *
12638  * Return: true if work posted to worker thread, otherwise false.
12639  **/
12640 static bool
12641 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12642                              struct lpfc_wcqe_complete *wcqe)
12643 {
12644         struct lpfc_iocbq *irspiocbq;
12645         unsigned long iflags;
12646         struct lpfc_sli_ring *pring = cq->pring;
12647         int txq_cnt = 0;
12648         int txcmplq_cnt = 0;
12649         int fcp_txcmplq_cnt = 0;
12650
12651         /* Get an irspiocbq for later ELS response processing use */
12652         irspiocbq = lpfc_sli_get_iocbq(phba);
12653         if (!irspiocbq) {
12654                 if (!list_empty(&pring->txq))
12655                         txq_cnt++;
12656                 if (!list_empty(&pring->txcmplq))
12657                         txcmplq_cnt++;
12658                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12659                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12660                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
12661                         txq_cnt, phba->iocb_cnt,
12662                         fcp_txcmplq_cnt,
12663                         txcmplq_cnt);
12664                 return false;
12665         }
12666
12667         /* Save off the slow-path queue event for work thread to process */
12668         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
12669         spin_lock_irqsave(&phba->hbalock, iflags);
12670         list_add_tail(&irspiocbq->cq_event.list,
12671                       &phba->sli4_hba.sp_queue_event);
12672         phba->hba_flag |= HBA_SP_QUEUE_EVT;
12673         spin_unlock_irqrestore(&phba->hbalock, iflags);
12674
12675         return true;
12676 }
12677
12678 /**
12679  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12680  * @phba: Pointer to HBA context object.
12681  * @wcqe: Pointer to work-queue completion queue entry.
12682  *
12683  * This routine handles slow-path WQ entry consumed event by invoking the
12684  * proper WQ release routine to the slow-path WQ.
12685  **/
12686 static void
12687 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12688                              struct lpfc_wcqe_release *wcqe)
12689 {
12690         /* sanity check on queue memory */
12691         if (unlikely(!phba->sli4_hba.els_wq))
12692                 return;
12693         /* Check for the slow-path ELS work queue */
12694         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12695                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12696                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12697         else
12698                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12699                                 "2579 Slow-path wqe consume event carries "
12700                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12701                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12702                                 phba->sli4_hba.els_wq->queue_id);
12703 }
12704
12705 /**
12706  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12707  * @phba: Pointer to HBA context object.
12708  * @cq: Pointer to a WQ completion queue.
12709  * @wcqe: Pointer to work-queue completion queue entry.
12710  *
12711  * This routine handles an XRI abort event.
12712  *
12713  * Return: true if work posted to worker thread, otherwise false.
12714  **/
12715 static bool
12716 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12717                                    struct lpfc_queue *cq,
12718                                    struct sli4_wcqe_xri_aborted *wcqe)
12719 {
12720         bool workposted = false;
12721         struct lpfc_cq_event *cq_event;
12722         unsigned long iflags;
12723
12724         /* Allocate a new internal CQ_EVENT entry */
12725         cq_event = lpfc_sli4_cq_event_alloc(phba);
12726         if (!cq_event) {
12727                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12728                                 "0602 Failed to allocate CQ_EVENT entry\n");
12729                 return false;
12730         }
12731
12732         /* Move the CQE into the proper xri abort event list */
12733         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12734         switch (cq->subtype) {
12735         case LPFC_FCP:
12736                 spin_lock_irqsave(&phba->hbalock, iflags);
12737                 list_add_tail(&cq_event->list,
12738                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12739                 /* Set the fcp xri abort event flag */
12740                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12741                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12742                 workposted = true;
12743                 break;
12744         case LPFC_ELS:
12745                 spin_lock_irqsave(&phba->hbalock, iflags);
12746                 list_add_tail(&cq_event->list,
12747                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12748                 /* Set the els xri abort event flag */
12749                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12750                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12751                 workposted = true;
12752                 break;
12753         case LPFC_NVME:
12754                 spin_lock_irqsave(&phba->hbalock, iflags);
12755                 list_add_tail(&cq_event->list,
12756                               &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12757                 /* Set the nvme xri abort event flag */
12758                 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12759                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12760                 workposted = true;
12761                 break;
12762         default:
12763                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12764                                 "0603 Invalid CQ subtype %d: "
12765                                 "%08x %08x %08x %08x\n",
12766                                 cq->subtype, wcqe->word0, wcqe->parameter,
12767                                 wcqe->word2, wcqe->word3);
12768                 lpfc_sli4_cq_event_release(phba, cq_event);
12769                 workposted = false;
12770                 break;
12771         }
12772         return workposted;
12773 }
12774
12775 /**
12776  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12777  * @phba: Pointer to HBA context object.
12778  * @rcqe: Pointer to receive-queue completion queue entry.
12779  *
12780  * This routine process a receive-queue completion queue entry.
12781  *
12782  * Return: true if work posted to worker thread, otherwise false.
12783  **/
12784 static bool
12785 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12786 {
12787         bool workposted = false;
12788         struct fc_frame_header *fc_hdr;
12789         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12790         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12791         struct hbq_dmabuf *dma_buf;
12792         uint32_t status, rq_id;
12793         unsigned long iflags;
12794
12795         /* sanity check on queue memory */
12796         if (unlikely(!hrq) || unlikely(!drq))
12797                 return workposted;
12798
12799         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12800                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12801         else
12802                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12803         if (rq_id != hrq->queue_id)
12804                 goto out;
12805
12806         status = bf_get(lpfc_rcqe_status, rcqe);
12807         switch (status) {
12808         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12809                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12810                                 "2537 Receive Frame Truncated!!\n");
12811                 hrq->RQ_buf_trunc++;
12812         case FC_STATUS_RQ_SUCCESS:
12813                 lpfc_sli4_rq_release(hrq, drq);
12814                 spin_lock_irqsave(&phba->hbalock, iflags);
12815                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12816                 if (!dma_buf) {
12817                         hrq->RQ_no_buf_found++;
12818                         spin_unlock_irqrestore(&phba->hbalock, iflags);
12819                         goto out;
12820                 }
12821                 hrq->RQ_rcv_buf++;
12822                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12823
12824                 /* If a NVME LS event (type 0x28), treat it as Fast path */
12825                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12826
12827                 /* save off the frame for the word thread to process */
12828                 list_add_tail(&dma_buf->cq_event.list,
12829                               &phba->sli4_hba.sp_queue_event);
12830                 /* Frame received */
12831                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12832                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12833                 workposted = true;
12834                 break;
12835         case FC_STATUS_INSUFF_BUF_NEED_BUF:
12836         case FC_STATUS_INSUFF_BUF_FRM_DISC:
12837                 hrq->RQ_no_posted_buf++;
12838                 /* Post more buffers if possible */
12839                 spin_lock_irqsave(&phba->hbalock, iflags);
12840                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12841                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12842                 workposted = true;
12843                 break;
12844         }
12845 out:
12846         return workposted;
12847 }
12848
12849 /**
12850  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12851  * @phba: Pointer to HBA context object.
12852  * @cq: Pointer to the completion queue.
12853  * @wcqe: Pointer to a completion queue entry.
12854  *
12855  * This routine process a slow-path work-queue or receive queue completion queue
12856  * entry.
12857  *
12858  * Return: true if work posted to worker thread, otherwise false.
12859  **/
12860 static bool
12861 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12862                          struct lpfc_cqe *cqe)
12863 {
12864         struct lpfc_cqe cqevt;
12865         bool workposted = false;
12866
12867         /* Copy the work queue CQE and convert endian order if needed */
12868         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
12869
12870         /* Check and process for different type of WCQE and dispatch */
12871         switch (bf_get(lpfc_cqe_code, &cqevt)) {
12872         case CQE_CODE_COMPL_WQE:
12873                 /* Process the WQ/RQ complete event */
12874                 phba->last_completion_time = jiffies;
12875                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
12876                                 (struct lpfc_wcqe_complete *)&cqevt);
12877                 break;
12878         case CQE_CODE_RELEASE_WQE:
12879                 /* Process the WQ release event */
12880                 lpfc_sli4_sp_handle_rel_wcqe(phba,
12881                                 (struct lpfc_wcqe_release *)&cqevt);
12882                 break;
12883         case CQE_CODE_XRI_ABORTED:
12884                 /* Process the WQ XRI abort event */
12885                 phba->last_completion_time = jiffies;
12886                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12887                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
12888                 break;
12889         case CQE_CODE_RECEIVE:
12890         case CQE_CODE_RECEIVE_V1:
12891                 /* Process the RQ event */
12892                 phba->last_completion_time = jiffies;
12893                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
12894                                 (struct lpfc_rcqe *)&cqevt);
12895                 break;
12896         default:
12897                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12898                                 "0388 Not a valid WCQE code: x%x\n",
12899                                 bf_get(lpfc_cqe_code, &cqevt));
12900                 break;
12901         }
12902         return workposted;
12903 }
12904
12905 /**
12906  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12907  * @phba: Pointer to HBA context object.
12908  * @eqe: Pointer to fast-path event queue entry.
12909  *
12910  * This routine process a event queue entry from the slow-path event queue.
12911  * It will check the MajorCode and MinorCode to determine this is for a
12912  * completion event on a completion queue, if not, an error shall be logged
12913  * and just return. Otherwise, it will get to the corresponding completion
12914  * queue and process all the entries on that completion queue, rearm the
12915  * completion queue, and then return.
12916  *
12917  **/
12918 static void
12919 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12920         struct lpfc_queue *speq)
12921 {
12922         struct lpfc_queue *cq = NULL, *childq;
12923         struct lpfc_cqe *cqe;
12924         bool workposted = false;
12925         int ecount = 0;
12926         uint16_t cqid;
12927
12928         /* Get the reference to the corresponding CQ */
12929         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12930
12931         list_for_each_entry(childq, &speq->child_list, list) {
12932                 if (childq->queue_id == cqid) {
12933                         cq = childq;
12934                         break;
12935                 }
12936         }
12937         if (unlikely(!cq)) {
12938                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12939                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12940                                         "0365 Slow-path CQ identifier "
12941                                         "(%d) does not exist\n", cqid);
12942                 return;
12943         }
12944
12945         /* Save EQ associated with this CQ */
12946         cq->assoc_qp = speq;
12947
12948         /* Process all the entries to the CQ */
12949         switch (cq->type) {
12950         case LPFC_MCQ:
12951                 while ((cqe = lpfc_sli4_cq_get(cq))) {
12952                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
12953                         if (!(++ecount % cq->entry_repost))
12954                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12955                         cq->CQ_mbox++;
12956                 }
12957                 break;
12958         case LPFC_WCQ:
12959                 while ((cqe = lpfc_sli4_cq_get(cq))) {
12960                         if ((cq->subtype == LPFC_FCP) ||
12961                             (cq->subtype == LPFC_NVME))
12962                                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
12963                                                                        cqe);
12964                         else
12965                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12966                                                                       cqe);
12967                         if (!(++ecount % cq->entry_repost))
12968                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12969                 }
12970
12971                 /* Track the max number of CQEs processed in 1 EQ */
12972                 if (ecount > cq->CQ_max_cqe)
12973                         cq->CQ_max_cqe = ecount;
12974                 break;
12975         default:
12976                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12977                                 "0370 Invalid completion queue type (%d)\n",
12978                                 cq->type);
12979                 return;
12980         }
12981
12982         /* Catch the no cq entry condition, log an error */
12983         if (unlikely(ecount == 0))
12984                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12985                                 "0371 No entry from the CQ: identifier "
12986                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12987
12988         /* In any case, flash and re-arm the RCQ */
12989         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12990
12991         /* wake up worker thread if there are works to be done */
12992         if (workposted)
12993                 lpfc_worker_wake_up(phba);
12994 }
12995
12996 /**
12997  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
12998  * @phba: Pointer to HBA context object.
12999  * @cq: Pointer to associated CQ
13000  * @wcqe: Pointer to work-queue completion queue entry.
13001  *
13002  * This routine process a fast-path work queue completion entry from fast-path
13003  * event queue for FCP command response completion.
13004  **/
13005 static void
13006 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13007                              struct lpfc_wcqe_complete *wcqe)
13008 {
13009         struct lpfc_sli_ring *pring = cq->pring;
13010         struct lpfc_iocbq *cmdiocbq;
13011         struct lpfc_iocbq irspiocbq;
13012         unsigned long iflags;
13013
13014         /* Check for response status */
13015         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13016                 /* If resource errors reported from HBA, reduce queue
13017                  * depth of the SCSI device.
13018                  */
13019                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13020                      IOSTAT_LOCAL_REJECT)) &&
13021                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
13022                      IOERR_NO_RESOURCES))
13023                         phba->lpfc_rampdown_queue_depth(phba);
13024
13025                 /* Log the error status */
13026                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13027                                 "0373 FCP complete error: status=x%x, "
13028                                 "hw_status=x%x, total_data_specified=%d, "
13029                                 "parameter=x%x, word3=x%x\n",
13030                                 bf_get(lpfc_wcqe_c_status, wcqe),
13031                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13032                                 wcqe->total_data_placed, wcqe->parameter,
13033                                 wcqe->word3);
13034         }
13035
13036         /* Look up the FCP command IOCB and create pseudo response IOCB */
13037         spin_lock_irqsave(&pring->ring_lock, iflags);
13038         pring->stats.iocb_event++;
13039         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13040                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13041         spin_unlock_irqrestore(&pring->ring_lock, iflags);
13042         if (unlikely(!cmdiocbq)) {
13043                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13044                                 "0374 FCP complete with no corresponding "
13045                                 "cmdiocb: iotag (%d)\n",
13046                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13047                 return;
13048         }
13049
13050         if (cq->assoc_qp)
13051                 cmdiocbq->isr_timestamp =
13052                         cq->assoc_qp->isr_timestamp;
13053
13054         if (cmdiocbq->iocb_cmpl == NULL) {
13055                 if (cmdiocbq->wqe_cmpl) {
13056                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13057                                 spin_lock_irqsave(&phba->hbalock, iflags);
13058                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13059                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13060                         }
13061
13062                         /* Pass the cmd_iocb and the wcqe to the upper layer */
13063                         (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13064                         return;
13065                 }
13066                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13067                                 "0375 FCP cmdiocb not callback function "
13068                                 "iotag: (%d)\n",
13069                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13070                 return;
13071         }
13072
13073         /* Fake the irspiocb and copy necessary response information */
13074         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13075
13076         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13077                 spin_lock_irqsave(&phba->hbalock, iflags);
13078                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13079                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13080         }
13081
13082         /* Pass the cmd_iocb and the rsp state to the upper layer */
13083         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13084 }
13085
13086 /**
13087  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13088  * @phba: Pointer to HBA context object.
13089  * @cq: Pointer to completion queue.
13090  * @wcqe: Pointer to work-queue completion queue entry.
13091  *
13092  * This routine handles an fast-path WQ entry consumed event by invoking the
13093  * proper WQ release routine to the slow-path WQ.
13094  **/
13095 static void
13096 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13097                              struct lpfc_wcqe_release *wcqe)
13098 {
13099         struct lpfc_queue *childwq;
13100         bool wqid_matched = false;
13101         uint16_t hba_wqid;
13102
13103         /* Check for fast-path FCP work queue release */
13104         hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13105         list_for_each_entry(childwq, &cq->child_list, list) {
13106                 if (childwq->queue_id == hba_wqid) {
13107                         lpfc_sli4_wq_release(childwq,
13108                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13109                         wqid_matched = true;
13110                         break;
13111                 }
13112         }
13113         /* Report warning log message if no match found */
13114         if (wqid_matched != true)
13115                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13116                                 "2580 Fast-path wqe consume event carries "
13117                                 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13118 }
13119
13120 /**
13121  * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13122  * @phba: Pointer to HBA context object.
13123  * @rcqe: Pointer to receive-queue completion queue entry.
13124  *
13125  * This routine process a receive-queue completion queue entry.
13126  *
13127  * Return: true if work posted to worker thread, otherwise false.
13128  **/
13129 static bool
13130 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13131                             struct lpfc_rcqe *rcqe)
13132 {
13133         bool workposted = false;
13134         struct lpfc_queue *hrq;
13135         struct lpfc_queue *drq;
13136         struct rqb_dmabuf *dma_buf;
13137         struct fc_frame_header *fc_hdr;
13138         uint32_t status, rq_id;
13139         unsigned long iflags;
13140         uint32_t fctl, idx;
13141
13142         if ((phba->nvmet_support == 0) ||
13143             (phba->sli4_hba.nvmet_cqset == NULL))
13144                 return workposted;
13145
13146         idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13147         hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13148         drq = phba->sli4_hba.nvmet_mrq_data[idx];
13149
13150         /* sanity check on queue memory */
13151         if (unlikely(!hrq) || unlikely(!drq))
13152                 return workposted;
13153
13154         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13155                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13156         else
13157                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13158
13159         if ((phba->nvmet_support == 0) ||
13160             (rq_id != hrq->queue_id))
13161                 return workposted;
13162
13163         status = bf_get(lpfc_rcqe_status, rcqe);
13164         switch (status) {
13165         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13166                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13167                                 "6126 Receive Frame Truncated!!\n");
13168                 hrq->RQ_buf_trunc++;
13169                 break;
13170         case FC_STATUS_RQ_SUCCESS:
13171                 lpfc_sli4_rq_release(hrq, drq);
13172                 spin_lock_irqsave(&phba->hbalock, iflags);
13173                 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13174                 if (!dma_buf) {
13175                         hrq->RQ_no_buf_found++;
13176                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13177                         goto out;
13178                 }
13179                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13180                 hrq->RQ_rcv_buf++;
13181                 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13182
13183                 /* Just some basic sanity checks on FCP Command frame */
13184                 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13185                 fc_hdr->fh_f_ctl[1] << 8 |
13186                 fc_hdr->fh_f_ctl[2]);
13187                 if (((fctl &
13188                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13189                     (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13190                     (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13191                         goto drop;
13192
13193                 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13194                         dma_buf->bytes_recv = bf_get(lpfc_rcqe_length,  rcqe);
13195                         lpfc_nvmet_unsol_fcp_event(
13196                                 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13197                                 cq->assoc_qp->isr_timestamp);
13198                         return false;
13199                 }
13200 drop:
13201                 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13202                 break;
13203         case FC_STATUS_INSUFF_BUF_NEED_BUF:
13204         case FC_STATUS_INSUFF_BUF_FRM_DISC:
13205                 hrq->RQ_no_posted_buf++;
13206                 /* Post more buffers if possible */
13207                 spin_lock_irqsave(&phba->hbalock, iflags);
13208                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13209                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13210                 workposted = true;
13211                 break;
13212         }
13213 out:
13214         return workposted;
13215 }
13216
13217 /**
13218  * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13219  * @cq: Pointer to the completion queue.
13220  * @eqe: Pointer to fast-path completion queue entry.
13221  *
13222  * This routine process a fast-path work queue completion entry from fast-path
13223  * event queue for FCP command response completion.
13224  **/
13225 static int
13226 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13227                          struct lpfc_cqe *cqe)
13228 {
13229         struct lpfc_wcqe_release wcqe;
13230         bool workposted = false;
13231
13232         /* Copy the work queue CQE and convert endian order if needed */
13233         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13234
13235         /* Check and process for different type of WCQE and dispatch */
13236         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13237         case CQE_CODE_COMPL_WQE:
13238         case CQE_CODE_NVME_ERSP:
13239                 cq->CQ_wq++;
13240                 /* Process the WQ complete event */
13241                 phba->last_completion_time = jiffies;
13242                 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13243                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13244                                 (struct lpfc_wcqe_complete *)&wcqe);
13245                 if (cq->subtype == LPFC_NVME_LS)
13246                         lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13247                                 (struct lpfc_wcqe_complete *)&wcqe);
13248                 break;
13249         case CQE_CODE_RELEASE_WQE:
13250                 cq->CQ_release_wqe++;
13251                 /* Process the WQ release event */
13252                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13253                                 (struct lpfc_wcqe_release *)&wcqe);
13254                 break;
13255         case CQE_CODE_XRI_ABORTED:
13256                 cq->CQ_xri_aborted++;
13257                 /* Process the WQ XRI abort event */
13258                 phba->last_completion_time = jiffies;
13259                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13260                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
13261                 break;
13262         case CQE_CODE_RECEIVE_V1:
13263         case CQE_CODE_RECEIVE:
13264                 phba->last_completion_time = jiffies;
13265                 if (cq->subtype == LPFC_NVMET) {
13266                         workposted = lpfc_sli4_nvmet_handle_rcqe(
13267                                 phba, cq, (struct lpfc_rcqe *)&wcqe);
13268                 }
13269                 break;
13270         default:
13271                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13272                                 "0144 Not a valid CQE code: x%x\n",
13273                                 bf_get(lpfc_wcqe_c_code, &wcqe));
13274                 break;
13275         }
13276         return workposted;
13277 }
13278
13279 /**
13280  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
13281  * @phba: Pointer to HBA context object.
13282  * @eqe: Pointer to fast-path event queue entry.
13283  *
13284  * This routine process a event queue entry from the fast-path event queue.
13285  * It will check the MajorCode and MinorCode to determine this is for a
13286  * completion event on a completion queue, if not, an error shall be logged
13287  * and just return. Otherwise, it will get to the corresponding completion
13288  * queue and process all the entries on the completion queue, rearm the
13289  * completion queue, and then return.
13290  **/
13291 static void
13292 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13293                         uint32_t qidx)
13294 {
13295         struct lpfc_queue *cq = NULL;
13296         struct lpfc_cqe *cqe;
13297         bool workposted = false;
13298         uint16_t cqid, id;
13299         int ecount = 0;
13300
13301         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13302                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13303                                 "0366 Not a valid completion "
13304                                 "event: majorcode=x%x, minorcode=x%x\n",
13305                                 bf_get_le32(lpfc_eqe_major_code, eqe),
13306                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
13307                 return;
13308         }
13309
13310         /* Get the reference to the corresponding CQ */
13311         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13312
13313         if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13314                 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13315                 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13316                         /* Process NVMET unsol rcv */
13317                         cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13318                         goto  process_cq;
13319                 }
13320         }
13321
13322         if (phba->sli4_hba.nvme_cq_map &&
13323             (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
13324                 /* Process NVME / NVMET command completion */
13325                 cq = phba->sli4_hba.nvme_cq[qidx];
13326                 goto  process_cq;
13327         }
13328
13329         if (phba->sli4_hba.fcp_cq_map &&
13330             (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13331                 /* Process FCP command completion */
13332                 cq = phba->sli4_hba.fcp_cq[qidx];
13333                 goto  process_cq;
13334         }
13335
13336         if (phba->sli4_hba.nvmels_cq &&
13337             (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13338                 /* Process NVME unsol rcv */
13339                 cq = phba->sli4_hba.nvmels_cq;
13340         }
13341
13342         /* Otherwise this is a Slow path event */
13343         if (cq == NULL) {
13344                 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13345                 return;
13346         }
13347
13348 process_cq:
13349         if (unlikely(cqid != cq->queue_id)) {
13350                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13351                                 "0368 Miss-matched fast-path completion "
13352                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13353                                 cqid, cq->queue_id);
13354                 return;
13355         }
13356
13357         /* Save EQ associated with this CQ */
13358         cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13359
13360         /* Process all the entries to the CQ */
13361         while ((cqe = lpfc_sli4_cq_get(cq))) {
13362                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13363                 if (!(++ecount % cq->entry_repost))
13364                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13365         }
13366
13367         /* Track the max number of CQEs processed in 1 EQ */
13368         if (ecount > cq->CQ_max_cqe)
13369                 cq->CQ_max_cqe = ecount;
13370
13371         /* Catch the no cq entry condition */
13372         if (unlikely(ecount == 0))
13373                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13374                                 "0369 No entry from fast-path completion "
13375                                 "queue fcpcqid=%d\n", cq->queue_id);
13376
13377         /* In any case, flash and re-arm the CQ */
13378         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13379
13380         /* wake up worker thread if there are works to be done */
13381         if (workposted)
13382                 lpfc_worker_wake_up(phba);
13383 }
13384
13385 static void
13386 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13387 {
13388         struct lpfc_eqe *eqe;
13389
13390         /* walk all the EQ entries and drop on the floor */
13391         while ((eqe = lpfc_sli4_eq_get(eq)))
13392                 ;
13393
13394         /* Clear and re-arm the EQ */
13395         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13396 }
13397
13398
13399 /**
13400  * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13401  *                           entry
13402  * @phba: Pointer to HBA context object.
13403  * @eqe: Pointer to fast-path event queue entry.
13404  *
13405  * This routine process a event queue entry from the Flash Optimized Fabric
13406  * event queue.  It will check the MajorCode and MinorCode to determine this
13407  * is for a completion event on a completion queue, if not, an error shall be
13408  * logged and just return. Otherwise, it will get to the corresponding
13409  * completion queue and process all the entries on the completion queue, rearm
13410  * the completion queue, and then return.
13411  **/
13412 static void
13413 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13414 {
13415         struct lpfc_queue *cq;
13416         struct lpfc_cqe *cqe;
13417         bool workposted = false;
13418         uint16_t cqid;
13419         int ecount = 0;
13420
13421         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13422                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13423                                 "9147 Not a valid completion "
13424                                 "event: majorcode=x%x, minorcode=x%x\n",
13425                                 bf_get_le32(lpfc_eqe_major_code, eqe),
13426                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
13427                 return;
13428         }
13429
13430         /* Get the reference to the corresponding CQ */
13431         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13432
13433         /* Next check for OAS */
13434         cq = phba->sli4_hba.oas_cq;
13435         if (unlikely(!cq)) {
13436                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13437                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13438                                         "9148 OAS completion queue "
13439                                         "does not exist\n");
13440                 return;
13441         }
13442
13443         if (unlikely(cqid != cq->queue_id)) {
13444                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13445                                 "9149 Miss-matched fast-path compl "
13446                                 "queue id: eqcqid=%d, fcpcqid=%d\n",
13447                                 cqid, cq->queue_id);
13448                 return;
13449         }
13450
13451         /* Process all the entries to the OAS CQ */
13452         while ((cqe = lpfc_sli4_cq_get(cq))) {
13453                 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13454                 if (!(++ecount % cq->entry_repost))
13455                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13456         }
13457
13458         /* Track the max number of CQEs processed in 1 EQ */
13459         if (ecount > cq->CQ_max_cqe)
13460                 cq->CQ_max_cqe = ecount;
13461
13462         /* Catch the no cq entry condition */
13463         if (unlikely(ecount == 0))
13464                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13465                                 "9153 No entry from fast-path completion "
13466                                 "queue fcpcqid=%d\n", cq->queue_id);
13467
13468         /* In any case, flash and re-arm the CQ */
13469         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13470
13471         /* wake up worker thread if there are works to be done */
13472         if (workposted)
13473                 lpfc_worker_wake_up(phba);
13474 }
13475
13476 /**
13477  * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13478  * @irq: Interrupt number.
13479  * @dev_id: The device context pointer.
13480  *
13481  * This function is directly called from the PCI layer as an interrupt
13482  * service routine when device with SLI-4 interface spec is enabled with
13483  * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13484  * IOCB ring event in the HBA. However, when the device is enabled with either
13485  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13486  * device-level interrupt handler. When the PCI slot is in error recovery
13487  * or the HBA is undergoing initialization, the interrupt handler will not
13488  * process the interrupt. The Flash Optimized Fabric ring event are handled in
13489  * the intrrupt context. This function is called without any lock held.
13490  * It gets the hbalock to access and update SLI data structures. Note that,
13491  * the EQ to CQ are one-to-one map such that the EQ index is
13492  * equal to that of CQ index.
13493  *
13494  * This function returns IRQ_HANDLED when interrupt is handled else it
13495  * returns IRQ_NONE.
13496  **/
13497 irqreturn_t
13498 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13499 {
13500         struct lpfc_hba *phba;
13501         struct lpfc_hba_eq_hdl *hba_eq_hdl;
13502         struct lpfc_queue *eq;
13503         struct lpfc_eqe *eqe;
13504         unsigned long iflag;
13505         int ecount = 0;
13506
13507         /* Get the driver's phba structure from the dev_id */
13508         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13509         phba = hba_eq_hdl->phba;
13510
13511         if (unlikely(!phba))
13512                 return IRQ_NONE;
13513
13514         /* Get to the EQ struct associated with this vector */
13515         eq = phba->sli4_hba.fof_eq;
13516         if (unlikely(!eq))
13517                 return IRQ_NONE;
13518
13519         /* Check device state for handling interrupt */
13520         if (unlikely(lpfc_intr_state_check(phba))) {
13521                 eq->EQ_badstate++;
13522                 /* Check again for link_state with lock held */
13523                 spin_lock_irqsave(&phba->hbalock, iflag);
13524                 if (phba->link_state < LPFC_LINK_DOWN)
13525                         /* Flush, clear interrupt, and rearm the EQ */
13526                         lpfc_sli4_eq_flush(phba, eq);
13527                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13528                 return IRQ_NONE;
13529         }
13530
13531         /*
13532          * Process all the event on FCP fast-path EQ
13533          */
13534         while ((eqe = lpfc_sli4_eq_get(eq))) {
13535                 lpfc_sli4_fof_handle_eqe(phba, eqe);
13536                 if (!(++ecount % eq->entry_repost))
13537                         lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13538                 eq->EQ_processed++;
13539         }
13540
13541         /* Track the max number of EQEs processed in 1 intr */
13542         if (ecount > eq->EQ_max_eqe)
13543                 eq->EQ_max_eqe = ecount;
13544
13545
13546         if (unlikely(ecount == 0)) {
13547                 eq->EQ_no_entry++;
13548
13549                 if (phba->intr_type == MSIX)
13550                         /* MSI-X treated interrupt served as no EQ share INT */
13551                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13552                                         "9145 MSI-X interrupt with no EQE\n");
13553                 else {
13554                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13555                                         "9146 ISR interrupt with no EQE\n");
13556                         /* Non MSI-X treated on interrupt as EQ share INT */
13557                         return IRQ_NONE;
13558                 }
13559         }
13560         /* Always clear and re-arm the fast-path EQ */
13561         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13562         return IRQ_HANDLED;
13563 }
13564
13565 /**
13566  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
13567  * @irq: Interrupt number.
13568  * @dev_id: The device context pointer.
13569  *
13570  * This function is directly called from the PCI layer as an interrupt
13571  * service routine when device with SLI-4 interface spec is enabled with
13572  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13573  * ring event in the HBA. However, when the device is enabled with either
13574  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13575  * device-level interrupt handler. When the PCI slot is in error recovery
13576  * or the HBA is undergoing initialization, the interrupt handler will not
13577  * process the interrupt. The SCSI FCP fast-path ring event are handled in
13578  * the intrrupt context. This function is called without any lock held.
13579  * It gets the hbalock to access and update SLI data structures. Note that,
13580  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13581  * equal to that of FCP CQ index.
13582  *
13583  * The link attention and ELS ring attention events are handled
13584  * by the worker thread. The interrupt handler signals the worker thread
13585  * and returns for these events. This function is called without any lock
13586  * held. It gets the hbalock to access and update SLI data structures.
13587  *
13588  * This function returns IRQ_HANDLED when interrupt is handled else it
13589  * returns IRQ_NONE.
13590  **/
13591 irqreturn_t
13592 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
13593 {
13594         struct lpfc_hba *phba;
13595         struct lpfc_hba_eq_hdl *hba_eq_hdl;
13596         struct lpfc_queue *fpeq;
13597         struct lpfc_eqe *eqe;
13598         unsigned long iflag;
13599         int ecount = 0;
13600         int hba_eqidx;
13601
13602         /* Get the driver's phba structure from the dev_id */
13603         hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13604         phba = hba_eq_hdl->phba;
13605         hba_eqidx = hba_eq_hdl->idx;
13606
13607         if (unlikely(!phba))
13608                 return IRQ_NONE;
13609         if (unlikely(!phba->sli4_hba.hba_eq))
13610                 return IRQ_NONE;
13611
13612         /* Get to the EQ struct associated with this vector */
13613         fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
13614         if (unlikely(!fpeq))
13615                 return IRQ_NONE;
13616
13617 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13618         if (phba->ktime_on)
13619                 fpeq->isr_timestamp = ktime_get_ns();
13620 #endif
13621
13622         if (lpfc_fcp_look_ahead) {
13623                 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
13624                         lpfc_sli4_eq_clr_intr(fpeq);
13625                 else {
13626                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13627                         return IRQ_NONE;
13628                 }
13629         }
13630
13631         /* Check device state for handling interrupt */
13632         if (unlikely(lpfc_intr_state_check(phba))) {
13633                 fpeq->EQ_badstate++;
13634                 /* Check again for link_state with lock held */
13635                 spin_lock_irqsave(&phba->hbalock, iflag);
13636                 if (phba->link_state < LPFC_LINK_DOWN)
13637                         /* Flush, clear interrupt, and rearm the EQ */
13638                         lpfc_sli4_eq_flush(phba, fpeq);
13639                 spin_unlock_irqrestore(&phba->hbalock, iflag);
13640                 if (lpfc_fcp_look_ahead)
13641                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13642                 return IRQ_NONE;
13643         }
13644
13645         /*
13646          * Process all the event on FCP fast-path EQ
13647          */
13648         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
13649                 if (eqe == NULL)
13650                         break;
13651
13652                 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13653                 if (!(++ecount % fpeq->entry_repost))
13654                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
13655                 fpeq->EQ_processed++;
13656         }
13657
13658         /* Track the max number of EQEs processed in 1 intr */
13659         if (ecount > fpeq->EQ_max_eqe)
13660                 fpeq->EQ_max_eqe = ecount;
13661
13662         /* Always clear and re-arm the fast-path EQ */
13663         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13664
13665         if (unlikely(ecount == 0)) {
13666                 fpeq->EQ_no_entry++;
13667
13668                 if (lpfc_fcp_look_ahead) {
13669                         atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13670                         return IRQ_NONE;
13671                 }
13672
13673                 if (phba->intr_type == MSIX)
13674                         /* MSI-X treated interrupt served as no EQ share INT */
13675                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13676                                         "0358 MSI-X interrupt with no EQE\n");
13677                 else
13678                         /* Non MSI-X treated on interrupt as EQ share INT */
13679                         return IRQ_NONE;
13680         }
13681
13682         if (lpfc_fcp_look_ahead)
13683                 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13684
13685         return IRQ_HANDLED;
13686 } /* lpfc_sli4_fp_intr_handler */
13687
13688 /**
13689  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13690  * @irq: Interrupt number.
13691  * @dev_id: The device context pointer.
13692  *
13693  * This function is the device-level interrupt handler to device with SLI-4
13694  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13695  * interrupt mode is enabled and there is an event in the HBA which requires
13696  * driver attention. This function invokes the slow-path interrupt attention
13697  * handling function and fast-path interrupt attention handling function in
13698  * turn to process the relevant HBA attention events. This function is called
13699  * without any lock held. It gets the hbalock to access and update SLI data
13700  * structures.
13701  *
13702  * This function returns IRQ_HANDLED when interrupt is handled, else it
13703  * returns IRQ_NONE.
13704  **/
13705 irqreturn_t
13706 lpfc_sli4_intr_handler(int irq, void *dev_id)
13707 {
13708         struct lpfc_hba  *phba;
13709         irqreturn_t hba_irq_rc;
13710         bool hba_handled = false;
13711         int qidx;
13712
13713         /* Get the driver's phba structure from the dev_id */
13714         phba = (struct lpfc_hba *)dev_id;
13715
13716         if (unlikely(!phba))
13717                 return IRQ_NONE;
13718
13719         /*
13720          * Invoke fast-path host attention interrupt handling as appropriate.
13721          */
13722         for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
13723                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
13724                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
13725                 if (hba_irq_rc == IRQ_HANDLED)
13726                         hba_handled |= true;
13727         }
13728
13729         if (phba->cfg_fof) {
13730                 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
13731                                         &phba->sli4_hba.hba_eq_hdl[qidx]);
13732                 if (hba_irq_rc == IRQ_HANDLED)
13733                         hba_handled |= true;
13734         }
13735
13736         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
13737 } /* lpfc_sli4_intr_handler */
13738
13739 /**
13740  * lpfc_sli4_queue_free - free a queue structure and associated memory
13741  * @queue: The queue structure to free.
13742  *
13743  * This function frees a queue structure and the DMAable memory used for
13744  * the host resident queue. This function must be called after destroying the
13745  * queue on the HBA.
13746  **/
13747 void
13748 lpfc_sli4_queue_free(struct lpfc_queue *queue)
13749 {
13750         struct lpfc_dmabuf *dmabuf;
13751
13752         if (!queue)
13753                 return;
13754
13755         while (!list_empty(&queue->page_list)) {
13756                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13757                                  list);
13758                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
13759                                   dmabuf->virt, dmabuf->phys);
13760                 kfree(dmabuf);
13761         }
13762         if (queue->rqbp) {
13763                 lpfc_free_rq_buffer(queue->phba, queue);
13764                 kfree(queue->rqbp);
13765         }
13766
13767         if (!list_empty(&queue->wq_list))
13768                 list_del(&queue->wq_list);
13769
13770         kfree(queue);
13771         return;
13772 }
13773
13774 /**
13775  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13776  * @phba: The HBA that this queue is being created on.
13777  * @entry_size: The size of each queue entry for this queue.
13778  * @entry count: The number of entries that this queue will handle.
13779  *
13780  * This function allocates a queue structure and the DMAable memory used for
13781  * the host resident queue. This function must be called before creating the
13782  * queue on the HBA.
13783  **/
13784 struct lpfc_queue *
13785 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13786                       uint32_t entry_count)
13787 {
13788         struct lpfc_queue *queue;
13789         struct lpfc_dmabuf *dmabuf;
13790         int x, total_qe_count;
13791         void *dma_pointer;
13792         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13793
13794         if (!phba->sli4_hba.pc_sli4_params.supported)
13795                 hw_page_size = SLI4_PAGE_SIZE;
13796
13797         queue = kzalloc(sizeof(struct lpfc_queue) +
13798                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13799         if (!queue)
13800                 return NULL;
13801         queue->page_count = (ALIGN(entry_size * entry_count,
13802                         hw_page_size))/hw_page_size;
13803
13804         /* If needed, Adjust page count to match the max the adapter supports */
13805         if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13806                 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13807
13808         INIT_LIST_HEAD(&queue->list);
13809         INIT_LIST_HEAD(&queue->wq_list);
13810         INIT_LIST_HEAD(&queue->page_list);
13811         INIT_LIST_HEAD(&queue->child_list);
13812         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13813                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13814                 if (!dmabuf)
13815                         goto out_fail;
13816                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13817                                                    hw_page_size, &dmabuf->phys,
13818                                                    GFP_KERNEL);
13819                 if (!dmabuf->virt) {
13820                         kfree(dmabuf);
13821                         goto out_fail;
13822                 }
13823                 dmabuf->buffer_tag = x;
13824                 list_add_tail(&dmabuf->list, &queue->page_list);
13825                 /* initialize queue's entry array */
13826                 dma_pointer = dmabuf->virt;
13827                 for (; total_qe_count < entry_count &&
13828                      dma_pointer < (hw_page_size + dmabuf->virt);
13829                      total_qe_count++, dma_pointer += entry_size) {
13830                         queue->qe[total_qe_count].address = dma_pointer;
13831                 }
13832         }
13833         queue->entry_size = entry_size;
13834         queue->entry_count = entry_count;
13835
13836         /*
13837          * entry_repost is calculated based on the number of entries in the
13838          * queue. This works out except for RQs. If buffers are NOT initially
13839          * posted for every RQE, entry_repost should be adjusted accordingly.
13840          */
13841         queue->entry_repost = (entry_count >> 3);
13842         if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13843                 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
13844         queue->phba = phba;
13845
13846         return queue;
13847 out_fail:
13848         lpfc_sli4_queue_free(queue);
13849         return NULL;
13850 }
13851
13852 /**
13853  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13854  * @phba: HBA structure that indicates port to create a queue on.
13855  * @pci_barset: PCI BAR set flag.
13856  *
13857  * This function shall perform iomap of the specified PCI BAR address to host
13858  * memory address if not already done so and return it. The returned host
13859  * memory address can be NULL.
13860  */
13861 static void __iomem *
13862 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13863 {
13864         if (!phba->pcidev)
13865                 return NULL;
13866
13867         switch (pci_barset) {
13868         case WQ_PCI_BAR_0_AND_1:
13869                 return phba->pci_bar0_memmap_p;
13870         case WQ_PCI_BAR_2_AND_3:
13871                 return phba->pci_bar2_memmap_p;
13872         case WQ_PCI_BAR_4_AND_5:
13873                 return phba->pci_bar4_memmap_p;
13874         default:
13875                 break;
13876         }
13877         return NULL;
13878 }
13879
13880 /**
13881  * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
13882  * @phba: HBA structure that indicates port to create a queue on.
13883  * @startq: The starting FCP EQ to modify
13884  *
13885  * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13886  * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
13887  * updated in one mailbox command.
13888  *
13889  * The @phba struct is used to send mailbox command to HBA. The @startq
13890  * is used to get the starting FCP EQ to change.
13891  * This function is asynchronous and will wait for the mailbox
13892  * command to finish before continuing.
13893  *
13894  * On success this function will return a zero. If unable to allocate enough
13895  * memory this function will return -ENOMEM. If the queue create mailbox command
13896  * fails this function will return -ENXIO.
13897  **/
13898 int
13899 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
13900 {
13901         struct lpfc_mbx_modify_eq_delay *eq_delay;
13902         LPFC_MBOXQ_t *mbox;
13903         struct lpfc_queue *eq;
13904         int cnt, rc, length, status = 0;
13905         uint32_t shdr_status, shdr_add_status;
13906         uint32_t result;
13907         int qidx;
13908         union lpfc_sli4_cfg_shdr *shdr;
13909         uint16_t dmult;
13910
13911         if (startq >= phba->io_channel_irqs)
13912                 return 0;
13913
13914         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13915         if (!mbox)
13916                 return -ENOMEM;
13917         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13918                   sizeof(struct lpfc_sli4_cfg_mhdr));
13919         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13920                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13921                          length, LPFC_SLI4_MBX_EMBED);
13922         eq_delay = &mbox->u.mqe.un.eq_delay;
13923
13924         /* Calculate delay multiper from maximum interrupt per second */
13925         result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13926         if (result > LPFC_DMULT_CONST || result == 0)
13927                 dmult = 0;
13928         else
13929                 dmult = LPFC_DMULT_CONST/result - 1;
13930
13931         cnt = 0;
13932         for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13933                 eq = phba->sli4_hba.hba_eq[qidx];
13934                 if (!eq)
13935                         continue;
13936                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13937                 eq_delay->u.request.eq[cnt].phase = 0;
13938                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13939                 cnt++;
13940                 if (cnt >= LPFC_MAX_EQ_DELAY_EQID_CNT)
13941                         break;
13942         }
13943         eq_delay->u.request.num_eq = cnt;
13944
13945         mbox->vport = phba->pport;
13946         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13947         mbox->context1 = NULL;
13948         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13949         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13950         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13951         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13952         if (shdr_status || shdr_add_status || rc) {
13953                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13954                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
13955                                 "status x%x add_status x%x, mbx status x%x\n",
13956                                 shdr_status, shdr_add_status, rc);
13957                 status = -ENXIO;
13958         }
13959         mempool_free(mbox, phba->mbox_mem_pool);
13960         return status;
13961 }
13962
13963 /**
13964  * lpfc_eq_create - Create an Event Queue on the HBA
13965  * @phba: HBA structure that indicates port to create a queue on.
13966  * @eq: The queue structure to use to create the event queue.
13967  * @imax: The maximum interrupt per second limit.
13968  *
13969  * This function creates an event queue, as detailed in @eq, on a port,
13970  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13971  *
13972  * The @phba struct is used to send mailbox command to HBA. The @eq struct
13973  * is used to get the entry count and entry size that are necessary to
13974  * determine the number of pages to allocate and use for this queue. This
13975  * function will send the EQ_CREATE mailbox command to the HBA to setup the
13976  * event queue. This function is asynchronous and will wait for the mailbox
13977  * command to finish before continuing.
13978  *
13979  * On success this function will return a zero. If unable to allocate enough
13980  * memory this function will return -ENOMEM. If the queue create mailbox command
13981  * fails this function will return -ENXIO.
13982  **/
13983 int
13984 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
13985 {
13986         struct lpfc_mbx_eq_create *eq_create;
13987         LPFC_MBOXQ_t *mbox;
13988         int rc, length, status = 0;
13989         struct lpfc_dmabuf *dmabuf;
13990         uint32_t shdr_status, shdr_add_status;
13991         union lpfc_sli4_cfg_shdr *shdr;
13992         uint16_t dmult;
13993         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13994
13995         /* sanity check on queue memory */
13996         if (!eq)
13997                 return -ENODEV;
13998         if (!phba->sli4_hba.pc_sli4_params.supported)
13999                 hw_page_size = SLI4_PAGE_SIZE;
14000
14001         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14002         if (!mbox)
14003                 return -ENOMEM;
14004         length = (sizeof(struct lpfc_mbx_eq_create) -
14005                   sizeof(struct lpfc_sli4_cfg_mhdr));
14006         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14007                          LPFC_MBOX_OPCODE_EQ_CREATE,
14008                          length, LPFC_SLI4_MBX_EMBED);
14009         eq_create = &mbox->u.mqe.un.eq_create;
14010         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14011                eq->page_count);
14012         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14013                LPFC_EQE_SIZE);
14014         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
14015         /* don't setup delay multiplier using EQ_CREATE */
14016         dmult = 0;
14017         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14018                dmult);
14019         switch (eq->entry_count) {
14020         default:
14021                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14022                                 "0360 Unsupported EQ count. (%d)\n",
14023                                 eq->entry_count);
14024                 if (eq->entry_count < 256)
14025                         return -EINVAL;
14026                 /* otherwise default to smallest count (drop through) */
14027         case 256:
14028                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14029                        LPFC_EQ_CNT_256);
14030                 break;
14031         case 512:
14032                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14033                        LPFC_EQ_CNT_512);
14034                 break;
14035         case 1024:
14036                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14037                        LPFC_EQ_CNT_1024);
14038                 break;
14039         case 2048:
14040                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14041                        LPFC_EQ_CNT_2048);
14042                 break;
14043         case 4096:
14044                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14045                        LPFC_EQ_CNT_4096);
14046                 break;
14047         }
14048         list_for_each_entry(dmabuf, &eq->page_list, list) {
14049                 memset(dmabuf->virt, 0, hw_page_size);
14050                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14051                                         putPaddrLow(dmabuf->phys);
14052                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14053                                         putPaddrHigh(dmabuf->phys);
14054         }
14055         mbox->vport = phba->pport;
14056         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14057         mbox->context1 = NULL;
14058         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14059         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14060         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14061         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14062         if (shdr_status || shdr_add_status || rc) {
14063                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14064                                 "2500 EQ_CREATE mailbox failed with "
14065                                 "status x%x add_status x%x, mbx status x%x\n",
14066                                 shdr_status, shdr_add_status, rc);
14067                 status = -ENXIO;
14068         }
14069         eq->type = LPFC_EQ;
14070         eq->subtype = LPFC_NONE;
14071         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14072         if (eq->queue_id == 0xFFFF)
14073                 status = -ENXIO;
14074         eq->host_index = 0;
14075         eq->hba_index = 0;
14076
14077         mempool_free(mbox, phba->mbox_mem_pool);
14078         return status;
14079 }
14080
14081 /**
14082  * lpfc_cq_create - Create a Completion Queue on the HBA
14083  * @phba: HBA structure that indicates port to create a queue on.
14084  * @cq: The queue structure to use to create the completion queue.
14085  * @eq: The event queue to bind this completion queue to.
14086  *
14087  * This function creates a completion queue, as detailed in @wq, on a port,
14088  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14089  *
14090  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14091  * is used to get the entry count and entry size that are necessary to
14092  * determine the number of pages to allocate and use for this queue. The @eq
14093  * is used to indicate which event queue to bind this completion queue to. This
14094  * function will send the CQ_CREATE mailbox command to the HBA to setup the
14095  * completion queue. This function is asynchronous and will wait for the mailbox
14096  * command to finish before continuing.
14097  *
14098  * On success this function will return a zero. If unable to allocate enough
14099  * memory this function will return -ENOMEM. If the queue create mailbox command
14100  * fails this function will return -ENXIO.
14101  **/
14102 int
14103 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14104                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14105 {
14106         struct lpfc_mbx_cq_create *cq_create;
14107         struct lpfc_dmabuf *dmabuf;
14108         LPFC_MBOXQ_t *mbox;
14109         int rc, length, status = 0;
14110         uint32_t shdr_status, shdr_add_status;
14111         union lpfc_sli4_cfg_shdr *shdr;
14112         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14113
14114         /* sanity check on queue memory */
14115         if (!cq || !eq)
14116                 return -ENODEV;
14117         if (!phba->sli4_hba.pc_sli4_params.supported)
14118                 hw_page_size = SLI4_PAGE_SIZE;
14119
14120         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14121         if (!mbox)
14122                 return -ENOMEM;
14123         length = (sizeof(struct lpfc_mbx_cq_create) -
14124                   sizeof(struct lpfc_sli4_cfg_mhdr));
14125         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14126                          LPFC_MBOX_OPCODE_CQ_CREATE,
14127                          length, LPFC_SLI4_MBX_EMBED);
14128         cq_create = &mbox->u.mqe.un.cq_create;
14129         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14130         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14131                     cq->page_count);
14132         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14133         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14134         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14135                phba->sli4_hba.pc_sli4_params.cqv);
14136         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14137                 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14138                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
14139                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14140                        eq->queue_id);
14141         } else {
14142                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14143                        eq->queue_id);
14144         }
14145         switch (cq->entry_count) {
14146         default:
14147                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14148                                 "0361 Unsupported CQ count: "
14149                                 "entry cnt %d sz %d pg cnt %d repost %d\n",
14150                                 cq->entry_count, cq->entry_size,
14151                                 cq->page_count, cq->entry_repost);
14152                 if (cq->entry_count < 256) {
14153                         status = -EINVAL;
14154                         goto out;
14155                 }
14156                 /* otherwise default to smallest count (drop through) */
14157         case 256:
14158                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14159                        LPFC_CQ_CNT_256);
14160                 break;
14161         case 512:
14162                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14163                        LPFC_CQ_CNT_512);
14164                 break;
14165         case 1024:
14166                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14167                        LPFC_CQ_CNT_1024);
14168                 break;
14169         }
14170         list_for_each_entry(dmabuf, &cq->page_list, list) {
14171                 memset(dmabuf->virt, 0, hw_page_size);
14172                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14173                                         putPaddrLow(dmabuf->phys);
14174                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14175                                         putPaddrHigh(dmabuf->phys);
14176         }
14177         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14178
14179         /* The IOCTL status is embedded in the mailbox subheader. */
14180         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14181         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14182         if (shdr_status || shdr_add_status || rc) {
14183                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14184                                 "2501 CQ_CREATE mailbox failed with "
14185                                 "status x%x add_status x%x, mbx status x%x\n",
14186                                 shdr_status, shdr_add_status, rc);
14187                 status = -ENXIO;
14188                 goto out;
14189         }
14190         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14191         if (cq->queue_id == 0xFFFF) {
14192                 status = -ENXIO;
14193                 goto out;
14194         }
14195         /* link the cq onto the parent eq child list */
14196         list_add_tail(&cq->list, &eq->child_list);
14197         /* Set up completion queue's type and subtype */
14198         cq->type = type;
14199         cq->subtype = subtype;
14200         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14201         cq->assoc_qid = eq->queue_id;
14202         cq->host_index = 0;
14203         cq->hba_index = 0;
14204
14205 out:
14206         mempool_free(mbox, phba->mbox_mem_pool);
14207         return status;
14208 }
14209
14210 /**
14211  * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14212  * @phba: HBA structure that indicates port to create a queue on.
14213  * @cqp: The queue structure array to use to create the completion queues.
14214  * @eqp: The event queue array to bind these completion queues to.
14215  *
14216  * This function creates a set of  completion queue, s to support MRQ
14217  * as detailed in @cqp, on a port,
14218  * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14219  *
14220  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14221  * is used to get the entry count and entry size that are necessary to
14222  * determine the number of pages to allocate and use for this queue. The @eq
14223  * is used to indicate which event queue to bind this completion queue to. This
14224  * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14225  * completion queue. This function is asynchronous and will wait for the mailbox
14226  * command to finish before continuing.
14227  *
14228  * On success this function will return a zero. If unable to allocate enough
14229  * memory this function will return -ENOMEM. If the queue create mailbox command
14230  * fails this function will return -ENXIO.
14231  **/
14232 int
14233 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14234                    struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14235 {
14236         struct lpfc_queue *cq;
14237         struct lpfc_queue *eq;
14238         struct lpfc_mbx_cq_create_set *cq_set;
14239         struct lpfc_dmabuf *dmabuf;
14240         LPFC_MBOXQ_t *mbox;
14241         int rc, length, alloclen, status = 0;
14242         int cnt, idx, numcq, page_idx = 0;
14243         uint32_t shdr_status, shdr_add_status;
14244         union lpfc_sli4_cfg_shdr *shdr;
14245         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14246
14247         /* sanity check on queue memory */
14248         numcq = phba->cfg_nvmet_mrq;
14249         if (!cqp || !eqp || !numcq)
14250                 return -ENODEV;
14251         if (!phba->sli4_hba.pc_sli4_params.supported)
14252                 hw_page_size = SLI4_PAGE_SIZE;
14253
14254         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14255         if (!mbox)
14256                 return -ENOMEM;
14257
14258         length = sizeof(struct lpfc_mbx_cq_create_set);
14259         length += ((numcq * cqp[0]->page_count) *
14260                    sizeof(struct dma_address));
14261         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14262                         LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14263                         LPFC_SLI4_MBX_NEMBED);
14264         if (alloclen < length) {
14265                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14266                                 "3098 Allocated DMA memory size (%d) is "
14267                                 "less than the requested DMA memory size "
14268                                 "(%d)\n", alloclen, length);
14269                 status = -ENOMEM;
14270                 goto out;
14271         }
14272         cq_set = mbox->sge_array->addr[0];
14273         shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14274         bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14275
14276         for (idx = 0; idx < numcq; idx++) {
14277                 cq = cqp[idx];
14278                 eq = eqp[idx];
14279                 if (!cq || !eq) {
14280                         status = -ENOMEM;
14281                         goto out;
14282                 }
14283
14284                 switch (idx) {
14285                 case 0:
14286                         bf_set(lpfc_mbx_cq_create_set_page_size,
14287                                &cq_set->u.request,
14288                                (hw_page_size / SLI4_PAGE_SIZE));
14289                         bf_set(lpfc_mbx_cq_create_set_num_pages,
14290                                &cq_set->u.request, cq->page_count);
14291                         bf_set(lpfc_mbx_cq_create_set_evt,
14292                                &cq_set->u.request, 1);
14293                         bf_set(lpfc_mbx_cq_create_set_valid,
14294                                &cq_set->u.request, 1);
14295                         bf_set(lpfc_mbx_cq_create_set_cqe_size,
14296                                &cq_set->u.request, 0);
14297                         bf_set(lpfc_mbx_cq_create_set_num_cq,
14298                                &cq_set->u.request, numcq);
14299                         switch (cq->entry_count) {
14300                         default:
14301                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14302                                                 "3118 Bad CQ count. (%d)\n",
14303                                                 cq->entry_count);
14304                                 if (cq->entry_count < 256) {
14305                                         status = -EINVAL;
14306                                         goto out;
14307                                 }
14308                                 /* otherwise default to smallest (drop thru) */
14309                         case 256:
14310                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14311                                        &cq_set->u.request, LPFC_CQ_CNT_256);
14312                                 break;
14313                         case 512:
14314                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14315                                        &cq_set->u.request, LPFC_CQ_CNT_512);
14316                                 break;
14317                         case 1024:
14318                                 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14319                                        &cq_set->u.request, LPFC_CQ_CNT_1024);
14320                                 break;
14321                         }
14322                         bf_set(lpfc_mbx_cq_create_set_eq_id0,
14323                                &cq_set->u.request, eq->queue_id);
14324                         break;
14325                 case 1:
14326                         bf_set(lpfc_mbx_cq_create_set_eq_id1,
14327                                &cq_set->u.request, eq->queue_id);
14328                         break;
14329                 case 2:
14330                         bf_set(lpfc_mbx_cq_create_set_eq_id2,
14331                                &cq_set->u.request, eq->queue_id);
14332                         break;
14333                 case 3:
14334                         bf_set(lpfc_mbx_cq_create_set_eq_id3,
14335                                &cq_set->u.request, eq->queue_id);
14336                         break;
14337                 case 4:
14338                         bf_set(lpfc_mbx_cq_create_set_eq_id4,
14339                                &cq_set->u.request, eq->queue_id);
14340                         break;
14341                 case 5:
14342                         bf_set(lpfc_mbx_cq_create_set_eq_id5,
14343                                &cq_set->u.request, eq->queue_id);
14344                         break;
14345                 case 6:
14346                         bf_set(lpfc_mbx_cq_create_set_eq_id6,
14347                                &cq_set->u.request, eq->queue_id);
14348                         break;
14349                 case 7:
14350                         bf_set(lpfc_mbx_cq_create_set_eq_id7,
14351                                &cq_set->u.request, eq->queue_id);
14352                         break;
14353                 case 8:
14354                         bf_set(lpfc_mbx_cq_create_set_eq_id8,
14355                                &cq_set->u.request, eq->queue_id);
14356                         break;
14357                 case 9:
14358                         bf_set(lpfc_mbx_cq_create_set_eq_id9,
14359                                &cq_set->u.request, eq->queue_id);
14360                         break;
14361                 case 10:
14362                         bf_set(lpfc_mbx_cq_create_set_eq_id10,
14363                                &cq_set->u.request, eq->queue_id);
14364                         break;
14365                 case 11:
14366                         bf_set(lpfc_mbx_cq_create_set_eq_id11,
14367                                &cq_set->u.request, eq->queue_id);
14368                         break;
14369                 case 12:
14370                         bf_set(lpfc_mbx_cq_create_set_eq_id12,
14371                                &cq_set->u.request, eq->queue_id);
14372                         break;
14373                 case 13:
14374                         bf_set(lpfc_mbx_cq_create_set_eq_id13,
14375                                &cq_set->u.request, eq->queue_id);
14376                         break;
14377                 case 14:
14378                         bf_set(lpfc_mbx_cq_create_set_eq_id14,
14379                                &cq_set->u.request, eq->queue_id);
14380                         break;
14381                 case 15:
14382                         bf_set(lpfc_mbx_cq_create_set_eq_id15,
14383                                &cq_set->u.request, eq->queue_id);
14384                         break;
14385                 }
14386
14387                 /* link the cq onto the parent eq child list */
14388                 list_add_tail(&cq->list, &eq->child_list);
14389                 /* Set up completion queue's type and subtype */
14390                 cq->type = type;
14391                 cq->subtype = subtype;
14392                 cq->assoc_qid = eq->queue_id;
14393                 cq->host_index = 0;
14394                 cq->hba_index = 0;
14395
14396                 rc = 0;
14397                 list_for_each_entry(dmabuf, &cq->page_list, list) {
14398                         memset(dmabuf->virt, 0, hw_page_size);
14399                         cnt = page_idx + dmabuf->buffer_tag;
14400                         cq_set->u.request.page[cnt].addr_lo =
14401                                         putPaddrLow(dmabuf->phys);
14402                         cq_set->u.request.page[cnt].addr_hi =
14403                                         putPaddrHigh(dmabuf->phys);
14404                         rc++;
14405                 }
14406                 page_idx += rc;
14407         }
14408
14409         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14410
14411         /* The IOCTL status is embedded in the mailbox subheader. */
14412         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14413         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14414         if (shdr_status || shdr_add_status || rc) {
14415                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14416                                 "3119 CQ_CREATE_SET mailbox failed with "
14417                                 "status x%x add_status x%x, mbx status x%x\n",
14418                                 shdr_status, shdr_add_status, rc);
14419                 status = -ENXIO;
14420                 goto out;
14421         }
14422         rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14423         if (rc == 0xFFFF) {
14424                 status = -ENXIO;
14425                 goto out;
14426         }
14427
14428         for (idx = 0; idx < numcq; idx++) {
14429                 cq = cqp[idx];
14430                 cq->queue_id = rc + idx;
14431         }
14432
14433 out:
14434         lpfc_sli4_mbox_cmd_free(phba, mbox);
14435         return status;
14436 }
14437
14438 /**
14439  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14440  * @phba: HBA structure that indicates port to create a queue on.
14441  * @mq: The queue structure to use to create the mailbox queue.
14442  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14443  * @cq: The completion queue to associate with this cq.
14444  *
14445  * This function provides failback (fb) functionality when the
14446  * mq_create_ext fails on older FW generations.  It's purpose is identical
14447  * to mq_create_ext otherwise.
14448  *
14449  * This routine cannot fail as all attributes were previously accessed and
14450  * initialized in mq_create_ext.
14451  **/
14452 static void
14453 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14454                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14455 {
14456         struct lpfc_mbx_mq_create *mq_create;
14457         struct lpfc_dmabuf *dmabuf;
14458         int length;
14459
14460         length = (sizeof(struct lpfc_mbx_mq_create) -
14461                   sizeof(struct lpfc_sli4_cfg_mhdr));
14462         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14463                          LPFC_MBOX_OPCODE_MQ_CREATE,
14464                          length, LPFC_SLI4_MBX_EMBED);
14465         mq_create = &mbox->u.mqe.un.mq_create;
14466         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14467                mq->page_count);
14468         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14469                cq->queue_id);
14470         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14471         switch (mq->entry_count) {
14472         case 16:
14473                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14474                        LPFC_MQ_RING_SIZE_16);
14475                 break;
14476         case 32:
14477                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14478                        LPFC_MQ_RING_SIZE_32);
14479                 break;
14480         case 64:
14481                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14482                        LPFC_MQ_RING_SIZE_64);
14483                 break;
14484         case 128:
14485                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14486                        LPFC_MQ_RING_SIZE_128);
14487                 break;
14488         }
14489         list_for_each_entry(dmabuf, &mq->page_list, list) {
14490                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14491                         putPaddrLow(dmabuf->phys);
14492                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14493                         putPaddrHigh(dmabuf->phys);
14494         }
14495 }
14496
14497 /**
14498  * lpfc_mq_create - Create a mailbox Queue on the HBA
14499  * @phba: HBA structure that indicates port to create a queue on.
14500  * @mq: The queue structure to use to create the mailbox queue.
14501  * @cq: The completion queue to associate with this cq.
14502  * @subtype: The queue's subtype.
14503  *
14504  * This function creates a mailbox queue, as detailed in @mq, on a port,
14505  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14506  *
14507  * The @phba struct is used to send mailbox command to HBA. The @cq struct
14508  * is used to get the entry count and entry size that are necessary to
14509  * determine the number of pages to allocate and use for this queue. This
14510  * function will send the MQ_CREATE mailbox command to the HBA to setup the
14511  * mailbox queue. This function is asynchronous and will wait for the mailbox
14512  * command to finish before continuing.
14513  *
14514  * On success this function will return a zero. If unable to allocate enough
14515  * memory this function will return -ENOMEM. If the queue create mailbox command
14516  * fails this function will return -ENXIO.
14517  **/
14518 int32_t
14519 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14520                struct lpfc_queue *cq, uint32_t subtype)
14521 {
14522         struct lpfc_mbx_mq_create *mq_create;
14523         struct lpfc_mbx_mq_create_ext *mq_create_ext;
14524         struct lpfc_dmabuf *dmabuf;
14525         LPFC_MBOXQ_t *mbox;
14526         int rc, length, status = 0;
14527         uint32_t shdr_status, shdr_add_status;
14528         union lpfc_sli4_cfg_shdr *shdr;
14529         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14530
14531         /* sanity check on queue memory */
14532         if (!mq || !cq)
14533                 return -ENODEV;
14534         if (!phba->sli4_hba.pc_sli4_params.supported)
14535                 hw_page_size = SLI4_PAGE_SIZE;
14536
14537         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14538         if (!mbox)
14539                 return -ENOMEM;
14540         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14541                   sizeof(struct lpfc_sli4_cfg_mhdr));
14542         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14543                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14544                          length, LPFC_SLI4_MBX_EMBED);
14545
14546         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
14547         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
14548         bf_set(lpfc_mbx_mq_create_ext_num_pages,
14549                &mq_create_ext->u.request, mq->page_count);
14550         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14551                &mq_create_ext->u.request, 1);
14552         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
14553                &mq_create_ext->u.request, 1);
14554         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14555                &mq_create_ext->u.request, 1);
14556         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14557                &mq_create_ext->u.request, 1);
14558         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14559                &mq_create_ext->u.request, 1);
14560         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
14561         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14562                phba->sli4_hba.pc_sli4_params.mqv);
14563         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14564                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14565                        cq->queue_id);
14566         else
14567                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14568                        cq->queue_id);
14569         switch (mq->entry_count) {
14570         default:
14571                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14572                                 "0362 Unsupported MQ count. (%d)\n",
14573                                 mq->entry_count);
14574                 if (mq->entry_count < 16) {
14575                         status = -EINVAL;
14576                         goto out;
14577                 }
14578                 /* otherwise default to smallest count (drop through) */
14579         case 16:
14580                 bf_set(lpfc_mq_context_ring_size,
14581                        &mq_create_ext->u.request.context,
14582                        LPFC_MQ_RING_SIZE_16);
14583                 break;
14584         case 32:
14585                 bf_set(lpfc_mq_context_ring_size,
14586                        &mq_create_ext->u.request.context,
14587                        LPFC_MQ_RING_SIZE_32);
14588                 break;
14589         case 64:
14590                 bf_set(lpfc_mq_context_ring_size,
14591                        &mq_create_ext->u.request.context,
14592                        LPFC_MQ_RING_SIZE_64);
14593                 break;
14594         case 128:
14595                 bf_set(lpfc_mq_context_ring_size,
14596                        &mq_create_ext->u.request.context,
14597                        LPFC_MQ_RING_SIZE_128);
14598                 break;
14599         }
14600         list_for_each_entry(dmabuf, &mq->page_list, list) {
14601                 memset(dmabuf->virt, 0, hw_page_size);
14602                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14603                                         putPaddrLow(dmabuf->phys);
14604                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
14605                                         putPaddrHigh(dmabuf->phys);
14606         }
14607         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14608         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14609                               &mq_create_ext->u.response);
14610         if (rc != MBX_SUCCESS) {
14611                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14612                                 "2795 MQ_CREATE_EXT failed with "
14613                                 "status x%x. Failback to MQ_CREATE.\n",
14614                                 rc);
14615                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14616                 mq_create = &mbox->u.mqe.un.mq_create;
14617                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14618                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14619                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14620                                       &mq_create->u.response);
14621         }
14622
14623         /* The IOCTL status is embedded in the mailbox subheader. */
14624         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14625         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14626         if (shdr_status || shdr_add_status || rc) {
14627                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14628                                 "2502 MQ_CREATE mailbox failed with "
14629                                 "status x%x add_status x%x, mbx status x%x\n",
14630                                 shdr_status, shdr_add_status, rc);
14631                 status = -ENXIO;
14632                 goto out;
14633         }
14634         if (mq->queue_id == 0xFFFF) {
14635                 status = -ENXIO;
14636                 goto out;
14637         }
14638         mq->type = LPFC_MQ;
14639         mq->assoc_qid = cq->queue_id;
14640         mq->subtype = subtype;
14641         mq->host_index = 0;
14642         mq->hba_index = 0;
14643
14644         /* link the mq onto the parent cq child list */
14645         list_add_tail(&mq->list, &cq->child_list);
14646 out:
14647         mempool_free(mbox, phba->mbox_mem_pool);
14648         return status;
14649 }
14650
14651 /**
14652  * lpfc_wq_create - Create a Work Queue on the HBA
14653  * @phba: HBA structure that indicates port to create a queue on.
14654  * @wq: The queue structure to use to create the work queue.
14655  * @cq: The completion queue to bind this work queue to.
14656  * @subtype: The subtype of the work queue indicating its functionality.
14657  *
14658  * This function creates a work queue, as detailed in @wq, on a port, described
14659  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14660  *
14661  * The @phba struct is used to send mailbox command to HBA. The @wq struct
14662  * is used to get the entry count and entry size that are necessary to
14663  * determine the number of pages to allocate and use for this queue. The @cq
14664  * is used to indicate which completion queue to bind this work queue to. This
14665  * function will send the WQ_CREATE mailbox command to the HBA to setup the
14666  * work queue. This function is asynchronous and will wait for the mailbox
14667  * command to finish before continuing.
14668  *
14669  * On success this function will return a zero. If unable to allocate enough
14670  * memory this function will return -ENOMEM. If the queue create mailbox command
14671  * fails this function will return -ENXIO.
14672  **/
14673 int
14674 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14675                struct lpfc_queue *cq, uint32_t subtype)
14676 {
14677         struct lpfc_mbx_wq_create *wq_create;
14678         struct lpfc_dmabuf *dmabuf;
14679         LPFC_MBOXQ_t *mbox;
14680         int rc, length, status = 0;
14681         uint32_t shdr_status, shdr_add_status;
14682         union lpfc_sli4_cfg_shdr *shdr;
14683         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14684         struct dma_address *page;
14685         void __iomem *bar_memmap_p;
14686         uint32_t db_offset;
14687         uint16_t pci_barset;
14688
14689         /* sanity check on queue memory */
14690         if (!wq || !cq)
14691                 return -ENODEV;
14692         if (!phba->sli4_hba.pc_sli4_params.supported)
14693                 hw_page_size = SLI4_PAGE_SIZE;
14694
14695         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14696         if (!mbox)
14697                 return -ENOMEM;
14698         length = (sizeof(struct lpfc_mbx_wq_create) -
14699                   sizeof(struct lpfc_sli4_cfg_mhdr));
14700         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14701                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14702                          length, LPFC_SLI4_MBX_EMBED);
14703         wq_create = &mbox->u.mqe.un.wq_create;
14704         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
14705         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14706                     wq->page_count);
14707         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14708                     cq->queue_id);
14709
14710         /* wqv is the earliest version supported, NOT the latest */
14711         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14712                phba->sli4_hba.pc_sli4_params.wqv);
14713
14714         switch (phba->sli4_hba.pc_sli4_params.wqv) {
14715         case LPFC_Q_CREATE_VERSION_0:
14716                 switch (wq->entry_size) {
14717                 default:
14718                 case 64:
14719                         /* Nothing to do, version 0 ONLY supports 64 byte */
14720                         page = wq_create->u.request.page;
14721                         break;
14722                 case 128:
14723                         if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14724                             LPFC_WQ_SZ128_SUPPORT)) {
14725                                 status = -ERANGE;
14726                                 goto out;
14727                         }
14728                         /* If we get here the HBA MUST also support V1 and
14729                          * we MUST use it
14730                          */
14731                         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14732                                LPFC_Q_CREATE_VERSION_1);
14733
14734                         bf_set(lpfc_mbx_wq_create_wqe_count,
14735                                &wq_create->u.request_1, wq->entry_count);
14736                         bf_set(lpfc_mbx_wq_create_wqe_size,
14737                                &wq_create->u.request_1,
14738                                LPFC_WQ_WQE_SIZE_128);
14739                         bf_set(lpfc_mbx_wq_create_page_size,
14740                                &wq_create->u.request_1,
14741                                LPFC_WQ_PAGE_SIZE_4096);
14742                         page = wq_create->u.request_1.page;
14743                         break;
14744                 }
14745                 break;
14746         case LPFC_Q_CREATE_VERSION_1:
14747                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14748                        wq->entry_count);
14749                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14750                        LPFC_Q_CREATE_VERSION_1);
14751
14752                 switch (wq->entry_size) {
14753                 default:
14754                 case 64:
14755                         bf_set(lpfc_mbx_wq_create_wqe_size,
14756                                &wq_create->u.request_1,
14757                                LPFC_WQ_WQE_SIZE_64);
14758                         break;
14759                 case 128:
14760                         if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14761                                 LPFC_WQ_SZ128_SUPPORT)) {
14762                                 status = -ERANGE;
14763                                 goto out;
14764                         }
14765                         bf_set(lpfc_mbx_wq_create_wqe_size,
14766                                &wq_create->u.request_1,
14767                                LPFC_WQ_WQE_SIZE_128);
14768                         break;
14769                 }
14770                 bf_set(lpfc_mbx_wq_create_page_size,
14771                        &wq_create->u.request_1,
14772                        LPFC_WQ_PAGE_SIZE_4096);
14773                 page = wq_create->u.request_1.page;
14774                 break;
14775         default:
14776                 status = -ERANGE;
14777                 goto out;
14778         }
14779
14780         list_for_each_entry(dmabuf, &wq->page_list, list) {
14781                 memset(dmabuf->virt, 0, hw_page_size);
14782                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14783                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
14784         }
14785
14786         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14787                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14788
14789         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14790         /* The IOCTL status is embedded in the mailbox subheader. */
14791         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14792         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14793         if (shdr_status || shdr_add_status || rc) {
14794                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14795                                 "2503 WQ_CREATE mailbox failed with "
14796                                 "status x%x add_status x%x, mbx status x%x\n",
14797                                 shdr_status, shdr_add_status, rc);
14798                 status = -ENXIO;
14799                 goto out;
14800         }
14801         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14802         if (wq->queue_id == 0xFFFF) {
14803                 status = -ENXIO;
14804                 goto out;
14805         }
14806         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14807                 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14808                                        &wq_create->u.response);
14809                 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14810                     (wq->db_format != LPFC_DB_RING_FORMAT)) {
14811                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14812                                         "3265 WQ[%d] doorbell format not "
14813                                         "supported: x%x\n", wq->queue_id,
14814                                         wq->db_format);
14815                         status = -EINVAL;
14816                         goto out;
14817                 }
14818                 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14819                                     &wq_create->u.response);
14820                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14821                 if (!bar_memmap_p) {
14822                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14823                                         "3263 WQ[%d] failed to memmap pci "
14824                                         "barset:x%x\n", wq->queue_id,
14825                                         pci_barset);
14826                         status = -ENOMEM;
14827                         goto out;
14828                 }
14829                 db_offset = wq_create->u.response.doorbell_offset;
14830                 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14831                     (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14832                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14833                                         "3252 WQ[%d] doorbell offset not "
14834                                         "supported: x%x\n", wq->queue_id,
14835                                         db_offset);
14836                         status = -EINVAL;
14837                         goto out;
14838                 }
14839                 wq->db_regaddr = bar_memmap_p + db_offset;
14840                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14841                                 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14842                                 "format:x%x\n", wq->queue_id, pci_barset,
14843                                 db_offset, wq->db_format);
14844         } else {
14845                 wq->db_format = LPFC_DB_LIST_FORMAT;
14846                 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14847         }
14848         wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14849         if (wq->pring == NULL) {
14850                 status = -ENOMEM;
14851                 goto out;
14852         }
14853         wq->type = LPFC_WQ;
14854         wq->assoc_qid = cq->queue_id;
14855         wq->subtype = subtype;
14856         wq->host_index = 0;
14857         wq->hba_index = 0;
14858         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
14859
14860         /* link the wq onto the parent cq child list */
14861         list_add_tail(&wq->list, &cq->child_list);
14862 out:
14863         mempool_free(mbox, phba->mbox_mem_pool);
14864         return status;
14865 }
14866
14867 /**
14868  * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14869  * @phba: HBA structure that indicates port to create a queue on.
14870  * @rq:   The queue structure to use for the receive queue.
14871  * @qno:  The associated HBQ number
14872  *
14873  *
14874  * For SLI4 we need to adjust the RQ repost value based on
14875  * the number of buffers that are initially posted to the RQ.
14876  */
14877 void
14878 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14879 {
14880         uint32_t cnt;
14881
14882         /* sanity check on queue memory */
14883         if (!rq)
14884                 return;
14885         cnt = lpfc_hbq_defs[qno]->entry_count;
14886
14887         /* Recalc repost for RQs based on buffers initially posted */
14888         cnt = (cnt >> 3);
14889         if (cnt < LPFC_QUEUE_MIN_REPOST)
14890                 cnt = LPFC_QUEUE_MIN_REPOST;
14891
14892         rq->entry_repost = cnt;
14893 }
14894
14895 /**
14896  * lpfc_rq_create - Create a Receive Queue on the HBA
14897  * @phba: HBA structure that indicates port to create a queue on.
14898  * @hrq: The queue structure to use to create the header receive queue.
14899  * @drq: The queue structure to use to create the data receive queue.
14900  * @cq: The completion queue to bind this work queue to.
14901  *
14902  * This function creates a receive buffer queue pair , as detailed in @hrq and
14903  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14904  * to the HBA.
14905  *
14906  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14907  * struct is used to get the entry count that is necessary to determine the
14908  * number of pages to use for this queue. The @cq is used to indicate which
14909  * completion queue to bind received buffers that are posted to these queues to.
14910  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14911  * receive queue pair. This function is asynchronous and will wait for the
14912  * mailbox command to finish before continuing.
14913  *
14914  * On success this function will return a zero. If unable to allocate enough
14915  * memory this function will return -ENOMEM. If the queue create mailbox command
14916  * fails this function will return -ENXIO.
14917  **/
14918 int
14919 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14920                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14921 {
14922         struct lpfc_mbx_rq_create *rq_create;
14923         struct lpfc_dmabuf *dmabuf;
14924         LPFC_MBOXQ_t *mbox;
14925         int rc, length, status = 0;
14926         uint32_t shdr_status, shdr_add_status;
14927         union lpfc_sli4_cfg_shdr *shdr;
14928         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14929         void __iomem *bar_memmap_p;
14930         uint32_t db_offset;
14931         uint16_t pci_barset;
14932
14933         /* sanity check on queue memory */
14934         if (!hrq || !drq || !cq)
14935                 return -ENODEV;
14936         if (!phba->sli4_hba.pc_sli4_params.supported)
14937                 hw_page_size = SLI4_PAGE_SIZE;
14938
14939         if (hrq->entry_count != drq->entry_count)
14940                 return -EINVAL;
14941         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14942         if (!mbox)
14943                 return -ENOMEM;
14944         length = (sizeof(struct lpfc_mbx_rq_create) -
14945                   sizeof(struct lpfc_sli4_cfg_mhdr));
14946         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14947                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14948                          length, LPFC_SLI4_MBX_EMBED);
14949         rq_create = &mbox->u.mqe.un.rq_create;
14950         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14951         bf_set(lpfc_mbox_hdr_version, &shdr->request,
14952                phba->sli4_hba.pc_sli4_params.rqv);
14953         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14954                 bf_set(lpfc_rq_context_rqe_count_1,
14955                        &rq_create->u.request.context,
14956                        hrq->entry_count);
14957                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
14958                 bf_set(lpfc_rq_context_rqe_size,
14959                        &rq_create->u.request.context,
14960                        LPFC_RQE_SIZE_8);
14961                 bf_set(lpfc_rq_context_page_size,
14962                        &rq_create->u.request.context,
14963                        LPFC_RQ_PAGE_SIZE_4096);
14964         } else {
14965                 switch (hrq->entry_count) {
14966                 default:
14967                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14968                                         "2535 Unsupported RQ count. (%d)\n",
14969                                         hrq->entry_count);
14970                         if (hrq->entry_count < 512) {
14971                                 status = -EINVAL;
14972                                 goto out;
14973                         }
14974                         /* otherwise default to smallest count (drop through) */
14975                 case 512:
14976                         bf_set(lpfc_rq_context_rqe_count,
14977                                &rq_create->u.request.context,
14978                                LPFC_RQ_RING_SIZE_512);
14979                         break;
14980                 case 1024:
14981                         bf_set(lpfc_rq_context_rqe_count,
14982                                &rq_create->u.request.context,
14983                                LPFC_RQ_RING_SIZE_1024);
14984                         break;
14985                 case 2048:
14986                         bf_set(lpfc_rq_context_rqe_count,
14987                                &rq_create->u.request.context,
14988                                LPFC_RQ_RING_SIZE_2048);
14989                         break;
14990                 case 4096:
14991                         bf_set(lpfc_rq_context_rqe_count,
14992                                &rq_create->u.request.context,
14993                                LPFC_RQ_RING_SIZE_4096);
14994                         break;
14995                 }
14996                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14997                        LPFC_HDR_BUF_SIZE);
14998         }
14999         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15000                cq->queue_id);
15001         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15002                hrq->page_count);
15003         list_for_each_entry(dmabuf, &hrq->page_list, list) {
15004                 memset(dmabuf->virt, 0, hw_page_size);
15005                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15006                                         putPaddrLow(dmabuf->phys);
15007                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15008                                         putPaddrHigh(dmabuf->phys);
15009         }
15010         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15011                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15012
15013         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15014         /* The IOCTL status is embedded in the mailbox subheader. */
15015         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15016         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15017         if (shdr_status || shdr_add_status || rc) {
15018                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15019                                 "2504 RQ_CREATE mailbox failed with "
15020                                 "status x%x add_status x%x, mbx status x%x\n",
15021                                 shdr_status, shdr_add_status, rc);
15022                 status = -ENXIO;
15023                 goto out;
15024         }
15025         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15026         if (hrq->queue_id == 0xFFFF) {
15027                 status = -ENXIO;
15028                 goto out;
15029         }
15030
15031         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15032                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15033                                         &rq_create->u.response);
15034                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15035                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15036                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15037                                         "3262 RQ [%d] doorbell format not "
15038                                         "supported: x%x\n", hrq->queue_id,
15039                                         hrq->db_format);
15040                         status = -EINVAL;
15041                         goto out;
15042                 }
15043
15044                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15045                                     &rq_create->u.response);
15046                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15047                 if (!bar_memmap_p) {
15048                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15049                                         "3269 RQ[%d] failed to memmap pci "
15050                                         "barset:x%x\n", hrq->queue_id,
15051                                         pci_barset);
15052                         status = -ENOMEM;
15053                         goto out;
15054                 }
15055
15056                 db_offset = rq_create->u.response.doorbell_offset;
15057                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15058                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15059                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15060                                         "3270 RQ[%d] doorbell offset not "
15061                                         "supported: x%x\n", hrq->queue_id,
15062                                         db_offset);
15063                         status = -EINVAL;
15064                         goto out;
15065                 }
15066                 hrq->db_regaddr = bar_memmap_p + db_offset;
15067                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15068                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15069                                 "format:x%x\n", hrq->queue_id, pci_barset,
15070                                 db_offset, hrq->db_format);
15071         } else {
15072                 hrq->db_format = LPFC_DB_RING_FORMAT;
15073                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15074         }
15075         hrq->type = LPFC_HRQ;
15076         hrq->assoc_qid = cq->queue_id;
15077         hrq->subtype = subtype;
15078         hrq->host_index = 0;
15079         hrq->hba_index = 0;
15080
15081         /* now create the data queue */
15082         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15083                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15084                          length, LPFC_SLI4_MBX_EMBED);
15085         bf_set(lpfc_mbox_hdr_version, &shdr->request,
15086                phba->sli4_hba.pc_sli4_params.rqv);
15087         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15088                 bf_set(lpfc_rq_context_rqe_count_1,
15089                        &rq_create->u.request.context, hrq->entry_count);
15090                 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
15091                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15092                        LPFC_RQE_SIZE_8);
15093                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15094                        (PAGE_SIZE/SLI4_PAGE_SIZE));
15095         } else {
15096                 switch (drq->entry_count) {
15097                 default:
15098                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15099                                         "2536 Unsupported RQ count. (%d)\n",
15100                                         drq->entry_count);
15101                         if (drq->entry_count < 512) {
15102                                 status = -EINVAL;
15103                                 goto out;
15104                         }
15105                         /* otherwise default to smallest count (drop through) */
15106                 case 512:
15107                         bf_set(lpfc_rq_context_rqe_count,
15108                                &rq_create->u.request.context,
15109                                LPFC_RQ_RING_SIZE_512);
15110                         break;
15111                 case 1024:
15112                         bf_set(lpfc_rq_context_rqe_count,
15113                                &rq_create->u.request.context,
15114                                LPFC_RQ_RING_SIZE_1024);
15115                         break;
15116                 case 2048:
15117                         bf_set(lpfc_rq_context_rqe_count,
15118                                &rq_create->u.request.context,
15119                                LPFC_RQ_RING_SIZE_2048);
15120                         break;
15121                 case 4096:
15122                         bf_set(lpfc_rq_context_rqe_count,
15123                                &rq_create->u.request.context,
15124                                LPFC_RQ_RING_SIZE_4096);
15125                         break;
15126                 }
15127                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15128                        LPFC_DATA_BUF_SIZE);
15129         }
15130         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15131                cq->queue_id);
15132         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15133                drq->page_count);
15134         list_for_each_entry(dmabuf, &drq->page_list, list) {
15135                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15136                                         putPaddrLow(dmabuf->phys);
15137                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15138                                         putPaddrHigh(dmabuf->phys);
15139         }
15140         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15141                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15142         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15143         /* The IOCTL status is embedded in the mailbox subheader. */
15144         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15145         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15146         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15147         if (shdr_status || shdr_add_status || rc) {
15148                 status = -ENXIO;
15149                 goto out;
15150         }
15151         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15152         if (drq->queue_id == 0xFFFF) {
15153                 status = -ENXIO;
15154                 goto out;
15155         }
15156         drq->type = LPFC_DRQ;
15157         drq->assoc_qid = cq->queue_id;
15158         drq->subtype = subtype;
15159         drq->host_index = 0;
15160         drq->hba_index = 0;
15161
15162         /* link the header and data RQs onto the parent cq child list */
15163         list_add_tail(&hrq->list, &cq->child_list);
15164         list_add_tail(&drq->list, &cq->child_list);
15165
15166 out:
15167         mempool_free(mbox, phba->mbox_mem_pool);
15168         return status;
15169 }
15170
15171 /**
15172  * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15173  * @phba: HBA structure that indicates port to create a queue on.
15174  * @hrqp: The queue structure array to use to create the header receive queues.
15175  * @drqp: The queue structure array to use to create the data receive queues.
15176  * @cqp: The completion queue array to bind these receive queues to.
15177  *
15178  * This function creates a receive buffer queue pair , as detailed in @hrq and
15179  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15180  * to the HBA.
15181  *
15182  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15183  * struct is used to get the entry count that is necessary to determine the
15184  * number of pages to use for this queue. The @cq is used to indicate which
15185  * completion queue to bind received buffers that are posted to these queues to.
15186  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15187  * receive queue pair. This function is asynchronous and will wait for the
15188  * mailbox command to finish before continuing.
15189  *
15190  * On success this function will return a zero. If unable to allocate enough
15191  * memory this function will return -ENOMEM. If the queue create mailbox command
15192  * fails this function will return -ENXIO.
15193  **/
15194 int
15195 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15196                 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15197                 uint32_t subtype)
15198 {
15199         struct lpfc_queue *hrq, *drq, *cq;
15200         struct lpfc_mbx_rq_create_v2 *rq_create;
15201         struct lpfc_dmabuf *dmabuf;
15202         LPFC_MBOXQ_t *mbox;
15203         int rc, length, alloclen, status = 0;
15204         int cnt, idx, numrq, page_idx = 0;
15205         uint32_t shdr_status, shdr_add_status;
15206         union lpfc_sli4_cfg_shdr *shdr;
15207         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15208
15209         numrq = phba->cfg_nvmet_mrq;
15210         /* sanity check on array memory */
15211         if (!hrqp || !drqp || !cqp || !numrq)
15212                 return -ENODEV;
15213         if (!phba->sli4_hba.pc_sli4_params.supported)
15214                 hw_page_size = SLI4_PAGE_SIZE;
15215
15216         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15217         if (!mbox)
15218                 return -ENOMEM;
15219
15220         length = sizeof(struct lpfc_mbx_rq_create_v2);
15221         length += ((2 * numrq * hrqp[0]->page_count) *
15222                    sizeof(struct dma_address));
15223
15224         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15225                                     LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15226                                     LPFC_SLI4_MBX_NEMBED);
15227         if (alloclen < length) {
15228                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15229                                 "3099 Allocated DMA memory size (%d) is "
15230                                 "less than the requested DMA memory size "
15231                                 "(%d)\n", alloclen, length);
15232                 status = -ENOMEM;
15233                 goto out;
15234         }
15235
15236
15237
15238         rq_create = mbox->sge_array->addr[0];
15239         shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15240
15241         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15242         cnt = 0;
15243
15244         for (idx = 0; idx < numrq; idx++) {
15245                 hrq = hrqp[idx];
15246                 drq = drqp[idx];
15247                 cq  = cqp[idx];
15248
15249                 /* sanity check on queue memory */
15250                 if (!hrq || !drq || !cq) {
15251                         status = -ENODEV;
15252                         goto out;
15253                 }
15254
15255                 if (hrq->entry_count != drq->entry_count) {
15256                         status = -EINVAL;
15257                         goto out;
15258                 }
15259
15260                 if (idx == 0) {
15261                         bf_set(lpfc_mbx_rq_create_num_pages,
15262                                &rq_create->u.request,
15263                                hrq->page_count);
15264                         bf_set(lpfc_mbx_rq_create_rq_cnt,
15265                                &rq_create->u.request, (numrq * 2));
15266                         bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15267                                1);
15268                         bf_set(lpfc_rq_context_base_cq,
15269                                &rq_create->u.request.context,
15270                                cq->queue_id);
15271                         bf_set(lpfc_rq_context_data_size,
15272                                &rq_create->u.request.context,
15273                                LPFC_DATA_BUF_SIZE);
15274                         bf_set(lpfc_rq_context_hdr_size,
15275                                &rq_create->u.request.context,
15276                                LPFC_HDR_BUF_SIZE);
15277                         bf_set(lpfc_rq_context_rqe_count_1,
15278                                &rq_create->u.request.context,
15279                                hrq->entry_count);
15280                         bf_set(lpfc_rq_context_rqe_size,
15281                                &rq_create->u.request.context,
15282                                LPFC_RQE_SIZE_8);
15283                         bf_set(lpfc_rq_context_page_size,
15284                                &rq_create->u.request.context,
15285                                (PAGE_SIZE/SLI4_PAGE_SIZE));
15286                 }
15287                 rc = 0;
15288                 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15289                         memset(dmabuf->virt, 0, hw_page_size);
15290                         cnt = page_idx + dmabuf->buffer_tag;
15291                         rq_create->u.request.page[cnt].addr_lo =
15292                                         putPaddrLow(dmabuf->phys);
15293                         rq_create->u.request.page[cnt].addr_hi =
15294                                         putPaddrHigh(dmabuf->phys);
15295                         rc++;
15296                 }
15297                 page_idx += rc;
15298
15299                 rc = 0;
15300                 list_for_each_entry(dmabuf, &drq->page_list, list) {
15301                         memset(dmabuf->virt, 0, hw_page_size);
15302                         cnt = page_idx + dmabuf->buffer_tag;
15303                         rq_create->u.request.page[cnt].addr_lo =
15304                                         putPaddrLow(dmabuf->phys);
15305                         rq_create->u.request.page[cnt].addr_hi =
15306                                         putPaddrHigh(dmabuf->phys);
15307                         rc++;
15308                 }
15309                 page_idx += rc;
15310
15311                 hrq->db_format = LPFC_DB_RING_FORMAT;
15312                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15313                 hrq->type = LPFC_HRQ;
15314                 hrq->assoc_qid = cq->queue_id;
15315                 hrq->subtype = subtype;
15316                 hrq->host_index = 0;
15317                 hrq->hba_index = 0;
15318
15319                 drq->db_format = LPFC_DB_RING_FORMAT;
15320                 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15321                 drq->type = LPFC_DRQ;
15322                 drq->assoc_qid = cq->queue_id;
15323                 drq->subtype = subtype;
15324                 drq->host_index = 0;
15325                 drq->hba_index = 0;
15326
15327                 list_add_tail(&hrq->list, &cq->child_list);
15328                 list_add_tail(&drq->list, &cq->child_list);
15329         }
15330
15331         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15332         /* The IOCTL status is embedded in the mailbox subheader. */
15333         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15334         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15335         if (shdr_status || shdr_add_status || rc) {
15336                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15337                                 "3120 RQ_CREATE mailbox failed with "
15338                                 "status x%x add_status x%x, mbx status x%x\n",
15339                                 shdr_status, shdr_add_status, rc);
15340                 status = -ENXIO;
15341                 goto out;
15342         }
15343         rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15344         if (rc == 0xFFFF) {
15345                 status = -ENXIO;
15346                 goto out;
15347         }
15348
15349         /* Initialize all RQs with associated queue id */
15350         for (idx = 0; idx < numrq; idx++) {
15351                 hrq = hrqp[idx];
15352                 hrq->queue_id = rc + (2 * idx);
15353                 drq = drqp[idx];
15354                 drq->queue_id = rc + (2 * idx) + 1;
15355         }
15356
15357 out:
15358         lpfc_sli4_mbox_cmd_free(phba, mbox);
15359         return status;
15360 }
15361
15362 /**
15363  * lpfc_eq_destroy - Destroy an event Queue on the HBA
15364  * @eq: The queue structure associated with the queue to destroy.
15365  *
15366  * This function destroys a queue, as detailed in @eq by sending an mailbox
15367  * command, specific to the type of queue, to the HBA.
15368  *
15369  * The @eq struct is used to get the queue ID of the queue to destroy.
15370  *
15371  * On success this function will return a zero. If the queue destroy mailbox
15372  * command fails this function will return -ENXIO.
15373  **/
15374 int
15375 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15376 {
15377         LPFC_MBOXQ_t *mbox;
15378         int rc, length, status = 0;
15379         uint32_t shdr_status, shdr_add_status;
15380         union lpfc_sli4_cfg_shdr *shdr;
15381
15382         /* sanity check on queue memory */
15383         if (!eq)
15384                 return -ENODEV;
15385         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15386         if (!mbox)
15387                 return -ENOMEM;
15388         length = (sizeof(struct lpfc_mbx_eq_destroy) -
15389                   sizeof(struct lpfc_sli4_cfg_mhdr));
15390         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15391                          LPFC_MBOX_OPCODE_EQ_DESTROY,
15392                          length, LPFC_SLI4_MBX_EMBED);
15393         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15394                eq->queue_id);
15395         mbox->vport = eq->phba->pport;
15396         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15397
15398         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15399         /* The IOCTL status is embedded in the mailbox subheader. */
15400         shdr = (union lpfc_sli4_cfg_shdr *)
15401                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15402         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15403         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15404         if (shdr_status || shdr_add_status || rc) {
15405                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15406                                 "2505 EQ_DESTROY mailbox failed with "
15407                                 "status x%x add_status x%x, mbx status x%x\n",
15408                                 shdr_status, shdr_add_status, rc);
15409                 status = -ENXIO;
15410         }
15411
15412         /* Remove eq from any list */
15413         list_del_init(&eq->list);
15414         mempool_free(mbox, eq->phba->mbox_mem_pool);
15415         return status;
15416 }
15417
15418 /**
15419  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15420  * @cq: The queue structure associated with the queue to destroy.
15421  *
15422  * This function destroys a queue, as detailed in @cq by sending an mailbox
15423  * command, specific to the type of queue, to the HBA.
15424  *
15425  * The @cq struct is used to get the queue ID of the queue to destroy.
15426  *
15427  * On success this function will return a zero. If the queue destroy mailbox
15428  * command fails this function will return -ENXIO.
15429  **/
15430 int
15431 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15432 {
15433         LPFC_MBOXQ_t *mbox;
15434         int rc, length, status = 0;
15435         uint32_t shdr_status, shdr_add_status;
15436         union lpfc_sli4_cfg_shdr *shdr;
15437
15438         /* sanity check on queue memory */
15439         if (!cq)
15440                 return -ENODEV;
15441         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15442         if (!mbox)
15443                 return -ENOMEM;
15444         length = (sizeof(struct lpfc_mbx_cq_destroy) -
15445                   sizeof(struct lpfc_sli4_cfg_mhdr));
15446         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15447                          LPFC_MBOX_OPCODE_CQ_DESTROY,
15448                          length, LPFC_SLI4_MBX_EMBED);
15449         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15450                cq->queue_id);
15451         mbox->vport = cq->phba->pport;
15452         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15453         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15454         /* The IOCTL status is embedded in the mailbox subheader. */
15455         shdr = (union lpfc_sli4_cfg_shdr *)
15456                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15457         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15458         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15459         if (shdr_status || shdr_add_status || rc) {
15460                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15461                                 "2506 CQ_DESTROY mailbox failed with "
15462                                 "status x%x add_status x%x, mbx status x%x\n",
15463                                 shdr_status, shdr_add_status, rc);
15464                 status = -ENXIO;
15465         }
15466         /* Remove cq from any list */
15467         list_del_init(&cq->list);
15468         mempool_free(mbox, cq->phba->mbox_mem_pool);
15469         return status;
15470 }
15471
15472 /**
15473  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15474  * @qm: The queue structure associated with the queue to destroy.
15475  *
15476  * This function destroys a queue, as detailed in @mq by sending an mailbox
15477  * command, specific to the type of queue, to the HBA.
15478  *
15479  * The @mq struct is used to get the queue ID of the queue to destroy.
15480  *
15481  * On success this function will return a zero. If the queue destroy mailbox
15482  * command fails this function will return -ENXIO.
15483  **/
15484 int
15485 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15486 {
15487         LPFC_MBOXQ_t *mbox;
15488         int rc, length, status = 0;
15489         uint32_t shdr_status, shdr_add_status;
15490         union lpfc_sli4_cfg_shdr *shdr;
15491
15492         /* sanity check on queue memory */
15493         if (!mq)
15494                 return -ENODEV;
15495         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15496         if (!mbox)
15497                 return -ENOMEM;
15498         length = (sizeof(struct lpfc_mbx_mq_destroy) -
15499                   sizeof(struct lpfc_sli4_cfg_mhdr));
15500         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15501                          LPFC_MBOX_OPCODE_MQ_DESTROY,
15502                          length, LPFC_SLI4_MBX_EMBED);
15503         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15504                mq->queue_id);
15505         mbox->vport = mq->phba->pport;
15506         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15507         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15508         /* The IOCTL status is embedded in the mailbox subheader. */
15509         shdr = (union lpfc_sli4_cfg_shdr *)
15510                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15511         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15512         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15513         if (shdr_status || shdr_add_status || rc) {
15514                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15515                                 "2507 MQ_DESTROY mailbox failed with "
15516                                 "status x%x add_status x%x, mbx status x%x\n",
15517                                 shdr_status, shdr_add_status, rc);
15518                 status = -ENXIO;
15519         }
15520         /* Remove mq from any list */
15521         list_del_init(&mq->list);
15522         mempool_free(mbox, mq->phba->mbox_mem_pool);
15523         return status;
15524 }
15525
15526 /**
15527  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15528  * @wq: The queue structure associated with the queue to destroy.
15529  *
15530  * This function destroys a queue, as detailed in @wq by sending an mailbox
15531  * command, specific to the type of queue, to the HBA.
15532  *
15533  * The @wq struct is used to get the queue ID of the queue to destroy.
15534  *
15535  * On success this function will return a zero. If the queue destroy mailbox
15536  * command fails this function will return -ENXIO.
15537  **/
15538 int
15539 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15540 {
15541         LPFC_MBOXQ_t *mbox;
15542         int rc, length, status = 0;
15543         uint32_t shdr_status, shdr_add_status;
15544         union lpfc_sli4_cfg_shdr *shdr;
15545
15546         /* sanity check on queue memory */
15547         if (!wq)
15548                 return -ENODEV;
15549         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15550         if (!mbox)
15551                 return -ENOMEM;
15552         length = (sizeof(struct lpfc_mbx_wq_destroy) -
15553                   sizeof(struct lpfc_sli4_cfg_mhdr));
15554         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15555                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15556                          length, LPFC_SLI4_MBX_EMBED);
15557         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15558                wq->queue_id);
15559         mbox->vport = wq->phba->pport;
15560         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15561         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15562         shdr = (union lpfc_sli4_cfg_shdr *)
15563                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15564         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15565         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15566         if (shdr_status || shdr_add_status || rc) {
15567                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15568                                 "2508 WQ_DESTROY mailbox failed with "
15569                                 "status x%x add_status x%x, mbx status x%x\n",
15570                                 shdr_status, shdr_add_status, rc);
15571                 status = -ENXIO;
15572         }
15573         /* Remove wq from any list */
15574         list_del_init(&wq->list);
15575         kfree(wq->pring);
15576         wq->pring = NULL;
15577         mempool_free(mbox, wq->phba->mbox_mem_pool);
15578         return status;
15579 }
15580
15581 /**
15582  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15583  * @rq: The queue structure associated with the queue to destroy.
15584  *
15585  * This function destroys a queue, as detailed in @rq by sending an mailbox
15586  * command, specific to the type of queue, to the HBA.
15587  *
15588  * The @rq struct is used to get the queue ID of the queue to destroy.
15589  *
15590  * On success this function will return a zero. If the queue destroy mailbox
15591  * command fails this function will return -ENXIO.
15592  **/
15593 int
15594 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15595                 struct lpfc_queue *drq)
15596 {
15597         LPFC_MBOXQ_t *mbox;
15598         int rc, length, status = 0;
15599         uint32_t shdr_status, shdr_add_status;
15600         union lpfc_sli4_cfg_shdr *shdr;
15601
15602         /* sanity check on queue memory */
15603         if (!hrq || !drq)
15604                 return -ENODEV;
15605         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15606         if (!mbox)
15607                 return -ENOMEM;
15608         length = (sizeof(struct lpfc_mbx_rq_destroy) -
15609                   sizeof(struct lpfc_sli4_cfg_mhdr));
15610         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15611                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15612                          length, LPFC_SLI4_MBX_EMBED);
15613         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15614                hrq->queue_id);
15615         mbox->vport = hrq->phba->pport;
15616         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15617         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15618         /* The IOCTL status is embedded in the mailbox subheader. */
15619         shdr = (union lpfc_sli4_cfg_shdr *)
15620                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15621         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15622         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15623         if (shdr_status || shdr_add_status || rc) {
15624                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15625                                 "2509 RQ_DESTROY mailbox failed with "
15626                                 "status x%x add_status x%x, mbx status x%x\n",
15627                                 shdr_status, shdr_add_status, rc);
15628                 if (rc != MBX_TIMEOUT)
15629                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
15630                 return -ENXIO;
15631         }
15632         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15633                drq->queue_id);
15634         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15635         shdr = (union lpfc_sli4_cfg_shdr *)
15636                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15637         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15638         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15639         if (shdr_status || shdr_add_status || rc) {
15640                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15641                                 "2510 RQ_DESTROY mailbox failed with "
15642                                 "status x%x add_status x%x, mbx status x%x\n",
15643                                 shdr_status, shdr_add_status, rc);
15644                 status = -ENXIO;
15645         }
15646         list_del_init(&hrq->list);
15647         list_del_init(&drq->list);
15648         mempool_free(mbox, hrq->phba->mbox_mem_pool);
15649         return status;
15650 }
15651
15652 /**
15653  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15654  * @phba: The virtual port for which this call being executed.
15655  * @pdma_phys_addr0: Physical address of the 1st SGL page.
15656  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15657  * @xritag: the xritag that ties this io to the SGL pages.
15658  *
15659  * This routine will post the sgl pages for the IO that has the xritag
15660  * that is in the iocbq structure. The xritag is assigned during iocbq
15661  * creation and persists for as long as the driver is loaded.
15662  * if the caller has fewer than 256 scatter gather segments to map then
15663  * pdma_phys_addr1 should be 0.
15664  * If the caller needs to map more than 256 scatter gather segment then
15665  * pdma_phys_addr1 should be a valid physical address.
15666  * physical address for SGLs must be 64 byte aligned.
15667  * If you are going to map 2 SGL's then the first one must have 256 entries
15668  * the second sgl can have between 1 and 256 entries.
15669  *
15670  * Return codes:
15671  *      0 - Success
15672  *      -ENXIO, -ENOMEM - Failure
15673  **/
15674 int
15675 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15676                 dma_addr_t pdma_phys_addr0,
15677                 dma_addr_t pdma_phys_addr1,
15678                 uint16_t xritag)
15679 {
15680         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15681         LPFC_MBOXQ_t *mbox;
15682         int rc;
15683         uint32_t shdr_status, shdr_add_status;
15684         uint32_t mbox_tmo;
15685         union lpfc_sli4_cfg_shdr *shdr;
15686
15687         if (xritag == NO_XRI) {
15688                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15689                                 "0364 Invalid param:\n");
15690                 return -EINVAL;
15691         }
15692
15693         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15694         if (!mbox)
15695                 return -ENOMEM;
15696
15697         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15698                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15699                         sizeof(struct lpfc_mbx_post_sgl_pages) -
15700                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15701
15702         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15703                                 &mbox->u.mqe.un.post_sgl_pages;
15704         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15705         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15706
15707         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15708                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15709         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15710                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15711
15712         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15713                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15714         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15715                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15716         if (!phba->sli4_hba.intr_enable)
15717                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15718         else {
15719                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15720                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15721         }
15722         /* The IOCTL status is embedded in the mailbox subheader. */
15723         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15724         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15725         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15726         if (rc != MBX_TIMEOUT)
15727                 mempool_free(mbox, phba->mbox_mem_pool);
15728         if (shdr_status || shdr_add_status || rc) {
15729                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15730                                 "2511 POST_SGL mailbox failed with "
15731                                 "status x%x add_status x%x, mbx status x%x\n",
15732                                 shdr_status, shdr_add_status, rc);
15733         }
15734         return 0;
15735 }
15736
15737 /**
15738  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
15739  * @phba: pointer to lpfc hba data structure.
15740  *
15741  * This routine is invoked to post rpi header templates to the
15742  * HBA consistent with the SLI-4 interface spec.  This routine
15743  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15744  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15745  *
15746  * Returns
15747  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15748  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
15749  **/
15750 static uint16_t
15751 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15752 {
15753         unsigned long xri;
15754
15755         /*
15756          * Fetch the next logical xri.  Because this index is logical,
15757          * the driver starts at 0 each time.
15758          */
15759         spin_lock_irq(&phba->hbalock);
15760         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15761                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
15762         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15763                 spin_unlock_irq(&phba->hbalock);
15764                 return NO_XRI;
15765         } else {
15766                 set_bit(xri, phba->sli4_hba.xri_bmask);
15767                 phba->sli4_hba.max_cfg_param.xri_used++;
15768         }
15769         spin_unlock_irq(&phba->hbalock);
15770         return xri;
15771 }
15772
15773 /**
15774  * lpfc_sli4_free_xri - Release an xri for reuse.
15775  * @phba: pointer to lpfc hba data structure.
15776  *
15777  * This routine is invoked to release an xri to the pool of
15778  * available rpis maintained by the driver.
15779  **/
15780 static void
15781 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15782 {
15783         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
15784                 phba->sli4_hba.max_cfg_param.xri_used--;
15785         }
15786 }
15787
15788 /**
15789  * lpfc_sli4_free_xri - Release an xri for reuse.
15790  * @phba: pointer to lpfc hba data structure.
15791  *
15792  * This routine is invoked to release an xri to the pool of
15793  * available rpis maintained by the driver.
15794  **/
15795 void
15796 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15797 {
15798         spin_lock_irq(&phba->hbalock);
15799         __lpfc_sli4_free_xri(phba, xri);
15800         spin_unlock_irq(&phba->hbalock);
15801 }
15802
15803 /**
15804  * lpfc_sli4_next_xritag - Get an xritag for the io
15805  * @phba: Pointer to HBA context object.
15806  *
15807  * This function gets an xritag for the iocb. If there is no unused xritag
15808  * it will return 0xffff.
15809  * The function returns the allocated xritag if successful, else returns zero.
15810  * Zero is not a valid xritag.
15811  * The caller is not required to hold any lock.
15812  **/
15813 uint16_t
15814 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15815 {
15816         uint16_t xri_index;
15817
15818         xri_index = lpfc_sli4_alloc_xri(phba);
15819         if (xri_index == NO_XRI)
15820                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15821                                 "2004 Failed to allocate XRI.last XRITAG is %d"
15822                                 " Max XRI is %d, Used XRI is %d\n",
15823                                 xri_index,
15824                                 phba->sli4_hba.max_cfg_param.max_xri,
15825                                 phba->sli4_hba.max_cfg_param.xri_used);
15826         return xri_index;
15827 }
15828
15829 /**
15830  * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
15831  * @phba: pointer to lpfc hba data structure.
15832  * @post_sgl_list: pointer to els sgl entry list.
15833  * @count: number of els sgl entries on the list.
15834  *
15835  * This routine is invoked to post a block of driver's sgl pages to the
15836  * HBA using non-embedded mailbox command. No Lock is held. This routine
15837  * is only called when the driver is loading and after all IO has been
15838  * stopped.
15839  **/
15840 static int
15841 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
15842                             struct list_head *post_sgl_list,
15843                             int post_cnt)
15844 {
15845         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
15846         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15847         struct sgl_page_pairs *sgl_pg_pairs;
15848         void *viraddr;
15849         LPFC_MBOXQ_t *mbox;
15850         uint32_t reqlen, alloclen, pg_pairs;
15851         uint32_t mbox_tmo;
15852         uint16_t xritag_start = 0;
15853         int rc = 0;
15854         uint32_t shdr_status, shdr_add_status;
15855         union lpfc_sli4_cfg_shdr *shdr;
15856
15857         reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
15858                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
15859         if (reqlen > SLI4_PAGE_SIZE) {
15860                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15861                                 "2559 Block sgl registration required DMA "
15862                                 "size (%d) great than a page\n", reqlen);
15863                 return -ENOMEM;
15864         }
15865
15866         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15867         if (!mbox)
15868                 return -ENOMEM;
15869
15870         /* Allocate DMA memory and set up the non-embedded mailbox command */
15871         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15872                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15873                          LPFC_SLI4_MBX_NEMBED);
15874
15875         if (alloclen < reqlen) {
15876                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15877                                 "0285 Allocated DMA memory size (%d) is "
15878                                 "less than the requested DMA memory "
15879                                 "size (%d)\n", alloclen, reqlen);
15880                 lpfc_sli4_mbox_cmd_free(phba, mbox);
15881                 return -ENOMEM;
15882         }
15883         /* Set up the SGL pages in the non-embedded DMA pages */
15884         viraddr = mbox->sge_array->addr[0];
15885         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15886         sgl_pg_pairs = &sgl->sgl_pg_pairs;
15887
15888         pg_pairs = 0;
15889         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
15890                 /* Set up the sge entry */
15891                 sgl_pg_pairs->sgl_pg0_addr_lo =
15892                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15893                 sgl_pg_pairs->sgl_pg0_addr_hi =
15894                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15895                 sgl_pg_pairs->sgl_pg1_addr_lo =
15896                                 cpu_to_le32(putPaddrLow(0));
15897                 sgl_pg_pairs->sgl_pg1_addr_hi =
15898                                 cpu_to_le32(putPaddrHigh(0));
15899
15900                 /* Keep the first xritag on the list */
15901                 if (pg_pairs == 0)
15902                         xritag_start = sglq_entry->sli4_xritag;
15903                 sgl_pg_pairs++;
15904                 pg_pairs++;
15905         }
15906
15907         /* Complete initialization and perform endian conversion. */
15908         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15909         bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
15910         sgl->word0 = cpu_to_le32(sgl->word0);
15911
15912         if (!phba->sli4_hba.intr_enable)
15913                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15914         else {
15915                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15916                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15917         }
15918         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15919         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15920         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15921         if (rc != MBX_TIMEOUT)
15922                 lpfc_sli4_mbox_cmd_free(phba, mbox);
15923         if (shdr_status || shdr_add_status || rc) {
15924                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15925                                 "2513 POST_SGL_BLOCK mailbox command failed "
15926                                 "status x%x add_status x%x mbx status x%x\n",
15927                                 shdr_status, shdr_add_status, rc);
15928                 rc = -ENXIO;
15929         }
15930         return rc;
15931 }
15932
15933 /**
15934  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15935  * @phba: pointer to lpfc hba data structure.
15936  * @sblist: pointer to scsi buffer list.
15937  * @count: number of scsi buffers on the list.
15938  *
15939  * This routine is invoked to post a block of @count scsi sgl pages from a
15940  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15941  * No Lock is held.
15942  *
15943  **/
15944 int
15945 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15946                               struct list_head *sblist,
15947                               int count)
15948 {
15949         struct lpfc_scsi_buf *psb;
15950         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15951         struct sgl_page_pairs *sgl_pg_pairs;
15952         void *viraddr;
15953         LPFC_MBOXQ_t *mbox;
15954         uint32_t reqlen, alloclen, pg_pairs;
15955         uint32_t mbox_tmo;
15956         uint16_t xritag_start = 0;
15957         int rc = 0;
15958         uint32_t shdr_status, shdr_add_status;
15959         dma_addr_t pdma_phys_bpl1;
15960         union lpfc_sli4_cfg_shdr *shdr;
15961
15962         /* Calculate the requested length of the dma memory */
15963         reqlen = count * sizeof(struct sgl_page_pairs) +
15964                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
15965         if (reqlen > SLI4_PAGE_SIZE) {
15966                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15967                                 "0217 Block sgl registration required DMA "
15968                                 "size (%d) great than a page\n", reqlen);
15969                 return -ENOMEM;
15970         }
15971         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15972         if (!mbox) {
15973                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15974                                 "0283 Failed to allocate mbox cmd memory\n");
15975                 return -ENOMEM;
15976         }
15977
15978         /* Allocate DMA memory and set up the non-embedded mailbox command */
15979         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15980                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15981                                 LPFC_SLI4_MBX_NEMBED);
15982
15983         if (alloclen < reqlen) {
15984                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15985                                 "2561 Allocated DMA memory size (%d) is "
15986                                 "less than the requested DMA memory "
15987                                 "size (%d)\n", alloclen, reqlen);
15988                 lpfc_sli4_mbox_cmd_free(phba, mbox);
15989                 return -ENOMEM;
15990         }
15991
15992         /* Get the first SGE entry from the non-embedded DMA memory */
15993         viraddr = mbox->sge_array->addr[0];
15994
15995         /* Set up the SGL pages in the non-embedded DMA pages */
15996         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15997         sgl_pg_pairs = &sgl->sgl_pg_pairs;
15998
15999         pg_pairs = 0;
16000         list_for_each_entry(psb, sblist, list) {
16001                 /* Set up the sge entry */
16002                 sgl_pg_pairs->sgl_pg0_addr_lo =
16003                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16004                 sgl_pg_pairs->sgl_pg0_addr_hi =
16005                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16006                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16007                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16008                 else
16009                         pdma_phys_bpl1 = 0;
16010                 sgl_pg_pairs->sgl_pg1_addr_lo =
16011                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16012                 sgl_pg_pairs->sgl_pg1_addr_hi =
16013                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16014                 /* Keep the first xritag on the list */
16015                 if (pg_pairs == 0)
16016                         xritag_start = psb->cur_iocbq.sli4_xritag;
16017                 sgl_pg_pairs++;
16018                 pg_pairs++;
16019         }
16020         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16021         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16022         /* Perform endian conversion if necessary */
16023         sgl->word0 = cpu_to_le32(sgl->word0);
16024
16025         if (!phba->sli4_hba.intr_enable)
16026                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16027         else {
16028                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16029                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16030         }
16031         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16032         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16033         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16034         if (rc != MBX_TIMEOUT)
16035                 lpfc_sli4_mbox_cmd_free(phba, mbox);
16036         if (shdr_status || shdr_add_status || rc) {
16037                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16038                                 "2564 POST_SGL_BLOCK mailbox command failed "
16039                                 "status x%x add_status x%x mbx status x%x\n",
16040                                 shdr_status, shdr_add_status, rc);
16041                 rc = -ENXIO;
16042         }
16043         return rc;
16044 }
16045
16046 static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
16047 static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
16048
16049 /**
16050  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16051  * @phba: pointer to lpfc_hba struct that the frame was received on
16052  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16053  *
16054  * This function checks the fields in the @fc_hdr to see if the FC frame is a
16055  * valid type of frame that the LPFC driver will handle. This function will
16056  * return a zero if the frame is a valid frame or a non zero value when the
16057  * frame does not pass the check.
16058  **/
16059 static int
16060 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16061 {
16062         /*  make rctl_names static to save stack space */
16063         struct fc_vft_header *fc_vft_hdr;
16064         uint32_t *header = (uint32_t *) fc_hdr;
16065
16066         switch (fc_hdr->fh_r_ctl) {
16067         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
16068         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
16069         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
16070         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
16071         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
16072         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
16073         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
16074         case FC_RCTL_DD_CMD_STATUS:     /* command status */
16075         case FC_RCTL_ELS_REQ:   /* extended link services request */
16076         case FC_RCTL_ELS_REP:   /* extended link services reply */
16077         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
16078         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
16079         case FC_RCTL_BA_NOP:    /* basic link service NOP */
16080         case FC_RCTL_BA_ABTS:   /* basic link service abort */
16081         case FC_RCTL_BA_RMC:    /* remove connection */
16082         case FC_RCTL_BA_ACC:    /* basic accept */
16083         case FC_RCTL_BA_RJT:    /* basic reject */
16084         case FC_RCTL_BA_PRMT:
16085         case FC_RCTL_ACK_1:     /* acknowledge_1 */
16086         case FC_RCTL_ACK_0:     /* acknowledge_0 */
16087         case FC_RCTL_P_RJT:     /* port reject */
16088         case FC_RCTL_F_RJT:     /* fabric reject */
16089         case FC_RCTL_P_BSY:     /* port busy */
16090         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
16091         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
16092         case FC_RCTL_LCR:       /* link credit reset */
16093         case FC_RCTL_END:       /* end */
16094                 break;
16095         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
16096                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16097                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16098                 return lpfc_fc_frame_check(phba, fc_hdr);
16099         default:
16100                 goto drop;
16101         }
16102         switch (fc_hdr->fh_type) {
16103         case FC_TYPE_BLS:
16104         case FC_TYPE_ELS:
16105         case FC_TYPE_FCP:
16106         case FC_TYPE_CT:
16107         case FC_TYPE_NVME:
16108                 break;
16109         case FC_TYPE_IP:
16110         case FC_TYPE_ILS:
16111         default:
16112                 goto drop;
16113         }
16114
16115         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16116                         "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16117                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16118                         lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16119                         lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
16120                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16121                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16122                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16123                         be32_to_cpu(header[6]));
16124         return 0;
16125 drop:
16126         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16127                         "2539 Dropped frame rctl:%s type:%s\n",
16128                         lpfc_rctl_names[fc_hdr->fh_r_ctl],
16129                         lpfc_type_names[fc_hdr->fh_type]);
16130         return 1;
16131 }
16132
16133 /**
16134  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16135  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16136  *
16137  * This function processes the FC header to retrieve the VFI from the VF
16138  * header, if one exists. This function will return the VFI if one exists
16139  * or 0 if no VSAN Header exists.
16140  **/
16141 static uint32_t
16142 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16143 {
16144         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16145
16146         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16147                 return 0;
16148         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16149 }
16150
16151 /**
16152  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16153  * @phba: Pointer to the HBA structure to search for the vport on
16154  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16155  * @fcfi: The FC Fabric ID that the frame came from
16156  *
16157  * This function searches the @phba for a vport that matches the content of the
16158  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16159  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16160  * returns the matching vport pointer or NULL if unable to match frame to a
16161  * vport.
16162  **/
16163 static struct lpfc_vport *
16164 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16165                        uint16_t fcfi, uint32_t did)
16166 {
16167         struct lpfc_vport **vports;
16168         struct lpfc_vport *vport = NULL;
16169         int i;
16170
16171         if (did == Fabric_DID)
16172                 return phba->pport;
16173         if ((phba->pport->fc_flag & FC_PT2PT) &&
16174                 !(phba->link_state == LPFC_HBA_READY))
16175                 return phba->pport;
16176
16177         vports = lpfc_create_vport_work_array(phba);
16178         if (vports != NULL) {
16179                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16180                         if (phba->fcf.fcfi == fcfi &&
16181                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16182                             vports[i]->fc_myDID == did) {
16183                                 vport = vports[i];
16184                                 break;
16185                         }
16186                 }
16187         }
16188         lpfc_destroy_vport_work_array(phba, vports);
16189         return vport;
16190 }
16191
16192 /**
16193  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16194  * @vport: The vport to work on.
16195  *
16196  * This function updates the receive sequence time stamp for this vport. The
16197  * receive sequence time stamp indicates the time that the last frame of the
16198  * the sequence that has been idle for the longest amount of time was received.
16199  * the driver uses this time stamp to indicate if any received sequences have
16200  * timed out.
16201  **/
16202 static void
16203 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16204 {
16205         struct lpfc_dmabuf *h_buf;
16206         struct hbq_dmabuf *dmabuf = NULL;
16207
16208         /* get the oldest sequence on the rcv list */
16209         h_buf = list_get_first(&vport->rcv_buffer_list,
16210                                struct lpfc_dmabuf, list);
16211         if (!h_buf)
16212                 return;
16213         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16214         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16215 }
16216
16217 /**
16218  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16219  * @vport: The vport that the received sequences were sent to.
16220  *
16221  * This function cleans up all outstanding received sequences. This is called
16222  * by the driver when a link event or user action invalidates all the received
16223  * sequences.
16224  **/
16225 void
16226 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16227 {
16228         struct lpfc_dmabuf *h_buf, *hnext;
16229         struct lpfc_dmabuf *d_buf, *dnext;
16230         struct hbq_dmabuf *dmabuf = NULL;
16231
16232         /* start with the oldest sequence on the rcv list */
16233         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16234                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16235                 list_del_init(&dmabuf->hbuf.list);
16236                 list_for_each_entry_safe(d_buf, dnext,
16237                                          &dmabuf->dbuf.list, list) {
16238                         list_del_init(&d_buf->list);
16239                         lpfc_in_buf_free(vport->phba, d_buf);
16240                 }
16241                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16242         }
16243 }
16244
16245 /**
16246  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16247  * @vport: The vport that the received sequences were sent to.
16248  *
16249  * This function determines whether any received sequences have timed out by
16250  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16251  * indicates that there is at least one timed out sequence this routine will
16252  * go through the received sequences one at a time from most inactive to most
16253  * active to determine which ones need to be cleaned up. Once it has determined
16254  * that a sequence needs to be cleaned up it will simply free up the resources
16255  * without sending an abort.
16256  **/
16257 void
16258 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16259 {
16260         struct lpfc_dmabuf *h_buf, *hnext;
16261         struct lpfc_dmabuf *d_buf, *dnext;
16262         struct hbq_dmabuf *dmabuf = NULL;
16263         unsigned long timeout;
16264         int abort_count = 0;
16265
16266         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16267                    vport->rcv_buffer_time_stamp);
16268         if (list_empty(&vport->rcv_buffer_list) ||
16269             time_before(jiffies, timeout))
16270                 return;
16271         /* start with the oldest sequence on the rcv list */
16272         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16273                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16274                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16275                            dmabuf->time_stamp);
16276                 if (time_before(jiffies, timeout))
16277                         break;
16278                 abort_count++;
16279                 list_del_init(&dmabuf->hbuf.list);
16280                 list_for_each_entry_safe(d_buf, dnext,
16281                                          &dmabuf->dbuf.list, list) {
16282                         list_del_init(&d_buf->list);
16283                         lpfc_in_buf_free(vport->phba, d_buf);
16284                 }
16285                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16286         }
16287         if (abort_count)
16288                 lpfc_update_rcv_time_stamp(vport);
16289 }
16290
16291 /**
16292  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16293  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16294  *
16295  * This function searches through the existing incomplete sequences that have
16296  * been sent to this @vport. If the frame matches one of the incomplete
16297  * sequences then the dbuf in the @dmabuf is added to the list of frames that
16298  * make up that sequence. If no sequence is found that matches this frame then
16299  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16300  * This function returns a pointer to the first dmabuf in the sequence list that
16301  * the frame was linked to.
16302  **/
16303 static struct hbq_dmabuf *
16304 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16305 {
16306         struct fc_frame_header *new_hdr;
16307         struct fc_frame_header *temp_hdr;
16308         struct lpfc_dmabuf *d_buf;
16309         struct lpfc_dmabuf *h_buf;
16310         struct hbq_dmabuf *seq_dmabuf = NULL;
16311         struct hbq_dmabuf *temp_dmabuf = NULL;
16312         uint8_t found = 0;
16313
16314         INIT_LIST_HEAD(&dmabuf->dbuf.list);
16315         dmabuf->time_stamp = jiffies;
16316         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16317
16318         /* Use the hdr_buf to find the sequence that this frame belongs to */
16319         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16320                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16321                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16322                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16323                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16324                         continue;
16325                 /* found a pending sequence that matches this frame */
16326                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16327                 break;
16328         }
16329         if (!seq_dmabuf) {
16330                 /*
16331                  * This indicates first frame received for this sequence.
16332                  * Queue the buffer on the vport's rcv_buffer_list.
16333                  */
16334                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16335                 lpfc_update_rcv_time_stamp(vport);
16336                 return dmabuf;
16337         }
16338         temp_hdr = seq_dmabuf->hbuf.virt;
16339         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16340                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16341                 list_del_init(&seq_dmabuf->hbuf.list);
16342                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16343                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16344                 lpfc_update_rcv_time_stamp(vport);
16345                 return dmabuf;
16346         }
16347         /* move this sequence to the tail to indicate a young sequence */
16348         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16349         seq_dmabuf->time_stamp = jiffies;
16350         lpfc_update_rcv_time_stamp(vport);
16351         if (list_empty(&seq_dmabuf->dbuf.list)) {
16352                 temp_hdr = dmabuf->hbuf.virt;
16353                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16354                 return seq_dmabuf;
16355         }
16356         /* find the correct place in the sequence to insert this frame */
16357         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16358         while (!found) {
16359                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16360                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16361                 /*
16362                  * If the frame's sequence count is greater than the frame on
16363                  * the list then insert the frame right after this frame
16364                  */
16365                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16366                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16367                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
16368                         found = 1;
16369                         break;
16370                 }
16371
16372                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16373                         break;
16374                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
16375         }
16376
16377         if (found)
16378                 return seq_dmabuf;
16379         return NULL;
16380 }
16381
16382 /**
16383  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16384  * @vport: pointer to a vitural port
16385  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16386  *
16387  * This function tries to abort from the partially assembed sequence, described
16388  * by the information from basic abbort @dmabuf. It checks to see whether such
16389  * partially assembled sequence held by the driver. If so, it shall free up all
16390  * the frames from the partially assembled sequence.
16391  *
16392  * Return
16393  * true  -- if there is matching partially assembled sequence present and all
16394  *          the frames freed with the sequence;
16395  * false -- if there is no matching partially assembled sequence present so
16396  *          nothing got aborted in the lower layer driver
16397  **/
16398 static bool
16399 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16400                             struct hbq_dmabuf *dmabuf)
16401 {
16402         struct fc_frame_header *new_hdr;
16403         struct fc_frame_header *temp_hdr;
16404         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16405         struct hbq_dmabuf *seq_dmabuf = NULL;
16406
16407         /* Use the hdr_buf to find the sequence that matches this frame */
16408         INIT_LIST_HEAD(&dmabuf->dbuf.list);
16409         INIT_LIST_HEAD(&dmabuf->hbuf.list);
16410         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16411         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16412                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16413                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16414                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16415                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16416                         continue;
16417                 /* found a pending sequence that matches this frame */
16418                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16419                 break;
16420         }
16421
16422         /* Free up all the frames from the partially assembled sequence */
16423         if (seq_dmabuf) {
16424                 list_for_each_entry_safe(d_buf, n_buf,
16425                                          &seq_dmabuf->dbuf.list, list) {
16426                         list_del_init(&d_buf->list);
16427                         lpfc_in_buf_free(vport->phba, d_buf);
16428                 }
16429                 return true;
16430         }
16431         return false;
16432 }
16433
16434 /**
16435  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16436  * @vport: pointer to a vitural port
16437  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16438  *
16439  * This function tries to abort from the assembed sequence from upper level
16440  * protocol, described by the information from basic abbort @dmabuf. It
16441  * checks to see whether such pending context exists at upper level protocol.
16442  * If so, it shall clean up the pending context.
16443  *
16444  * Return
16445  * true  -- if there is matching pending context of the sequence cleaned
16446  *          at ulp;
16447  * false -- if there is no matching pending context of the sequence present
16448  *          at ulp.
16449  **/
16450 static bool
16451 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16452 {
16453         struct lpfc_hba *phba = vport->phba;
16454         int handled;
16455
16456         /* Accepting abort at ulp with SLI4 only */
16457         if (phba->sli_rev < LPFC_SLI_REV4)
16458                 return false;
16459
16460         /* Register all caring upper level protocols to attend abort */
16461         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16462         if (handled)
16463                 return true;
16464
16465         return false;
16466 }
16467
16468 /**
16469  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
16470  * @phba: Pointer to HBA context object.
16471  * @cmd_iocbq: pointer to the command iocbq structure.
16472  * @rsp_iocbq: pointer to the response iocbq structure.
16473  *
16474  * This function handles the sequence abort response iocb command complete
16475  * event. It properly releases the memory allocated to the sequence abort
16476  * accept iocb.
16477  **/
16478 static void
16479 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
16480                              struct lpfc_iocbq *cmd_iocbq,
16481                              struct lpfc_iocbq *rsp_iocbq)
16482 {
16483         struct lpfc_nodelist *ndlp;
16484
16485         if (cmd_iocbq) {
16486                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16487                 lpfc_nlp_put(ndlp);
16488                 lpfc_nlp_not_used(ndlp);
16489                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
16490         }
16491
16492         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16493         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16494                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16495                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
16496                         rsp_iocbq->iocb.ulpStatus,
16497                         rsp_iocbq->iocb.un.ulpWord[4]);
16498 }
16499
16500 /**
16501  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16502  * @phba: Pointer to HBA context object.
16503  * @xri: xri id in transaction.
16504  *
16505  * This function validates the xri maps to the known range of XRIs allocated an
16506  * used by the driver.
16507  **/
16508 uint16_t
16509 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16510                       uint16_t xri)
16511 {
16512         uint16_t i;
16513
16514         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16515                 if (xri == phba->sli4_hba.xri_ids[i])
16516                         return i;
16517         }
16518         return NO_XRI;
16519 }
16520
16521 /**
16522  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
16523  * @phba: Pointer to HBA context object.
16524  * @fc_hdr: pointer to a FC frame header.
16525  *
16526  * This function sends a basic response to a previous unsol sequence abort
16527  * event after aborting the sequence handling.
16528  **/
16529 void
16530 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16531                         struct fc_frame_header *fc_hdr, bool aborted)
16532 {
16533         struct lpfc_hba *phba = vport->phba;
16534         struct lpfc_iocbq *ctiocb = NULL;
16535         struct lpfc_nodelist *ndlp;
16536         uint16_t oxid, rxid, xri, lxri;
16537         uint32_t sid, fctl;
16538         IOCB_t *icmd;
16539         int rc;
16540
16541         if (!lpfc_is_link_up(phba))
16542                 return;
16543
16544         sid = sli4_sid_from_fc_hdr(fc_hdr);
16545         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
16546         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
16547
16548         ndlp = lpfc_findnode_did(vport, sid);
16549         if (!ndlp) {
16550                 ndlp = lpfc_nlp_init(vport, sid);
16551                 if (!ndlp) {
16552                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16553                                          "1268 Failed to allocate ndlp for "
16554                                          "oxid:x%x SID:x%x\n", oxid, sid);
16555                         return;
16556                 }
16557                 /* Put ndlp onto pport node list */
16558                 lpfc_enqueue_node(vport, ndlp);
16559         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16560                 /* re-setup ndlp without removing from node list */
16561                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16562                 if (!ndlp) {
16563                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16564                                          "3275 Failed to active ndlp found "
16565                                          "for oxid:x%x SID:x%x\n", oxid, sid);
16566                         return;
16567                 }
16568         }
16569
16570         /* Allocate buffer for rsp iocb */
16571         ctiocb = lpfc_sli_get_iocbq(phba);
16572         if (!ctiocb)
16573                 return;
16574
16575         /* Extract the F_CTL field from FC_HDR */
16576         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16577
16578         icmd = &ctiocb->iocb;
16579         icmd->un.xseq64.bdl.bdeSize = 0;
16580         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
16581         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16582         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16583         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16584
16585         /* Fill in the rest of iocb fields */
16586         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16587         icmd->ulpBdeCount = 0;
16588         icmd->ulpLe = 1;
16589         icmd->ulpClass = CLASS3;
16590         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
16591         ctiocb->context1 = lpfc_nlp_get(ndlp);
16592
16593         ctiocb->iocb_cmpl = NULL;
16594         ctiocb->vport = phba->pport;
16595         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
16596         ctiocb->sli4_lxritag = NO_XRI;
16597         ctiocb->sli4_xritag = NO_XRI;
16598
16599         if (fctl & FC_FC_EX_CTX)
16600                 /* Exchange responder sent the abort so we
16601                  * own the oxid.
16602                  */
16603                 xri = oxid;
16604         else
16605                 xri = rxid;
16606         lxri = lpfc_sli4_xri_inrange(phba, xri);
16607         if (lxri != NO_XRI)
16608                 lpfc_set_rrq_active(phba, ndlp, lxri,
16609                         (xri == oxid) ? rxid : oxid, 0);
16610         /* For BA_ABTS from exchange responder, if the logical xri with
16611          * the oxid maps to the FCP XRI range, the port no longer has
16612          * that exchange context, send a BLS_RJT. Override the IOCB for
16613          * a BA_RJT.
16614          */
16615         if ((fctl & FC_FC_EX_CTX) &&
16616             (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
16617                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16618                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16619                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16620                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16621         }
16622
16623         /* If BA_ABTS failed to abort a partially assembled receive sequence,
16624          * the driver no longer has that exchange, send a BLS_RJT. Override
16625          * the IOCB for a BA_RJT.
16626          */
16627         if (aborted == false) {
16628                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16629                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16630                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16631                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16632         }
16633
16634         if (fctl & FC_FC_EX_CTX) {
16635                 /* ABTS sent by responder to CT exchange, construction
16636                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16637                  * field and RX_ID from ABTS for RX_ID field.
16638                  */
16639                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
16640         } else {
16641                 /* ABTS sent by initiator to CT exchange, construction
16642                  * of BA_ACC will need to allocate a new XRI as for the
16643                  * XRI_TAG field.
16644                  */
16645                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
16646         }
16647         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
16648         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
16649
16650         /* Xmit CT abts response on exchange <xid> */
16651         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16652                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16653                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
16654
16655         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16656         if (rc == IOCB_ERROR) {
16657                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16658                                  "2925 Failed to issue CT ABTS RSP x%x on "
16659                                  "xri x%x, Data x%x\n",
16660                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16661                                  phba->link_state);
16662                 lpfc_nlp_put(ndlp);
16663                 ctiocb->context1 = NULL;
16664                 lpfc_sli_release_iocbq(phba, ctiocb);
16665         }
16666 }
16667
16668 /**
16669  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16670  * @vport: Pointer to the vport on which this sequence was received
16671  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16672  *
16673  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16674  * receive sequence is only partially assembed by the driver, it shall abort
16675  * the partially assembled frames for the sequence. Otherwise, if the
16676  * unsolicited receive sequence has been completely assembled and passed to
16677  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16678  * unsolicited sequence has been aborted. After that, it will issue a basic
16679  * accept to accept the abort.
16680  **/
16681 static void
16682 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16683                              struct hbq_dmabuf *dmabuf)
16684 {
16685         struct lpfc_hba *phba = vport->phba;
16686         struct fc_frame_header fc_hdr;
16687         uint32_t fctl;
16688         bool aborted;
16689
16690         /* Make a copy of fc_hdr before the dmabuf being released */
16691         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
16692         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
16693
16694         if (fctl & FC_FC_EX_CTX) {
16695                 /* ABTS by responder to exchange, no cleanup needed */
16696                 aborted = true;
16697         } else {
16698                 /* ABTS by initiator to exchange, need to do cleanup */
16699                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16700                 if (aborted == false)
16701                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
16702         }
16703         lpfc_in_buf_free(phba, &dmabuf->dbuf);
16704
16705         if (phba->nvmet_support) {
16706                 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16707                 return;
16708         }
16709
16710         /* Respond with BA_ACC or BA_RJT accordingly */
16711         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
16712 }
16713
16714 /**
16715  * lpfc_seq_complete - Indicates if a sequence is complete
16716  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16717  *
16718  * This function checks the sequence, starting with the frame described by
16719  * @dmabuf, to see if all the frames associated with this sequence are present.
16720  * the frames associated with this sequence are linked to the @dmabuf using the
16721  * dbuf list. This function looks for two major things. 1) That the first frame
16722  * has a sequence count of zero. 2) There is a frame with last frame of sequence
16723  * set. 3) That there are no holes in the sequence count. The function will
16724  * return 1 when the sequence is complete, otherwise it will return 0.
16725  **/
16726 static int
16727 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16728 {
16729         struct fc_frame_header *hdr;
16730         struct lpfc_dmabuf *d_buf;
16731         struct hbq_dmabuf *seq_dmabuf;
16732         uint32_t fctl;
16733         int seq_count = 0;
16734
16735         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16736         /* make sure first fame of sequence has a sequence count of zero */
16737         if (hdr->fh_seq_cnt != seq_count)
16738                 return 0;
16739         fctl = (hdr->fh_f_ctl[0] << 16 |
16740                 hdr->fh_f_ctl[1] << 8 |
16741                 hdr->fh_f_ctl[2]);
16742         /* If last frame of sequence we can return success. */
16743         if (fctl & FC_FC_END_SEQ)
16744                 return 1;
16745         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16746                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16747                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16748                 /* If there is a hole in the sequence count then fail. */
16749                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
16750                         return 0;
16751                 fctl = (hdr->fh_f_ctl[0] << 16 |
16752                         hdr->fh_f_ctl[1] << 8 |
16753                         hdr->fh_f_ctl[2]);
16754                 /* If last frame of sequence we can return success. */
16755                 if (fctl & FC_FC_END_SEQ)
16756                         return 1;
16757         }
16758         return 0;
16759 }
16760
16761 /**
16762  * lpfc_prep_seq - Prep sequence for ULP processing
16763  * @vport: Pointer to the vport on which this sequence was received
16764  * @dmabuf: pointer to a dmabuf that describes the FC sequence
16765  *
16766  * This function takes a sequence, described by a list of frames, and creates
16767  * a list of iocbq structures to describe the sequence. This iocbq list will be
16768  * used to issue to the generic unsolicited sequence handler. This routine
16769  * returns a pointer to the first iocbq in the list. If the function is unable
16770  * to allocate an iocbq then it throw out the received frames that were not
16771  * able to be described and return a pointer to the first iocbq. If unable to
16772  * allocate any iocbqs (including the first) this function will return NULL.
16773  **/
16774 static struct lpfc_iocbq *
16775 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16776 {
16777         struct hbq_dmabuf *hbq_buf;
16778         struct lpfc_dmabuf *d_buf, *n_buf;
16779         struct lpfc_iocbq *first_iocbq, *iocbq;
16780         struct fc_frame_header *fc_hdr;
16781         uint32_t sid;
16782         uint32_t len, tot_len;
16783         struct ulp_bde64 *pbde;
16784
16785         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16786         /* remove from receive buffer list */
16787         list_del_init(&seq_dmabuf->hbuf.list);
16788         lpfc_update_rcv_time_stamp(vport);
16789         /* get the Remote Port's SID */
16790         sid = sli4_sid_from_fc_hdr(fc_hdr);
16791         tot_len = 0;
16792         /* Get an iocbq struct to fill in. */
16793         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16794         if (first_iocbq) {
16795                 /* Initialize the first IOCB. */
16796                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
16797                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
16798                 first_iocbq->vport = vport;
16799
16800                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16801                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16802                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16803                         first_iocbq->iocb.un.rcvels.parmRo =
16804                                 sli4_did_from_fc_hdr(fc_hdr);
16805                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16806                 } else
16807                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
16808                 first_iocbq->iocb.ulpContext = NO_XRI;
16809                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16810                         be16_to_cpu(fc_hdr->fh_ox_id);
16811                 /* iocbq is prepped for internal consumption.  Physical vpi. */
16812                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16813                         vport->phba->vpi_ids[vport->vpi];
16814                 /* put the first buffer into the first IOCBq */
16815                 tot_len = bf_get(lpfc_rcqe_length,
16816                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16817
16818                 first_iocbq->context2 = &seq_dmabuf->dbuf;
16819                 first_iocbq->context3 = NULL;
16820                 first_iocbq->iocb.ulpBdeCount = 1;
16821                 if (tot_len > LPFC_DATA_BUF_SIZE)
16822                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16823                                                         LPFC_DATA_BUF_SIZE;
16824                 else
16825                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16826
16827                 first_iocbq->iocb.un.rcvels.remoteID = sid;
16828
16829                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16830         }
16831         iocbq = first_iocbq;
16832         /*
16833          * Each IOCBq can have two Buffers assigned, so go through the list
16834          * of buffers for this sequence and save two buffers in each IOCBq
16835          */
16836         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16837                 if (!iocbq) {
16838                         lpfc_in_buf_free(vport->phba, d_buf);
16839                         continue;
16840                 }
16841                 if (!iocbq->context3) {
16842                         iocbq->context3 = d_buf;
16843                         iocbq->iocb.ulpBdeCount++;
16844                         /* We need to get the size out of the right CQE */
16845                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16846                         len = bf_get(lpfc_rcqe_length,
16847                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
16848                         pbde = (struct ulp_bde64 *)
16849                                         &iocbq->iocb.unsli3.sli3Words[4];
16850                         if (len > LPFC_DATA_BUF_SIZE)
16851                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16852                         else
16853                                 pbde->tus.f.bdeSize = len;
16854
16855                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16856                         tot_len += len;
16857                 } else {
16858                         iocbq = lpfc_sli_get_iocbq(vport->phba);
16859                         if (!iocbq) {
16860                                 if (first_iocbq) {
16861                                         first_iocbq->iocb.ulpStatus =
16862                                                         IOSTAT_FCP_RSP_ERROR;
16863                                         first_iocbq->iocb.un.ulpWord[4] =
16864                                                         IOERR_NO_RESOURCES;
16865                                 }
16866                                 lpfc_in_buf_free(vport->phba, d_buf);
16867                                 continue;
16868                         }
16869                         /* We need to get the size out of the right CQE */
16870                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16871                         len = bf_get(lpfc_rcqe_length,
16872                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
16873                         iocbq->context2 = d_buf;
16874                         iocbq->context3 = NULL;
16875                         iocbq->iocb.ulpBdeCount = 1;
16876                         if (len > LPFC_DATA_BUF_SIZE)
16877                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16878                                                         LPFC_DATA_BUF_SIZE;
16879                         else
16880                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16881
16882                         tot_len += len;
16883                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16884
16885                         iocbq->iocb.un.rcvels.remoteID = sid;
16886                         list_add_tail(&iocbq->list, &first_iocbq->list);
16887                 }
16888         }
16889         return first_iocbq;
16890 }
16891
16892 static void
16893 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16894                           struct hbq_dmabuf *seq_dmabuf)
16895 {
16896         struct fc_frame_header *fc_hdr;
16897         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16898         struct lpfc_hba *phba = vport->phba;
16899
16900         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16901         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16902         if (!iocbq) {
16903                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16904                                 "2707 Ring %d handler: Failed to allocate "
16905                                 "iocb Rctl x%x Type x%x received\n",
16906                                 LPFC_ELS_RING,
16907                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16908                 return;
16909         }
16910         if (!lpfc_complete_unsol_iocb(phba,
16911                                       phba->sli4_hba.els_wq->pring,
16912                                       iocbq, fc_hdr->fh_r_ctl,
16913                                       fc_hdr->fh_type))
16914                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16915                                 "2540 Ring %d handler: unexpected Rctl "
16916                                 "x%x Type x%x received\n",
16917                                 LPFC_ELS_RING,
16918                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16919
16920         /* Free iocb created in lpfc_prep_seq */
16921         list_for_each_entry_safe(curr_iocb, next_iocb,
16922                 &iocbq->list, list) {
16923                 list_del_init(&curr_iocb->list);
16924                 lpfc_sli_release_iocbq(phba, curr_iocb);
16925         }
16926         lpfc_sli_release_iocbq(phba, iocbq);
16927 }
16928
16929 /**
16930  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16931  * @phba: Pointer to HBA context object.
16932  *
16933  * This function is called with no lock held. This function processes all
16934  * the received buffers and gives it to upper layers when a received buffer
16935  * indicates that it is the final frame in the sequence. The interrupt
16936  * service routine processes received buffers at interrupt contexts.
16937  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16938  * appropriate receive function when the final frame in a sequence is received.
16939  **/
16940 void
16941 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16942                                  struct hbq_dmabuf *dmabuf)
16943 {
16944         struct hbq_dmabuf *seq_dmabuf;
16945         struct fc_frame_header *fc_hdr;
16946         struct lpfc_vport *vport;
16947         uint32_t fcfi;
16948         uint32_t did;
16949
16950         /* Process each received buffer */
16951         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16952
16953         /* check to see if this a valid type of frame */
16954         if (lpfc_fc_frame_check(phba, fc_hdr)) {
16955                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16956                 return;
16957         }
16958
16959         if ((bf_get(lpfc_cqe_code,
16960                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16961                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16962                               &dmabuf->cq_event.cqe.rcqe_cmpl);
16963         else
16964                 fcfi = bf_get(lpfc_rcqe_fcf_id,
16965                               &dmabuf->cq_event.cqe.rcqe_cmpl);
16966
16967         /* d_id this frame is directed to */
16968         did = sli4_did_from_fc_hdr(fc_hdr);
16969
16970         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
16971         if (!vport) {
16972                 /* throw out the frame */
16973                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16974                 return;
16975         }
16976
16977         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16978         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16979                 (did != Fabric_DID)) {
16980                 /*
16981                  * Throw out the frame if we are not pt2pt.
16982                  * The pt2pt protocol allows for discovery frames
16983                  * to be received without a registered VPI.
16984                  */
16985                 if (!(vport->fc_flag & FC_PT2PT) ||
16986                         (phba->link_state == LPFC_HBA_READY)) {
16987                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
16988                         return;
16989                 }
16990         }
16991
16992         /* Handle the basic abort sequence (BA_ABTS) event */
16993         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16994                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16995                 return;
16996         }
16997
16998         /* Link this frame */
16999         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17000         if (!seq_dmabuf) {
17001                 /* unable to add frame to vport - throw it out */
17002                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17003                 return;
17004         }
17005         /* If not last frame in sequence continue processing frames. */
17006         if (!lpfc_seq_complete(seq_dmabuf))
17007                 return;
17008
17009         /* Send the complete sequence to the upper layer protocol */
17010         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
17011 }
17012
17013 /**
17014  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17015  * @phba: pointer to lpfc hba data structure.
17016  *
17017  * This routine is invoked to post rpi header templates to the
17018  * HBA consistent with the SLI-4 interface spec.  This routine
17019  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17020  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17021  *
17022  * This routine does not require any locks.  It's usage is expected
17023  * to be driver load or reset recovery when the driver is
17024  * sequential.
17025  *
17026  * Return codes
17027  *      0 - successful
17028  *      -EIO - The mailbox failed to complete successfully.
17029  *      When this error occurs, the driver is not guaranteed
17030  *      to have any rpi regions posted to the device and
17031  *      must either attempt to repost the regions or take a
17032  *      fatal error.
17033  **/
17034 int
17035 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17036 {
17037         struct lpfc_rpi_hdr *rpi_page;
17038         uint32_t rc = 0;
17039         uint16_t lrpi = 0;
17040
17041         /* SLI4 ports that support extents do not require RPI headers. */
17042         if (!phba->sli4_hba.rpi_hdrs_in_use)
17043                 goto exit;
17044         if (phba->sli4_hba.extents_in_use)
17045                 return -EIO;
17046
17047         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
17048                 /*
17049                  * Assign the rpi headers a physical rpi only if the driver
17050                  * has not initialized those resources.  A port reset only
17051                  * needs the headers posted.
17052                  */
17053                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17054                     LPFC_RPI_RSRC_RDY)
17055                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17056
17057                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17058                 if (rc != MBX_SUCCESS) {
17059                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17060                                         "2008 Error %d posting all rpi "
17061                                         "headers\n", rc);
17062                         rc = -EIO;
17063                         break;
17064                 }
17065         }
17066
17067  exit:
17068         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17069                LPFC_RPI_RSRC_RDY);
17070         return rc;
17071 }
17072
17073 /**
17074  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17075  * @phba: pointer to lpfc hba data structure.
17076  * @rpi_page:  pointer to the rpi memory region.
17077  *
17078  * This routine is invoked to post a single rpi header to the
17079  * HBA consistent with the SLI-4 interface spec.  This memory region
17080  * maps up to 64 rpi context regions.
17081  *
17082  * Return codes
17083  *      0 - successful
17084  *      -ENOMEM - No available memory
17085  *      -EIO - The mailbox failed to complete successfully.
17086  **/
17087 int
17088 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17089 {
17090         LPFC_MBOXQ_t *mboxq;
17091         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17092         uint32_t rc = 0;
17093         uint32_t shdr_status, shdr_add_status;
17094         union lpfc_sli4_cfg_shdr *shdr;
17095
17096         /* SLI4 ports that support extents do not require RPI headers. */
17097         if (!phba->sli4_hba.rpi_hdrs_in_use)
17098                 return rc;
17099         if (phba->sli4_hba.extents_in_use)
17100                 return -EIO;
17101
17102         /* The port is notified of the header region via a mailbox command. */
17103         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17104         if (!mboxq) {
17105                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17106                                 "2001 Unable to allocate memory for issuing "
17107                                 "SLI_CONFIG_SPECIAL mailbox command\n");
17108                 return -ENOMEM;
17109         }
17110
17111         /* Post all rpi memory regions to the port. */
17112         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
17113         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17114                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17115                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
17116                          sizeof(struct lpfc_sli4_cfg_mhdr),
17117                          LPFC_SLI4_MBX_EMBED);
17118
17119
17120         /* Post the physical rpi to the port for this rpi header. */
17121         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17122                rpi_page->start_rpi);
17123         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17124                hdr_tmpl, rpi_page->page_count);
17125
17126         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17127         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
17128         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17129         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17130         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17131         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17132         if (rc != MBX_TIMEOUT)
17133                 mempool_free(mboxq, phba->mbox_mem_pool);
17134         if (shdr_status || shdr_add_status || rc) {
17135                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17136                                 "2514 POST_RPI_HDR mailbox failed with "
17137                                 "status x%x add_status x%x, mbx status x%x\n",
17138                                 shdr_status, shdr_add_status, rc);
17139                 rc = -ENXIO;
17140         }
17141         return rc;
17142 }
17143
17144 /**
17145  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17146  * @phba: pointer to lpfc hba data structure.
17147  *
17148  * This routine is invoked to post rpi header templates to the
17149  * HBA consistent with the SLI-4 interface spec.  This routine
17150  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17151  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17152  *
17153  * Returns
17154  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17155  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17156  **/
17157 int
17158 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17159 {
17160         unsigned long rpi;
17161         uint16_t max_rpi, rpi_limit;
17162         uint16_t rpi_remaining, lrpi = 0;
17163         struct lpfc_rpi_hdr *rpi_hdr;
17164         unsigned long iflag;
17165
17166         /*
17167          * Fetch the next logical rpi.  Because this index is logical,
17168          * the  driver starts at 0 each time.
17169          */
17170         spin_lock_irqsave(&phba->hbalock, iflag);
17171         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17172         rpi_limit = phba->sli4_hba.next_rpi;
17173
17174         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17175         if (rpi >= rpi_limit)
17176                 rpi = LPFC_RPI_ALLOC_ERROR;
17177         else {
17178                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17179                 phba->sli4_hba.max_cfg_param.rpi_used++;
17180                 phba->sli4_hba.rpi_count++;
17181         }
17182         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17183                         "0001 rpi:%x max:%x lim:%x\n",
17184                         (int) rpi, max_rpi, rpi_limit);
17185
17186         /*
17187          * Don't try to allocate more rpi header regions if the device limit
17188          * has been exhausted.
17189          */
17190         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17191             (phba->sli4_hba.rpi_count >= max_rpi)) {
17192                 spin_unlock_irqrestore(&phba->hbalock, iflag);
17193                 return rpi;
17194         }
17195
17196         /*
17197          * RPI header postings are not required for SLI4 ports capable of
17198          * extents.
17199          */
17200         if (!phba->sli4_hba.rpi_hdrs_in_use) {
17201                 spin_unlock_irqrestore(&phba->hbalock, iflag);
17202                 return rpi;
17203         }
17204
17205         /*
17206          * If the driver is running low on rpi resources, allocate another
17207          * page now.  Note that the next_rpi value is used because
17208          * it represents how many are actually in use whereas max_rpi notes
17209          * how many are supported max by the device.
17210          */
17211         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
17212         spin_unlock_irqrestore(&phba->hbalock, iflag);
17213         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17214                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17215                 if (!rpi_hdr) {
17216                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17217                                         "2002 Error Could not grow rpi "
17218                                         "count\n");
17219                 } else {
17220                         lrpi = rpi_hdr->start_rpi;
17221                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17222                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17223                 }
17224         }
17225
17226         return rpi;
17227 }
17228
17229 /**
17230  * lpfc_sli4_free_rpi - Release an rpi for reuse.
17231  * @phba: pointer to lpfc hba data structure.
17232  *
17233  * This routine is invoked to release an rpi to the pool of
17234  * available rpis maintained by the driver.
17235  **/
17236 static void
17237 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17238 {
17239         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17240                 phba->sli4_hba.rpi_count--;
17241                 phba->sli4_hba.max_cfg_param.rpi_used--;
17242         }
17243 }
17244
17245 /**
17246  * lpfc_sli4_free_rpi - Release an rpi for reuse.
17247  * @phba: pointer to lpfc hba data structure.
17248  *
17249  * This routine is invoked to release an rpi to the pool of
17250  * available rpis maintained by the driver.
17251  **/
17252 void
17253 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17254 {
17255         spin_lock_irq(&phba->hbalock);
17256         __lpfc_sli4_free_rpi(phba, rpi);
17257         spin_unlock_irq(&phba->hbalock);
17258 }
17259
17260 /**
17261  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17262  * @phba: pointer to lpfc hba data structure.
17263  *
17264  * This routine is invoked to remove the memory region that
17265  * provided rpi via a bitmask.
17266  **/
17267 void
17268 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17269 {
17270         kfree(phba->sli4_hba.rpi_bmask);
17271         kfree(phba->sli4_hba.rpi_ids);
17272         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
17273 }
17274
17275 /**
17276  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17277  * @phba: pointer to lpfc hba data structure.
17278  *
17279  * This routine is invoked to remove the memory region that
17280  * provided rpi via a bitmask.
17281  **/
17282 int
17283 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17284         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
17285 {
17286         LPFC_MBOXQ_t *mboxq;
17287         struct lpfc_hba *phba = ndlp->phba;
17288         int rc;
17289
17290         /* The port is notified of the header region via a mailbox command. */
17291         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17292         if (!mboxq)
17293                 return -ENOMEM;
17294
17295         /* Post all rpi memory regions to the port. */
17296         lpfc_resume_rpi(mboxq, ndlp);
17297         if (cmpl) {
17298                 mboxq->mbox_cmpl = cmpl;
17299                 mboxq->context1 = arg;
17300                 mboxq->context2 = ndlp;
17301         } else
17302                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17303         mboxq->vport = ndlp->vport;
17304         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17305         if (rc == MBX_NOT_FINISHED) {
17306                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17307                                 "2010 Resume RPI Mailbox failed "
17308                                 "status %d, mbxStatus x%x\n", rc,
17309                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17310                 mempool_free(mboxq, phba->mbox_mem_pool);
17311                 return -EIO;
17312         }
17313         return 0;
17314 }
17315
17316 /**
17317  * lpfc_sli4_init_vpi - Initialize a vpi with the port
17318  * @vport: Pointer to the vport for which the vpi is being initialized
17319  *
17320  * This routine is invoked to activate a vpi with the port.
17321  *
17322  * Returns:
17323  *    0 success
17324  *    -Evalue otherwise
17325  **/
17326 int
17327 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
17328 {
17329         LPFC_MBOXQ_t *mboxq;
17330         int rc = 0;
17331         int retval = MBX_SUCCESS;
17332         uint32_t mbox_tmo;
17333         struct lpfc_hba *phba = vport->phba;
17334         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17335         if (!mboxq)
17336                 return -ENOMEM;
17337         lpfc_init_vpi(phba, mboxq, vport->vpi);
17338         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
17339         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
17340         if (rc != MBX_SUCCESS) {
17341                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
17342                                 "2022 INIT VPI Mailbox failed "
17343                                 "status %d, mbxStatus x%x\n", rc,
17344                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17345                 retval = -EIO;
17346         }
17347         if (rc != MBX_TIMEOUT)
17348                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
17349
17350         return retval;
17351 }
17352
17353 /**
17354  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17355  * @phba: pointer to lpfc hba data structure.
17356  * @mboxq: Pointer to mailbox object.
17357  *
17358  * This routine is invoked to manually add a single FCF record. The caller
17359  * must pass a completely initialized FCF_Record.  This routine takes
17360  * care of the nonembedded mailbox operations.
17361  **/
17362 static void
17363 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17364 {
17365         void *virt_addr;
17366         union lpfc_sli4_cfg_shdr *shdr;
17367         uint32_t shdr_status, shdr_add_status;
17368
17369         virt_addr = mboxq->sge_array->addr[0];
17370         /* The IOCTL status is embedded in the mailbox subheader. */
17371         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17372         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17373         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17374
17375         if ((shdr_status || shdr_add_status) &&
17376                 (shdr_status != STATUS_FCF_IN_USE))
17377                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17378                         "2558 ADD_FCF_RECORD mailbox failed with "
17379                         "status x%x add_status x%x\n",
17380                         shdr_status, shdr_add_status);
17381
17382         lpfc_sli4_mbox_cmd_free(phba, mboxq);
17383 }
17384
17385 /**
17386  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17387  * @phba: pointer to lpfc hba data structure.
17388  * @fcf_record:  pointer to the initialized fcf record to add.
17389  *
17390  * This routine is invoked to manually add a single FCF record. The caller
17391  * must pass a completely initialized FCF_Record.  This routine takes
17392  * care of the nonembedded mailbox operations.
17393  **/
17394 int
17395 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17396 {
17397         int rc = 0;
17398         LPFC_MBOXQ_t *mboxq;
17399         uint8_t *bytep;
17400         void *virt_addr;
17401         struct lpfc_mbx_sge sge;
17402         uint32_t alloc_len, req_len;
17403         uint32_t fcfindex;
17404
17405         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17406         if (!mboxq) {
17407                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17408                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17409                 return -ENOMEM;
17410         }
17411
17412         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17413                   sizeof(uint32_t);
17414
17415         /* Allocate DMA memory and set up the non-embedded mailbox command */
17416         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17417                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17418                                      req_len, LPFC_SLI4_MBX_NEMBED);
17419         if (alloc_len < req_len) {
17420                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17421                         "2523 Allocated DMA memory size (x%x) is "
17422                         "less than the requested DMA memory "
17423                         "size (x%x)\n", alloc_len, req_len);
17424                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17425                 return -ENOMEM;
17426         }
17427
17428         /*
17429          * Get the first SGE entry from the non-embedded DMA memory.  This
17430          * routine only uses a single SGE.
17431          */
17432         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
17433         virt_addr = mboxq->sge_array->addr[0];
17434         /*
17435          * Configure the FCF record for FCFI 0.  This is the driver's
17436          * hardcoded default and gets used in nonFIP mode.
17437          */
17438         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17439         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17440         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17441
17442         /*
17443          * Copy the fcf_index and the FCF Record Data. The data starts after
17444          * the FCoE header plus word10. The data copy needs to be endian
17445          * correct.
17446          */
17447         bytep += sizeof(uint32_t);
17448         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17449         mboxq->vport = phba->pport;
17450         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17451         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17452         if (rc == MBX_NOT_FINISHED) {
17453                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17454                         "2515 ADD_FCF_RECORD mailbox failed with "
17455                         "status 0x%x\n", rc);
17456                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17457                 rc = -EIO;
17458         } else
17459                 rc = 0;
17460
17461         return rc;
17462 }
17463
17464 /**
17465  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17466  * @phba: pointer to lpfc hba data structure.
17467  * @fcf_record:  pointer to the fcf record to write the default data.
17468  * @fcf_index: FCF table entry index.
17469  *
17470  * This routine is invoked to build the driver's default FCF record.  The
17471  * values used are hardcoded.  This routine handles memory initialization.
17472  *
17473  **/
17474 void
17475 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17476                                 struct fcf_record *fcf_record,
17477                                 uint16_t fcf_index)
17478 {
17479         memset(fcf_record, 0, sizeof(struct fcf_record));
17480         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17481         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17482         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17483         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17484         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17485         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17486         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17487         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17488         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17489         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17490         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17491         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17492         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
17493         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
17494         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17495         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17496                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17497         /* Set the VLAN bit map */
17498         if (phba->valid_vlan) {
17499                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17500                         = 1 << (phba->vlan_id % 8);
17501         }
17502 }
17503
17504 /**
17505  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
17506  * @phba: pointer to lpfc hba data structure.
17507  * @fcf_index: FCF table entry offset.
17508  *
17509  * This routine is invoked to scan the entire FCF table by reading FCF
17510  * record and processing it one at a time starting from the @fcf_index
17511  * for initial FCF discovery or fast FCF failover rediscovery.
17512  *
17513  * Return 0 if the mailbox command is submitted successfully, none 0
17514  * otherwise.
17515  **/
17516 int
17517 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17518 {
17519         int rc = 0, error;
17520         LPFC_MBOXQ_t *mboxq;
17521
17522         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
17523         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
17524         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17525         if (!mboxq) {
17526                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17527                                 "2000 Failed to allocate mbox for "
17528                                 "READ_FCF cmd\n");
17529                 error = -ENOMEM;
17530                 goto fail_fcf_scan;
17531         }
17532         /* Construct the read FCF record mailbox command */
17533         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17534         if (rc) {
17535                 error = -EINVAL;
17536                 goto fail_fcf_scan;
17537         }
17538         /* Issue the mailbox command asynchronously */
17539         mboxq->vport = phba->pport;
17540         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
17541
17542         spin_lock_irq(&phba->hbalock);
17543         phba->hba_flag |= FCF_TS_INPROG;
17544         spin_unlock_irq(&phba->hbalock);
17545
17546         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17547         if (rc == MBX_NOT_FINISHED)
17548                 error = -EIO;
17549         else {
17550                 /* Reset eligible FCF count for new scan */
17551                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
17552                         phba->fcf.eligible_fcf_cnt = 0;
17553                 error = 0;
17554         }
17555 fail_fcf_scan:
17556         if (error) {
17557                 if (mboxq)
17558                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
17559                 /* FCF scan failed, clear FCF_TS_INPROG flag */
17560                 spin_lock_irq(&phba->hbalock);
17561                 phba->hba_flag &= ~FCF_TS_INPROG;
17562                 spin_unlock_irq(&phba->hbalock);
17563         }
17564         return error;
17565 }
17566
17567 /**
17568  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
17569  * @phba: pointer to lpfc hba data structure.
17570  * @fcf_index: FCF table entry offset.
17571  *
17572  * This routine is invoked to read an FCF record indicated by @fcf_index
17573  * and to use it for FLOGI roundrobin FCF failover.
17574  *
17575  * Return 0 if the mailbox command is submitted successfully, none 0
17576  * otherwise.
17577  **/
17578 int
17579 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17580 {
17581         int rc = 0, error;
17582         LPFC_MBOXQ_t *mboxq;
17583
17584         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17585         if (!mboxq) {
17586                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17587                                 "2763 Failed to allocate mbox for "
17588                                 "READ_FCF cmd\n");
17589                 error = -ENOMEM;
17590                 goto fail_fcf_read;
17591         }
17592         /* Construct the read FCF record mailbox command */
17593         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17594         if (rc) {
17595                 error = -EINVAL;
17596                 goto fail_fcf_read;
17597         }
17598         /* Issue the mailbox command asynchronously */
17599         mboxq->vport = phba->pport;
17600         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17601         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17602         if (rc == MBX_NOT_FINISHED)
17603                 error = -EIO;
17604         else
17605                 error = 0;
17606
17607 fail_fcf_read:
17608         if (error && mboxq)
17609                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17610         return error;
17611 }
17612
17613 /**
17614  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17615  * @phba: pointer to lpfc hba data structure.
17616  * @fcf_index: FCF table entry offset.
17617  *
17618  * This routine is invoked to read an FCF record indicated by @fcf_index to
17619  * determine whether it's eligible for FLOGI roundrobin failover list.
17620  *
17621  * Return 0 if the mailbox command is submitted successfully, none 0
17622  * otherwise.
17623  **/
17624 int
17625 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17626 {
17627         int rc = 0, error;
17628         LPFC_MBOXQ_t *mboxq;
17629
17630         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17631         if (!mboxq) {
17632                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17633                                 "2758 Failed to allocate mbox for "
17634                                 "READ_FCF cmd\n");
17635                                 error = -ENOMEM;
17636                                 goto fail_fcf_read;
17637         }
17638         /* Construct the read FCF record mailbox command */
17639         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17640         if (rc) {
17641                 error = -EINVAL;
17642                 goto fail_fcf_read;
17643         }
17644         /* Issue the mailbox command asynchronously */
17645         mboxq->vport = phba->pport;
17646         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17647         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17648         if (rc == MBX_NOT_FINISHED)
17649                 error = -EIO;
17650         else
17651                 error = 0;
17652
17653 fail_fcf_read:
17654         if (error && mboxq)
17655                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17656         return error;
17657 }
17658
17659 /**
17660  * lpfc_check_next_fcf_pri_level
17661  * phba pointer to the lpfc_hba struct for this port.
17662  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17663  * routine when the rr_bmask is empty. The FCF indecies are put into the
17664  * rr_bmask based on their priority level. Starting from the highest priority
17665  * to the lowest. The most likely FCF candidate will be in the highest
17666  * priority group. When this routine is called it searches the fcf_pri list for
17667  * next lowest priority group and repopulates the rr_bmask with only those
17668  * fcf_indexes.
17669  * returns:
17670  * 1=success 0=failure
17671  **/
17672 static int
17673 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17674 {
17675         uint16_t next_fcf_pri;
17676         uint16_t last_index;
17677         struct lpfc_fcf_pri *fcf_pri;
17678         int rc;
17679         int ret = 0;
17680
17681         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17682                         LPFC_SLI4_FCF_TBL_INDX_MAX);
17683         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17684                         "3060 Last IDX %d\n", last_index);
17685
17686         /* Verify the priority list has 2 or more entries */
17687         spin_lock_irq(&phba->hbalock);
17688         if (list_empty(&phba->fcf.fcf_pri_list) ||
17689             list_is_singular(&phba->fcf.fcf_pri_list)) {
17690                 spin_unlock_irq(&phba->hbalock);
17691                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17692                         "3061 Last IDX %d\n", last_index);
17693                 return 0; /* Empty rr list */
17694         }
17695         spin_unlock_irq(&phba->hbalock);
17696
17697         next_fcf_pri = 0;
17698         /*
17699          * Clear the rr_bmask and set all of the bits that are at this
17700          * priority.
17701          */
17702         memset(phba->fcf.fcf_rr_bmask, 0,
17703                         sizeof(*phba->fcf.fcf_rr_bmask));
17704         spin_lock_irq(&phba->hbalock);
17705         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17706                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17707                         continue;
17708                 /*
17709                  * the 1st priority that has not FLOGI failed
17710                  * will be the highest.
17711                  */
17712                 if (!next_fcf_pri)
17713                         next_fcf_pri = fcf_pri->fcf_rec.priority;
17714                 spin_unlock_irq(&phba->hbalock);
17715                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17716                         rc = lpfc_sli4_fcf_rr_index_set(phba,
17717                                                 fcf_pri->fcf_rec.fcf_index);
17718                         if (rc)
17719                                 return 0;
17720                 }
17721                 spin_lock_irq(&phba->hbalock);
17722         }
17723         /*
17724          * if next_fcf_pri was not set above and the list is not empty then
17725          * we have failed flogis on all of them. So reset flogi failed
17726          * and start at the beginning.
17727          */
17728         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17729                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17730                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17731                         /*
17732                          * the 1st priority that has not FLOGI failed
17733                          * will be the highest.
17734                          */
17735                         if (!next_fcf_pri)
17736                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
17737                         spin_unlock_irq(&phba->hbalock);
17738                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17739                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
17740                                                 fcf_pri->fcf_rec.fcf_index);
17741                                 if (rc)
17742                                         return 0;
17743                         }
17744                         spin_lock_irq(&phba->hbalock);
17745                 }
17746         } else
17747                 ret = 1;
17748         spin_unlock_irq(&phba->hbalock);
17749
17750         return ret;
17751 }
17752 /**
17753  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17754  * @phba: pointer to lpfc hba data structure.
17755  *
17756  * This routine is to get the next eligible FCF record index in a round
17757  * robin fashion. If the next eligible FCF record index equals to the
17758  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
17759  * shall be returned, otherwise, the next eligible FCF record's index
17760  * shall be returned.
17761  **/
17762 uint16_t
17763 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17764 {
17765         uint16_t next_fcf_index;
17766
17767 initial_priority:
17768         /* Search start from next bit of currently registered FCF index */
17769         next_fcf_index = phba->fcf.current_rec.fcf_indx;
17770
17771 next_priority:
17772         /* Determine the next fcf index to check */
17773         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
17774         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17775                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
17776                                        next_fcf_index);
17777
17778         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
17779         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17780                 /*
17781                  * If we have wrapped then we need to clear the bits that
17782                  * have been tested so that we can detect when we should
17783                  * change the priority level.
17784                  */
17785                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17786                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
17787         }
17788
17789
17790         /* Check roundrobin failover list empty condition */
17791         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17792                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17793                 /*
17794                  * If next fcf index is not found check if there are lower
17795                  * Priority level fcf's in the fcf_priority list.
17796                  * Set up the rr_bmask with all of the avaiable fcf bits
17797                  * at that level and continue the selection process.
17798                  */
17799                 if (lpfc_check_next_fcf_pri_level(phba))
17800                         goto initial_priority;
17801                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17802                                 "2844 No roundrobin failover FCF available\n");
17803                 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17804                         return LPFC_FCOE_FCF_NEXT_NONE;
17805                 else {
17806                         lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17807                                 "3063 Only FCF available idx %d, flag %x\n",
17808                                 next_fcf_index,
17809                         phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17810                         return next_fcf_index;
17811                 }
17812         }
17813
17814         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17815                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
17816                 LPFC_FCF_FLOGI_FAILED) {
17817                 if (list_is_singular(&phba->fcf.fcf_pri_list))
17818                         return LPFC_FCOE_FCF_NEXT_NONE;
17819
17820                 goto next_priority;
17821         }
17822
17823         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17824                         "2845 Get next roundrobin failover FCF (x%x)\n",
17825                         next_fcf_index);
17826
17827         return next_fcf_index;
17828 }
17829
17830 /**
17831  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17832  * @phba: pointer to lpfc hba data structure.
17833  *
17834  * This routine sets the FCF record index in to the eligible bmask for
17835  * roundrobin failover search. It checks to make sure that the index
17836  * does not go beyond the range of the driver allocated bmask dimension
17837  * before setting the bit.
17838  *
17839  * Returns 0 if the index bit successfully set, otherwise, it returns
17840  * -EINVAL.
17841  **/
17842 int
17843 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17844 {
17845         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17846                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17847                                 "2610 FCF (x%x) reached driver's book "
17848                                 "keeping dimension:x%x\n",
17849                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17850                 return -EINVAL;
17851         }
17852         /* Set the eligible FCF record index bmask */
17853         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17854
17855         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17856                         "2790 Set FCF (x%x) to roundrobin FCF failover "
17857                         "bmask\n", fcf_index);
17858
17859         return 0;
17860 }
17861
17862 /**
17863  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
17864  * @phba: pointer to lpfc hba data structure.
17865  *
17866  * This routine clears the FCF record index from the eligible bmask for
17867  * roundrobin failover search. It checks to make sure that the index
17868  * does not go beyond the range of the driver allocated bmask dimension
17869  * before clearing the bit.
17870  **/
17871 void
17872 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17873 {
17874         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
17875         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17876                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17877                                 "2762 FCF (x%x) reached driver's book "
17878                                 "keeping dimension:x%x\n",
17879                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17880                 return;
17881         }
17882         /* Clear the eligible FCF record index bmask */
17883         spin_lock_irq(&phba->hbalock);
17884         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17885                                  list) {
17886                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17887                         list_del_init(&fcf_pri->list);
17888                         break;
17889                 }
17890         }
17891         spin_unlock_irq(&phba->hbalock);
17892         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17893
17894         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17895                         "2791 Clear FCF (x%x) from roundrobin failover "
17896                         "bmask\n", fcf_index);
17897 }
17898
17899 /**
17900  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17901  * @phba: pointer to lpfc hba data structure.
17902  *
17903  * This routine is the completion routine for the rediscover FCF table mailbox
17904  * command. If the mailbox command returned failure, it will try to stop the
17905  * FCF rediscover wait timer.
17906  **/
17907 static void
17908 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17909 {
17910         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17911         uint32_t shdr_status, shdr_add_status;
17912
17913         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17914
17915         shdr_status = bf_get(lpfc_mbox_hdr_status,
17916                              &redisc_fcf->header.cfg_shdr.response);
17917         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17918                              &redisc_fcf->header.cfg_shdr.response);
17919         if (shdr_status || shdr_add_status) {
17920                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17921                                 "2746 Requesting for FCF rediscovery failed "
17922                                 "status x%x add_status x%x\n",
17923                                 shdr_status, shdr_add_status);
17924                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
17925                         spin_lock_irq(&phba->hbalock);
17926                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
17927                         spin_unlock_irq(&phba->hbalock);
17928                         /*
17929                          * CVL event triggered FCF rediscover request failed,
17930                          * last resort to re-try current registered FCF entry.
17931                          */
17932                         lpfc_retry_pport_discovery(phba);
17933                 } else {
17934                         spin_lock_irq(&phba->hbalock);
17935                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
17936                         spin_unlock_irq(&phba->hbalock);
17937                         /*
17938                          * DEAD FCF event triggered FCF rediscover request
17939                          * failed, last resort to fail over as a link down
17940                          * to FCF registration.
17941                          */
17942                         lpfc_sli4_fcf_dead_failthrough(phba);
17943                 }
17944         } else {
17945                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17946                                 "2775 Start FCF rediscover quiescent timer\n");
17947                 /*
17948                  * Start FCF rediscovery wait timer for pending FCF
17949                  * before rescan FCF record table.
17950                  */
17951                 lpfc_fcf_redisc_wait_start_timer(phba);
17952         }
17953
17954         mempool_free(mbox, phba->mbox_mem_pool);
17955 }
17956
17957 /**
17958  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
17959  * @phba: pointer to lpfc hba data structure.
17960  *
17961  * This routine is invoked to request for rediscovery of the entire FCF table
17962  * by the port.
17963  **/
17964 int
17965 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17966 {
17967         LPFC_MBOXQ_t *mbox;
17968         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17969         int rc, length;
17970
17971         /* Cancel retry delay timers to all vports before FCF rediscover */
17972         lpfc_cancel_all_vport_retry_delay_timer(phba);
17973
17974         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17975         if (!mbox) {
17976                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17977                                 "2745 Failed to allocate mbox for "
17978                                 "requesting FCF rediscover.\n");
17979                 return -ENOMEM;
17980         }
17981
17982         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17983                   sizeof(struct lpfc_sli4_cfg_mhdr));
17984         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17985                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17986                          length, LPFC_SLI4_MBX_EMBED);
17987
17988         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17989         /* Set count to 0 for invalidating the entire FCF database */
17990         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17991
17992         /* Issue the mailbox command asynchronously */
17993         mbox->vport = phba->pport;
17994         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17995         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17996
17997         if (rc == MBX_NOT_FINISHED) {
17998                 mempool_free(mbox, phba->mbox_mem_pool);
17999                 return -EIO;
18000         }
18001         return 0;
18002 }
18003
18004 /**
18005  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18006  * @phba: pointer to lpfc hba data structure.
18007  *
18008  * This function is the failover routine as a last resort to the FCF DEAD
18009  * event when driver failed to perform fast FCF failover.
18010  **/
18011 void
18012 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18013 {
18014         uint32_t link_state;
18015
18016         /*
18017          * Last resort as FCF DEAD event failover will treat this as
18018          * a link down, but save the link state because we don't want
18019          * it to be changed to Link Down unless it is already down.
18020          */
18021         link_state = phba->link_state;
18022         lpfc_linkdown(phba);
18023         phba->link_state = link_state;
18024
18025         /* Unregister FCF if no devices connected to it */
18026         lpfc_unregister_unused_fcf(phba);
18027 }
18028
18029 /**
18030  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
18031  * @phba: pointer to lpfc hba data structure.
18032  * @rgn23_data: pointer to configure region 23 data.
18033  *
18034  * This function gets SLI3 port configure region 23 data through memory dump
18035  * mailbox command. When it successfully retrieves data, the size of the data
18036  * will be returned, otherwise, 0 will be returned.
18037  **/
18038 static uint32_t
18039 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18040 {
18041         LPFC_MBOXQ_t *pmb = NULL;
18042         MAILBOX_t *mb;
18043         uint32_t offset = 0;
18044         int rc;
18045
18046         if (!rgn23_data)
18047                 return 0;
18048
18049         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18050         if (!pmb) {
18051                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18052                                 "2600 failed to allocate mailbox memory\n");
18053                 return 0;
18054         }
18055         mb = &pmb->u.mb;
18056
18057         do {
18058                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18059                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18060
18061                 if (rc != MBX_SUCCESS) {
18062                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
18063                                         "2601 failed to read config "
18064                                         "region 23, rc 0x%x Status 0x%x\n",
18065                                         rc, mb->mbxStatus);
18066                         mb->un.varDmp.word_cnt = 0;
18067                 }
18068                 /*
18069                  * dump mem may return a zero when finished or we got a
18070                  * mailbox error, either way we are done.
18071                  */
18072                 if (mb->un.varDmp.word_cnt == 0)
18073                         break;
18074                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18075                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18076
18077                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18078                                        rgn23_data + offset,
18079                                        mb->un.varDmp.word_cnt);
18080                 offset += mb->un.varDmp.word_cnt;
18081         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18082
18083         mempool_free(pmb, phba->mbox_mem_pool);
18084         return offset;
18085 }
18086
18087 /**
18088  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18089  * @phba: pointer to lpfc hba data structure.
18090  * @rgn23_data: pointer to configure region 23 data.
18091  *
18092  * This function gets SLI4 port configure region 23 data through memory dump
18093  * mailbox command. When it successfully retrieves data, the size of the data
18094  * will be returned, otherwise, 0 will be returned.
18095  **/
18096 static uint32_t
18097 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18098 {
18099         LPFC_MBOXQ_t *mboxq = NULL;
18100         struct lpfc_dmabuf *mp = NULL;
18101         struct lpfc_mqe *mqe;
18102         uint32_t data_length = 0;
18103         int rc;
18104
18105         if (!rgn23_data)
18106                 return 0;
18107
18108         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18109         if (!mboxq) {
18110                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18111                                 "3105 failed to allocate mailbox memory\n");
18112                 return 0;
18113         }
18114
18115         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18116                 goto out;
18117         mqe = &mboxq->u.mqe;
18118         mp = (struct lpfc_dmabuf *) mboxq->context1;
18119         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18120         if (rc)
18121                 goto out;
18122         data_length = mqe->un.mb_words[5];
18123         if (data_length == 0)
18124                 goto out;
18125         if (data_length > DMP_RGN23_SIZE) {
18126                 data_length = 0;
18127                 goto out;
18128         }
18129         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18130 out:
18131         mempool_free(mboxq, phba->mbox_mem_pool);
18132         if (mp) {
18133                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18134                 kfree(mp);
18135         }
18136         return data_length;
18137 }
18138
18139 /**
18140  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18141  * @phba: pointer to lpfc hba data structure.
18142  *
18143  * This function read region 23 and parse TLV for port status to
18144  * decide if the user disaled the port. If the TLV indicates the
18145  * port is disabled, the hba_flag is set accordingly.
18146  **/
18147 void
18148 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18149 {
18150         uint8_t *rgn23_data = NULL;
18151         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18152         uint32_t offset = 0;
18153
18154         /* Get adapter Region 23 data */
18155         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18156         if (!rgn23_data)
18157                 goto out;
18158
18159         if (phba->sli_rev < LPFC_SLI_REV4)
18160                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18161         else {
18162                 if_type = bf_get(lpfc_sli_intf_if_type,
18163                                  &phba->sli4_hba.sli_intf);
18164                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18165                         goto out;
18166                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18167         }
18168
18169         if (!data_size)
18170                 goto out;
18171
18172         /* Check the region signature first */
18173         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18174                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18175                         "2619 Config region 23 has bad signature\n");
18176                         goto out;
18177         }
18178         offset += 4;
18179
18180         /* Check the data structure version */
18181         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18182                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18183                         "2620 Config region 23 has bad version\n");
18184                 goto out;
18185         }
18186         offset += 4;
18187
18188         /* Parse TLV entries in the region */
18189         while (offset < data_size) {
18190                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18191                         break;
18192                 /*
18193                  * If the TLV is not driver specific TLV or driver id is
18194                  * not linux driver id, skip the record.
18195                  */
18196                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18197                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18198                     (rgn23_data[offset + 3] != 0)) {
18199                         offset += rgn23_data[offset + 1] * 4 + 4;
18200                         continue;
18201                 }
18202
18203                 /* Driver found a driver specific TLV in the config region */
18204                 sub_tlv_len = rgn23_data[offset + 1] * 4;
18205                 offset += 4;
18206                 tlv_offset = 0;
18207
18208                 /*
18209                  * Search for configured port state sub-TLV.
18210                  */
18211                 while ((offset < data_size) &&
18212                         (tlv_offset < sub_tlv_len)) {
18213                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18214                                 offset += 4;
18215                                 tlv_offset += 4;
18216                                 break;
18217                         }
18218                         if (rgn23_data[offset] != PORT_STE_TYPE) {
18219                                 offset += rgn23_data[offset + 1] * 4 + 4;
18220                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18221                                 continue;
18222                         }
18223
18224                         /* This HBA contains PORT_STE configured */
18225                         if (!rgn23_data[offset + 2])
18226                                 phba->hba_flag |= LINK_DISABLED;
18227
18228                         goto out;
18229                 }
18230         }
18231
18232 out:
18233         kfree(rgn23_data);
18234         return;
18235 }
18236
18237 /**
18238  * lpfc_wr_object - write an object to the firmware
18239  * @phba: HBA structure that indicates port to create a queue on.
18240  * @dmabuf_list: list of dmabufs to write to the port.
18241  * @size: the total byte value of the objects to write to the port.
18242  * @offset: the current offset to be used to start the transfer.
18243  *
18244  * This routine will create a wr_object mailbox command to send to the port.
18245  * the mailbox command will be constructed using the dma buffers described in
18246  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18247  * BDEs that the imbedded mailbox can support. The @offset variable will be
18248  * used to indicate the starting offset of the transfer and will also return
18249  * the offset after the write object mailbox has completed. @size is used to
18250  * determine the end of the object and whether the eof bit should be set.
18251  *
18252  * Return 0 is successful and offset will contain the the new offset to use
18253  * for the next write.
18254  * Return negative value for error cases.
18255  **/
18256 int
18257 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18258                uint32_t size, uint32_t *offset)
18259 {
18260         struct lpfc_mbx_wr_object *wr_object;
18261         LPFC_MBOXQ_t *mbox;
18262         int rc = 0, i = 0;
18263         uint32_t shdr_status, shdr_add_status;
18264         uint32_t mbox_tmo;
18265         union lpfc_sli4_cfg_shdr *shdr;
18266         struct lpfc_dmabuf *dmabuf;
18267         uint32_t written = 0;
18268
18269         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18270         if (!mbox)
18271                 return -ENOMEM;
18272
18273         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18274                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
18275                         sizeof(struct lpfc_mbx_wr_object) -
18276                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18277
18278         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18279         wr_object->u.request.write_offset = *offset;
18280         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18281         wr_object->u.request.object_name[0] =
18282                 cpu_to_le32(wr_object->u.request.object_name[0]);
18283         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18284         list_for_each_entry(dmabuf, dmabuf_list, list) {
18285                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18286                         break;
18287                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18288                 wr_object->u.request.bde[i].addrHigh =
18289                         putPaddrHigh(dmabuf->phys);
18290                 if (written + SLI4_PAGE_SIZE >= size) {
18291                         wr_object->u.request.bde[i].tus.f.bdeSize =
18292                                 (size - written);
18293                         written += (size - written);
18294                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18295                 } else {
18296                         wr_object->u.request.bde[i].tus.f.bdeSize =
18297                                 SLI4_PAGE_SIZE;
18298                         written += SLI4_PAGE_SIZE;
18299                 }
18300                 i++;
18301         }
18302         wr_object->u.request.bde_count = i;
18303         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18304         if (!phba->sli4_hba.intr_enable)
18305                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18306         else {
18307                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18308                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18309         }
18310         /* The IOCTL status is embedded in the mailbox subheader. */
18311         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18312         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18313         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18314         if (rc != MBX_TIMEOUT)
18315                 mempool_free(mbox, phba->mbox_mem_pool);
18316         if (shdr_status || shdr_add_status || rc) {
18317                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18318                                 "3025 Write Object mailbox failed with "
18319                                 "status x%x add_status x%x, mbx status x%x\n",
18320                                 shdr_status, shdr_add_status, rc);
18321                 rc = -ENXIO;
18322         } else
18323                 *offset += wr_object->u.response.actual_write_length;
18324         return rc;
18325 }
18326
18327 /**
18328  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18329  * @vport: pointer to vport data structure.
18330  *
18331  * This function iterate through the mailboxq and clean up all REG_LOGIN
18332  * and REG_VPI mailbox commands associated with the vport. This function
18333  * is called when driver want to restart discovery of the vport due to
18334  * a Clear Virtual Link event.
18335  **/
18336 void
18337 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18338 {
18339         struct lpfc_hba *phba = vport->phba;
18340         LPFC_MBOXQ_t *mb, *nextmb;
18341         struct lpfc_dmabuf *mp;
18342         struct lpfc_nodelist *ndlp;
18343         struct lpfc_nodelist *act_mbx_ndlp = NULL;
18344         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
18345         LIST_HEAD(mbox_cmd_list);
18346         uint8_t restart_loop;
18347
18348         /* Clean up internally queued mailbox commands with the vport */
18349         spin_lock_irq(&phba->hbalock);
18350         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18351                 if (mb->vport != vport)
18352                         continue;
18353
18354                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18355                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
18356                         continue;
18357
18358                 list_del(&mb->list);
18359                 list_add_tail(&mb->list, &mbox_cmd_list);
18360         }
18361         /* Clean up active mailbox command with the vport */
18362         mb = phba->sli.mbox_active;
18363         if (mb && (mb->vport == vport)) {
18364                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18365                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
18366                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18367                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18368                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18369                         /* Put reference count for delayed processing */
18370                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18371                         /* Unregister the RPI when mailbox complete */
18372                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18373                 }
18374         }
18375         /* Cleanup any mailbox completions which are not yet processed */
18376         do {
18377                 restart_loop = 0;
18378                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18379                         /*
18380                          * If this mailox is already processed or it is
18381                          * for another vport ignore it.
18382                          */
18383                         if ((mb->vport != vport) ||
18384                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18385                                 continue;
18386
18387                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18388                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18389                                 continue;
18390
18391                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18392                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18393                                 ndlp = (struct lpfc_nodelist *)mb->context2;
18394                                 /* Unregister the RPI when mailbox complete */
18395                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18396                                 restart_loop = 1;
18397                                 spin_unlock_irq(&phba->hbalock);
18398                                 spin_lock(shost->host_lock);
18399                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18400                                 spin_unlock(shost->host_lock);
18401                                 spin_lock_irq(&phba->hbalock);
18402                                 break;
18403                         }
18404                 }
18405         } while (restart_loop);
18406
18407         spin_unlock_irq(&phba->hbalock);
18408
18409         /* Release the cleaned-up mailbox commands */
18410         while (!list_empty(&mbox_cmd_list)) {
18411                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
18412                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18413                         mp = (struct lpfc_dmabuf *) (mb->context1);
18414                         if (mp) {
18415                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18416                                 kfree(mp);
18417                         }
18418                         ndlp = (struct lpfc_nodelist *) mb->context2;
18419                         mb->context2 = NULL;
18420                         if (ndlp) {
18421                                 spin_lock(shost->host_lock);
18422                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18423                                 spin_unlock(shost->host_lock);
18424                                 lpfc_nlp_put(ndlp);
18425                         }
18426                 }
18427                 mempool_free(mb, phba->mbox_mem_pool);
18428         }
18429
18430         /* Release the ndlp with the cleaned-up active mailbox command */
18431         if (act_mbx_ndlp) {
18432                 spin_lock(shost->host_lock);
18433                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18434                 spin_unlock(shost->host_lock);
18435                 lpfc_nlp_put(act_mbx_ndlp);
18436         }
18437 }
18438
18439 /**
18440  * lpfc_drain_txq - Drain the txq
18441  * @phba: Pointer to HBA context object.
18442  *
18443  * This function attempt to submit IOCBs on the txq
18444  * to the adapter.  For SLI4 adapters, the txq contains
18445  * ELS IOCBs that have been deferred because the there
18446  * are no SGLs.  This congestion can occur with large
18447  * vport counts during node discovery.
18448  **/
18449
18450 uint32_t
18451 lpfc_drain_txq(struct lpfc_hba *phba)
18452 {
18453         LIST_HEAD(completions);
18454         struct lpfc_sli_ring *pring;
18455         struct lpfc_iocbq *piocbq = NULL;
18456         unsigned long iflags = 0;
18457         char *fail_msg = NULL;
18458         struct lpfc_sglq *sglq;
18459         union lpfc_wqe128 wqe128;
18460         union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
18461         uint32_t txq_cnt = 0;
18462
18463         pring = lpfc_phba_elsring(phba);
18464
18465         spin_lock_irqsave(&pring->ring_lock, iflags);
18466         list_for_each_entry(piocbq, &pring->txq, list) {
18467                 txq_cnt++;
18468         }
18469
18470         if (txq_cnt > pring->txq_max)
18471                 pring->txq_max = txq_cnt;
18472
18473         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18474
18475         while (!list_empty(&pring->txq)) {
18476                 spin_lock_irqsave(&pring->ring_lock, iflags);
18477
18478                 piocbq = lpfc_sli_ringtx_get(phba, pring);
18479                 if (!piocbq) {
18480                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18481                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18482                                 "2823 txq empty and txq_cnt is %d\n ",
18483                                 txq_cnt);
18484                         break;
18485                 }
18486                 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
18487                 if (!sglq) {
18488                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
18489                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18490                         break;
18491                 }
18492                 txq_cnt--;
18493
18494                 /* The xri and iocb resources secured,
18495                  * attempt to issue request
18496                  */
18497                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
18498                 piocbq->sli4_xritag = sglq->sli4_xritag;
18499                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18500                         fail_msg = "to convert bpl to sgl";
18501                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
18502                         fail_msg = "to convert iocb to wqe";
18503                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
18504                         fail_msg = " - Wq is full";
18505                 else
18506                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18507
18508                 if (fail_msg) {
18509                         /* Failed means we can't issue and need to cancel */
18510                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18511                                         "2822 IOCB failed %s iotag 0x%x "
18512                                         "xri 0x%x\n",
18513                                         fail_msg,
18514                                         piocbq->iotag, piocbq->sli4_xritag);
18515                         list_add_tail(&piocbq->list, &completions);
18516                 }
18517                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18518         }
18519
18520         /* Cancel all the IOCBs that cannot be issued */
18521         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18522                                 IOERR_SLI_ABORTED);
18523
18524         return txq_cnt;
18525 }
18526
18527 /**
18528  * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18529  * @phba: Pointer to HBA context object.
18530  * @pwqe: Pointer to command WQE.
18531  * @sglq: Pointer to the scatter gather queue object.
18532  *
18533  * This routine converts the bpl or bde that is in the WQE
18534  * to a sgl list for the sli4 hardware. The physical address
18535  * of the bpl/bde is converted back to a virtual address.
18536  * If the WQE contains a BPL then the list of BDE's is
18537  * converted to sli4_sge's. If the WQE contains a single
18538  * BDE then it is converted to a single sli_sge.
18539  * The WQE is still in cpu endianness so the contents of
18540  * the bpl can be used without byte swapping.
18541  *
18542  * Returns valid XRI = Success, NO_XRI = Failure.
18543  */
18544 static uint16_t
18545 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18546                  struct lpfc_sglq *sglq)
18547 {
18548         uint16_t xritag = NO_XRI;
18549         struct ulp_bde64 *bpl = NULL;
18550         struct ulp_bde64 bde;
18551         struct sli4_sge *sgl  = NULL;
18552         struct lpfc_dmabuf *dmabuf;
18553         union lpfc_wqe *wqe;
18554         int numBdes = 0;
18555         int i = 0;
18556         uint32_t offset = 0; /* accumulated offset in the sg request list */
18557         int inbound = 0; /* number of sg reply entries inbound from firmware */
18558         uint32_t cmd;
18559
18560         if (!pwqeq || !sglq)
18561                 return xritag;
18562
18563         sgl  = (struct sli4_sge *)sglq->sgl;
18564         wqe = &pwqeq->wqe;
18565         pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18566
18567         cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18568         if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18569                 return sglq->sli4_xritag;
18570         numBdes = pwqeq->rsvd2;
18571         if (numBdes) {
18572                 /* The addrHigh and addrLow fields within the WQE
18573                  * have not been byteswapped yet so there is no
18574                  * need to swap them back.
18575                  */
18576                 if (pwqeq->context3)
18577                         dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18578                 else
18579                         return xritag;
18580
18581                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
18582                 if (!bpl)
18583                         return xritag;
18584
18585                 for (i = 0; i < numBdes; i++) {
18586                         /* Should already be byte swapped. */
18587                         sgl->addr_hi = bpl->addrHigh;
18588                         sgl->addr_lo = bpl->addrLow;
18589
18590                         sgl->word2 = le32_to_cpu(sgl->word2);
18591                         if ((i+1) == numBdes)
18592                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
18593                         else
18594                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
18595                         /* swap the size field back to the cpu so we
18596                          * can assign it to the sgl.
18597                          */
18598                         bde.tus.w = le32_to_cpu(bpl->tus.w);
18599                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18600                         /* The offsets in the sgl need to be accumulated
18601                          * separately for the request and reply lists.
18602                          * The request is always first, the reply follows.
18603                          */
18604                         switch (cmd) {
18605                         case CMD_GEN_REQUEST64_WQE:
18606                                 /* add up the reply sg entries */
18607                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18608                                         inbound++;
18609                                 /* first inbound? reset the offset */
18610                                 if (inbound == 1)
18611                                         offset = 0;
18612                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18613                                 bf_set(lpfc_sli4_sge_type, sgl,
18614                                         LPFC_SGE_TYPE_DATA);
18615                                 offset += bde.tus.f.bdeSize;
18616                                 break;
18617                         case CMD_FCP_TRSP64_WQE:
18618                                 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18619                                 bf_set(lpfc_sli4_sge_type, sgl,
18620                                         LPFC_SGE_TYPE_DATA);
18621                                 break;
18622                         case CMD_FCP_TSEND64_WQE:
18623                         case CMD_FCP_TRECEIVE64_WQE:
18624                                 bf_set(lpfc_sli4_sge_type, sgl,
18625                                         bpl->tus.f.bdeFlags);
18626                                 if (i < 3)
18627                                         offset = 0;
18628                                 else
18629                                         offset += bde.tus.f.bdeSize;
18630                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18631                                 break;
18632                         }
18633                         sgl->word2 = cpu_to_le32(sgl->word2);
18634                         bpl++;
18635                         sgl++;
18636                 }
18637         } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18638                 /* The addrHigh and addrLow fields of the BDE have not
18639                  * been byteswapped yet so they need to be swapped
18640                  * before putting them in the sgl.
18641                  */
18642                 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18643                 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18644                 sgl->word2 = le32_to_cpu(sgl->word2);
18645                 bf_set(lpfc_sli4_sge_last, sgl, 1);
18646                 sgl->word2 = cpu_to_le32(sgl->word2);
18647                 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18648         }
18649         return sglq->sli4_xritag;
18650 }
18651
18652 /**
18653  * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18654  * @phba: Pointer to HBA context object.
18655  * @ring_number: Base sli ring number
18656  * @pwqe: Pointer to command WQE.
18657  **/
18658 int
18659 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18660                     struct lpfc_iocbq *pwqe)
18661 {
18662         union lpfc_wqe *wqe = &pwqe->wqe;
18663         struct lpfc_nvmet_rcv_ctx *ctxp;
18664         struct lpfc_queue *wq;
18665         struct lpfc_sglq *sglq;
18666         struct lpfc_sli_ring *pring;
18667         unsigned long iflags;
18668
18669         /* NVME_LS and NVME_LS ABTS requests. */
18670         if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18671                 pring =  phba->sli4_hba.nvmels_wq->pring;
18672                 spin_lock_irqsave(&pring->ring_lock, iflags);
18673                 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18674                 if (!sglq) {
18675                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18676                         return WQE_BUSY;
18677                 }
18678                 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18679                 pwqe->sli4_xritag = sglq->sli4_xritag;
18680                 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18681                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18682                         return WQE_ERROR;
18683                 }
18684                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18685                        pwqe->sli4_xritag);
18686                 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18687                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18688                         return WQE_ERROR;
18689                 }
18690                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18691                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18692                 return 0;
18693         }
18694
18695         /* NVME_FCREQ and NVME_ABTS requests */
18696         if (pwqe->iocb_flag & LPFC_IO_NVME) {
18697                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18698                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18699
18700                 spin_lock_irqsave(&pring->ring_lock, iflags);
18701                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18702                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18703                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18704                 if (lpfc_sli4_wq_put(wq, wqe)) {
18705                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18706                         return WQE_ERROR;
18707                 }
18708                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18709                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18710                 return 0;
18711         }
18712
18713         /* NVMET requests */
18714         if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18715                 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18716                 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18717
18718                 spin_lock_irqsave(&pring->ring_lock, iflags);
18719                 ctxp = pwqe->context2;
18720                 sglq = ctxp->rqb_buffer->sglq;
18721                 if (pwqe->sli4_xritag ==  NO_XRI) {
18722                         pwqe->sli4_lxritag = sglq->sli4_lxritag;
18723                         pwqe->sli4_xritag = sglq->sli4_xritag;
18724                 }
18725                 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18726                        pwqe->sli4_xritag);
18727                 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18728                 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18729                       phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18730                 if (lpfc_sli4_wq_put(wq, wqe)) {
18731                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
18732                         return WQE_ERROR;
18733                 }
18734                 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18735                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18736                 return 0;
18737         }
18738         return WQE_ERROR;
18739 }