GNU Linux-libre 4.4.284-gnu1
[releases.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2015 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69                                     struct lpfc_cqe *);
70 static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71                                        int);
72 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73                         uint32_t);
74 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
75 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
76
77 static IOCB_t *
78 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
79 {
80         return &iocbq->iocb;
81 }
82
83 /**
84  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
85  * @q: The Work Queue to operate on.
86  * @wqe: The work Queue Entry to put on the Work queue.
87  *
88  * This routine will copy the contents of @wqe to the next available entry on
89  * the @q. This function will then ring the Work Queue Doorbell to signal the
90  * HBA to start processing the Work Queue Entry. This function returns 0 if
91  * successful. If no entries are available on @q then this function will return
92  * -ENOMEM.
93  * The caller is expected to hold the hbalock when calling this routine.
94  **/
95 static uint32_t
96 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
97 {
98         union lpfc_wqe *temp_wqe;
99         struct lpfc_register doorbell;
100         uint32_t host_index;
101         uint32_t idx;
102
103         /* sanity check on queue memory */
104         if (unlikely(!q))
105                 return -ENOMEM;
106         temp_wqe = q->qe[q->host_index].wqe;
107
108         /* If the host has not yet processed the next entry then we are done */
109         idx = ((q->host_index + 1) % q->entry_count);
110         if (idx == q->hba_index) {
111                 q->WQ_overflow++;
112                 return -ENOMEM;
113         }
114         q->WQ_posted++;
115         /* set consumption flag every once in a while */
116         if (!((q->host_index + 1) % q->entry_repost))
117                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
118         else
119                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
120         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
121                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
122         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
123         /* ensure WQE bcopy flushed before doorbell write */
124         wmb();
125
126         /* Update the host index before invoking device */
127         host_index = q->host_index;
128
129         q->host_index = idx;
130
131         /* Ring Doorbell */
132         doorbell.word0 = 0;
133         if (q->db_format == LPFC_DB_LIST_FORMAT) {
134                 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
135                 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
136                 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
137         } else if (q->db_format == LPFC_DB_RING_FORMAT) {
138                 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
139                 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
140         } else {
141                 return -EINVAL;
142         }
143         writel(doorbell.word0, q->db_regaddr);
144
145         return 0;
146 }
147
148 /**
149  * lpfc_sli4_wq_release - Updates internal hba index for WQ
150  * @q: The Work Queue to operate on.
151  * @index: The index to advance the hba index to.
152  *
153  * This routine will update the HBA index of a queue to reflect consumption of
154  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
155  * an entry the host calls this function to update the queue's internal
156  * pointers. This routine returns the number of entries that were consumed by
157  * the HBA.
158  **/
159 static uint32_t
160 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
161 {
162         uint32_t released = 0;
163
164         /* sanity check on queue memory */
165         if (unlikely(!q))
166                 return 0;
167
168         if (q->hba_index == index)
169                 return 0;
170         do {
171                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
172                 released++;
173         } while (q->hba_index != index);
174         return released;
175 }
176
177 /**
178  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
179  * @q: The Mailbox Queue to operate on.
180  * @wqe: The Mailbox Queue Entry to put on the Work queue.
181  *
182  * This routine will copy the contents of @mqe to the next available entry on
183  * the @q. This function will then ring the Work Queue Doorbell to signal the
184  * HBA to start processing the Work Queue Entry. This function returns 0 if
185  * successful. If no entries are available on @q then this function will return
186  * -ENOMEM.
187  * The caller is expected to hold the hbalock when calling this routine.
188  **/
189 static uint32_t
190 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
191 {
192         struct lpfc_mqe *temp_mqe;
193         struct lpfc_register doorbell;
194
195         /* sanity check on queue memory */
196         if (unlikely(!q))
197                 return -ENOMEM;
198         temp_mqe = q->qe[q->host_index].mqe;
199
200         /* If the host has not yet processed the next entry then we are done */
201         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
202                 return -ENOMEM;
203         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
204         /* Save off the mailbox pointer for completion */
205         q->phba->mbox = (MAILBOX_t *)temp_mqe;
206
207         /* Update the host index before invoking device */
208         q->host_index = ((q->host_index + 1) % q->entry_count);
209
210         /* Ring Doorbell */
211         doorbell.word0 = 0;
212         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
213         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
214         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
215         return 0;
216 }
217
218 /**
219  * lpfc_sli4_mq_release - Updates internal hba index for MQ
220  * @q: The Mailbox Queue to operate on.
221  *
222  * This routine will update the HBA index of a queue to reflect consumption of
223  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
224  * an entry the host calls this function to update the queue's internal
225  * pointers. This routine returns the number of entries that were consumed by
226  * the HBA.
227  **/
228 static uint32_t
229 lpfc_sli4_mq_release(struct lpfc_queue *q)
230 {
231         /* sanity check on queue memory */
232         if (unlikely(!q))
233                 return 0;
234
235         /* Clear the mailbox pointer for completion */
236         q->phba->mbox = NULL;
237         q->hba_index = ((q->hba_index + 1) % q->entry_count);
238         return 1;
239 }
240
241 /**
242  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
243  * @q: The Event Queue to get the first valid EQE from
244  *
245  * This routine will get the first valid Event Queue Entry from @q, update
246  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
247  * the Queue (no more work to do), or the Queue is full of EQEs that have been
248  * processed, but not popped back to the HBA then this routine will return NULL.
249  **/
250 static struct lpfc_eqe *
251 lpfc_sli4_eq_get(struct lpfc_queue *q)
252 {
253         struct lpfc_eqe *eqe;
254         uint32_t idx;
255
256         /* sanity check on queue memory */
257         if (unlikely(!q))
258                 return NULL;
259         eqe = q->qe[q->hba_index].eqe;
260
261         /* If the next EQE is not valid then we are done */
262         if (!bf_get_le32(lpfc_eqe_valid, eqe))
263                 return NULL;
264         /* If the host has not yet processed the next entry then we are done */
265         idx = ((q->hba_index + 1) % q->entry_count);
266         if (idx == q->host_index)
267                 return NULL;
268
269         q->hba_index = idx;
270
271         /*
272          * insert barrier for instruction interlock : data from the hardware
273          * must have the valid bit checked before it can be copied and acted
274          * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
275          * instructions allowing action on content before valid bit checked,
276          * add barrier here as well. May not be needed as "content" is a
277          * single 32-bit entity here (vs multi word structure for cq's).
278          */
279         mb();
280         return eqe;
281 }
282
283 /**
284  * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
285  * @q: The Event Queue to disable interrupts
286  *
287  **/
288 static inline void
289 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
290 {
291         struct lpfc_register doorbell;
292
293         doorbell.word0 = 0;
294         bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
295         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
296         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
297                 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
298         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
299         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
300 }
301
302 /**
303  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
304  * @q: The Event Queue that the host has completed processing for.
305  * @arm: Indicates whether the host wants to arms this CQ.
306  *
307  * This routine will mark all Event Queue Entries on @q, from the last
308  * known completed entry to the last entry that was processed, as completed
309  * by clearing the valid bit for each completion queue entry. Then it will
310  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
311  * The internal host index in the @q will be updated by this routine to indicate
312  * that the host has finished processing the entries. The @arm parameter
313  * indicates that the queue should be rearmed when ringing the doorbell.
314  *
315  * This function will return the number of EQEs that were popped.
316  **/
317 uint32_t
318 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
319 {
320         uint32_t released = 0;
321         struct lpfc_eqe *temp_eqe;
322         struct lpfc_register doorbell;
323
324         /* sanity check on queue memory */
325         if (unlikely(!q))
326                 return 0;
327
328         /* while there are valid entries */
329         while (q->hba_index != q->host_index) {
330                 temp_eqe = q->qe[q->host_index].eqe;
331                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
332                 released++;
333                 q->host_index = ((q->host_index + 1) % q->entry_count);
334         }
335         if (unlikely(released == 0 && !arm))
336                 return 0;
337
338         /* ring doorbell for number popped */
339         doorbell.word0 = 0;
340         if (arm) {
341                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
342                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
343         }
344         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
345         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
346         bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
347                         (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
348         bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
349         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
350         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
351         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
352                 readl(q->phba->sli4_hba.EQCQDBregaddr);
353         return released;
354 }
355
356 /**
357  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
358  * @q: The Completion Queue to get the first valid CQE from
359  *
360  * This routine will get the first valid Completion Queue Entry from @q, update
361  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
362  * the Queue (no more work to do), or the Queue is full of CQEs that have been
363  * processed, but not popped back to the HBA then this routine will return NULL.
364  **/
365 static struct lpfc_cqe *
366 lpfc_sli4_cq_get(struct lpfc_queue *q)
367 {
368         struct lpfc_cqe *cqe;
369         uint32_t idx;
370
371         /* sanity check on queue memory */
372         if (unlikely(!q))
373                 return NULL;
374
375         /* If the next CQE is not valid then we are done */
376         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
377                 return NULL;
378         /* If the host has not yet processed the next entry then we are done */
379         idx = ((q->hba_index + 1) % q->entry_count);
380         if (idx == q->host_index)
381                 return NULL;
382
383         cqe = q->qe[q->hba_index].cqe;
384         q->hba_index = idx;
385
386         /*
387          * insert barrier for instruction interlock : data from the hardware
388          * must have the valid bit checked before it can be copied and acted
389          * upon. Speculative instructions were allowing a bcopy at the start
390          * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
391          * after our return, to copy data before the valid bit check above
392          * was done. As such, some of the copied data was stale. The barrier
393          * ensures the check is before any data is copied.
394          */
395         mb();
396         return cqe;
397 }
398
399 /**
400  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
401  * @q: The Completion Queue that the host has completed processing for.
402  * @arm: Indicates whether the host wants to arms this CQ.
403  *
404  * This routine will mark all Completion queue entries on @q, from the last
405  * known completed entry to the last entry that was processed, as completed
406  * by clearing the valid bit for each completion queue entry. Then it will
407  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
408  * The internal host index in the @q will be updated by this routine to indicate
409  * that the host has finished processing the entries. The @arm parameter
410  * indicates that the queue should be rearmed when ringing the doorbell.
411  *
412  * This function will return the number of CQEs that were released.
413  **/
414 uint32_t
415 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
416 {
417         uint32_t released = 0;
418         struct lpfc_cqe *temp_qe;
419         struct lpfc_register doorbell;
420
421         /* sanity check on queue memory */
422         if (unlikely(!q))
423                 return 0;
424         /* while there are valid entries */
425         while (q->hba_index != q->host_index) {
426                 temp_qe = q->qe[q->host_index].cqe;
427                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
428                 released++;
429                 q->host_index = ((q->host_index + 1) % q->entry_count);
430         }
431         if (unlikely(released == 0 && !arm))
432                 return 0;
433
434         /* ring doorbell for number popped */
435         doorbell.word0 = 0;
436         if (arm)
437                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
438         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
439         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
440         bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
441                         (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
442         bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
443         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
444         return released;
445 }
446
447 /**
448  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
449  * @q: The Header Receive Queue to operate on.
450  * @wqe: The Receive Queue Entry to put on the Receive queue.
451  *
452  * This routine will copy the contents of @wqe to the next available entry on
453  * the @q. This function will then ring the Receive Queue Doorbell to signal the
454  * HBA to start processing the Receive Queue Entry. This function returns the
455  * index that the rqe was copied to if successful. If no entries are available
456  * on @q then this function will return -ENOMEM.
457  * The caller is expected to hold the hbalock when calling this routine.
458  **/
459 static int
460 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
461                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
462 {
463         struct lpfc_rqe *temp_hrqe;
464         struct lpfc_rqe *temp_drqe;
465         struct lpfc_register doorbell;
466         int put_index;
467
468         /* sanity check on queue memory */
469         if (unlikely(!hq) || unlikely(!dq))
470                 return -ENOMEM;
471         put_index = hq->host_index;
472         temp_hrqe = hq->qe[hq->host_index].rqe;
473         temp_drqe = dq->qe[dq->host_index].rqe;
474
475         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
476                 return -EINVAL;
477         if (hq->host_index != dq->host_index)
478                 return -EINVAL;
479         /* If the host has not yet processed the next entry then we are done */
480         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
481                 return -EBUSY;
482         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
483         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
484
485         /* Update the host index to point to the next slot */
486         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
487         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
488
489         /* Ring The Header Receive Queue Doorbell */
490         if (!(hq->host_index % hq->entry_repost)) {
491                 doorbell.word0 = 0;
492                 if (hq->db_format == LPFC_DB_RING_FORMAT) {
493                         bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
494                                hq->entry_repost);
495                         bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
496                 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
497                         bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
498                                hq->entry_repost);
499                         bf_set(lpfc_rq_db_list_fm_index, &doorbell,
500                                hq->host_index);
501                         bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
502                 } else {
503                         return -EINVAL;
504                 }
505                 writel(doorbell.word0, hq->db_regaddr);
506         }
507         return put_index;
508 }
509
510 /**
511  * lpfc_sli4_rq_release - Updates internal hba index for RQ
512  * @q: The Header Receive Queue to operate on.
513  *
514  * This routine will update the HBA index of a queue to reflect consumption of
515  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
516  * consumed an entry the host calls this function to update the queue's
517  * internal pointers. This routine returns the number of entries that were
518  * consumed by the HBA.
519  **/
520 static uint32_t
521 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
522 {
523         /* sanity check on queue memory */
524         if (unlikely(!hq) || unlikely(!dq))
525                 return 0;
526
527         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
528                 return 0;
529         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
530         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
531         return 1;
532 }
533
534 /**
535  * lpfc_cmd_iocb - Get next command iocb entry in the ring
536  * @phba: Pointer to HBA context object.
537  * @pring: Pointer to driver SLI ring object.
538  *
539  * This function returns pointer to next command iocb entry
540  * in the command ring. The caller must hold hbalock to prevent
541  * other threads consume the next command iocb.
542  * SLI-2/SLI-3 provide different sized iocbs.
543  **/
544 static inline IOCB_t *
545 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
546 {
547         return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
548                            pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
549 }
550
551 /**
552  * lpfc_resp_iocb - Get next response iocb entry in the ring
553  * @phba: Pointer to HBA context object.
554  * @pring: Pointer to driver SLI ring object.
555  *
556  * This function returns pointer to next response iocb entry
557  * in the response ring. The caller must hold hbalock to make sure
558  * that no other thread consume the next response iocb.
559  * SLI-2/SLI-3 provide different sized iocbs.
560  **/
561 static inline IOCB_t *
562 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
563 {
564         return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
565                            pring->sli.sli3.rspidx * phba->iocb_rsp_size);
566 }
567
568 /**
569  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
570  * @phba: Pointer to HBA context object.
571  *
572  * This function is called with hbalock held. This function
573  * allocates a new driver iocb object from the iocb pool. If the
574  * allocation is successful, it returns pointer to the newly
575  * allocated iocb object else it returns NULL.
576  **/
577 struct lpfc_iocbq *
578 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
579 {
580         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
581         struct lpfc_iocbq * iocbq = NULL;
582
583         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
584         if (iocbq)
585                 phba->iocb_cnt++;
586         if (phba->iocb_cnt > phba->iocb_max)
587                 phba->iocb_max = phba->iocb_cnt;
588         return iocbq;
589 }
590
591 /**
592  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
593  * @phba: Pointer to HBA context object.
594  * @xritag: XRI value.
595  *
596  * This function clears the sglq pointer from the array of acive
597  * sglq's. The xritag that is passed in is used to index into the
598  * array. Before the xritag can be used it needs to be adjusted
599  * by subtracting the xribase.
600  *
601  * Returns sglq ponter = success, NULL = Failure.
602  **/
603 static struct lpfc_sglq *
604 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
605 {
606         struct lpfc_sglq *sglq;
607
608         sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
609         phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
610         return sglq;
611 }
612
613 /**
614  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
615  * @phba: Pointer to HBA context object.
616  * @xritag: XRI value.
617  *
618  * This function returns the sglq pointer from the array of acive
619  * sglq's. The xritag that is passed in is used to index into the
620  * array. Before the xritag can be used it needs to be adjusted
621  * by subtracting the xribase.
622  *
623  * Returns sglq ponter = success, NULL = Failure.
624  **/
625 struct lpfc_sglq *
626 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
627 {
628         struct lpfc_sglq *sglq;
629
630         sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
631         return sglq;
632 }
633
634 /**
635  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
636  * @phba: Pointer to HBA context object.
637  * @xritag: xri used in this exchange.
638  * @rrq: The RRQ to be cleared.
639  *
640  **/
641 void
642 lpfc_clr_rrq_active(struct lpfc_hba *phba,
643                     uint16_t xritag,
644                     struct lpfc_node_rrq *rrq)
645 {
646         struct lpfc_nodelist *ndlp = NULL;
647
648         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
649                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
650
651         /* The target DID could have been swapped (cable swap)
652          * we should use the ndlp from the findnode if it is
653          * available.
654          */
655         if ((!ndlp) && rrq->ndlp)
656                 ndlp = rrq->ndlp;
657
658         if (!ndlp)
659                 goto out;
660
661         if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
662                 rrq->send_rrq = 0;
663                 rrq->xritag = 0;
664                 rrq->rrq_stop_time = 0;
665         }
666 out:
667         mempool_free(rrq, phba->rrq_pool);
668 }
669
670 /**
671  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
672  * @phba: Pointer to HBA context object.
673  *
674  * This function is called with hbalock held. This function
675  * Checks if stop_time (ratov from setting rrq active) has
676  * been reached, if it has and the send_rrq flag is set then
677  * it will call lpfc_send_rrq. If the send_rrq flag is not set
678  * then it will just call the routine to clear the rrq and
679  * free the rrq resource.
680  * The timer is set to the next rrq that is going to expire before
681  * leaving the routine.
682  *
683  **/
684 void
685 lpfc_handle_rrq_active(struct lpfc_hba *phba)
686 {
687         struct lpfc_node_rrq *rrq;
688         struct lpfc_node_rrq *nextrrq;
689         unsigned long next_time;
690         unsigned long iflags;
691         LIST_HEAD(send_rrq);
692
693         spin_lock_irqsave(&phba->hbalock, iflags);
694         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
695         next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
696         list_for_each_entry_safe(rrq, nextrrq,
697                                  &phba->active_rrq_list, list) {
698                 if (time_after(jiffies, rrq->rrq_stop_time))
699                         list_move(&rrq->list, &send_rrq);
700                 else if (time_before(rrq->rrq_stop_time, next_time))
701                         next_time = rrq->rrq_stop_time;
702         }
703         spin_unlock_irqrestore(&phba->hbalock, iflags);
704         if ((!list_empty(&phba->active_rrq_list)) &&
705             (!(phba->pport->load_flag & FC_UNLOADING)))
706                 mod_timer(&phba->rrq_tmr, next_time);
707         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
708                 list_del(&rrq->list);
709                 if (!rrq->send_rrq)
710                         /* this call will free the rrq */
711                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
712                 else if (lpfc_send_rrq(phba, rrq)) {
713                         /* if we send the rrq then the completion handler
714                         *  will clear the bit in the xribitmap.
715                         */
716                         lpfc_clr_rrq_active(phba, rrq->xritag,
717                                             rrq);
718                 }
719         }
720 }
721
722 /**
723  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
724  * @vport: Pointer to vport context object.
725  * @xri: The xri used in the exchange.
726  * @did: The targets DID for this exchange.
727  *
728  * returns NULL = rrq not found in the phba->active_rrq_list.
729  *         rrq = rrq for this xri and target.
730  **/
731 struct lpfc_node_rrq *
732 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
733 {
734         struct lpfc_hba *phba = vport->phba;
735         struct lpfc_node_rrq *rrq;
736         struct lpfc_node_rrq *nextrrq;
737         unsigned long iflags;
738
739         if (phba->sli_rev != LPFC_SLI_REV4)
740                 return NULL;
741         spin_lock_irqsave(&phba->hbalock, iflags);
742         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
743                 if (rrq->vport == vport && rrq->xritag == xri &&
744                                 rrq->nlp_DID == did){
745                         list_del(&rrq->list);
746                         spin_unlock_irqrestore(&phba->hbalock, iflags);
747                         return rrq;
748                 }
749         }
750         spin_unlock_irqrestore(&phba->hbalock, iflags);
751         return NULL;
752 }
753
754 /**
755  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
756  * @vport: Pointer to vport context object.
757  * @ndlp: Pointer to the lpfc_node_list structure.
758  * If ndlp is NULL Remove all active RRQs for this vport from the
759  * phba->active_rrq_list and clear the rrq.
760  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
761  **/
762 void
763 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
764
765 {
766         struct lpfc_hba *phba = vport->phba;
767         struct lpfc_node_rrq *rrq;
768         struct lpfc_node_rrq *nextrrq;
769         unsigned long iflags;
770         LIST_HEAD(rrq_list);
771
772         if (phba->sli_rev != LPFC_SLI_REV4)
773                 return;
774         if (!ndlp) {
775                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
776                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
777         }
778         spin_lock_irqsave(&phba->hbalock, iflags);
779         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
780                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
781                         list_move(&rrq->list, &rrq_list);
782         spin_unlock_irqrestore(&phba->hbalock, iflags);
783
784         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
785                 list_del(&rrq->list);
786                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
787         }
788 }
789
790 /**
791  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
792  * @phba: Pointer to HBA context object.
793  * @ndlp: Targets nodelist pointer for this exchange.
794  * @xritag the xri in the bitmap to test.
795  *
796  * This function is called with hbalock held. This function
797  * returns 0 = rrq not active for this xri
798  *         1 = rrq is valid for this xri.
799  **/
800 int
801 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
802                         uint16_t  xritag)
803 {
804         if (!ndlp)
805                 return 0;
806         if (!ndlp->active_rrqs_xri_bitmap)
807                 return 0;
808         if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
809                         return 1;
810         else
811                 return 0;
812 }
813
814 /**
815  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
816  * @phba: Pointer to HBA context object.
817  * @ndlp: nodelist pointer for this target.
818  * @xritag: xri used in this exchange.
819  * @rxid: Remote Exchange ID.
820  * @send_rrq: Flag used to determine if we should send rrq els cmd.
821  *
822  * This function takes the hbalock.
823  * The active bit is always set in the active rrq xri_bitmap even
824  * if there is no slot avaiable for the other rrq information.
825  *
826  * returns 0 rrq actived for this xri
827  *         < 0 No memory or invalid ndlp.
828  **/
829 int
830 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
831                     uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
832 {
833         unsigned long iflags;
834         struct lpfc_node_rrq *rrq;
835         int empty;
836
837         if (!ndlp)
838                 return -EINVAL;
839
840         if (!phba->cfg_enable_rrq)
841                 return -EINVAL;
842
843         spin_lock_irqsave(&phba->hbalock, iflags);
844         if (phba->pport->load_flag & FC_UNLOADING) {
845                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
846                 goto out;
847         }
848
849         /*
850          * set the active bit even if there is no mem available.
851          */
852         if (NLP_CHK_FREE_REQ(ndlp))
853                 goto out;
854
855         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
856                 goto out;
857
858         if (!ndlp->active_rrqs_xri_bitmap)
859                 goto out;
860
861         if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
862                 goto out;
863
864         spin_unlock_irqrestore(&phba->hbalock, iflags);
865         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
866         if (!rrq) {
867                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
868                                 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
869                                 " DID:0x%x Send:%d\n",
870                                 xritag, rxid, ndlp->nlp_DID, send_rrq);
871                 return -EINVAL;
872         }
873         if (phba->cfg_enable_rrq == 1)
874                 rrq->send_rrq = send_rrq;
875         else
876                 rrq->send_rrq = 0;
877         rrq->xritag = xritag;
878         rrq->rrq_stop_time = jiffies +
879                                 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
880         rrq->ndlp = ndlp;
881         rrq->nlp_DID = ndlp->nlp_DID;
882         rrq->vport = ndlp->vport;
883         rrq->rxid = rxid;
884         spin_lock_irqsave(&phba->hbalock, iflags);
885         empty = list_empty(&phba->active_rrq_list);
886         list_add_tail(&rrq->list, &phba->active_rrq_list);
887         phba->hba_flag |= HBA_RRQ_ACTIVE;
888         if (empty)
889                 lpfc_worker_wake_up(phba);
890         spin_unlock_irqrestore(&phba->hbalock, iflags);
891         return 0;
892 out:
893         spin_unlock_irqrestore(&phba->hbalock, iflags);
894         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
895                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
896                         " DID:0x%x Send:%d\n",
897                         xritag, rxid, ndlp->nlp_DID, send_rrq);
898         return -EINVAL;
899 }
900
901 /**
902  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
903  * @phba: Pointer to HBA context object.
904  * @piocb: Pointer to the iocbq.
905  *
906  * This function is called with the ring lock held. This function
907  * gets a new driver sglq object from the sglq list. If the
908  * list is not empty then it is successful, it returns pointer to the newly
909  * allocated sglq object else it returns NULL.
910  **/
911 static struct lpfc_sglq *
912 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
913 {
914         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
915         struct lpfc_sglq *sglq = NULL;
916         struct lpfc_sglq *start_sglq = NULL;
917         struct lpfc_scsi_buf *lpfc_cmd;
918         struct lpfc_nodelist *ndlp;
919         int found = 0;
920
921         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
922                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
923                 ndlp = lpfc_cmd->rdata->pnode;
924         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
925                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
926                 ndlp = piocbq->context_un.ndlp;
927         } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
928                 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
929                         ndlp = NULL;
930                 else
931                         ndlp = piocbq->context_un.ndlp;
932         } else {
933                 ndlp = piocbq->context1;
934         }
935
936         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
937         start_sglq = sglq;
938         while (!found) {
939                 if (!sglq)
940                         return NULL;
941                 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
942                         /* This xri has an rrq outstanding for this DID.
943                          * put it back in the list and get another xri.
944                          */
945                         list_add_tail(&sglq->list, lpfc_sgl_list);
946                         sglq = NULL;
947                         list_remove_head(lpfc_sgl_list, sglq,
948                                                 struct lpfc_sglq, list);
949                         if (sglq == start_sglq) {
950                                 sglq = NULL;
951                                 break;
952                         } else
953                                 continue;
954                 }
955                 sglq->ndlp = ndlp;
956                 found = 1;
957                 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
958                 sglq->state = SGL_ALLOCATED;
959         }
960         return sglq;
961 }
962
963 /**
964  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
965  * @phba: Pointer to HBA context object.
966  *
967  * This function is called with no lock held. This function
968  * allocates a new driver iocb object from the iocb pool. If the
969  * allocation is successful, it returns pointer to the newly
970  * allocated iocb object else it returns NULL.
971  **/
972 struct lpfc_iocbq *
973 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
974 {
975         struct lpfc_iocbq * iocbq = NULL;
976         unsigned long iflags;
977
978         spin_lock_irqsave(&phba->hbalock, iflags);
979         iocbq = __lpfc_sli_get_iocbq(phba);
980         spin_unlock_irqrestore(&phba->hbalock, iflags);
981         return iocbq;
982 }
983
984 /**
985  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
986  * @phba: Pointer to HBA context object.
987  * @iocbq: Pointer to driver iocb object.
988  *
989  * This function is called with hbalock held to release driver
990  * iocb object to the iocb pool. The iotag in the iocb object
991  * does not change for each use of the iocb object. This function
992  * clears all other fields of the iocb object when it is freed.
993  * The sqlq structure that holds the xritag and phys and virtual
994  * mappings for the scatter gather list is retrieved from the
995  * active array of sglq. The get of the sglq pointer also clears
996  * the entry in the array. If the status of the IO indiactes that
997  * this IO was aborted then the sglq entry it put on the
998  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
999  * IO has good status or fails for any other reason then the sglq
1000  * entry is added to the free list (lpfc_sgl_list).
1001  **/
1002 static void
1003 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1004 {
1005         struct lpfc_sglq *sglq;
1006         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1007         unsigned long iflag = 0;
1008         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
1009
1010         if (iocbq->sli4_xritag == NO_XRI)
1011                 sglq = NULL;
1012         else
1013                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1014
1015
1016         if (sglq)  {
1017                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1018                         (sglq->state != SGL_XRI_ABORTED)) {
1019                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1020                                         iflag);
1021                         list_add(&sglq->list,
1022                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1023                         spin_unlock_irqrestore(
1024                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
1025                 } else {
1026                         spin_lock_irqsave(&pring->ring_lock, iflag);
1027                         sglq->state = SGL_FREED;
1028                         sglq->ndlp = NULL;
1029                         list_add_tail(&sglq->list,
1030                                 &phba->sli4_hba.lpfc_sgl_list);
1031                         spin_unlock_irqrestore(&pring->ring_lock, iflag);
1032
1033                         /* Check if TXQ queue needs to be serviced */
1034                         if (!list_empty(&pring->txq))
1035                                 lpfc_worker_wake_up(phba);
1036                 }
1037         }
1038
1039
1040         /*
1041          * Clean all volatile data fields, preserve iotag and node struct.
1042          */
1043         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1044         iocbq->sli4_lxritag = NO_XRI;
1045         iocbq->sli4_xritag = NO_XRI;
1046         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1047 }
1048
1049
1050 /**
1051  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1052  * @phba: Pointer to HBA context object.
1053  * @iocbq: Pointer to driver iocb object.
1054  *
1055  * This function is called with hbalock held to release driver
1056  * iocb object to the iocb pool. The iotag in the iocb object
1057  * does not change for each use of the iocb object. This function
1058  * clears all other fields of the iocb object when it is freed.
1059  **/
1060 static void
1061 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1062 {
1063         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1064
1065
1066         /*
1067          * Clean all volatile data fields, preserve iotag and node struct.
1068          */
1069         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1070         iocbq->sli4_xritag = NO_XRI;
1071         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1072 }
1073
1074 /**
1075  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1076  * @phba: Pointer to HBA context object.
1077  * @iocbq: Pointer to driver iocb object.
1078  *
1079  * This function is called with hbalock held to release driver
1080  * iocb object to the iocb pool. The iotag in the iocb object
1081  * does not change for each use of the iocb object. This function
1082  * clears all other fields of the iocb object when it is freed.
1083  **/
1084 static void
1085 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1086 {
1087         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1088         phba->iocb_cnt--;
1089 }
1090
1091 /**
1092  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1093  * @phba: Pointer to HBA context object.
1094  * @iocbq: Pointer to driver iocb object.
1095  *
1096  * This function is called with no lock held to release the iocb to
1097  * iocb pool.
1098  **/
1099 void
1100 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1101 {
1102         unsigned long iflags;
1103
1104         /*
1105          * Clean all volatile data fields, preserve iotag and node struct.
1106          */
1107         spin_lock_irqsave(&phba->hbalock, iflags);
1108         __lpfc_sli_release_iocbq(phba, iocbq);
1109         spin_unlock_irqrestore(&phba->hbalock, iflags);
1110 }
1111
1112 /**
1113  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1114  * @phba: Pointer to HBA context object.
1115  * @iocblist: List of IOCBs.
1116  * @ulpstatus: ULP status in IOCB command field.
1117  * @ulpWord4: ULP word-4 in IOCB command field.
1118  *
1119  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1120  * on the list by invoking the complete callback function associated with the
1121  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1122  * fields.
1123  **/
1124 void
1125 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1126                       uint32_t ulpstatus, uint32_t ulpWord4)
1127 {
1128         struct lpfc_iocbq *piocb;
1129
1130         while (!list_empty(iocblist)) {
1131                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1132                 if (!piocb->iocb_cmpl)
1133                         lpfc_sli_release_iocbq(phba, piocb);
1134                 else {
1135                         piocb->iocb.ulpStatus = ulpstatus;
1136                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1137                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1138                 }
1139         }
1140         return;
1141 }
1142
1143 /**
1144  * lpfc_sli_iocb_cmd_type - Get the iocb type
1145  * @iocb_cmnd: iocb command code.
1146  *
1147  * This function is called by ring event handler function to get the iocb type.
1148  * This function translates the iocb command to an iocb command type used to
1149  * decide the final disposition of each completed IOCB.
1150  * The function returns
1151  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1152  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1153  * LPFC_ABORT_IOCB   if it is an abort iocb
1154  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1155  *
1156  * The caller is not required to hold any lock.
1157  **/
1158 static lpfc_iocb_type
1159 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1160 {
1161         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1162
1163         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1164                 return 0;
1165
1166         switch (iocb_cmnd) {
1167         case CMD_XMIT_SEQUENCE_CR:
1168         case CMD_XMIT_SEQUENCE_CX:
1169         case CMD_XMIT_BCAST_CN:
1170         case CMD_XMIT_BCAST_CX:
1171         case CMD_ELS_REQUEST_CR:
1172         case CMD_ELS_REQUEST_CX:
1173         case CMD_CREATE_XRI_CR:
1174         case CMD_CREATE_XRI_CX:
1175         case CMD_GET_RPI_CN:
1176         case CMD_XMIT_ELS_RSP_CX:
1177         case CMD_GET_RPI_CR:
1178         case CMD_FCP_IWRITE_CR:
1179         case CMD_FCP_IWRITE_CX:
1180         case CMD_FCP_IREAD_CR:
1181         case CMD_FCP_IREAD_CX:
1182         case CMD_FCP_ICMND_CR:
1183         case CMD_FCP_ICMND_CX:
1184         case CMD_FCP_TSEND_CX:
1185         case CMD_FCP_TRSP_CX:
1186         case CMD_FCP_TRECEIVE_CX:
1187         case CMD_FCP_AUTO_TRSP_CX:
1188         case CMD_ADAPTER_MSG:
1189         case CMD_ADAPTER_DUMP:
1190         case CMD_XMIT_SEQUENCE64_CR:
1191         case CMD_XMIT_SEQUENCE64_CX:
1192         case CMD_XMIT_BCAST64_CN:
1193         case CMD_XMIT_BCAST64_CX:
1194         case CMD_ELS_REQUEST64_CR:
1195         case CMD_ELS_REQUEST64_CX:
1196         case CMD_FCP_IWRITE64_CR:
1197         case CMD_FCP_IWRITE64_CX:
1198         case CMD_FCP_IREAD64_CR:
1199         case CMD_FCP_IREAD64_CX:
1200         case CMD_FCP_ICMND64_CR:
1201         case CMD_FCP_ICMND64_CX:
1202         case CMD_FCP_TSEND64_CX:
1203         case CMD_FCP_TRSP64_CX:
1204         case CMD_FCP_TRECEIVE64_CX:
1205         case CMD_GEN_REQUEST64_CR:
1206         case CMD_GEN_REQUEST64_CX:
1207         case CMD_XMIT_ELS_RSP64_CX:
1208         case DSSCMD_IWRITE64_CR:
1209         case DSSCMD_IWRITE64_CX:
1210         case DSSCMD_IREAD64_CR:
1211         case DSSCMD_IREAD64_CX:
1212                 type = LPFC_SOL_IOCB;
1213                 break;
1214         case CMD_ABORT_XRI_CN:
1215         case CMD_ABORT_XRI_CX:
1216         case CMD_CLOSE_XRI_CN:
1217         case CMD_CLOSE_XRI_CX:
1218         case CMD_XRI_ABORTED_CX:
1219         case CMD_ABORT_MXRI64_CN:
1220         case CMD_XMIT_BLS_RSP64_CX:
1221                 type = LPFC_ABORT_IOCB;
1222                 break;
1223         case CMD_RCV_SEQUENCE_CX:
1224         case CMD_RCV_ELS_REQ_CX:
1225         case CMD_RCV_SEQUENCE64_CX:
1226         case CMD_RCV_ELS_REQ64_CX:
1227         case CMD_ASYNC_STATUS:
1228         case CMD_IOCB_RCV_SEQ64_CX:
1229         case CMD_IOCB_RCV_ELS64_CX:
1230         case CMD_IOCB_RCV_CONT64_CX:
1231         case CMD_IOCB_RET_XRI64_CX:
1232                 type = LPFC_UNSOL_IOCB;
1233                 break;
1234         case CMD_IOCB_XMIT_MSEQ64_CR:
1235         case CMD_IOCB_XMIT_MSEQ64_CX:
1236         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1237         case CMD_IOCB_RCV_ELS_LIST64_CX:
1238         case CMD_IOCB_CLOSE_EXTENDED_CN:
1239         case CMD_IOCB_ABORT_EXTENDED_CN:
1240         case CMD_IOCB_RET_HBQE64_CN:
1241         case CMD_IOCB_FCP_IBIDIR64_CR:
1242         case CMD_IOCB_FCP_IBIDIR64_CX:
1243         case CMD_IOCB_FCP_ITASKMGT64_CX:
1244         case CMD_IOCB_LOGENTRY_CN:
1245         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1246                 printk("%s - Unhandled SLI-3 Command x%x\n",
1247                                 __func__, iocb_cmnd);
1248                 type = LPFC_UNKNOWN_IOCB;
1249                 break;
1250         default:
1251                 type = LPFC_UNKNOWN_IOCB;
1252                 break;
1253         }
1254
1255         return type;
1256 }
1257
1258 /**
1259  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1260  * @phba: Pointer to HBA context object.
1261  *
1262  * This function is called from SLI initialization code
1263  * to configure every ring of the HBA's SLI interface. The
1264  * caller is not required to hold any lock. This function issues
1265  * a config_ring mailbox command for each ring.
1266  * This function returns zero if successful else returns a negative
1267  * error code.
1268  **/
1269 static int
1270 lpfc_sli_ring_map(struct lpfc_hba *phba)
1271 {
1272         struct lpfc_sli *psli = &phba->sli;
1273         LPFC_MBOXQ_t *pmb;
1274         MAILBOX_t *pmbox;
1275         int i, rc, ret = 0;
1276
1277         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1278         if (!pmb)
1279                 return -ENOMEM;
1280         pmbox = &pmb->u.mb;
1281         phba->link_state = LPFC_INIT_MBX_CMDS;
1282         for (i = 0; i < psli->num_rings; i++) {
1283                 lpfc_config_ring(phba, i, pmb);
1284                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1285                 if (rc != MBX_SUCCESS) {
1286                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1287                                         "0446 Adapter failed to init (%d), "
1288                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1289                                         "ring %d\n",
1290                                         rc, pmbox->mbxCommand,
1291                                         pmbox->mbxStatus, i);
1292                         phba->link_state = LPFC_HBA_ERROR;
1293                         ret = -ENXIO;
1294                         break;
1295                 }
1296         }
1297         mempool_free(pmb, phba->mbox_mem_pool);
1298         return ret;
1299 }
1300
1301 /**
1302  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1303  * @phba: Pointer to HBA context object.
1304  * @pring: Pointer to driver SLI ring object.
1305  * @piocb: Pointer to the driver iocb object.
1306  *
1307  * This function is called with hbalock held. The function adds the
1308  * new iocb to txcmplq of the given ring. This function always returns
1309  * 0. If this function is called for ELS ring, this function checks if
1310  * there is a vport associated with the ELS command. This function also
1311  * starts els_tmofunc timer if this is an ELS command.
1312  **/
1313 static int
1314 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1315                         struct lpfc_iocbq *piocb)
1316 {
1317         list_add_tail(&piocb->list, &pring->txcmplq);
1318         piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1319
1320         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1321            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1322            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN) &&
1323          (!(piocb->vport->load_flag & FC_UNLOADING))) {
1324                 if (!piocb->vport)
1325                         BUG();
1326                 else
1327                         mod_timer(&piocb->vport->els_tmofunc,
1328                                 jiffies +
1329                                 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1330         }
1331
1332
1333         return 0;
1334 }
1335
1336 /**
1337  * lpfc_sli_ringtx_get - Get first element of the txq
1338  * @phba: Pointer to HBA context object.
1339  * @pring: Pointer to driver SLI ring object.
1340  *
1341  * This function is called with hbalock held to get next
1342  * iocb in txq of the given ring. If there is any iocb in
1343  * the txq, the function returns first iocb in the list after
1344  * removing the iocb from the list, else it returns NULL.
1345  **/
1346 struct lpfc_iocbq *
1347 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1348 {
1349         struct lpfc_iocbq *cmd_iocb;
1350
1351         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1352         return cmd_iocb;
1353 }
1354
1355 /**
1356  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1357  * @phba: Pointer to HBA context object.
1358  * @pring: Pointer to driver SLI ring object.
1359  *
1360  * This function is called with hbalock held and the caller must post the
1361  * iocb without releasing the lock. If the caller releases the lock,
1362  * iocb slot returned by the function is not guaranteed to be available.
1363  * The function returns pointer to the next available iocb slot if there
1364  * is available slot in the ring, else it returns NULL.
1365  * If the get index of the ring is ahead of the put index, the function
1366  * will post an error attention event to the worker thread to take the
1367  * HBA to offline state.
1368  **/
1369 static IOCB_t *
1370 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1371 {
1372         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1373         uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1374         if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1375            (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1376                 pring->sli.sli3.next_cmdidx = 0;
1377
1378         if (unlikely(pring->sli.sli3.local_getidx ==
1379                 pring->sli.sli3.next_cmdidx)) {
1380
1381                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1382
1383                 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1384                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1385                                         "0315 Ring %d issue: portCmdGet %d "
1386                                         "is bigger than cmd ring %d\n",
1387                                         pring->ringno,
1388                                         pring->sli.sli3.local_getidx,
1389                                         max_cmd_idx);
1390
1391                         phba->link_state = LPFC_HBA_ERROR;
1392                         /*
1393                          * All error attention handlers are posted to
1394                          * worker thread
1395                          */
1396                         phba->work_ha |= HA_ERATT;
1397                         phba->work_hs = HS_FFER3;
1398
1399                         lpfc_worker_wake_up(phba);
1400
1401                         return NULL;
1402                 }
1403
1404                 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1405                         return NULL;
1406         }
1407
1408         return lpfc_cmd_iocb(phba, pring);
1409 }
1410
1411 /**
1412  * lpfc_sli_next_iotag - Get an iotag for the iocb
1413  * @phba: Pointer to HBA context object.
1414  * @iocbq: Pointer to driver iocb object.
1415  *
1416  * This function gets an iotag for the iocb. If there is no unused iotag and
1417  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1418  * array and assigns a new iotag.
1419  * The function returns the allocated iotag if successful, else returns zero.
1420  * Zero is not a valid iotag.
1421  * The caller is not required to hold any lock.
1422  **/
1423 uint16_t
1424 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1425 {
1426         struct lpfc_iocbq **new_arr;
1427         struct lpfc_iocbq **old_arr;
1428         size_t new_len;
1429         struct lpfc_sli *psli = &phba->sli;
1430         uint16_t iotag;
1431
1432         spin_lock_irq(&phba->hbalock);
1433         iotag = psli->last_iotag;
1434         if(++iotag < psli->iocbq_lookup_len) {
1435                 psli->last_iotag = iotag;
1436                 psli->iocbq_lookup[iotag] = iocbq;
1437                 spin_unlock_irq(&phba->hbalock);
1438                 iocbq->iotag = iotag;
1439                 return iotag;
1440         } else if (psli->iocbq_lookup_len < (0xffff
1441                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1442                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1443                 spin_unlock_irq(&phba->hbalock);
1444                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1445                                   GFP_KERNEL);
1446                 if (new_arr) {
1447                         spin_lock_irq(&phba->hbalock);
1448                         old_arr = psli->iocbq_lookup;
1449                         if (new_len <= psli->iocbq_lookup_len) {
1450                                 /* highly unprobable case */
1451                                 kfree(new_arr);
1452                                 iotag = psli->last_iotag;
1453                                 if(++iotag < psli->iocbq_lookup_len) {
1454                                         psli->last_iotag = iotag;
1455                                         psli->iocbq_lookup[iotag] = iocbq;
1456                                         spin_unlock_irq(&phba->hbalock);
1457                                         iocbq->iotag = iotag;
1458                                         return iotag;
1459                                 }
1460                                 spin_unlock_irq(&phba->hbalock);
1461                                 return 0;
1462                         }
1463                         if (psli->iocbq_lookup)
1464                                 memcpy(new_arr, old_arr,
1465                                        ((psli->last_iotag  + 1) *
1466                                         sizeof (struct lpfc_iocbq *)));
1467                         psli->iocbq_lookup = new_arr;
1468                         psli->iocbq_lookup_len = new_len;
1469                         psli->last_iotag = iotag;
1470                         psli->iocbq_lookup[iotag] = iocbq;
1471                         spin_unlock_irq(&phba->hbalock);
1472                         iocbq->iotag = iotag;
1473                         kfree(old_arr);
1474                         return iotag;
1475                 }
1476         } else
1477                 spin_unlock_irq(&phba->hbalock);
1478
1479         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1480                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1481                         psli->last_iotag);
1482
1483         return 0;
1484 }
1485
1486 /**
1487  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1488  * @phba: Pointer to HBA context object.
1489  * @pring: Pointer to driver SLI ring object.
1490  * @iocb: Pointer to iocb slot in the ring.
1491  * @nextiocb: Pointer to driver iocb object which need to be
1492  *            posted to firmware.
1493  *
1494  * This function is called with hbalock held to post a new iocb to
1495  * the firmware. This function copies the new iocb to ring iocb slot and
1496  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1497  * a completion call back for this iocb else the function will free the
1498  * iocb object.
1499  **/
1500 static void
1501 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1502                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1503 {
1504         /*
1505          * Set up an iotag
1506          */
1507         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1508
1509
1510         if (pring->ringno == LPFC_ELS_RING) {
1511                 lpfc_debugfs_slow_ring_trc(phba,
1512                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1513                         *(((uint32_t *) &nextiocb->iocb) + 4),
1514                         *(((uint32_t *) &nextiocb->iocb) + 6),
1515                         *(((uint32_t *) &nextiocb->iocb) + 7));
1516         }
1517
1518         /*
1519          * Issue iocb command to adapter
1520          */
1521         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1522         wmb();
1523         pring->stats.iocb_cmd++;
1524
1525         /*
1526          * If there is no completion routine to call, we can release the
1527          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1528          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1529          */
1530         if (nextiocb->iocb_cmpl)
1531                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1532         else
1533                 __lpfc_sli_release_iocbq(phba, nextiocb);
1534
1535         /*
1536          * Let the HBA know what IOCB slot will be the next one the
1537          * driver will put a command into.
1538          */
1539         pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1540         writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1541 }
1542
1543 /**
1544  * lpfc_sli_update_full_ring - Update the chip attention register
1545  * @phba: Pointer to HBA context object.
1546  * @pring: Pointer to driver SLI ring object.
1547  *
1548  * The caller is not required to hold any lock for calling this function.
1549  * This function updates the chip attention bits for the ring to inform firmware
1550  * that there are pending work to be done for this ring and requests an
1551  * interrupt when there is space available in the ring. This function is
1552  * called when the driver is unable to post more iocbs to the ring due
1553  * to unavailability of space in the ring.
1554  **/
1555 static void
1556 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1557 {
1558         int ringno = pring->ringno;
1559
1560         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1561
1562         wmb();
1563
1564         /*
1565          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1566          * The HBA will tell us when an IOCB entry is available.
1567          */
1568         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1569         readl(phba->CAregaddr); /* flush */
1570
1571         pring->stats.iocb_cmd_full++;
1572 }
1573
1574 /**
1575  * lpfc_sli_update_ring - Update chip attention register
1576  * @phba: Pointer to HBA context object.
1577  * @pring: Pointer to driver SLI ring object.
1578  *
1579  * This function updates the chip attention register bit for the
1580  * given ring to inform HBA that there is more work to be done
1581  * in this ring. The caller is not required to hold any lock.
1582  **/
1583 static void
1584 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1585 {
1586         int ringno = pring->ringno;
1587
1588         /*
1589          * Tell the HBA that there is work to do in this ring.
1590          */
1591         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1592                 wmb();
1593                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1594                 readl(phba->CAregaddr); /* flush */
1595         }
1596 }
1597
1598 /**
1599  * lpfc_sli_resume_iocb - Process iocbs in the txq
1600  * @phba: Pointer to HBA context object.
1601  * @pring: Pointer to driver SLI ring object.
1602  *
1603  * This function is called with hbalock held to post pending iocbs
1604  * in the txq to the firmware. This function is called when driver
1605  * detects space available in the ring.
1606  **/
1607 static void
1608 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1609 {
1610         IOCB_t *iocb;
1611         struct lpfc_iocbq *nextiocb;
1612
1613         /*
1614          * Check to see if:
1615          *  (a) there is anything on the txq to send
1616          *  (b) link is up
1617          *  (c) link attention events can be processed (fcp ring only)
1618          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1619          */
1620
1621         if (lpfc_is_link_up(phba) &&
1622             (!list_empty(&pring->txq)) &&
1623             (pring->ringno != phba->sli.fcp_ring ||
1624              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1625
1626                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1627                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1628                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1629
1630                 if (iocb)
1631                         lpfc_sli_update_ring(phba, pring);
1632                 else
1633                         lpfc_sli_update_full_ring(phba, pring);
1634         }
1635
1636         return;
1637 }
1638
1639 /**
1640  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1641  * @phba: Pointer to HBA context object.
1642  * @hbqno: HBQ number.
1643  *
1644  * This function is called with hbalock held to get the next
1645  * available slot for the given HBQ. If there is free slot
1646  * available for the HBQ it will return pointer to the next available
1647  * HBQ entry else it will return NULL.
1648  **/
1649 static struct lpfc_hbq_entry *
1650 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1651 {
1652         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1653
1654         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1655             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1656                 hbqp->next_hbqPutIdx = 0;
1657
1658         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1659                 uint32_t raw_index = phba->hbq_get[hbqno];
1660                 uint32_t getidx = le32_to_cpu(raw_index);
1661
1662                 hbqp->local_hbqGetIdx = getidx;
1663
1664                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1665                         lpfc_printf_log(phba, KERN_ERR,
1666                                         LOG_SLI | LOG_VPORT,
1667                                         "1802 HBQ %d: local_hbqGetIdx "
1668                                         "%u is > than hbqp->entry_count %u\n",
1669                                         hbqno, hbqp->local_hbqGetIdx,
1670                                         hbqp->entry_count);
1671
1672                         phba->link_state = LPFC_HBA_ERROR;
1673                         return NULL;
1674                 }
1675
1676                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1677                         return NULL;
1678         }
1679
1680         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1681                         hbqp->hbqPutIdx;
1682 }
1683
1684 /**
1685  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1686  * @phba: Pointer to HBA context object.
1687  *
1688  * This function is called with no lock held to free all the
1689  * hbq buffers while uninitializing the SLI interface. It also
1690  * frees the HBQ buffers returned by the firmware but not yet
1691  * processed by the upper layers.
1692  **/
1693 void
1694 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1695 {
1696         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1697         struct hbq_dmabuf *hbq_buf;
1698         unsigned long flags;
1699         int i, hbq_count;
1700         uint32_t hbqno;
1701
1702         hbq_count = lpfc_sli_hbq_count();
1703         /* Return all memory used by all HBQs */
1704         spin_lock_irqsave(&phba->hbalock, flags);
1705         for (i = 0; i < hbq_count; ++i) {
1706                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1707                                 &phba->hbqs[i].hbq_buffer_list, list) {
1708                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1709                         list_del(&hbq_buf->dbuf.list);
1710                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1711                 }
1712                 phba->hbqs[i].buffer_count = 0;
1713         }
1714         /* Return all HBQ buffer that are in-fly */
1715         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1716                                  list) {
1717                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1718                 list_del(&hbq_buf->dbuf.list);
1719                 if (hbq_buf->tag == -1) {
1720                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1721                                 (phba, hbq_buf);
1722                 } else {
1723                         hbqno = hbq_buf->tag >> 16;
1724                         if (hbqno >= LPFC_MAX_HBQS)
1725                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1726                                         (phba, hbq_buf);
1727                         else
1728                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1729                                         hbq_buf);
1730                 }
1731         }
1732
1733         /* Mark the HBQs not in use */
1734         phba->hbq_in_use = 0;
1735         spin_unlock_irqrestore(&phba->hbalock, flags);
1736 }
1737
1738 /**
1739  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1740  * @phba: Pointer to HBA context object.
1741  * @hbqno: HBQ number.
1742  * @hbq_buf: Pointer to HBQ buffer.
1743  *
1744  * This function is called with the hbalock held to post a
1745  * hbq buffer to the firmware. If the function finds an empty
1746  * slot in the HBQ, it will post the buffer. The function will return
1747  * pointer to the hbq entry if it successfully post the buffer
1748  * else it will return NULL.
1749  **/
1750 static int
1751 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1752                          struct hbq_dmabuf *hbq_buf)
1753 {
1754         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1755 }
1756
1757 /**
1758  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1759  * @phba: Pointer to HBA context object.
1760  * @hbqno: HBQ number.
1761  * @hbq_buf: Pointer to HBQ buffer.
1762  *
1763  * This function is called with the hbalock held to post a hbq buffer to the
1764  * firmware. If the function finds an empty slot in the HBQ, it will post the
1765  * buffer and place it on the hbq_buffer_list. The function will return zero if
1766  * it successfully post the buffer else it will return an error.
1767  **/
1768 static int
1769 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1770                             struct hbq_dmabuf *hbq_buf)
1771 {
1772         struct lpfc_hbq_entry *hbqe;
1773         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1774
1775         /* Get next HBQ entry slot to use */
1776         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1777         if (hbqe) {
1778                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1779
1780                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1781                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1782                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1783                 hbqe->bde.tus.f.bdeFlags = 0;
1784                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1785                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1786                                 /* Sync SLIM */
1787                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1788                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1789                                 /* flush */
1790                 readl(phba->hbq_put + hbqno);
1791                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1792                 return 0;
1793         } else
1794                 return -ENOMEM;
1795 }
1796
1797 /**
1798  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1799  * @phba: Pointer to HBA context object.
1800  * @hbqno: HBQ number.
1801  * @hbq_buf: Pointer to HBQ buffer.
1802  *
1803  * This function is called with the hbalock held to post an RQE to the SLI4
1804  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1805  * the hbq_buffer_list and return zero, otherwise it will return an error.
1806  **/
1807 static int
1808 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1809                             struct hbq_dmabuf *hbq_buf)
1810 {
1811         int rc;
1812         struct lpfc_rqe hrqe;
1813         struct lpfc_rqe drqe;
1814
1815         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1816         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1817         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1818         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1819         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1820                               &hrqe, &drqe);
1821         if (rc < 0)
1822                 return rc;
1823         hbq_buf->tag = rc;
1824         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1825         return 0;
1826 }
1827
1828 /* HBQ for ELS and CT traffic. */
1829 static struct lpfc_hbq_init lpfc_els_hbq = {
1830         .rn = 1,
1831         .entry_count = 256,
1832         .mask_count = 0,
1833         .profile = 0,
1834         .ring_mask = (1 << LPFC_ELS_RING),
1835         .buffer_count = 0,
1836         .init_count = 40,
1837         .add_count = 40,
1838 };
1839
1840 /* HBQ for the extra ring if needed */
1841 static struct lpfc_hbq_init lpfc_extra_hbq = {
1842         .rn = 1,
1843         .entry_count = 200,
1844         .mask_count = 0,
1845         .profile = 0,
1846         .ring_mask = (1 << LPFC_EXTRA_RING),
1847         .buffer_count = 0,
1848         .init_count = 0,
1849         .add_count = 5,
1850 };
1851
1852 /* Array of HBQs */
1853 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1854         &lpfc_els_hbq,
1855         &lpfc_extra_hbq,
1856 };
1857
1858 /**
1859  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1860  * @phba: Pointer to HBA context object.
1861  * @hbqno: HBQ number.
1862  * @count: Number of HBQ buffers to be posted.
1863  *
1864  * This function is called with no lock held to post more hbq buffers to the
1865  * given HBQ. The function returns the number of HBQ buffers successfully
1866  * posted.
1867  **/
1868 static int
1869 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1870 {
1871         uint32_t i, posted = 0;
1872         unsigned long flags;
1873         struct hbq_dmabuf *hbq_buffer;
1874         LIST_HEAD(hbq_buf_list);
1875         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1876                 return 0;
1877
1878         if ((phba->hbqs[hbqno].buffer_count + count) >
1879             lpfc_hbq_defs[hbqno]->entry_count)
1880                 count = lpfc_hbq_defs[hbqno]->entry_count -
1881                                         phba->hbqs[hbqno].buffer_count;
1882         if (!count)
1883                 return 0;
1884         /* Allocate HBQ entries */
1885         for (i = 0; i < count; i++) {
1886                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1887                 if (!hbq_buffer)
1888                         break;
1889                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1890         }
1891         /* Check whether HBQ is still in use */
1892         spin_lock_irqsave(&phba->hbalock, flags);
1893         if (!phba->hbq_in_use)
1894                 goto err;
1895         while (!list_empty(&hbq_buf_list)) {
1896                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1897                                  dbuf.list);
1898                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1899                                       (hbqno << 16));
1900                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1901                         phba->hbqs[hbqno].buffer_count++;
1902                         posted++;
1903                 } else
1904                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1905         }
1906         spin_unlock_irqrestore(&phba->hbalock, flags);
1907         return posted;
1908 err:
1909         spin_unlock_irqrestore(&phba->hbalock, flags);
1910         while (!list_empty(&hbq_buf_list)) {
1911                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1912                                  dbuf.list);
1913                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1914         }
1915         return 0;
1916 }
1917
1918 /**
1919  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1920  * @phba: Pointer to HBA context object.
1921  * @qno: HBQ number.
1922  *
1923  * This function posts more buffers to the HBQ. This function
1924  * is called with no lock held. The function returns the number of HBQ entries
1925  * successfully allocated.
1926  **/
1927 int
1928 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1929 {
1930         if (phba->sli_rev == LPFC_SLI_REV4)
1931                 return 0;
1932         else
1933                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1934                                          lpfc_hbq_defs[qno]->add_count);
1935 }
1936
1937 /**
1938  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1939  * @phba: Pointer to HBA context object.
1940  * @qno:  HBQ queue number.
1941  *
1942  * This function is called from SLI initialization code path with
1943  * no lock held to post initial HBQ buffers to firmware. The
1944  * function returns the number of HBQ entries successfully allocated.
1945  **/
1946 static int
1947 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1948 {
1949         if (phba->sli_rev == LPFC_SLI_REV4)
1950                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1951                                         lpfc_hbq_defs[qno]->entry_count);
1952         else
1953                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1954                                          lpfc_hbq_defs[qno]->init_count);
1955 }
1956
1957 /**
1958  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1959  * @phba: Pointer to HBA context object.
1960  * @hbqno: HBQ number.
1961  *
1962  * This function removes the first hbq buffer on an hbq list and returns a
1963  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1964  **/
1965 static struct hbq_dmabuf *
1966 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1967 {
1968         struct lpfc_dmabuf *d_buf;
1969
1970         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1971         if (!d_buf)
1972                 return NULL;
1973         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1974 }
1975
1976 /**
1977  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1978  * @phba: Pointer to HBA context object.
1979  * @tag: Tag of the hbq buffer.
1980  *
1981  * This function is called with hbalock held. This function searches
1982  * for the hbq buffer associated with the given tag in the hbq buffer
1983  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1984  * it returns NULL.
1985  **/
1986 static struct hbq_dmabuf *
1987 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1988 {
1989         struct lpfc_dmabuf *d_buf;
1990         struct hbq_dmabuf *hbq_buf;
1991         uint32_t hbqno;
1992
1993         hbqno = tag >> 16;
1994         if (hbqno >= LPFC_MAX_HBQS)
1995                 return NULL;
1996
1997         spin_lock_irq(&phba->hbalock);
1998         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1999                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2000                 if (hbq_buf->tag == tag) {
2001                         spin_unlock_irq(&phba->hbalock);
2002                         return hbq_buf;
2003                 }
2004         }
2005         spin_unlock_irq(&phba->hbalock);
2006         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2007                         "1803 Bad hbq tag. Data: x%x x%x\n",
2008                         tag, phba->hbqs[tag >> 16].buffer_count);
2009         return NULL;
2010 }
2011
2012 /**
2013  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2014  * @phba: Pointer to HBA context object.
2015  * @hbq_buffer: Pointer to HBQ buffer.
2016  *
2017  * This function is called with hbalock. This function gives back
2018  * the hbq buffer to firmware. If the HBQ does not have space to
2019  * post the buffer, it will free the buffer.
2020  **/
2021 void
2022 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2023 {
2024         uint32_t hbqno;
2025
2026         if (hbq_buffer) {
2027                 hbqno = hbq_buffer->tag >> 16;
2028                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2029                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2030         }
2031 }
2032
2033 /**
2034  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2035  * @mbxCommand: mailbox command code.
2036  *
2037  * This function is called by the mailbox event handler function to verify
2038  * that the completed mailbox command is a legitimate mailbox command. If the
2039  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2040  * and the mailbox event handler will take the HBA offline.
2041  **/
2042 static int
2043 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2044 {
2045         uint8_t ret;
2046
2047         switch (mbxCommand) {
2048         case MBX_LOAD_SM:
2049         case MBX_READ_NV:
2050         case MBX_WRITE_NV:
2051         case MBX_WRITE_VPARMS:
2052         case MBX_RUN_BIU_DIAG:
2053         case MBX_INIT_LINK:
2054         case MBX_DOWN_LINK:
2055         case MBX_CONFIG_LINK:
2056         case MBX_CONFIG_RING:
2057         case MBX_RESET_RING:
2058         case MBX_READ_CONFIG:
2059         case MBX_READ_RCONFIG:
2060         case MBX_READ_SPARM:
2061         case MBX_READ_STATUS:
2062         case MBX_READ_RPI:
2063         case MBX_READ_XRI:
2064         case MBX_READ_REV:
2065         case MBX_READ_LNK_STAT:
2066         case MBX_REG_LOGIN:
2067         case MBX_UNREG_LOGIN:
2068         case MBX_CLEAR_LA:
2069         case MBX_DUMP_MEMORY:
2070         case MBX_DUMP_CONTEXT:
2071         case MBX_RUN_DIAGS:
2072         case MBX_RESTART:
2073         case MBX_UPDATE_CFG:
2074         case MBX_DOWN_LOAD:
2075         case MBX_DEL_LD_ENTRY:
2076         case MBX_RUN_PROGRAM:
2077         case MBX_SET_MASK:
2078         case MBX_SET_VARIABLE:
2079         case MBX_UNREG_D_ID:
2080         case MBX_KILL_BOARD:
2081         case MBX_CONFIG_FARP:
2082         case MBX_BEACON:
2083         case MBX_LOAD_AREA:
2084         case MBX_RUN_BIU_DIAG64:
2085         case MBX_CONFIG_PORT:
2086         case MBX_READ_SPARM64:
2087         case MBX_READ_RPI64:
2088         case MBX_REG_LOGIN64:
2089         case MBX_READ_TOPOLOGY:
2090         case MBX_WRITE_WWN:
2091         case MBX_SET_DEBUG:
2092         case MBX_LOAD_EXP_ROM:
2093         case MBX_ASYNCEVT_ENABLE:
2094         case MBX_REG_VPI:
2095         case MBX_UNREG_VPI:
2096         case MBX_HEARTBEAT:
2097         case MBX_PORT_CAPABILITIES:
2098         case MBX_PORT_IOV_CONTROL:
2099         case MBX_SLI4_CONFIG:
2100         case MBX_SLI4_REQ_FTRS:
2101         case MBX_REG_FCFI:
2102         case MBX_UNREG_FCFI:
2103         case MBX_REG_VFI:
2104         case MBX_UNREG_VFI:
2105         case MBX_INIT_VPI:
2106         case MBX_INIT_VFI:
2107         case MBX_RESUME_RPI:
2108         case MBX_READ_EVENT_LOG_STATUS:
2109         case MBX_READ_EVENT_LOG:
2110         case MBX_SECURITY_MGMT:
2111         case MBX_AUTH_PORT:
2112         case MBX_ACCESS_VDATA:
2113                 ret = mbxCommand;
2114                 break;
2115         default:
2116                 ret = MBX_SHUTDOWN;
2117                 break;
2118         }
2119         return ret;
2120 }
2121
2122 /**
2123  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2124  * @phba: Pointer to HBA context object.
2125  * @pmboxq: Pointer to mailbox command.
2126  *
2127  * This is completion handler function for mailbox commands issued from
2128  * lpfc_sli_issue_mbox_wait function. This function is called by the
2129  * mailbox event handler function with no lock held. This function
2130  * will wake up thread waiting on the wait queue pointed by context1
2131  * of the mailbox.
2132  **/
2133 void
2134 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2135 {
2136         wait_queue_head_t *pdone_q;
2137         unsigned long drvr_flag;
2138
2139         /*
2140          * If pdone_q is empty, the driver thread gave up waiting and
2141          * continued running.
2142          */
2143         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2144         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2145         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2146         if (pdone_q)
2147                 wake_up_interruptible(pdone_q);
2148         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2149         return;
2150 }
2151
2152
2153 /**
2154  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2155  * @phba: Pointer to HBA context object.
2156  * @pmb: Pointer to mailbox object.
2157  *
2158  * This function is the default mailbox completion handler. It
2159  * frees the memory resources associated with the completed mailbox
2160  * command. If the completed command is a REG_LOGIN mailbox command,
2161  * this function will issue a UREG_LOGIN to re-claim the RPI.
2162  **/
2163 void
2164 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2165 {
2166         struct lpfc_vport  *vport = pmb->vport;
2167         struct lpfc_dmabuf *mp;
2168         struct lpfc_nodelist *ndlp;
2169         struct Scsi_Host *shost;
2170         uint16_t rpi, vpi;
2171         int rc;
2172
2173         mp = (struct lpfc_dmabuf *) (pmb->context1);
2174
2175         if (mp) {
2176                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2177                 kfree(mp);
2178         }
2179
2180         /*
2181          * If a REG_LOGIN succeeded  after node is destroyed or node
2182          * is in re-discovery driver need to cleanup the RPI.
2183          */
2184         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2185             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2186             !pmb->u.mb.mbxStatus) {
2187                 rpi = pmb->u.mb.un.varWords[0];
2188                 vpi = pmb->u.mb.un.varRegLogin.vpi;
2189                 if (phba->sli_rev == LPFC_SLI_REV4)
2190                         vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
2191                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2192                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2193                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2194                 if (rc != MBX_NOT_FINISHED)
2195                         return;
2196         }
2197
2198         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2199                 !(phba->pport->load_flag & FC_UNLOADING) &&
2200                 !pmb->u.mb.mbxStatus) {
2201                 shost = lpfc_shost_from_vport(vport);
2202                 spin_lock_irq(shost->host_lock);
2203                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2204                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2205                 spin_unlock_irq(shost->host_lock);
2206         }
2207
2208         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2209                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2210                 lpfc_nlp_put(ndlp);
2211                 pmb->context2 = NULL;
2212         }
2213
2214         /* Check security permission status on INIT_LINK mailbox command */
2215         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2216             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2217                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2218                                 "2860 SLI authentication is required "
2219                                 "for INIT_LINK but has not done yet\n");
2220
2221         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2222                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2223         else
2224                 mempool_free(pmb, phba->mbox_mem_pool);
2225 }
2226  /**
2227  * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2228  * @phba: Pointer to HBA context object.
2229  * @pmb: Pointer to mailbox object.
2230  *
2231  * This function is the unreg rpi mailbox completion handler. It
2232  * frees the memory resources associated with the completed mailbox
2233  * command. An additional refrenece is put on the ndlp to prevent
2234  * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2235  * the unreg mailbox command completes, this routine puts the
2236  * reference back.
2237  *
2238  **/
2239 void
2240 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2241 {
2242         struct lpfc_vport  *vport = pmb->vport;
2243         struct lpfc_nodelist *ndlp;
2244
2245         ndlp = pmb->context1;
2246         if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2247                 if (phba->sli_rev == LPFC_SLI_REV4 &&
2248                     (bf_get(lpfc_sli_intf_if_type,
2249                      &phba->sli4_hba.sli_intf) ==
2250                      LPFC_SLI_INTF_IF_TYPE_2)) {
2251                         if (ndlp) {
2252                                 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2253                                                  "0010 UNREG_LOGIN vpi:%x "
2254                                                  "rpi:%x DID:%x map:%x %p\n",
2255                                                  vport->vpi, ndlp->nlp_rpi,
2256                                                  ndlp->nlp_DID,
2257                                                  ndlp->nlp_usg_map, ndlp);
2258                                 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2259                                 lpfc_nlp_put(ndlp);
2260                         }
2261                 }
2262         }
2263
2264         mempool_free(pmb, phba->mbox_mem_pool);
2265 }
2266
2267 /**
2268  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2269  * @phba: Pointer to HBA context object.
2270  *
2271  * This function is called with no lock held. This function processes all
2272  * the completed mailbox commands and gives it to upper layers. The interrupt
2273  * service routine processes mailbox completion interrupt and adds completed
2274  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2275  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2276  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2277  * function returns the mailbox commands to the upper layer by calling the
2278  * completion handler function of each mailbox.
2279  **/
2280 int
2281 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2282 {
2283         MAILBOX_t *pmbox;
2284         LPFC_MBOXQ_t *pmb;
2285         int rc;
2286         LIST_HEAD(cmplq);
2287
2288         phba->sli.slistat.mbox_event++;
2289
2290         /* Get all completed mailboxe buffers into the cmplq */
2291         spin_lock_irq(&phba->hbalock);
2292         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2293         spin_unlock_irq(&phba->hbalock);
2294
2295         /* Get a Mailbox buffer to setup mailbox commands for callback */
2296         do {
2297                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2298                 if (pmb == NULL)
2299                         break;
2300
2301                 pmbox = &pmb->u.mb;
2302
2303                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2304                         if (pmb->vport) {
2305                                 lpfc_debugfs_disc_trc(pmb->vport,
2306                                         LPFC_DISC_TRC_MBOX_VPORT,
2307                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2308                                         (uint32_t)pmbox->mbxCommand,
2309                                         pmbox->un.varWords[0],
2310                                         pmbox->un.varWords[1]);
2311                         }
2312                         else {
2313                                 lpfc_debugfs_disc_trc(phba->pport,
2314                                         LPFC_DISC_TRC_MBOX,
2315                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2316                                         (uint32_t)pmbox->mbxCommand,
2317                                         pmbox->un.varWords[0],
2318                                         pmbox->un.varWords[1]);
2319                         }
2320                 }
2321
2322                 /*
2323                  * It is a fatal error if unknown mbox command completion.
2324                  */
2325                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2326                     MBX_SHUTDOWN) {
2327                         /* Unknown mailbox command compl */
2328                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2329                                         "(%d):0323 Unknown Mailbox command "
2330                                         "x%x (x%x/x%x) Cmpl\n",
2331                                         pmb->vport ? pmb->vport->vpi : 0,
2332                                         pmbox->mbxCommand,
2333                                         lpfc_sli_config_mbox_subsys_get(phba,
2334                                                                         pmb),
2335                                         lpfc_sli_config_mbox_opcode_get(phba,
2336                                                                         pmb));
2337                         phba->link_state = LPFC_HBA_ERROR;
2338                         phba->work_hs = HS_FFER3;
2339                         lpfc_handle_eratt(phba);
2340                         continue;
2341                 }
2342
2343                 if (pmbox->mbxStatus) {
2344                         phba->sli.slistat.mbox_stat_err++;
2345                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2346                                 /* Mbox cmd cmpl error - RETRYing */
2347                                 lpfc_printf_log(phba, KERN_INFO,
2348                                         LOG_MBOX | LOG_SLI,
2349                                         "(%d):0305 Mbox cmd cmpl "
2350                                         "error - RETRYing Data: x%x "
2351                                         "(x%x/x%x) x%x x%x x%x\n",
2352                                         pmb->vport ? pmb->vport->vpi : 0,
2353                                         pmbox->mbxCommand,
2354                                         lpfc_sli_config_mbox_subsys_get(phba,
2355                                                                         pmb),
2356                                         lpfc_sli_config_mbox_opcode_get(phba,
2357                                                                         pmb),
2358                                         pmbox->mbxStatus,
2359                                         pmbox->un.varWords[0],
2360                                         pmb->vport->port_state);
2361                                 pmbox->mbxStatus = 0;
2362                                 pmbox->mbxOwner = OWN_HOST;
2363                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2364                                 if (rc != MBX_NOT_FINISHED)
2365                                         continue;
2366                         }
2367                 }
2368
2369                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2370                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2371                                 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2372                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2373                                 "x%x x%x x%x\n",
2374                                 pmb->vport ? pmb->vport->vpi : 0,
2375                                 pmbox->mbxCommand,
2376                                 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2377                                 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2378                                 pmb->mbox_cmpl,
2379                                 *((uint32_t *) pmbox),
2380                                 pmbox->un.varWords[0],
2381                                 pmbox->un.varWords[1],
2382                                 pmbox->un.varWords[2],
2383                                 pmbox->un.varWords[3],
2384                                 pmbox->un.varWords[4],
2385                                 pmbox->un.varWords[5],
2386                                 pmbox->un.varWords[6],
2387                                 pmbox->un.varWords[7],
2388                                 pmbox->un.varWords[8],
2389                                 pmbox->un.varWords[9],
2390                                 pmbox->un.varWords[10]);
2391
2392                 if (pmb->mbox_cmpl)
2393                         pmb->mbox_cmpl(phba,pmb);
2394         } while (1);
2395         return 0;
2396 }
2397
2398 /**
2399  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2400  * @phba: Pointer to HBA context object.
2401  * @pring: Pointer to driver SLI ring object.
2402  * @tag: buffer tag.
2403  *
2404  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2405  * is set in the tag the buffer is posted for a particular exchange,
2406  * the function will return the buffer without replacing the buffer.
2407  * If the buffer is for unsolicited ELS or CT traffic, this function
2408  * returns the buffer and also posts another buffer to the firmware.
2409  **/
2410 static struct lpfc_dmabuf *
2411 lpfc_sli_get_buff(struct lpfc_hba *phba,
2412                   struct lpfc_sli_ring *pring,
2413                   uint32_t tag)
2414 {
2415         struct hbq_dmabuf *hbq_entry;
2416
2417         if (tag & QUE_BUFTAG_BIT)
2418                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2419         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2420         if (!hbq_entry)
2421                 return NULL;
2422         return &hbq_entry->dbuf;
2423 }
2424
2425 /**
2426  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2427  * @phba: Pointer to HBA context object.
2428  * @pring: Pointer to driver SLI ring object.
2429  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2430  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2431  * @fch_type: the type for the first frame of the sequence.
2432  *
2433  * This function is called with no lock held. This function uses the r_ctl and
2434  * type of the received sequence to find the correct callback function to call
2435  * to process the sequence.
2436  **/
2437 static int
2438 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2439                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2440                          uint32_t fch_type)
2441 {
2442         int i;
2443
2444         /* unSolicited Responses */
2445         if (pring->prt[0].profile) {
2446                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2447                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2448                                                                         saveq);
2449                 return 1;
2450         }
2451         /* We must search, based on rctl / type
2452            for the right routine */
2453         for (i = 0; i < pring->num_mask; i++) {
2454                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2455                     (pring->prt[i].type == fch_type)) {
2456                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2457                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2458                                                 (phba, pring, saveq);
2459                         return 1;
2460                 }
2461         }
2462         return 0;
2463 }
2464
2465 /**
2466  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2467  * @phba: Pointer to HBA context object.
2468  * @pring: Pointer to driver SLI ring object.
2469  * @saveq: Pointer to the unsolicited iocb.
2470  *
2471  * This function is called with no lock held by the ring event handler
2472  * when there is an unsolicited iocb posted to the response ring by the
2473  * firmware. This function gets the buffer associated with the iocbs
2474  * and calls the event handler for the ring. This function handles both
2475  * qring buffers and hbq buffers.
2476  * When the function returns 1 the caller can free the iocb object otherwise
2477  * upper layer functions will free the iocb objects.
2478  **/
2479 static int
2480 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2481                             struct lpfc_iocbq *saveq)
2482 {
2483         IOCB_t           * irsp;
2484         WORD5            * w5p;
2485         uint32_t           Rctl, Type;
2486         struct lpfc_iocbq *iocbq;
2487         struct lpfc_dmabuf *dmzbuf;
2488
2489         irsp = &(saveq->iocb);
2490
2491         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2492                 if (pring->lpfc_sli_rcv_async_status)
2493                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2494                 else
2495                         lpfc_printf_log(phba,
2496                                         KERN_WARNING,
2497                                         LOG_SLI,
2498                                         "0316 Ring %d handler: unexpected "
2499                                         "ASYNC_STATUS iocb received evt_code "
2500                                         "0x%x\n",
2501                                         pring->ringno,
2502                                         irsp->un.asyncstat.evt_code);
2503                 return 1;
2504         }
2505
2506         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2507                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2508                 if (irsp->ulpBdeCount > 0) {
2509                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2510                                         irsp->un.ulpWord[3]);
2511                         lpfc_in_buf_free(phba, dmzbuf);
2512                 }
2513
2514                 if (irsp->ulpBdeCount > 1) {
2515                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2516                                         irsp->unsli3.sli3Words[3]);
2517                         lpfc_in_buf_free(phba, dmzbuf);
2518                 }
2519
2520                 if (irsp->ulpBdeCount > 2) {
2521                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2522                                 irsp->unsli3.sli3Words[7]);
2523                         lpfc_in_buf_free(phba, dmzbuf);
2524                 }
2525
2526                 return 1;
2527         }
2528
2529         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2530                 if (irsp->ulpBdeCount != 0) {
2531                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2532                                                 irsp->un.ulpWord[3]);
2533                         if (!saveq->context2)
2534                                 lpfc_printf_log(phba,
2535                                         KERN_ERR,
2536                                         LOG_SLI,
2537                                         "0341 Ring %d Cannot find buffer for "
2538                                         "an unsolicited iocb. tag 0x%x\n",
2539                                         pring->ringno,
2540                                         irsp->un.ulpWord[3]);
2541                 }
2542                 if (irsp->ulpBdeCount == 2) {
2543                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2544                                                 irsp->unsli3.sli3Words[7]);
2545                         if (!saveq->context3)
2546                                 lpfc_printf_log(phba,
2547                                         KERN_ERR,
2548                                         LOG_SLI,
2549                                         "0342 Ring %d Cannot find buffer for an"
2550                                         " unsolicited iocb. tag 0x%x\n",
2551                                         pring->ringno,
2552                                         irsp->unsli3.sli3Words[7]);
2553                 }
2554                 list_for_each_entry(iocbq, &saveq->list, list) {
2555                         irsp = &(iocbq->iocb);
2556                         if (irsp->ulpBdeCount != 0) {
2557                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2558                                                         irsp->un.ulpWord[3]);
2559                                 if (!iocbq->context2)
2560                                         lpfc_printf_log(phba,
2561                                                 KERN_ERR,
2562                                                 LOG_SLI,
2563                                                 "0343 Ring %d Cannot find "
2564                                                 "buffer for an unsolicited iocb"
2565                                                 ". tag 0x%x\n", pring->ringno,
2566                                                 irsp->un.ulpWord[3]);
2567                         }
2568                         if (irsp->ulpBdeCount == 2) {
2569                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2570                                                 irsp->unsli3.sli3Words[7]);
2571                                 if (!iocbq->context3)
2572                                         lpfc_printf_log(phba,
2573                                                 KERN_ERR,
2574                                                 LOG_SLI,
2575                                                 "0344 Ring %d Cannot find "
2576                                                 "buffer for an unsolicited "
2577                                                 "iocb. tag 0x%x\n",
2578                                                 pring->ringno,
2579                                                 irsp->unsli3.sli3Words[7]);
2580                         }
2581                 }
2582         }
2583         if (irsp->ulpBdeCount != 0 &&
2584             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2585              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2586                 int found = 0;
2587
2588                 /* search continue save q for same XRI */
2589                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2590                         if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2591                                 saveq->iocb.unsli3.rcvsli3.ox_id) {
2592                                 list_add_tail(&saveq->list, &iocbq->list);
2593                                 found = 1;
2594                                 break;
2595                         }
2596                 }
2597                 if (!found)
2598                         list_add_tail(&saveq->clist,
2599                                       &pring->iocb_continue_saveq);
2600                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2601                         list_del_init(&iocbq->clist);
2602                         saveq = iocbq;
2603                         irsp = &(saveq->iocb);
2604                 } else
2605                         return 0;
2606         }
2607         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2608             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2609             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2610                 Rctl = FC_RCTL_ELS_REQ;
2611                 Type = FC_TYPE_ELS;
2612         } else {
2613                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2614                 Rctl = w5p->hcsw.Rctl;
2615                 Type = w5p->hcsw.Type;
2616
2617                 /* Firmware Workaround */
2618                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2619                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2620                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2621                         Rctl = FC_RCTL_ELS_REQ;
2622                         Type = FC_TYPE_ELS;
2623                         w5p->hcsw.Rctl = Rctl;
2624                         w5p->hcsw.Type = Type;
2625                 }
2626         }
2627
2628         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2629                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2630                                 "0313 Ring %d handler: unexpected Rctl x%x "
2631                                 "Type x%x received\n",
2632                                 pring->ringno, Rctl, Type);
2633
2634         return 1;
2635 }
2636
2637 /**
2638  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2639  * @phba: Pointer to HBA context object.
2640  * @pring: Pointer to driver SLI ring object.
2641  * @prspiocb: Pointer to response iocb object.
2642  *
2643  * This function looks up the iocb_lookup table to get the command iocb
2644  * corresponding to the given response iocb using the iotag of the
2645  * response iocb. This function is called with the hbalock held.
2646  * This function returns the command iocb object if it finds the command
2647  * iocb else returns NULL.
2648  **/
2649 static struct lpfc_iocbq *
2650 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2651                       struct lpfc_sli_ring *pring,
2652                       struct lpfc_iocbq *prspiocb)
2653 {
2654         struct lpfc_iocbq *cmd_iocb = NULL;
2655         uint16_t iotag;
2656
2657         iotag = prspiocb->iocb.ulpIoTag;
2658
2659         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2660                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2661                 list_del_init(&cmd_iocb->list);
2662                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2663                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2664                 }
2665                 return cmd_iocb;
2666         }
2667
2668         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2669                         "0317 iotag x%x is out off "
2670                         "range: max iotag x%x wd0 x%x\n",
2671                         iotag, phba->sli.last_iotag,
2672                         *(((uint32_t *) &prspiocb->iocb) + 7));
2673         return NULL;
2674 }
2675
2676 /**
2677  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2678  * @phba: Pointer to HBA context object.
2679  * @pring: Pointer to driver SLI ring object.
2680  * @iotag: IOCB tag.
2681  *
2682  * This function looks up the iocb_lookup table to get the command iocb
2683  * corresponding to the given iotag. This function is called with the
2684  * hbalock held.
2685  * This function returns the command iocb object if it finds the command
2686  * iocb else returns NULL.
2687  **/
2688 static struct lpfc_iocbq *
2689 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2690                              struct lpfc_sli_ring *pring, uint16_t iotag)
2691 {
2692         struct lpfc_iocbq *cmd_iocb;
2693
2694         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2695                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2696                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2697                         /* remove from txcmpl queue list */
2698                         list_del_init(&cmd_iocb->list);
2699                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2700                         return cmd_iocb;
2701                 }
2702         }
2703         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2704                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2705                         iotag, phba->sli.last_iotag);
2706         return NULL;
2707 }
2708
2709 /**
2710  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2711  * @phba: Pointer to HBA context object.
2712  * @pring: Pointer to driver SLI ring object.
2713  * @saveq: Pointer to the response iocb to be processed.
2714  *
2715  * This function is called by the ring event handler for non-fcp
2716  * rings when there is a new response iocb in the response ring.
2717  * The caller is not required to hold any locks. This function
2718  * gets the command iocb associated with the response iocb and
2719  * calls the completion handler for the command iocb. If there
2720  * is no completion handler, the function will free the resources
2721  * associated with command iocb. If the response iocb is for
2722  * an already aborted command iocb, the status of the completion
2723  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2724  * This function always returns 1.
2725  **/
2726 static int
2727 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2728                           struct lpfc_iocbq *saveq)
2729 {
2730         struct lpfc_iocbq *cmdiocbp;
2731         int rc = 1;
2732         unsigned long iflag;
2733
2734         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2735         spin_lock_irqsave(&phba->hbalock, iflag);
2736         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2737         spin_unlock_irqrestore(&phba->hbalock, iflag);
2738
2739         if (cmdiocbp) {
2740                 if (cmdiocbp->iocb_cmpl) {
2741                         /*
2742                          * If an ELS command failed send an event to mgmt
2743                          * application.
2744                          */
2745                         if (saveq->iocb.ulpStatus &&
2746                              (pring->ringno == LPFC_ELS_RING) &&
2747                              (cmdiocbp->iocb.ulpCommand ==
2748                                 CMD_ELS_REQUEST64_CR))
2749                                 lpfc_send_els_failure_event(phba,
2750                                         cmdiocbp, saveq);
2751
2752                         /*
2753                          * Post all ELS completions to the worker thread.
2754                          * All other are passed to the completion callback.
2755                          */
2756                         if (pring->ringno == LPFC_ELS_RING) {
2757                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2758                                     (cmdiocbp->iocb_flag &
2759                                                         LPFC_DRIVER_ABORTED)) {
2760                                         spin_lock_irqsave(&phba->hbalock,
2761                                                           iflag);
2762                                         cmdiocbp->iocb_flag &=
2763                                                 ~LPFC_DRIVER_ABORTED;
2764                                         spin_unlock_irqrestore(&phba->hbalock,
2765                                                                iflag);
2766                                         saveq->iocb.ulpStatus =
2767                                                 IOSTAT_LOCAL_REJECT;
2768                                         saveq->iocb.un.ulpWord[4] =
2769                                                 IOERR_SLI_ABORTED;
2770
2771                                         /* Firmware could still be in progress
2772                                          * of DMAing payload, so don't free data
2773                                          * buffer till after a hbeat.
2774                                          */
2775                                         spin_lock_irqsave(&phba->hbalock,
2776                                                           iflag);
2777                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2778                                         spin_unlock_irqrestore(&phba->hbalock,
2779                                                                iflag);
2780                                 }
2781                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2782                                         if (saveq->iocb_flag &
2783                                             LPFC_EXCHANGE_BUSY) {
2784                                                 /* Set cmdiocb flag for the
2785                                                  * exchange busy so sgl (xri)
2786                                                  * will not be released until
2787                                                  * the abort xri is received
2788                                                  * from hba.
2789                                                  */
2790                                                 spin_lock_irqsave(
2791                                                         &phba->hbalock, iflag);
2792                                                 cmdiocbp->iocb_flag |=
2793                                                         LPFC_EXCHANGE_BUSY;
2794                                                 spin_unlock_irqrestore(
2795                                                         &phba->hbalock, iflag);
2796                                         }
2797                                         if (cmdiocbp->iocb_flag &
2798                                             LPFC_DRIVER_ABORTED) {
2799                                                 /*
2800                                                  * Clear LPFC_DRIVER_ABORTED
2801                                                  * bit in case it was driver
2802                                                  * initiated abort.
2803                                                  */
2804                                                 spin_lock_irqsave(
2805                                                         &phba->hbalock, iflag);
2806                                                 cmdiocbp->iocb_flag &=
2807                                                         ~LPFC_DRIVER_ABORTED;
2808                                                 spin_unlock_irqrestore(
2809                                                         &phba->hbalock, iflag);
2810                                                 cmdiocbp->iocb.ulpStatus =
2811                                                         IOSTAT_LOCAL_REJECT;
2812                                                 cmdiocbp->iocb.un.ulpWord[4] =
2813                                                         IOERR_ABORT_REQUESTED;
2814                                                 /*
2815                                                  * For SLI4, irsiocb contains
2816                                                  * NO_XRI in sli_xritag, it
2817                                                  * shall not affect releasing
2818                                                  * sgl (xri) process.
2819                                                  */
2820                                                 saveq->iocb.ulpStatus =
2821                                                         IOSTAT_LOCAL_REJECT;
2822                                                 saveq->iocb.un.ulpWord[4] =
2823                                                         IOERR_SLI_ABORTED;
2824                                                 spin_lock_irqsave(
2825                                                         &phba->hbalock, iflag);
2826                                                 saveq->iocb_flag |=
2827                                                         LPFC_DELAY_MEM_FREE;
2828                                                 spin_unlock_irqrestore(
2829                                                         &phba->hbalock, iflag);
2830                                         }
2831                                 }
2832                         }
2833                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2834                 } else
2835                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2836         } else {
2837                 /*
2838                  * Unknown initiating command based on the response iotag.
2839                  * This could be the case on the ELS ring because of
2840                  * lpfc_els_abort().
2841                  */
2842                 if (pring->ringno != LPFC_ELS_RING) {
2843                         /*
2844                          * Ring <ringno> handler: unexpected completion IoTag
2845                          * <IoTag>
2846                          */
2847                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2848                                          "0322 Ring %d handler: "
2849                                          "unexpected completion IoTag x%x "
2850                                          "Data: x%x x%x x%x x%x\n",
2851                                          pring->ringno,
2852                                          saveq->iocb.ulpIoTag,
2853                                          saveq->iocb.ulpStatus,
2854                                          saveq->iocb.un.ulpWord[4],
2855                                          saveq->iocb.ulpCommand,
2856                                          saveq->iocb.ulpContext);
2857                 }
2858         }
2859
2860         return rc;
2861 }
2862
2863 /**
2864  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2865  * @phba: Pointer to HBA context object.
2866  * @pring: Pointer to driver SLI ring object.
2867  *
2868  * This function is called from the iocb ring event handlers when
2869  * put pointer is ahead of the get pointer for a ring. This function signal
2870  * an error attention condition to the worker thread and the worker
2871  * thread will transition the HBA to offline state.
2872  **/
2873 static void
2874 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2875 {
2876         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2877         /*
2878          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2879          * rsp ring <portRspMax>
2880          */
2881         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2882                         "0312 Ring %d handler: portRspPut %d "
2883                         "is bigger than rsp ring %d\n",
2884                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2885                         pring->sli.sli3.numRiocb);
2886
2887         phba->link_state = LPFC_HBA_ERROR;
2888
2889         /*
2890          * All error attention handlers are posted to
2891          * worker thread
2892          */
2893         phba->work_ha |= HA_ERATT;
2894         phba->work_hs = HS_FFER3;
2895
2896         lpfc_worker_wake_up(phba);
2897
2898         return;
2899 }
2900
2901 /**
2902  * lpfc_poll_eratt - Error attention polling timer timeout handler
2903  * @ptr: Pointer to address of HBA context object.
2904  *
2905  * This function is invoked by the Error Attention polling timer when the
2906  * timer times out. It will check the SLI Error Attention register for
2907  * possible attention events. If so, it will post an Error Attention event
2908  * and wake up worker thread to process it. Otherwise, it will set up the
2909  * Error Attention polling timer for the next poll.
2910  **/
2911 void lpfc_poll_eratt(unsigned long ptr)
2912 {
2913         struct lpfc_hba *phba;
2914         uint32_t eratt = 0;
2915         uint64_t sli_intr, cnt;
2916
2917         phba = (struct lpfc_hba *)ptr;
2918
2919         /* Here we will also keep track of interrupts per sec of the hba */
2920         sli_intr = phba->sli.slistat.sli_intr;
2921
2922         if (phba->sli.slistat.sli_prev_intr > sli_intr)
2923                 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2924                         sli_intr);
2925         else
2926                 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2927
2928         /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2929         do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2930         phba->sli.slistat.sli_ips = cnt;
2931
2932         phba->sli.slistat.sli_prev_intr = sli_intr;
2933
2934         /* Check chip HA register for error event */
2935         eratt = lpfc_sli_check_eratt(phba);
2936
2937         if (eratt)
2938                 /* Tell the worker thread there is work to do */
2939                 lpfc_worker_wake_up(phba);
2940         else
2941                 /* Restart the timer for next eratt poll */
2942                 mod_timer(&phba->eratt_poll,
2943                           jiffies +
2944                           msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
2945         return;
2946 }
2947
2948
2949 /**
2950  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2951  * @phba: Pointer to HBA context object.
2952  * @pring: Pointer to driver SLI ring object.
2953  * @mask: Host attention register mask for this ring.
2954  *
2955  * This function is called from the interrupt context when there is a ring
2956  * event for the fcp ring. The caller does not hold any lock.
2957  * The function processes each response iocb in the response ring until it
2958  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2959  * LE bit set. The function will call the completion handler of the command iocb
2960  * if the response iocb indicates a completion for a command iocb or it is
2961  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2962  * function if this is an unsolicited iocb.
2963  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2964  * to check it explicitly.
2965  */
2966 int
2967 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2968                                 struct lpfc_sli_ring *pring, uint32_t mask)
2969 {
2970         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2971         IOCB_t *irsp = NULL;
2972         IOCB_t *entry = NULL;
2973         struct lpfc_iocbq *cmdiocbq = NULL;
2974         struct lpfc_iocbq rspiocbq;
2975         uint32_t status;
2976         uint32_t portRspPut, portRspMax;
2977         int rc = 1;
2978         lpfc_iocb_type type;
2979         unsigned long iflag;
2980         uint32_t rsp_cmpl = 0;
2981
2982         spin_lock_irqsave(&phba->hbalock, iflag);
2983         pring->stats.iocb_event++;
2984
2985         /*
2986          * The next available response entry should never exceed the maximum
2987          * entries.  If it does, treat it as an adapter hardware error.
2988          */
2989         portRspMax = pring->sli.sli3.numRiocb;
2990         portRspPut = le32_to_cpu(pgp->rspPutInx);
2991         if (unlikely(portRspPut >= portRspMax)) {
2992                 lpfc_sli_rsp_pointers_error(phba, pring);
2993                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2994                 return 1;
2995         }
2996         if (phba->fcp_ring_in_use) {
2997                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2998                 return 1;
2999         } else
3000                 phba->fcp_ring_in_use = 1;
3001
3002         rmb();
3003         while (pring->sli.sli3.rspidx != portRspPut) {
3004                 /*
3005                  * Fetch an entry off the ring and copy it into a local data
3006                  * structure.  The copy involves a byte-swap since the
3007                  * network byte order and pci byte orders are different.
3008                  */
3009                 entry = lpfc_resp_iocb(phba, pring);
3010                 phba->last_completion_time = jiffies;
3011
3012                 if (++pring->sli.sli3.rspidx >= portRspMax)
3013                         pring->sli.sli3.rspidx = 0;
3014
3015                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3016                                       (uint32_t *) &rspiocbq.iocb,
3017                                       phba->iocb_rsp_size);
3018                 INIT_LIST_HEAD(&(rspiocbq.list));
3019                 irsp = &rspiocbq.iocb;
3020
3021                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3022                 pring->stats.iocb_rsp++;
3023                 rsp_cmpl++;
3024
3025                 if (unlikely(irsp->ulpStatus)) {
3026                         /*
3027                          * If resource errors reported from HBA, reduce
3028                          * queuedepths of the SCSI device.
3029                          */
3030                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3031                             ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3032                              IOERR_NO_RESOURCES)) {
3033                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3034                                 phba->lpfc_rampdown_queue_depth(phba);
3035                                 spin_lock_irqsave(&phba->hbalock, iflag);
3036                         }
3037
3038                         /* Rsp ring <ringno> error: IOCB */
3039                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3040                                         "0336 Rsp Ring %d error: IOCB Data: "
3041                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3042                                         pring->ringno,
3043                                         irsp->un.ulpWord[0],
3044                                         irsp->un.ulpWord[1],
3045                                         irsp->un.ulpWord[2],
3046                                         irsp->un.ulpWord[3],
3047                                         irsp->un.ulpWord[4],
3048                                         irsp->un.ulpWord[5],
3049                                         *(uint32_t *)&irsp->un1,
3050                                         *((uint32_t *)&irsp->un1 + 1));
3051                 }
3052
3053                 switch (type) {
3054                 case LPFC_ABORT_IOCB:
3055                 case LPFC_SOL_IOCB:
3056                         /*
3057                          * Idle exchange closed via ABTS from port.  No iocb
3058                          * resources need to be recovered.
3059                          */
3060                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3061                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3062                                                 "0333 IOCB cmd 0x%x"
3063                                                 " processed. Skipping"
3064                                                 " completion\n",
3065                                                 irsp->ulpCommand);
3066                                 break;
3067                         }
3068
3069                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3070                                                          &rspiocbq);
3071                         if (unlikely(!cmdiocbq))
3072                                 break;
3073                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3074                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3075                         if (cmdiocbq->iocb_cmpl) {
3076                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3077                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3078                                                       &rspiocbq);
3079                                 spin_lock_irqsave(&phba->hbalock, iflag);
3080                         }
3081                         break;
3082                 case LPFC_UNSOL_IOCB:
3083                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3084                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3085                         spin_lock_irqsave(&phba->hbalock, iflag);
3086                         break;
3087                 default:
3088                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3089                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3090                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3091                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3092                                        MAX_MSG_DATA);
3093                                 dev_warn(&((phba->pcidev)->dev),
3094                                          "lpfc%d: %s\n",
3095                                          phba->brd_no, adaptermsg);
3096                         } else {
3097                                 /* Unknown IOCB command */
3098                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3099                                                 "0334 Unknown IOCB command "
3100                                                 "Data: x%x, x%x x%x x%x x%x\n",
3101                                                 type, irsp->ulpCommand,
3102                                                 irsp->ulpStatus,
3103                                                 irsp->ulpIoTag,
3104                                                 irsp->ulpContext);
3105                         }
3106                         break;
3107                 }
3108
3109                 /*
3110                  * The response IOCB has been processed.  Update the ring
3111                  * pointer in SLIM.  If the port response put pointer has not
3112                  * been updated, sync the pgp->rspPutInx and fetch the new port
3113                  * response put pointer.
3114                  */
3115                 writel(pring->sli.sli3.rspidx,
3116                         &phba->host_gp[pring->ringno].rspGetInx);
3117
3118                 if (pring->sli.sli3.rspidx == portRspPut)
3119                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3120         }
3121
3122         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3123                 pring->stats.iocb_rsp_full++;
3124                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3125                 writel(status, phba->CAregaddr);
3126                 readl(phba->CAregaddr);
3127         }
3128         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3129                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3130                 pring->stats.iocb_cmd_empty++;
3131
3132                 /* Force update of the local copy of cmdGetInx */
3133                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3134                 lpfc_sli_resume_iocb(phba, pring);
3135
3136                 if ((pring->lpfc_sli_cmd_available))
3137                         (pring->lpfc_sli_cmd_available) (phba, pring);
3138
3139         }
3140
3141         phba->fcp_ring_in_use = 0;
3142         spin_unlock_irqrestore(&phba->hbalock, iflag);
3143         return rc;
3144 }
3145
3146 /**
3147  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3148  * @phba: Pointer to HBA context object.
3149  * @pring: Pointer to driver SLI ring object.
3150  * @rspiocbp: Pointer to driver response IOCB object.
3151  *
3152  * This function is called from the worker thread when there is a slow-path
3153  * response IOCB to process. This function chains all the response iocbs until
3154  * seeing the iocb with the LE bit set. The function will call
3155  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3156  * completion of a command iocb. The function will call the
3157  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3158  * The function frees the resources or calls the completion handler if this
3159  * iocb is an abort completion. The function returns NULL when the response
3160  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3161  * this function shall chain the iocb on to the iocb_continueq and return the
3162  * response iocb passed in.
3163  **/
3164 static struct lpfc_iocbq *
3165 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3166                         struct lpfc_iocbq *rspiocbp)
3167 {
3168         struct lpfc_iocbq *saveq;
3169         struct lpfc_iocbq *cmdiocbp;
3170         struct lpfc_iocbq *next_iocb;
3171         IOCB_t *irsp = NULL;
3172         uint32_t free_saveq;
3173         uint8_t iocb_cmd_type;
3174         lpfc_iocb_type type;
3175         unsigned long iflag;
3176         int rc;
3177
3178         spin_lock_irqsave(&phba->hbalock, iflag);
3179         /* First add the response iocb to the countinueq list */
3180         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3181         pring->iocb_continueq_cnt++;
3182
3183         /* Now, determine whether the list is completed for processing */
3184         irsp = &rspiocbp->iocb;
3185         if (irsp->ulpLe) {
3186                 /*
3187                  * By default, the driver expects to free all resources
3188                  * associated with this iocb completion.
3189                  */
3190                 free_saveq = 1;
3191                 saveq = list_get_first(&pring->iocb_continueq,
3192                                        struct lpfc_iocbq, list);
3193                 irsp = &(saveq->iocb);
3194                 list_del_init(&pring->iocb_continueq);
3195                 pring->iocb_continueq_cnt = 0;
3196
3197                 pring->stats.iocb_rsp++;
3198
3199                 /*
3200                  * If resource errors reported from HBA, reduce
3201                  * queuedepths of the SCSI device.
3202                  */
3203                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3204                     ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3205                      IOERR_NO_RESOURCES)) {
3206                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3207                         phba->lpfc_rampdown_queue_depth(phba);
3208                         spin_lock_irqsave(&phba->hbalock, iflag);
3209                 }
3210
3211                 if (irsp->ulpStatus) {
3212                         /* Rsp ring <ringno> error: IOCB */
3213                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3214                                         "0328 Rsp Ring %d error: "
3215                                         "IOCB Data: "
3216                                         "x%x x%x x%x x%x "
3217                                         "x%x x%x x%x x%x "
3218                                         "x%x x%x x%x x%x "
3219                                         "x%x x%x x%x x%x\n",
3220                                         pring->ringno,
3221                                         irsp->un.ulpWord[0],
3222                                         irsp->un.ulpWord[1],
3223                                         irsp->un.ulpWord[2],
3224                                         irsp->un.ulpWord[3],
3225                                         irsp->un.ulpWord[4],
3226                                         irsp->un.ulpWord[5],
3227                                         *(((uint32_t *) irsp) + 6),
3228                                         *(((uint32_t *) irsp) + 7),
3229                                         *(((uint32_t *) irsp) + 8),
3230                                         *(((uint32_t *) irsp) + 9),
3231                                         *(((uint32_t *) irsp) + 10),
3232                                         *(((uint32_t *) irsp) + 11),
3233                                         *(((uint32_t *) irsp) + 12),
3234                                         *(((uint32_t *) irsp) + 13),
3235                                         *(((uint32_t *) irsp) + 14),
3236                                         *(((uint32_t *) irsp) + 15));
3237                 }
3238
3239                 /*
3240                  * Fetch the IOCB command type and call the correct completion
3241                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3242                  * get freed back to the lpfc_iocb_list by the discovery
3243                  * kernel thread.
3244                  */
3245                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3246                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3247                 switch (type) {
3248                 case LPFC_SOL_IOCB:
3249                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3250                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3251                         spin_lock_irqsave(&phba->hbalock, iflag);
3252                         break;
3253
3254                 case LPFC_UNSOL_IOCB:
3255                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3256                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3257                         spin_lock_irqsave(&phba->hbalock, iflag);
3258                         if (!rc)
3259                                 free_saveq = 0;
3260                         break;
3261
3262                 case LPFC_ABORT_IOCB:
3263                         cmdiocbp = NULL;
3264                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3265                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3266                                                                  saveq);
3267                         if (cmdiocbp) {
3268                                 /* Call the specified completion routine */
3269                                 if (cmdiocbp->iocb_cmpl) {
3270                                         spin_unlock_irqrestore(&phba->hbalock,
3271                                                                iflag);
3272                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3273                                                               saveq);
3274                                         spin_lock_irqsave(&phba->hbalock,
3275                                                           iflag);
3276                                 } else
3277                                         __lpfc_sli_release_iocbq(phba,
3278                                                                  cmdiocbp);
3279                         }
3280                         break;
3281
3282                 case LPFC_UNKNOWN_IOCB:
3283                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3284                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3285                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3286                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3287                                        MAX_MSG_DATA);
3288                                 dev_warn(&((phba->pcidev)->dev),
3289                                          "lpfc%d: %s\n",
3290                                          phba->brd_no, adaptermsg);
3291                         } else {
3292                                 /* Unknown IOCB command */
3293                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3294                                                 "0335 Unknown IOCB "
3295                                                 "command Data: x%x "
3296                                                 "x%x x%x x%x\n",
3297                                                 irsp->ulpCommand,
3298                                                 irsp->ulpStatus,
3299                                                 irsp->ulpIoTag,
3300                                                 irsp->ulpContext);
3301                         }
3302                         break;
3303                 }
3304
3305                 if (free_saveq) {
3306                         list_for_each_entry_safe(rspiocbp, next_iocb,
3307                                                  &saveq->list, list) {
3308                                 list_del_init(&rspiocbp->list);
3309                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3310                         }
3311                         __lpfc_sli_release_iocbq(phba, saveq);
3312                 }
3313                 rspiocbp = NULL;
3314         }
3315         spin_unlock_irqrestore(&phba->hbalock, iflag);
3316         return rspiocbp;
3317 }
3318
3319 /**
3320  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3321  * @phba: Pointer to HBA context object.
3322  * @pring: Pointer to driver SLI ring object.
3323  * @mask: Host attention register mask for this ring.
3324  *
3325  * This routine wraps the actual slow_ring event process routine from the
3326  * API jump table function pointer from the lpfc_hba struct.
3327  **/
3328 void
3329 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3330                                 struct lpfc_sli_ring *pring, uint32_t mask)
3331 {
3332         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3333 }
3334
3335 /**
3336  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3337  * @phba: Pointer to HBA context object.
3338  * @pring: Pointer to driver SLI ring object.
3339  * @mask: Host attention register mask for this ring.
3340  *
3341  * This function is called from the worker thread when there is a ring event
3342  * for non-fcp rings. The caller does not hold any lock. The function will
3343  * remove each response iocb in the response ring and calls the handle
3344  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3345  **/
3346 static void
3347 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3348                                    struct lpfc_sli_ring *pring, uint32_t mask)
3349 {
3350         struct lpfc_pgp *pgp;
3351         IOCB_t *entry;
3352         IOCB_t *irsp = NULL;
3353         struct lpfc_iocbq *rspiocbp = NULL;
3354         uint32_t portRspPut, portRspMax;
3355         unsigned long iflag;
3356         uint32_t status;
3357
3358         pgp = &phba->port_gp[pring->ringno];
3359         spin_lock_irqsave(&phba->hbalock, iflag);
3360         pring->stats.iocb_event++;
3361
3362         /*
3363          * The next available response entry should never exceed the maximum
3364          * entries.  If it does, treat it as an adapter hardware error.
3365          */
3366         portRspMax = pring->sli.sli3.numRiocb;
3367         portRspPut = le32_to_cpu(pgp->rspPutInx);
3368         if (portRspPut >= portRspMax) {
3369                 /*
3370                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3371                  * rsp ring <portRspMax>
3372                  */
3373                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3374                                 "0303 Ring %d handler: portRspPut %d "
3375                                 "is bigger than rsp ring %d\n",
3376                                 pring->ringno, portRspPut, portRspMax);
3377
3378                 phba->link_state = LPFC_HBA_ERROR;
3379                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3380
3381                 phba->work_hs = HS_FFER3;
3382                 lpfc_handle_eratt(phba);
3383
3384                 return;
3385         }
3386
3387         rmb();
3388         while (pring->sli.sli3.rspidx != portRspPut) {
3389                 /*
3390                  * Build a completion list and call the appropriate handler.
3391                  * The process is to get the next available response iocb, get
3392                  * a free iocb from the list, copy the response data into the
3393                  * free iocb, insert to the continuation list, and update the
3394                  * next response index to slim.  This process makes response
3395                  * iocb's in the ring available to DMA as fast as possible but
3396                  * pays a penalty for a copy operation.  Since the iocb is
3397                  * only 32 bytes, this penalty is considered small relative to
3398                  * the PCI reads for register values and a slim write.  When
3399                  * the ulpLe field is set, the entire Command has been
3400                  * received.
3401                  */
3402                 entry = lpfc_resp_iocb(phba, pring);
3403
3404                 phba->last_completion_time = jiffies;
3405                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3406                 if (rspiocbp == NULL) {
3407                         printk(KERN_ERR "%s: out of buffers! Failing "
3408                                "completion.\n", __func__);
3409                         break;
3410                 }
3411
3412                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3413                                       phba->iocb_rsp_size);
3414                 irsp = &rspiocbp->iocb;
3415
3416                 if (++pring->sli.sli3.rspidx >= portRspMax)
3417                         pring->sli.sli3.rspidx = 0;
3418
3419                 if (pring->ringno == LPFC_ELS_RING) {
3420                         lpfc_debugfs_slow_ring_trc(phba,
3421                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3422                                 *(((uint32_t *) irsp) + 4),
3423                                 *(((uint32_t *) irsp) + 6),
3424                                 *(((uint32_t *) irsp) + 7));
3425                 }
3426
3427                 writel(pring->sli.sli3.rspidx,
3428                         &phba->host_gp[pring->ringno].rspGetInx);
3429
3430                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3431                 /* Handle the response IOCB */
3432                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3433                 spin_lock_irqsave(&phba->hbalock, iflag);
3434
3435                 /*
3436                  * If the port response put pointer has not been updated, sync
3437                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3438                  * response put pointer.
3439                  */
3440                 if (pring->sli.sli3.rspidx == portRspPut) {
3441                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3442                 }
3443         } /* while (pring->sli.sli3.rspidx != portRspPut) */
3444
3445         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3446                 /* At least one response entry has been freed */
3447                 pring->stats.iocb_rsp_full++;
3448                 /* SET RxRE_RSP in Chip Att register */
3449                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3450                 writel(status, phba->CAregaddr);
3451                 readl(phba->CAregaddr); /* flush */
3452         }
3453         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3454                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3455                 pring->stats.iocb_cmd_empty++;
3456
3457                 /* Force update of the local copy of cmdGetInx */
3458                 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3459                 lpfc_sli_resume_iocb(phba, pring);
3460
3461                 if ((pring->lpfc_sli_cmd_available))
3462                         (pring->lpfc_sli_cmd_available) (phba, pring);
3463
3464         }
3465
3466         spin_unlock_irqrestore(&phba->hbalock, iflag);
3467         return;
3468 }
3469
3470 /**
3471  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3472  * @phba: Pointer to HBA context object.
3473  * @pring: Pointer to driver SLI ring object.
3474  * @mask: Host attention register mask for this ring.
3475  *
3476  * This function is called from the worker thread when there is a pending
3477  * ELS response iocb on the driver internal slow-path response iocb worker
3478  * queue. The caller does not hold any lock. The function will remove each
3479  * response iocb from the response worker queue and calls the handle
3480  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3481  **/
3482 static void
3483 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3484                                    struct lpfc_sli_ring *pring, uint32_t mask)
3485 {
3486         struct lpfc_iocbq *irspiocbq;
3487         struct hbq_dmabuf *dmabuf;
3488         struct lpfc_cq_event *cq_event;
3489         unsigned long iflag;
3490         int count = 0;
3491
3492         spin_lock_irqsave(&phba->hbalock, iflag);
3493         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3494         spin_unlock_irqrestore(&phba->hbalock, iflag);
3495         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3496                 /* Get the response iocb from the head of work queue */
3497                 spin_lock_irqsave(&phba->hbalock, iflag);
3498                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3499                                  cq_event, struct lpfc_cq_event, list);
3500                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3501
3502                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3503                 case CQE_CODE_COMPL_WQE:
3504                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3505                                                  cq_event);
3506                         /* Translate ELS WCQE to response IOCBQ */
3507                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3508                                                                    irspiocbq);
3509                         if (irspiocbq)
3510                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3511                                                            irspiocbq);
3512                         count++;
3513                         break;
3514                 case CQE_CODE_RECEIVE:
3515                 case CQE_CODE_RECEIVE_V1:
3516                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3517                                               cq_event);
3518                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3519                         count++;
3520                         break;
3521                 default:
3522                         break;
3523                 }
3524
3525                 /* Limit the number of events to 64 to avoid soft lockups */
3526                 if (count == 64)
3527                         break;
3528         }
3529 }
3530
3531 /**
3532  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3533  * @phba: Pointer to HBA context object.
3534  * @pring: Pointer to driver SLI ring object.
3535  *
3536  * This function aborts all iocbs in the given ring and frees all the iocb
3537  * objects in txq. This function issues an abort iocb for all the iocb commands
3538  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3539  * the return of this function. The caller is not required to hold any locks.
3540  **/
3541 void
3542 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3543 {
3544         LIST_HEAD(completions);
3545         struct lpfc_iocbq *iocb, *next_iocb;
3546
3547         if (pring->ringno == LPFC_ELS_RING) {
3548                 lpfc_fabric_abort_hba(phba);
3549         }
3550
3551         /* Error everything on txq and txcmplq
3552          * First do the txq.
3553          */
3554         if (phba->sli_rev >= LPFC_SLI_REV4) {
3555                 spin_lock_irq(&pring->ring_lock);
3556                 list_splice_init(&pring->txq, &completions);
3557                 pring->txq_cnt = 0;
3558                 spin_unlock_irq(&pring->ring_lock);
3559
3560                 spin_lock_irq(&phba->hbalock);
3561                 /* Next issue ABTS for everything on the txcmplq */
3562                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3563                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3564                 spin_unlock_irq(&phba->hbalock);
3565         } else {
3566                 spin_lock_irq(&phba->hbalock);
3567                 list_splice_init(&pring->txq, &completions);
3568                 pring->txq_cnt = 0;
3569
3570                 /* Next issue ABTS for everything on the txcmplq */
3571                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3572                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3573                 spin_unlock_irq(&phba->hbalock);
3574         }
3575
3576         /* Cancel all the IOCBs from the completions list */
3577         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3578                               IOERR_SLI_ABORTED);
3579 }
3580
3581 /**
3582  * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3583  * @phba: Pointer to HBA context object.
3584  * @pring: Pointer to driver SLI ring object.
3585  *
3586  * This function aborts all iocbs in FCP rings and frees all the iocb
3587  * objects in txq. This function issues an abort iocb for all the iocb commands
3588  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3589  * the return of this function. The caller is not required to hold any locks.
3590  **/
3591 void
3592 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3593 {
3594         struct lpfc_sli *psli = &phba->sli;
3595         struct lpfc_sli_ring  *pring;
3596         uint32_t i;
3597
3598         /* Look on all the FCP Rings for the iotag */
3599         if (phba->sli_rev >= LPFC_SLI_REV4) {
3600                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3601                         pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3602                         lpfc_sli_abort_iocb_ring(phba, pring);
3603                 }
3604         } else {
3605                 pring = &psli->ring[psli->fcp_ring];
3606                 lpfc_sli_abort_iocb_ring(phba, pring);
3607         }
3608 }
3609
3610
3611 /**
3612  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3613  * @phba: Pointer to HBA context object.
3614  *
3615  * This function flushes all iocbs in the fcp ring and frees all the iocb
3616  * objects in txq and txcmplq. This function will not issue abort iocbs
3617  * for all the iocb commands in txcmplq, they will just be returned with
3618  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3619  * slot has been permanently disabled.
3620  **/
3621 void
3622 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3623 {
3624         LIST_HEAD(txq);
3625         LIST_HEAD(txcmplq);
3626         struct lpfc_sli *psli = &phba->sli;
3627         struct lpfc_sli_ring  *pring;
3628         uint32_t i;
3629
3630         spin_lock_irq(&phba->hbalock);
3631         /* Indicate the I/O queues are flushed */
3632         phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3633         spin_unlock_irq(&phba->hbalock);
3634
3635         /* Look on all the FCP Rings for the iotag */
3636         if (phba->sli_rev >= LPFC_SLI_REV4) {
3637                 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3638                         pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3639
3640                         spin_lock_irq(&pring->ring_lock);
3641                         /* Retrieve everything on txq */
3642                         list_splice_init(&pring->txq, &txq);
3643                         /* Retrieve everything on the txcmplq */
3644                         list_splice_init(&pring->txcmplq, &txcmplq);
3645                         pring->txq_cnt = 0;
3646                         pring->txcmplq_cnt = 0;
3647                         spin_unlock_irq(&pring->ring_lock);
3648
3649                         /* Flush the txq */
3650                         lpfc_sli_cancel_iocbs(phba, &txq,
3651                                               IOSTAT_LOCAL_REJECT,
3652                                               IOERR_SLI_DOWN);
3653                         /* Flush the txcmpq */
3654                         lpfc_sli_cancel_iocbs(phba, &txcmplq,
3655                                               IOSTAT_LOCAL_REJECT,
3656                                               IOERR_SLI_DOWN);
3657                 }
3658         } else {
3659                 pring = &psli->ring[psli->fcp_ring];
3660
3661                 spin_lock_irq(&phba->hbalock);
3662                 /* Retrieve everything on txq */
3663                 list_splice_init(&pring->txq, &txq);
3664                 /* Retrieve everything on the txcmplq */
3665                 list_splice_init(&pring->txcmplq, &txcmplq);
3666                 pring->txq_cnt = 0;
3667                 pring->txcmplq_cnt = 0;
3668                 spin_unlock_irq(&phba->hbalock);
3669
3670                 /* Flush the txq */
3671                 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3672                                       IOERR_SLI_DOWN);
3673                 /* Flush the txcmpq */
3674                 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3675                                       IOERR_SLI_DOWN);
3676         }
3677 }
3678
3679 /**
3680  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3681  * @phba: Pointer to HBA context object.
3682  * @mask: Bit mask to be checked.
3683  *
3684  * This function reads the host status register and compares
3685  * with the provided bit mask to check if HBA completed
3686  * the restart. This function will wait in a loop for the
3687  * HBA to complete restart. If the HBA does not restart within
3688  * 15 iterations, the function will reset the HBA again. The
3689  * function returns 1 when HBA fail to restart otherwise returns
3690  * zero.
3691  **/
3692 static int
3693 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3694 {
3695         uint32_t status;
3696         int i = 0;
3697         int retval = 0;
3698
3699         /* Read the HBA Host Status Register */
3700         if (lpfc_readl(phba->HSregaddr, &status))
3701                 return 1;
3702
3703         /*
3704          * Check status register every 100ms for 5 retries, then every
3705          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3706          * every 2.5 sec for 4.
3707          * Break our of the loop if errors occurred during init.
3708          */
3709         while (((status & mask) != mask) &&
3710                !(status & HS_FFERM) &&
3711                i++ < 20) {
3712
3713                 if (i <= 5)
3714                         msleep(10);
3715                 else if (i <= 10)
3716                         msleep(500);
3717                 else
3718                         msleep(2500);
3719
3720                 if (i == 15) {
3721                                 /* Do post */
3722                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3723                         lpfc_sli_brdrestart(phba);
3724                 }
3725                 /* Read the HBA Host Status Register */
3726                 if (lpfc_readl(phba->HSregaddr, &status)) {
3727                         retval = 1;
3728                         break;
3729                 }
3730         }
3731
3732         /* Check to see if any errors occurred during init */
3733         if ((status & HS_FFERM) || (i >= 20)) {
3734                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3735                                 "2751 Adapter failed to restart, "
3736                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3737                                 status,
3738                                 readl(phba->MBslimaddr + 0xa8),
3739                                 readl(phba->MBslimaddr + 0xac));
3740                 phba->link_state = LPFC_HBA_ERROR;
3741                 retval = 1;
3742         }
3743
3744         return retval;
3745 }
3746
3747 /**
3748  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3749  * @phba: Pointer to HBA context object.
3750  * @mask: Bit mask to be checked.
3751  *
3752  * This function checks the host status register to check if HBA is
3753  * ready. This function will wait in a loop for the HBA to be ready
3754  * If the HBA is not ready , the function will will reset the HBA PCI
3755  * function again. The function returns 1 when HBA fail to be ready
3756  * otherwise returns zero.
3757  **/
3758 static int
3759 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3760 {
3761         uint32_t status;
3762         int retval = 0;
3763
3764         /* Read the HBA Host Status Register */
3765         status = lpfc_sli4_post_status_check(phba);
3766
3767         if (status) {
3768                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3769                 lpfc_sli_brdrestart(phba);
3770                 status = lpfc_sli4_post_status_check(phba);
3771         }
3772
3773         /* Check to see if any errors occurred during init */
3774         if (status) {
3775                 phba->link_state = LPFC_HBA_ERROR;
3776                 retval = 1;
3777         } else
3778                 phba->sli4_hba.intr_enable = 0;
3779
3780         return retval;
3781 }
3782
3783 /**
3784  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3785  * @phba: Pointer to HBA context object.
3786  * @mask: Bit mask to be checked.
3787  *
3788  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3789  * from the API jump table function pointer from the lpfc_hba struct.
3790  **/
3791 int
3792 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3793 {
3794         return phba->lpfc_sli_brdready(phba, mask);
3795 }
3796
3797 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3798
3799 /**
3800  * lpfc_reset_barrier - Make HBA ready for HBA reset
3801  * @phba: Pointer to HBA context object.
3802  *
3803  * This function is called before resetting an HBA. This function is called
3804  * with hbalock held and requests HBA to quiesce DMAs before a reset.
3805  **/
3806 void lpfc_reset_barrier(struct lpfc_hba *phba)
3807 {
3808         uint32_t __iomem *resp_buf;
3809         uint32_t __iomem *mbox_buf;
3810         volatile uint32_t mbox;
3811         uint32_t hc_copy, ha_copy, resp_data;
3812         int  i;
3813         uint8_t hdrtype;
3814
3815         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3816         if (hdrtype != 0x80 ||
3817             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3818              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3819                 return;
3820
3821         /*
3822          * Tell the other part of the chip to suspend temporarily all
3823          * its DMA activity.
3824          */
3825         resp_buf = phba->MBslimaddr;
3826
3827         /* Disable the error attention */
3828         if (lpfc_readl(phba->HCregaddr, &hc_copy))
3829                 return;
3830         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3831         readl(phba->HCregaddr); /* flush */
3832         phba->link_flag |= LS_IGNORE_ERATT;
3833
3834         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3835                 return;
3836         if (ha_copy & HA_ERATT) {
3837                 /* Clear Chip error bit */
3838                 writel(HA_ERATT, phba->HAregaddr);
3839                 phba->pport->stopped = 1;
3840         }
3841
3842         mbox = 0;
3843         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3844         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3845
3846         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3847         mbox_buf = phba->MBslimaddr;
3848         writel(mbox, mbox_buf);
3849
3850         for (i = 0; i < 50; i++) {
3851                 if (lpfc_readl((resp_buf + 1), &resp_data))
3852                         return;
3853                 if (resp_data != ~(BARRIER_TEST_PATTERN))
3854                         mdelay(1);
3855                 else
3856                         break;
3857         }
3858         resp_data = 0;
3859         if (lpfc_readl((resp_buf + 1), &resp_data))
3860                 return;
3861         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
3862                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3863                     phba->pport->stopped)
3864                         goto restore_hc;
3865                 else
3866                         goto clear_errat;
3867         }
3868
3869         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3870         resp_data = 0;
3871         for (i = 0; i < 500; i++) {
3872                 if (lpfc_readl(resp_buf, &resp_data))
3873                         return;
3874                 if (resp_data != mbox)
3875                         mdelay(1);
3876                 else
3877                         break;
3878         }
3879
3880 clear_errat:
3881
3882         while (++i < 500) {
3883                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3884                         return;
3885                 if (!(ha_copy & HA_ERATT))
3886                         mdelay(1);
3887                 else
3888                         break;
3889         }
3890
3891         if (readl(phba->HAregaddr) & HA_ERATT) {
3892                 writel(HA_ERATT, phba->HAregaddr);
3893                 phba->pport->stopped = 1;
3894         }
3895
3896 restore_hc:
3897         phba->link_flag &= ~LS_IGNORE_ERATT;
3898         writel(hc_copy, phba->HCregaddr);
3899         readl(phba->HCregaddr); /* flush */
3900 }
3901
3902 /**
3903  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3904  * @phba: Pointer to HBA context object.
3905  *
3906  * This function issues a kill_board mailbox command and waits for
3907  * the error attention interrupt. This function is called for stopping
3908  * the firmware processing. The caller is not required to hold any
3909  * locks. This function calls lpfc_hba_down_post function to free
3910  * any pending commands after the kill. The function will return 1 when it
3911  * fails to kill the board else will return 0.
3912  **/
3913 int
3914 lpfc_sli_brdkill(struct lpfc_hba *phba)
3915 {
3916         struct lpfc_sli *psli;
3917         LPFC_MBOXQ_t *pmb;
3918         uint32_t status;
3919         uint32_t ha_copy;
3920         int retval;
3921         int i = 0;
3922
3923         psli = &phba->sli;
3924
3925         /* Kill HBA */
3926         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3927                         "0329 Kill HBA Data: x%x x%x\n",
3928                         phba->pport->port_state, psli->sli_flag);
3929
3930         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3931         if (!pmb)
3932                 return 1;
3933
3934         /* Disable the error attention */
3935         spin_lock_irq(&phba->hbalock);
3936         if (lpfc_readl(phba->HCregaddr, &status)) {
3937                 spin_unlock_irq(&phba->hbalock);
3938                 mempool_free(pmb, phba->mbox_mem_pool);
3939                 return 1;
3940         }
3941         status &= ~HC_ERINT_ENA;
3942         writel(status, phba->HCregaddr);
3943         readl(phba->HCregaddr); /* flush */
3944         phba->link_flag |= LS_IGNORE_ERATT;
3945         spin_unlock_irq(&phba->hbalock);
3946
3947         lpfc_kill_board(phba, pmb);
3948         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3949         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3950
3951         if (retval != MBX_SUCCESS) {
3952                 if (retval != MBX_BUSY)
3953                         mempool_free(pmb, phba->mbox_mem_pool);
3954                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3955                                 "2752 KILL_BOARD command failed retval %d\n",
3956                                 retval);
3957                 spin_lock_irq(&phba->hbalock);
3958                 phba->link_flag &= ~LS_IGNORE_ERATT;
3959                 spin_unlock_irq(&phba->hbalock);
3960                 return 1;
3961         }
3962
3963         spin_lock_irq(&phba->hbalock);
3964         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3965         spin_unlock_irq(&phba->hbalock);
3966
3967         mempool_free(pmb, phba->mbox_mem_pool);
3968
3969         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3970          * attention every 100ms for 3 seconds. If we don't get ERATT after
3971          * 3 seconds we still set HBA_ERROR state because the status of the
3972          * board is now undefined.
3973          */
3974         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3975                 return 1;
3976         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3977                 mdelay(100);
3978                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3979                         return 1;
3980         }
3981
3982         del_timer_sync(&psli->mbox_tmo);
3983         if (ha_copy & HA_ERATT) {
3984                 writel(HA_ERATT, phba->HAregaddr);
3985                 phba->pport->stopped = 1;
3986         }
3987         spin_lock_irq(&phba->hbalock);
3988         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3989         psli->mbox_active = NULL;
3990         phba->link_flag &= ~LS_IGNORE_ERATT;
3991         spin_unlock_irq(&phba->hbalock);
3992
3993         lpfc_hba_down_post(phba);
3994         phba->link_state = LPFC_HBA_ERROR;
3995
3996         return ha_copy & HA_ERATT ? 0 : 1;
3997 }
3998
3999 /**
4000  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4001  * @phba: Pointer to HBA context object.
4002  *
4003  * This function resets the HBA by writing HC_INITFF to the control
4004  * register. After the HBA resets, this function resets all the iocb ring
4005  * indices. This function disables PCI layer parity checking during
4006  * the reset.
4007  * This function returns 0 always.
4008  * The caller is not required to hold any locks.
4009  **/
4010 int
4011 lpfc_sli_brdreset(struct lpfc_hba *phba)
4012 {
4013         struct lpfc_sli *psli;
4014         struct lpfc_sli_ring *pring;
4015         uint16_t cfg_value;
4016         int i;
4017
4018         psli = &phba->sli;
4019
4020         /* Reset HBA */
4021         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4022                         "0325 Reset HBA Data: x%x x%x\n",
4023                         phba->pport->port_state, psli->sli_flag);
4024
4025         /* perform board reset */
4026         phba->fc_eventTag = 0;
4027         phba->link_events = 0;
4028         phba->pport->fc_myDID = 0;
4029         phba->pport->fc_prevDID = 0;
4030
4031         /* Turn off parity checking and serr during the physical reset */
4032         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4033         pci_write_config_word(phba->pcidev, PCI_COMMAND,
4034                               (cfg_value &
4035                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4036
4037         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4038
4039         /* Now toggle INITFF bit in the Host Control Register */
4040         writel(HC_INITFF, phba->HCregaddr);
4041         mdelay(1);
4042         readl(phba->HCregaddr); /* flush */
4043         writel(0, phba->HCregaddr);
4044         readl(phba->HCregaddr); /* flush */
4045
4046         /* Restore PCI cmd register */
4047         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4048
4049         /* Initialize relevant SLI info */
4050         for (i = 0; i < psli->num_rings; i++) {
4051                 pring = &psli->ring[i];
4052                 pring->flag = 0;
4053                 pring->sli.sli3.rspidx = 0;
4054                 pring->sli.sli3.next_cmdidx  = 0;
4055                 pring->sli.sli3.local_getidx = 0;
4056                 pring->sli.sli3.cmdidx = 0;
4057                 pring->missbufcnt = 0;
4058         }
4059
4060         phba->link_state = LPFC_WARM_START;
4061         return 0;
4062 }
4063
4064 /**
4065  * lpfc_sli4_brdreset - Reset a sli-4 HBA
4066  * @phba: Pointer to HBA context object.
4067  *
4068  * This function resets a SLI4 HBA. This function disables PCI layer parity
4069  * checking during resets the device. The caller is not required to hold
4070  * any locks.
4071  *
4072  * This function returns 0 always.
4073  **/
4074 int
4075 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4076 {
4077         struct lpfc_sli *psli = &phba->sli;
4078         uint16_t cfg_value;
4079         int rc = 0;
4080
4081         /* Reset HBA */
4082         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4083                         "0295 Reset HBA Data: x%x x%x x%x\n",
4084                         phba->pport->port_state, psli->sli_flag,
4085                         phba->hba_flag);
4086
4087         /* perform board reset */
4088         phba->fc_eventTag = 0;
4089         phba->link_events = 0;
4090         phba->pport->fc_myDID = 0;
4091         phba->pport->fc_prevDID = 0;
4092
4093         spin_lock_irq(&phba->hbalock);
4094         psli->sli_flag &= ~(LPFC_PROCESS_LA);
4095         phba->fcf.fcf_flag = 0;
4096         spin_unlock_irq(&phba->hbalock);
4097
4098         /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4099         if (phba->hba_flag & HBA_FW_DUMP_OP) {
4100                 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4101                 return rc;
4102         }
4103
4104         /* Now physically reset the device */
4105         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4106                         "0389 Performing PCI function reset!\n");
4107
4108         /* Turn off parity checking and serr during the physical reset */
4109         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4110         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4111                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4112
4113         /* Perform FCoE PCI function reset before freeing queue memory */
4114         rc = lpfc_pci_function_reset(phba);
4115         lpfc_sli4_queue_destroy(phba);
4116
4117         /* Restore PCI cmd register */
4118         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4119
4120         return rc;
4121 }
4122
4123 /**
4124  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4125  * @phba: Pointer to HBA context object.
4126  *
4127  * This function is called in the SLI initialization code path to
4128  * restart the HBA. The caller is not required to hold any lock.
4129  * This function writes MBX_RESTART mailbox command to the SLIM and
4130  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4131  * function to free any pending commands. The function enables
4132  * POST only during the first initialization. The function returns zero.
4133  * The function does not guarantee completion of MBX_RESTART mailbox
4134  * command before the return of this function.
4135  **/
4136 static int
4137 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4138 {
4139         MAILBOX_t *mb;
4140         struct lpfc_sli *psli;
4141         volatile uint32_t word0;
4142         void __iomem *to_slim;
4143         uint32_t hba_aer_enabled;
4144
4145         spin_lock_irq(&phba->hbalock);
4146
4147         /* Take PCIe device Advanced Error Reporting (AER) state */
4148         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4149
4150         psli = &phba->sli;
4151
4152         /* Restart HBA */
4153         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4154                         "0337 Restart HBA Data: x%x x%x\n",
4155                         phba->pport->port_state, psli->sli_flag);
4156
4157         word0 = 0;
4158         mb = (MAILBOX_t *) &word0;
4159         mb->mbxCommand = MBX_RESTART;
4160         mb->mbxHc = 1;
4161
4162         lpfc_reset_barrier(phba);
4163
4164         to_slim = phba->MBslimaddr;
4165         writel(*(uint32_t *) mb, to_slim);
4166         readl(to_slim); /* flush */
4167
4168         /* Only skip post after fc_ffinit is completed */
4169         if (phba->pport->port_state)
4170                 word0 = 1;      /* This is really setting up word1 */
4171         else
4172                 word0 = 0;      /* This is really setting up word1 */
4173         to_slim = phba->MBslimaddr + sizeof (uint32_t);
4174         writel(*(uint32_t *) mb, to_slim);
4175         readl(to_slim); /* flush */
4176
4177         lpfc_sli_brdreset(phba);
4178         phba->pport->stopped = 0;
4179         phba->link_state = LPFC_INIT_START;
4180         phba->hba_flag = 0;
4181         spin_unlock_irq(&phba->hbalock);
4182
4183         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4184         psli->stats_start = get_seconds();
4185
4186         /* Give the INITFF and Post time to settle. */
4187         mdelay(100);
4188
4189         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4190         if (hba_aer_enabled)
4191                 pci_disable_pcie_error_reporting(phba->pcidev);
4192
4193         lpfc_hba_down_post(phba);
4194
4195         return 0;
4196 }
4197
4198 /**
4199  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4200  * @phba: Pointer to HBA context object.
4201  *
4202  * This function is called in the SLI initialization code path to restart
4203  * a SLI4 HBA. The caller is not required to hold any lock.
4204  * At the end of the function, it calls lpfc_hba_down_post function to
4205  * free any pending commands.
4206  **/
4207 static int
4208 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4209 {
4210         struct lpfc_sli *psli = &phba->sli;
4211         uint32_t hba_aer_enabled;
4212         int rc;
4213
4214         /* Restart HBA */
4215         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4216                         "0296 Restart HBA Data: x%x x%x\n",
4217                         phba->pport->port_state, psli->sli_flag);
4218
4219         /* Take PCIe device Advanced Error Reporting (AER) state */
4220         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4221
4222         rc = lpfc_sli4_brdreset(phba);
4223
4224         spin_lock_irq(&phba->hbalock);
4225         phba->pport->stopped = 0;
4226         phba->link_state = LPFC_INIT_START;
4227         phba->hba_flag = 0;
4228         spin_unlock_irq(&phba->hbalock);
4229
4230         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4231         psli->stats_start = get_seconds();
4232
4233         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4234         if (hba_aer_enabled)
4235                 pci_disable_pcie_error_reporting(phba->pcidev);
4236
4237         lpfc_hba_down_post(phba);
4238
4239         return rc;
4240 }
4241
4242 /**
4243  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4244  * @phba: Pointer to HBA context object.
4245  *
4246  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4247  * API jump table function pointer from the lpfc_hba struct.
4248 **/
4249 int
4250 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4251 {
4252         return phba->lpfc_sli_brdrestart(phba);
4253 }
4254
4255 /**
4256  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4257  * @phba: Pointer to HBA context object.
4258  *
4259  * This function is called after a HBA restart to wait for successful
4260  * restart of the HBA. Successful restart of the HBA is indicated by
4261  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4262  * iteration, the function will restart the HBA again. The function returns
4263  * zero if HBA successfully restarted else returns negative error code.
4264  **/
4265 static int
4266 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4267 {
4268         uint32_t status, i = 0;
4269
4270         /* Read the HBA Host Status Register */
4271         if (lpfc_readl(phba->HSregaddr, &status))
4272                 return -EIO;
4273
4274         /* Check status register to see what current state is */
4275         i = 0;
4276         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4277
4278                 /* Check every 10ms for 10 retries, then every 100ms for 90
4279                  * retries, then every 1 sec for 50 retires for a total of
4280                  * ~60 seconds before reset the board again and check every
4281                  * 1 sec for 50 retries. The up to 60 seconds before the
4282                  * board ready is required by the Falcon FIPS zeroization
4283                  * complete, and any reset the board in between shall cause
4284                  * restart of zeroization, further delay the board ready.
4285                  */
4286                 if (i++ >= 200) {
4287                         /* Adapter failed to init, timeout, status reg
4288                            <status> */
4289                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4290                                         "0436 Adapter failed to init, "
4291                                         "timeout, status reg x%x, "
4292                                         "FW Data: A8 x%x AC x%x\n", status,
4293                                         readl(phba->MBslimaddr + 0xa8),
4294                                         readl(phba->MBslimaddr + 0xac));
4295                         phba->link_state = LPFC_HBA_ERROR;
4296                         return -ETIMEDOUT;
4297                 }
4298
4299                 /* Check to see if any errors occurred during init */
4300                 if (status & HS_FFERM) {
4301                         /* ERROR: During chipset initialization */
4302                         /* Adapter failed to init, chipset, status reg
4303                            <status> */
4304                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4305                                         "0437 Adapter failed to init, "
4306                                         "chipset, status reg x%x, "
4307                                         "FW Data: A8 x%x AC x%x\n", status,
4308                                         readl(phba->MBslimaddr + 0xa8),
4309                                         readl(phba->MBslimaddr + 0xac));
4310                         phba->link_state = LPFC_HBA_ERROR;
4311                         return -EIO;
4312                 }
4313
4314                 if (i <= 10)
4315                         msleep(10);
4316                 else if (i <= 100)
4317                         msleep(100);
4318                 else
4319                         msleep(1000);
4320
4321                 if (i == 150) {
4322                         /* Do post */
4323                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4324                         lpfc_sli_brdrestart(phba);
4325                 }
4326                 /* Read the HBA Host Status Register */
4327                 if (lpfc_readl(phba->HSregaddr, &status))
4328                         return -EIO;
4329         }
4330
4331         /* Check to see if any errors occurred during init */
4332         if (status & HS_FFERM) {
4333                 /* ERROR: During chipset initialization */
4334                 /* Adapter failed to init, chipset, status reg <status> */
4335                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4336                                 "0438 Adapter failed to init, chipset, "
4337                                 "status reg x%x, "
4338                                 "FW Data: A8 x%x AC x%x\n", status,
4339                                 readl(phba->MBslimaddr + 0xa8),
4340                                 readl(phba->MBslimaddr + 0xac));
4341                 phba->link_state = LPFC_HBA_ERROR;
4342                 return -EIO;
4343         }
4344
4345         /* Clear all interrupt enable conditions */
4346         writel(0, phba->HCregaddr);
4347         readl(phba->HCregaddr); /* flush */
4348
4349         /* setup host attn register */
4350         writel(0xffffffff, phba->HAregaddr);
4351         readl(phba->HAregaddr); /* flush */
4352         return 0;
4353 }
4354
4355 /**
4356  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4357  *
4358  * This function calculates and returns the number of HBQs required to be
4359  * configured.
4360  **/
4361 int
4362 lpfc_sli_hbq_count(void)
4363 {
4364         return ARRAY_SIZE(lpfc_hbq_defs);
4365 }
4366
4367 /**
4368  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4369  *
4370  * This function adds the number of hbq entries in every HBQ to get
4371  * the total number of hbq entries required for the HBA and returns
4372  * the total count.
4373  **/
4374 static int
4375 lpfc_sli_hbq_entry_count(void)
4376 {
4377         int  hbq_count = lpfc_sli_hbq_count();
4378         int  count = 0;
4379         int  i;
4380
4381         for (i = 0; i < hbq_count; ++i)
4382                 count += lpfc_hbq_defs[i]->entry_count;
4383         return count;
4384 }
4385
4386 /**
4387  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4388  *
4389  * This function calculates amount of memory required for all hbq entries
4390  * to be configured and returns the total memory required.
4391  **/
4392 int
4393 lpfc_sli_hbq_size(void)
4394 {
4395         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4396 }
4397
4398 /**
4399  * lpfc_sli_hbq_setup - configure and initialize HBQs
4400  * @phba: Pointer to HBA context object.
4401  *
4402  * This function is called during the SLI initialization to configure
4403  * all the HBQs and post buffers to the HBQ. The caller is not
4404  * required to hold any locks. This function will return zero if successful
4405  * else it will return negative error code.
4406  **/
4407 static int
4408 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4409 {
4410         int  hbq_count = lpfc_sli_hbq_count();
4411         LPFC_MBOXQ_t *pmb;
4412         MAILBOX_t *pmbox;
4413         uint32_t hbqno;
4414         uint32_t hbq_entry_index;
4415
4416                                 /* Get a Mailbox buffer to setup mailbox
4417                                  * commands for HBA initialization
4418                                  */
4419         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4420
4421         if (!pmb)
4422                 return -ENOMEM;
4423
4424         pmbox = &pmb->u.mb;
4425
4426         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4427         phba->link_state = LPFC_INIT_MBX_CMDS;
4428         phba->hbq_in_use = 1;
4429
4430         hbq_entry_index = 0;
4431         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4432                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4433                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4434                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4435                 phba->hbqs[hbqno].entry_count =
4436                         lpfc_hbq_defs[hbqno]->entry_count;
4437                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4438                         hbq_entry_index, pmb);
4439                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4440
4441                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4442                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4443                            mbxStatus <status>, ring <num> */
4444
4445                         lpfc_printf_log(phba, KERN_ERR,
4446                                         LOG_SLI | LOG_VPORT,
4447                                         "1805 Adapter failed to init. "
4448                                         "Data: x%x x%x x%x\n",
4449                                         pmbox->mbxCommand,
4450                                         pmbox->mbxStatus, hbqno);
4451
4452                         phba->link_state = LPFC_HBA_ERROR;
4453                         mempool_free(pmb, phba->mbox_mem_pool);
4454                         return -ENXIO;
4455                 }
4456         }
4457         phba->hbq_count = hbq_count;
4458
4459         mempool_free(pmb, phba->mbox_mem_pool);
4460
4461         /* Initially populate or replenish the HBQs */
4462         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4463                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4464         return 0;
4465 }
4466
4467 /**
4468  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4469  * @phba: Pointer to HBA context object.
4470  *
4471  * This function is called during the SLI initialization to configure
4472  * all the HBQs and post buffers to the HBQ. The caller is not
4473  * required to hold any locks. This function will return zero if successful
4474  * else it will return negative error code.
4475  **/
4476 static int
4477 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4478 {
4479         phba->hbq_in_use = 1;
4480         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4481         phba->hbq_count = 1;
4482         /* Initially populate or replenish the HBQs */
4483         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4484         return 0;
4485 }
4486
4487 /**
4488  * lpfc_sli_config_port - Issue config port mailbox command
4489  * @phba: Pointer to HBA context object.
4490  * @sli_mode: sli mode - 2/3
4491  *
4492  * This function is called by the sli intialization code path
4493  * to issue config_port mailbox command. This function restarts the
4494  * HBA firmware and issues a config_port mailbox command to configure
4495  * the SLI interface in the sli mode specified by sli_mode
4496  * variable. The caller is not required to hold any locks.
4497  * The function returns 0 if successful, else returns negative error
4498  * code.
4499  **/
4500 int
4501 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4502 {
4503         LPFC_MBOXQ_t *pmb;
4504         uint32_t resetcount = 0, rc = 0, done = 0;
4505
4506         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4507         if (!pmb) {
4508                 phba->link_state = LPFC_HBA_ERROR;
4509                 return -ENOMEM;
4510         }
4511
4512         phba->sli_rev = sli_mode;
4513         while (resetcount < 2 && !done) {
4514                 spin_lock_irq(&phba->hbalock);
4515                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4516                 spin_unlock_irq(&phba->hbalock);
4517                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4518                 lpfc_sli_brdrestart(phba);
4519                 rc = lpfc_sli_chipset_init(phba);
4520                 if (rc)
4521                         break;
4522
4523                 spin_lock_irq(&phba->hbalock);
4524                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4525                 spin_unlock_irq(&phba->hbalock);
4526                 resetcount++;
4527
4528                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4529                  * value of 0 means the call was successful.  Any other
4530                  * nonzero value is a failure, but if ERESTART is returned,
4531                  * the driver may reset the HBA and try again.
4532                  */
4533                 rc = lpfc_config_port_prep(phba);
4534                 if (rc == -ERESTART) {
4535                         phba->link_state = LPFC_LINK_UNKNOWN;
4536                         continue;
4537                 } else if (rc)
4538                         break;
4539
4540                 phba->link_state = LPFC_INIT_MBX_CMDS;
4541                 lpfc_config_port(phba, pmb);
4542                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4543                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4544                                         LPFC_SLI3_HBQ_ENABLED |
4545                                         LPFC_SLI3_CRP_ENABLED |
4546                                         LPFC_SLI3_BG_ENABLED |
4547                                         LPFC_SLI3_DSS_ENABLED);
4548                 if (rc != MBX_SUCCESS) {
4549                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4550                                 "0442 Adapter failed to init, mbxCmd x%x "
4551                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4552                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4553                         spin_lock_irq(&phba->hbalock);
4554                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4555                         spin_unlock_irq(&phba->hbalock);
4556                         rc = -ENXIO;
4557                 } else {
4558                         /* Allow asynchronous mailbox command to go through */
4559                         spin_lock_irq(&phba->hbalock);
4560                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4561                         spin_unlock_irq(&phba->hbalock);
4562                         done = 1;
4563
4564                         if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4565                             (pmb->u.mb.un.varCfgPort.gasabt == 0))
4566                                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4567                                         "3110 Port did not grant ASABT\n");
4568                 }
4569         }
4570         if (!done) {
4571                 rc = -EINVAL;
4572                 goto do_prep_failed;
4573         }
4574         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4575                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4576                         rc = -ENXIO;
4577                         goto do_prep_failed;
4578                 }
4579                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4580                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4581                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4582                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4583                                 phba->max_vpi : phba->max_vports;
4584
4585                 } else
4586                         phba->max_vpi = 0;
4587                 phba->fips_level = 0;
4588                 phba->fips_spec_rev = 0;
4589                 if (pmb->u.mb.un.varCfgPort.gdss) {
4590                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4591                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4592                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4593                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4594                                         "2850 Security Crypto Active. FIPS x%d "
4595                                         "(Spec Rev: x%d)",
4596                                         phba->fips_level, phba->fips_spec_rev);
4597                 }
4598                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4599                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4600                                         "2856 Config Port Security Crypto "
4601                                         "Error: x%x ",
4602                                         pmb->u.mb.un.varCfgPort.sec_err);
4603                 }
4604                 if (pmb->u.mb.un.varCfgPort.gerbm)
4605                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4606                 if (pmb->u.mb.un.varCfgPort.gcrp)
4607                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4608
4609                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4610                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4611
4612                 if (phba->cfg_enable_bg) {
4613                         if (pmb->u.mb.un.varCfgPort.gbg)
4614                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4615                         else
4616                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4617                                                 "0443 Adapter did not grant "
4618                                                 "BlockGuard\n");
4619                 }
4620         } else {
4621                 phba->hbq_get = NULL;
4622                 phba->port_gp = phba->mbox->us.s2.port;
4623                 phba->max_vpi = 0;
4624         }
4625 do_prep_failed:
4626         mempool_free(pmb, phba->mbox_mem_pool);
4627         return rc;
4628 }
4629
4630
4631 /**
4632  * lpfc_sli_hba_setup - SLI intialization function
4633  * @phba: Pointer to HBA context object.
4634  *
4635  * This function is the main SLI intialization function. This function
4636  * is called by the HBA intialization code, HBA reset code and HBA
4637  * error attention handler code. Caller is not required to hold any
4638  * locks. This function issues config_port mailbox command to configure
4639  * the SLI, setup iocb rings and HBQ rings. In the end the function
4640  * calls the config_port_post function to issue init_link mailbox
4641  * command and to start the discovery. The function will return zero
4642  * if successful, else it will return negative error code.
4643  **/
4644 int
4645 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4646 {
4647         uint32_t rc;
4648         int  mode = 3, i;
4649         int longs;
4650
4651         switch (lpfc_sli_mode) {
4652         case 2:
4653                 if (phba->cfg_enable_npiv) {
4654                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4655                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4656                                 "parameter (%d) to auto (0).\n",
4657                                 lpfc_sli_mode);
4658                         break;
4659                 }
4660                 mode = 2;
4661                 break;
4662         case 0:
4663         case 3:
4664                 break;
4665         default:
4666                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4667                                 "1819 Unrecognized lpfc_sli_mode "
4668                                 "parameter: %d.\n", lpfc_sli_mode);
4669
4670                 break;
4671         }
4672
4673         rc = lpfc_sli_config_port(phba, mode);
4674
4675         if (rc && lpfc_sli_mode == 3)
4676                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4677                                 "1820 Unable to select SLI-3.  "
4678                                 "Not supported by adapter.\n");
4679         if (rc && mode != 2)
4680                 rc = lpfc_sli_config_port(phba, 2);
4681         if (rc)
4682                 goto lpfc_sli_hba_setup_error;
4683
4684         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4685         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4686                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4687                 if (!rc) {
4688                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4689                                         "2709 This device supports "
4690                                         "Advanced Error Reporting (AER)\n");
4691                         spin_lock_irq(&phba->hbalock);
4692                         phba->hba_flag |= HBA_AER_ENABLED;
4693                         spin_unlock_irq(&phba->hbalock);
4694                 } else {
4695                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4696                                         "2708 This device does not support "
4697                                         "Advanced Error Reporting (AER): %d\n",
4698                                         rc);
4699                         phba->cfg_aer_support = 0;
4700                 }
4701         }
4702
4703         if (phba->sli_rev == 3) {
4704                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4705                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4706         } else {
4707                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4708                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4709                 phba->sli3_options = 0;
4710         }
4711
4712         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4713                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4714                         phba->sli_rev, phba->max_vpi);
4715         rc = lpfc_sli_ring_map(phba);
4716
4717         if (rc)
4718                 goto lpfc_sli_hba_setup_error;
4719
4720         /* Initialize VPIs. */
4721         if (phba->sli_rev == LPFC_SLI_REV3) {
4722                 /*
4723                  * The VPI bitmask and physical ID array are allocated
4724                  * and initialized once only - at driver load.  A port
4725                  * reset doesn't need to reinitialize this memory.
4726                  */
4727                 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4728                         longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4729                         phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4730                                                   GFP_KERNEL);
4731                         if (!phba->vpi_bmask) {
4732                                 rc = -ENOMEM;
4733                                 goto lpfc_sli_hba_setup_error;
4734                         }
4735
4736                         phba->vpi_ids = kzalloc(
4737                                         (phba->max_vpi+1) * sizeof(uint16_t),
4738                                         GFP_KERNEL);
4739                         if (!phba->vpi_ids) {
4740                                 kfree(phba->vpi_bmask);
4741                                 rc = -ENOMEM;
4742                                 goto lpfc_sli_hba_setup_error;
4743                         }
4744                         for (i = 0; i < phba->max_vpi; i++)
4745                                 phba->vpi_ids[i] = i;
4746                 }
4747         }
4748
4749         /* Init HBQs */
4750         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4751                 rc = lpfc_sli_hbq_setup(phba);
4752                 if (rc)
4753                         goto lpfc_sli_hba_setup_error;
4754         }
4755         spin_lock_irq(&phba->hbalock);
4756         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4757         spin_unlock_irq(&phba->hbalock);
4758
4759         rc = lpfc_config_port_post(phba);
4760         if (rc)
4761                 goto lpfc_sli_hba_setup_error;
4762
4763         return rc;
4764
4765 lpfc_sli_hba_setup_error:
4766         phba->link_state = LPFC_HBA_ERROR;
4767         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4768                         "0445 Firmware initialization failed\n");
4769         return rc;
4770 }
4771
4772 /**
4773  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4774  * @phba: Pointer to HBA context object.
4775  * @mboxq: mailbox pointer.
4776  * This function issue a dump mailbox command to read config region
4777  * 23 and parse the records in the region and populate driver
4778  * data structure.
4779  **/
4780 static int
4781 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4782 {
4783         LPFC_MBOXQ_t *mboxq;
4784         struct lpfc_dmabuf *mp;
4785         struct lpfc_mqe *mqe;
4786         uint32_t data_length;
4787         int rc;
4788
4789         /* Program the default value of vlan_id and fc_map */
4790         phba->valid_vlan = 0;
4791         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4792         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4793         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4794
4795         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4796         if (!mboxq)
4797                 return -ENOMEM;
4798
4799         mqe = &mboxq->u.mqe;
4800         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4801                 rc = -ENOMEM;
4802                 goto out_free_mboxq;
4803         }
4804
4805         mp = (struct lpfc_dmabuf *) mboxq->context1;
4806         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4807
4808         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4809                         "(%d):2571 Mailbox cmd x%x Status x%x "
4810                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4811                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4812                         "CQ: x%x x%x x%x x%x\n",
4813                         mboxq->vport ? mboxq->vport->vpi : 0,
4814                         bf_get(lpfc_mqe_command, mqe),
4815                         bf_get(lpfc_mqe_status, mqe),
4816                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4817                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4818                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4819                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4820                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4821                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4822                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4823                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4824                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4825                         mboxq->mcqe.word0,
4826                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4827                         mboxq->mcqe.trailer);
4828
4829         if (rc) {
4830                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4831                 kfree(mp);
4832                 rc = -EIO;
4833                 goto out_free_mboxq;
4834         }
4835         data_length = mqe->un.mb_words[5];
4836         if (data_length > DMP_RGN23_SIZE) {
4837                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4838                 kfree(mp);
4839                 rc = -EIO;
4840                 goto out_free_mboxq;
4841         }
4842
4843         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4844         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4845         kfree(mp);
4846         rc = 0;
4847
4848 out_free_mboxq:
4849         mempool_free(mboxq, phba->mbox_mem_pool);
4850         return rc;
4851 }
4852
4853 /**
4854  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4855  * @phba: pointer to lpfc hba data structure.
4856  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4857  * @vpd: pointer to the memory to hold resulting port vpd data.
4858  * @vpd_size: On input, the number of bytes allocated to @vpd.
4859  *            On output, the number of data bytes in @vpd.
4860  *
4861  * This routine executes a READ_REV SLI4 mailbox command.  In
4862  * addition, this routine gets the port vpd data.
4863  *
4864  * Return codes
4865  *      0 - successful
4866  *      -ENOMEM - could not allocated memory.
4867  **/
4868 static int
4869 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4870                     uint8_t *vpd, uint32_t *vpd_size)
4871 {
4872         int rc = 0;
4873         uint32_t dma_size;
4874         struct lpfc_dmabuf *dmabuf;
4875         struct lpfc_mqe *mqe;
4876
4877         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4878         if (!dmabuf)
4879                 return -ENOMEM;
4880
4881         /*
4882          * Get a DMA buffer for the vpd data resulting from the READ_REV
4883          * mailbox command.
4884          */
4885         dma_size = *vpd_size;
4886         dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4887                                            &dmabuf->phys, GFP_KERNEL);
4888         if (!dmabuf->virt) {
4889                 kfree(dmabuf);
4890                 return -ENOMEM;
4891         }
4892
4893         /*
4894          * The SLI4 implementation of READ_REV conflicts at word1,
4895          * bits 31:16 and SLI4 adds vpd functionality not present
4896          * in SLI3.  This code corrects the conflicts.
4897          */
4898         lpfc_read_rev(phba, mboxq);
4899         mqe = &mboxq->u.mqe;
4900         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4901         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4902         mqe->un.read_rev.word1 &= 0x0000FFFF;
4903         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4904         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4905
4906         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4907         if (rc) {
4908                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4909                                   dmabuf->virt, dmabuf->phys);
4910                 kfree(dmabuf);
4911                 return -EIO;
4912         }
4913
4914         /*
4915          * The available vpd length cannot be bigger than the
4916          * DMA buffer passed to the port.  Catch the less than
4917          * case and update the caller's size.
4918          */
4919         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4920                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4921
4922         memcpy(vpd, dmabuf->virt, *vpd_size);
4923
4924         dma_free_coherent(&phba->pcidev->dev, dma_size,
4925                           dmabuf->virt, dmabuf->phys);
4926         kfree(dmabuf);
4927         return 0;
4928 }
4929
4930 /**
4931  * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4932  * @phba: pointer to lpfc hba data structure.
4933  *
4934  * This routine retrieves SLI4 device physical port name this PCI function
4935  * is attached to.
4936  *
4937  * Return codes
4938  *      0 - successful
4939  *      otherwise - failed to retrieve physical port name
4940  **/
4941 static int
4942 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4943 {
4944         LPFC_MBOXQ_t *mboxq;
4945         struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4946         struct lpfc_controller_attribute *cntl_attr;
4947         struct lpfc_mbx_get_port_name *get_port_name;
4948         void *virtaddr = NULL;
4949         uint32_t alloclen, reqlen;
4950         uint32_t shdr_status, shdr_add_status;
4951         union lpfc_sli4_cfg_shdr *shdr;
4952         char cport_name = 0;
4953         int rc;
4954
4955         /* We assume nothing at this point */
4956         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4957         phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4958
4959         mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4960         if (!mboxq)
4961                 return -ENOMEM;
4962         /* obtain link type and link number via READ_CONFIG */
4963         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4964         lpfc_sli4_read_config(phba);
4965         if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4966                 goto retrieve_ppname;
4967
4968         /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4969         reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4970         alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4971                         LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4972                         LPFC_SLI4_MBX_NEMBED);
4973         if (alloclen < reqlen) {
4974                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4975                                 "3084 Allocated DMA memory size (%d) is "
4976                                 "less than the requested DMA memory size "
4977                                 "(%d)\n", alloclen, reqlen);
4978                 rc = -ENOMEM;
4979                 goto out_free_mboxq;
4980         }
4981         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4982         virtaddr = mboxq->sge_array->addr[0];
4983         mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4984         shdr = &mbx_cntl_attr->cfg_shdr;
4985         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4986         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4987         if (shdr_status || shdr_add_status || rc) {
4988                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4989                                 "3085 Mailbox x%x (x%x/x%x) failed, "
4990                                 "rc:x%x, status:x%x, add_status:x%x\n",
4991                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4992                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4993                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4994                                 rc, shdr_status, shdr_add_status);
4995                 rc = -ENXIO;
4996                 goto out_free_mboxq;
4997         }
4998         cntl_attr = &mbx_cntl_attr->cntl_attr;
4999         phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5000         phba->sli4_hba.lnk_info.lnk_tp =
5001                 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5002         phba->sli4_hba.lnk_info.lnk_no =
5003                 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5004         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5005                         "3086 lnk_type:%d, lnk_numb:%d\n",
5006                         phba->sli4_hba.lnk_info.lnk_tp,
5007                         phba->sli4_hba.lnk_info.lnk_no);
5008
5009 retrieve_ppname:
5010         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5011                 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5012                 sizeof(struct lpfc_mbx_get_port_name) -
5013                 sizeof(struct lpfc_sli4_cfg_mhdr),
5014                 LPFC_SLI4_MBX_EMBED);
5015         get_port_name = &mboxq->u.mqe.un.get_port_name;
5016         shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5017         bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5018         bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5019                 phba->sli4_hba.lnk_info.lnk_tp);
5020         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5021         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5022         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5023         if (shdr_status || shdr_add_status || rc) {
5024                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5025                                 "3087 Mailbox x%x (x%x/x%x) failed: "
5026                                 "rc:x%x, status:x%x, add_status:x%x\n",
5027                                 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5028                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5029                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5030                                 rc, shdr_status, shdr_add_status);
5031                 rc = -ENXIO;
5032                 goto out_free_mboxq;
5033         }
5034         switch (phba->sli4_hba.lnk_info.lnk_no) {
5035         case LPFC_LINK_NUMBER_0:
5036                 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5037                                 &get_port_name->u.response);
5038                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5039                 break;
5040         case LPFC_LINK_NUMBER_1:
5041                 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5042                                 &get_port_name->u.response);
5043                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5044                 break;
5045         case LPFC_LINK_NUMBER_2:
5046                 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5047                                 &get_port_name->u.response);
5048                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5049                 break;
5050         case LPFC_LINK_NUMBER_3:
5051                 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5052                                 &get_port_name->u.response);
5053                 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5054                 break;
5055         default:
5056                 break;
5057         }
5058
5059         if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5060                 phba->Port[0] = cport_name;
5061                 phba->Port[1] = '\0';
5062                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5063                                 "3091 SLI get port name: %s\n", phba->Port);
5064         }
5065
5066 out_free_mboxq:
5067         if (rc != MBX_TIMEOUT) {
5068                 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5069                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
5070                 else
5071                         mempool_free(mboxq, phba->mbox_mem_pool);
5072         }
5073         return rc;
5074 }
5075
5076 /**
5077  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5078  * @phba: pointer to lpfc hba data structure.
5079  *
5080  * This routine is called to explicitly arm the SLI4 device's completion and
5081  * event queues
5082  **/
5083 static void
5084 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5085 {
5086         int fcp_eqidx;
5087
5088         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5089         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
5090         fcp_eqidx = 0;
5091         if (phba->sli4_hba.fcp_cq) {
5092                 do {
5093                         lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5094                                              LPFC_QUEUE_REARM);
5095                 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
5096         }
5097
5098         if (phba->cfg_fof)
5099                 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5100
5101         if (phba->sli4_hba.hba_eq) {
5102                 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
5103                      fcp_eqidx++)
5104                         lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
5105                                              LPFC_QUEUE_REARM);
5106         }
5107
5108         if (phba->cfg_fof)
5109                 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
5110 }
5111
5112 /**
5113  * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5114  * @phba: Pointer to HBA context object.
5115  * @type: The resource extent type.
5116  * @extnt_count: buffer to hold port available extent count.
5117  * @extnt_size: buffer to hold element count per extent.
5118  *
5119  * This function calls the port and retrievs the number of available
5120  * extents and their size for a particular extent type.
5121  *
5122  * Returns: 0 if successful.  Nonzero otherwise.
5123  **/
5124 int
5125 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5126                                uint16_t *extnt_count, uint16_t *extnt_size)
5127 {
5128         int rc = 0;
5129         uint32_t length;
5130         uint32_t mbox_tmo;
5131         struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5132         LPFC_MBOXQ_t *mbox;
5133
5134         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5135         if (!mbox)
5136                 return -ENOMEM;
5137
5138         /* Find out how many extents are available for this resource type */
5139         length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5140                   sizeof(struct lpfc_sli4_cfg_mhdr));
5141         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5142                          LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5143                          length, LPFC_SLI4_MBX_EMBED);
5144
5145         /* Send an extents count of 0 - the GET doesn't use it. */
5146         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5147                                         LPFC_SLI4_MBX_EMBED);
5148         if (unlikely(rc)) {
5149                 rc = -EIO;
5150                 goto err_exit;
5151         }
5152
5153         if (!phba->sli4_hba.intr_enable)
5154                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5155         else {
5156                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5157                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5158         }
5159         if (unlikely(rc)) {
5160                 rc = -EIO;
5161                 goto err_exit;
5162         }
5163
5164         rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5165         if (bf_get(lpfc_mbox_hdr_status,
5166                    &rsrc_info->header.cfg_shdr.response)) {
5167                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5168                                 "2930 Failed to get resource extents "
5169                                 "Status 0x%x Add'l Status 0x%x\n",
5170                                 bf_get(lpfc_mbox_hdr_status,
5171                                        &rsrc_info->header.cfg_shdr.response),
5172                                 bf_get(lpfc_mbox_hdr_add_status,
5173                                        &rsrc_info->header.cfg_shdr.response));
5174                 rc = -EIO;
5175                 goto err_exit;
5176         }
5177
5178         *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5179                               &rsrc_info->u.rsp);
5180         *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5181                              &rsrc_info->u.rsp);
5182
5183         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5184                         "3162 Retrieved extents type-%d from port: count:%d, "
5185                         "size:%d\n", type, *extnt_count, *extnt_size);
5186
5187 err_exit:
5188         mempool_free(mbox, phba->mbox_mem_pool);
5189         return rc;
5190 }
5191
5192 /**
5193  * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5194  * @phba: Pointer to HBA context object.
5195  * @type: The extent type to check.
5196  *
5197  * This function reads the current available extents from the port and checks
5198  * if the extent count or extent size has changed since the last access.
5199  * Callers use this routine post port reset to understand if there is a
5200  * extent reprovisioning requirement.
5201  *
5202  * Returns:
5203  *   -Error: error indicates problem.
5204  *   1: Extent count or size has changed.
5205  *   0: No changes.
5206  **/
5207 static int
5208 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5209 {
5210         uint16_t curr_ext_cnt, rsrc_ext_cnt;
5211         uint16_t size_diff, rsrc_ext_size;
5212         int rc = 0;
5213         struct lpfc_rsrc_blks *rsrc_entry;
5214         struct list_head *rsrc_blk_list = NULL;
5215
5216         size_diff = 0;
5217         curr_ext_cnt = 0;
5218         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5219                                             &rsrc_ext_cnt,
5220                                             &rsrc_ext_size);
5221         if (unlikely(rc))
5222                 return -EIO;
5223
5224         switch (type) {
5225         case LPFC_RSC_TYPE_FCOE_RPI:
5226                 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5227                 break;
5228         case LPFC_RSC_TYPE_FCOE_VPI:
5229                 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5230                 break;
5231         case LPFC_RSC_TYPE_FCOE_XRI:
5232                 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5233                 break;
5234         case LPFC_RSC_TYPE_FCOE_VFI:
5235                 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5236                 break;
5237         default:
5238                 break;
5239         }
5240
5241         list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5242                 curr_ext_cnt++;
5243                 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5244                         size_diff++;
5245         }
5246
5247         if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5248                 rc = 1;
5249
5250         return rc;
5251 }
5252
5253 /**
5254  * lpfc_sli4_cfg_post_extnts -
5255  * @phba: Pointer to HBA context object.
5256  * @extnt_cnt - number of available extents.
5257  * @type - the extent type (rpi, xri, vfi, vpi).
5258  * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5259  * @mbox - pointer to the caller's allocated mailbox structure.
5260  *
5261  * This function executes the extents allocation request.  It also
5262  * takes care of the amount of memory needed to allocate or get the
5263  * allocated extents. It is the caller's responsibility to evaluate
5264  * the response.
5265  *
5266  * Returns:
5267  *   -Error:  Error value describes the condition found.
5268  *   0: if successful
5269  **/
5270 static int
5271 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5272                           uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5273 {
5274         int rc = 0;
5275         uint32_t req_len;
5276         uint32_t emb_len;
5277         uint32_t alloc_len, mbox_tmo;
5278
5279         /* Calculate the total requested length of the dma memory */
5280         req_len = extnt_cnt * sizeof(uint16_t);
5281
5282         /*
5283          * Calculate the size of an embedded mailbox.  The uint32_t
5284          * accounts for extents-specific word.
5285          */
5286         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5287                 sizeof(uint32_t);
5288
5289         /*
5290          * Presume the allocation and response will fit into an embedded
5291          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5292          */
5293         *emb = LPFC_SLI4_MBX_EMBED;
5294         if (req_len > emb_len) {
5295                 req_len = extnt_cnt * sizeof(uint16_t) +
5296                         sizeof(union lpfc_sli4_cfg_shdr) +
5297                         sizeof(uint32_t);
5298                 *emb = LPFC_SLI4_MBX_NEMBED;
5299         }
5300
5301         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5302                                      LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5303                                      req_len, *emb);
5304         if (alloc_len < req_len) {
5305                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5306                         "2982 Allocated DMA memory size (x%x) is "
5307                         "less than the requested DMA memory "
5308                         "size (x%x)\n", alloc_len, req_len);
5309                 return -ENOMEM;
5310         }
5311         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5312         if (unlikely(rc))
5313                 return -EIO;
5314
5315         if (!phba->sli4_hba.intr_enable)
5316                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5317         else {
5318                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5319                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5320         }
5321
5322         if (unlikely(rc))
5323                 rc = -EIO;
5324         return rc;
5325 }
5326
5327 /**
5328  * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5329  * @phba: Pointer to HBA context object.
5330  * @type:  The resource extent type to allocate.
5331  *
5332  * This function allocates the number of elements for the specified
5333  * resource type.
5334  **/
5335 static int
5336 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5337 {
5338         bool emb = false;
5339         uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5340         uint16_t rsrc_id, rsrc_start, j, k;
5341         uint16_t *ids;
5342         int i, rc;
5343         unsigned long longs;
5344         unsigned long *bmask;
5345         struct lpfc_rsrc_blks *rsrc_blks;
5346         LPFC_MBOXQ_t *mbox;
5347         uint32_t length;
5348         struct lpfc_id_range *id_array = NULL;
5349         void *virtaddr = NULL;
5350         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5351         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5352         struct list_head *ext_blk_list;
5353
5354         rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5355                                             &rsrc_cnt,
5356                                             &rsrc_size);
5357         if (unlikely(rc))
5358                 return -EIO;
5359
5360         if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5361                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5362                         "3009 No available Resource Extents "
5363                         "for resource type 0x%x: Count: 0x%x, "
5364                         "Size 0x%x\n", type, rsrc_cnt,
5365                         rsrc_size);
5366                 return -ENOMEM;
5367         }
5368
5369         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5370                         "2903 Post resource extents type-0x%x: "
5371                         "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5372
5373         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5374         if (!mbox)
5375                 return -ENOMEM;
5376
5377         rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5378         if (unlikely(rc)) {
5379                 rc = -EIO;
5380                 goto err_exit;
5381         }
5382
5383         /*
5384          * Figure out where the response is located.  Then get local pointers
5385          * to the response data.  The port does not guarantee to respond to
5386          * all extents counts request so update the local variable with the
5387          * allocated count from the port.
5388          */
5389         if (emb == LPFC_SLI4_MBX_EMBED) {
5390                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5391                 id_array = &rsrc_ext->u.rsp.id[0];
5392                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5393         } else {
5394                 virtaddr = mbox->sge_array->addr[0];
5395                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5396                 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5397                 id_array = &n_rsrc->id;
5398         }
5399
5400         longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5401         rsrc_id_cnt = rsrc_cnt * rsrc_size;
5402
5403         /*
5404          * Based on the resource size and count, correct the base and max
5405          * resource values.
5406          */
5407         length = sizeof(struct lpfc_rsrc_blks);
5408         switch (type) {
5409         case LPFC_RSC_TYPE_FCOE_RPI:
5410                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5411                                                    sizeof(unsigned long),
5412                                                    GFP_KERNEL);
5413                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5414                         rc = -ENOMEM;
5415                         goto err_exit;
5416                 }
5417                 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5418                                                  sizeof(uint16_t),
5419                                                  GFP_KERNEL);
5420                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5421                         kfree(phba->sli4_hba.rpi_bmask);
5422                         rc = -ENOMEM;
5423                         goto err_exit;
5424                 }
5425
5426                 /*
5427                  * The next_rpi was initialized with the maximum available
5428                  * count but the port may allocate a smaller number.  Catch
5429                  * that case and update the next_rpi.
5430                  */
5431                 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5432
5433                 /* Initialize local ptrs for common extent processing later. */
5434                 bmask = phba->sli4_hba.rpi_bmask;
5435                 ids = phba->sli4_hba.rpi_ids;
5436                 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5437                 break;
5438         case LPFC_RSC_TYPE_FCOE_VPI:
5439                 phba->vpi_bmask = kzalloc(longs *
5440                                           sizeof(unsigned long),
5441                                           GFP_KERNEL);
5442                 if (unlikely(!phba->vpi_bmask)) {
5443                         rc = -ENOMEM;
5444                         goto err_exit;
5445                 }
5446                 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5447                                          sizeof(uint16_t),
5448                                          GFP_KERNEL);
5449                 if (unlikely(!phba->vpi_ids)) {
5450                         kfree(phba->vpi_bmask);
5451                         rc = -ENOMEM;
5452                         goto err_exit;
5453                 }
5454
5455                 /* Initialize local ptrs for common extent processing later. */
5456                 bmask = phba->vpi_bmask;
5457                 ids = phba->vpi_ids;
5458                 ext_blk_list = &phba->lpfc_vpi_blk_list;
5459                 break;
5460         case LPFC_RSC_TYPE_FCOE_XRI:
5461                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5462                                                    sizeof(unsigned long),
5463                                                    GFP_KERNEL);
5464                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5465                         rc = -ENOMEM;
5466                         goto err_exit;
5467                 }
5468                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5469                 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5470                                                  sizeof(uint16_t),
5471                                                  GFP_KERNEL);
5472                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5473                         kfree(phba->sli4_hba.xri_bmask);
5474                         rc = -ENOMEM;
5475                         goto err_exit;
5476                 }
5477
5478                 /* Initialize local ptrs for common extent processing later. */
5479                 bmask = phba->sli4_hba.xri_bmask;
5480                 ids = phba->sli4_hba.xri_ids;
5481                 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5482                 break;
5483         case LPFC_RSC_TYPE_FCOE_VFI:
5484                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5485                                                    sizeof(unsigned long),
5486                                                    GFP_KERNEL);
5487                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5488                         rc = -ENOMEM;
5489                         goto err_exit;
5490                 }
5491                 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5492                                                  sizeof(uint16_t),
5493                                                  GFP_KERNEL);
5494                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5495                         kfree(phba->sli4_hba.vfi_bmask);
5496                         rc = -ENOMEM;
5497                         goto err_exit;
5498                 }
5499
5500                 /* Initialize local ptrs for common extent processing later. */
5501                 bmask = phba->sli4_hba.vfi_bmask;
5502                 ids = phba->sli4_hba.vfi_ids;
5503                 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5504                 break;
5505         default:
5506                 /* Unsupported Opcode.  Fail call. */
5507                 id_array = NULL;
5508                 bmask = NULL;
5509                 ids = NULL;
5510                 ext_blk_list = NULL;
5511                 goto err_exit;
5512         }
5513
5514         /*
5515          * Complete initializing the extent configuration with the
5516          * allocated ids assigned to this function.  The bitmask serves
5517          * as an index into the array and manages the available ids.  The
5518          * array just stores the ids communicated to the port via the wqes.
5519          */
5520         for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5521                 if ((i % 2) == 0)
5522                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5523                                          &id_array[k]);
5524                 else
5525                         rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5526                                          &id_array[k]);
5527
5528                 rsrc_blks = kzalloc(length, GFP_KERNEL);
5529                 if (unlikely(!rsrc_blks)) {
5530                         rc = -ENOMEM;
5531                         kfree(bmask);
5532                         kfree(ids);
5533                         goto err_exit;
5534                 }
5535                 rsrc_blks->rsrc_start = rsrc_id;
5536                 rsrc_blks->rsrc_size = rsrc_size;
5537                 list_add_tail(&rsrc_blks->list, ext_blk_list);
5538                 rsrc_start = rsrc_id;
5539                 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5540                         phba->sli4_hba.scsi_xri_start = rsrc_start +
5541                                 lpfc_sli4_get_els_iocb_cnt(phba);
5542
5543                 while (rsrc_id < (rsrc_start + rsrc_size)) {
5544                         ids[j] = rsrc_id;
5545                         rsrc_id++;
5546                         j++;
5547                 }
5548                 /* Entire word processed.  Get next word.*/
5549                 if ((i % 2) == 1)
5550                         k++;
5551         }
5552  err_exit:
5553         lpfc_sli4_mbox_cmd_free(phba, mbox);
5554         return rc;
5555 }
5556
5557 /**
5558  * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5559  * @phba: Pointer to HBA context object.
5560  * @type: the extent's type.
5561  *
5562  * This function deallocates all extents of a particular resource type.
5563  * SLI4 does not allow for deallocating a particular extent range.  It
5564  * is the caller's responsibility to release all kernel memory resources.
5565  **/
5566 static int
5567 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5568 {
5569         int rc;
5570         uint32_t length, mbox_tmo = 0;
5571         LPFC_MBOXQ_t *mbox;
5572         struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5573         struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5574
5575         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5576         if (!mbox)
5577                 return -ENOMEM;
5578
5579         /*
5580          * This function sends an embedded mailbox because it only sends the
5581          * the resource type.  All extents of this type are released by the
5582          * port.
5583          */
5584         length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5585                   sizeof(struct lpfc_sli4_cfg_mhdr));
5586         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5587                          LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5588                          length, LPFC_SLI4_MBX_EMBED);
5589
5590         /* Send an extents count of 0 - the dealloc doesn't use it. */
5591         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5592                                         LPFC_SLI4_MBX_EMBED);
5593         if (unlikely(rc)) {
5594                 rc = -EIO;
5595                 goto out_free_mbox;
5596         }
5597         if (!phba->sli4_hba.intr_enable)
5598                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5599         else {
5600                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5601                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5602         }
5603         if (unlikely(rc)) {
5604                 rc = -EIO;
5605                 goto out_free_mbox;
5606         }
5607
5608         dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5609         if (bf_get(lpfc_mbox_hdr_status,
5610                    &dealloc_rsrc->header.cfg_shdr.response)) {
5611                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5612                                 "2919 Failed to release resource extents "
5613                                 "for type %d - Status 0x%x Add'l Status 0x%x. "
5614                                 "Resource memory not released.\n",
5615                                 type,
5616                                 bf_get(lpfc_mbox_hdr_status,
5617                                     &dealloc_rsrc->header.cfg_shdr.response),
5618                                 bf_get(lpfc_mbox_hdr_add_status,
5619                                     &dealloc_rsrc->header.cfg_shdr.response));
5620                 rc = -EIO;
5621                 goto out_free_mbox;
5622         }
5623
5624         /* Release kernel memory resources for the specific type. */
5625         switch (type) {
5626         case LPFC_RSC_TYPE_FCOE_VPI:
5627                 kfree(phba->vpi_bmask);
5628                 kfree(phba->vpi_ids);
5629                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5630                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5631                                     &phba->lpfc_vpi_blk_list, list) {
5632                         list_del_init(&rsrc_blk->list);
5633                         kfree(rsrc_blk);
5634                 }
5635                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5636                 break;
5637         case LPFC_RSC_TYPE_FCOE_XRI:
5638                 kfree(phba->sli4_hba.xri_bmask);
5639                 kfree(phba->sli4_hba.xri_ids);
5640                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5641                                     &phba->sli4_hba.lpfc_xri_blk_list, list) {
5642                         list_del_init(&rsrc_blk->list);
5643                         kfree(rsrc_blk);
5644                 }
5645                 break;
5646         case LPFC_RSC_TYPE_FCOE_VFI:
5647                 kfree(phba->sli4_hba.vfi_bmask);
5648                 kfree(phba->sli4_hba.vfi_ids);
5649                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5650                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5651                                     &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5652                         list_del_init(&rsrc_blk->list);
5653                         kfree(rsrc_blk);
5654                 }
5655                 break;
5656         case LPFC_RSC_TYPE_FCOE_RPI:
5657                 /* RPI bitmask and physical id array are cleaned up earlier. */
5658                 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5659                                     &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5660                         list_del_init(&rsrc_blk->list);
5661                         kfree(rsrc_blk);
5662                 }
5663                 break;
5664         default:
5665                 break;
5666         }
5667
5668         bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5669
5670  out_free_mbox:
5671         mempool_free(mbox, phba->mbox_mem_pool);
5672         return rc;
5673 }
5674
5675 /**
5676  * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5677  * @phba: Pointer to HBA context object.
5678  *
5679  * This function allocates all SLI4 resource identifiers.
5680  **/
5681 int
5682 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5683 {
5684         int i, rc, error = 0;
5685         uint16_t count, base;
5686         unsigned long longs;
5687
5688         if (!phba->sli4_hba.rpi_hdrs_in_use)
5689                 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5690         if (phba->sli4_hba.extents_in_use) {
5691                 /*
5692                  * The port supports resource extents. The XRI, VPI, VFI, RPI
5693                  * resource extent count must be read and allocated before
5694                  * provisioning the resource id arrays.
5695                  */
5696                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5697                     LPFC_IDX_RSRC_RDY) {
5698                         /*
5699                          * Extent-based resources are set - the driver could
5700                          * be in a port reset. Figure out if any corrective
5701                          * actions need to be taken.
5702                          */
5703                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5704                                                  LPFC_RSC_TYPE_FCOE_VFI);
5705                         if (rc != 0)
5706                                 error++;
5707                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5708                                                  LPFC_RSC_TYPE_FCOE_VPI);
5709                         if (rc != 0)
5710                                 error++;
5711                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5712                                                  LPFC_RSC_TYPE_FCOE_XRI);
5713                         if (rc != 0)
5714                                 error++;
5715                         rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5716                                                  LPFC_RSC_TYPE_FCOE_RPI);
5717                         if (rc != 0)
5718                                 error++;
5719
5720                         /*
5721                          * It's possible that the number of resources
5722                          * provided to this port instance changed between
5723                          * resets.  Detect this condition and reallocate
5724                          * resources.  Otherwise, there is no action.
5725                          */
5726                         if (error) {
5727                                 lpfc_printf_log(phba, KERN_INFO,
5728                                                 LOG_MBOX | LOG_INIT,
5729                                                 "2931 Detected extent resource "
5730                                                 "change.  Reallocating all "
5731                                                 "extents.\n");
5732                                 rc = lpfc_sli4_dealloc_extent(phba,
5733                                                  LPFC_RSC_TYPE_FCOE_VFI);
5734                                 rc = lpfc_sli4_dealloc_extent(phba,
5735                                                  LPFC_RSC_TYPE_FCOE_VPI);
5736                                 rc = lpfc_sli4_dealloc_extent(phba,
5737                                                  LPFC_RSC_TYPE_FCOE_XRI);
5738                                 rc = lpfc_sli4_dealloc_extent(phba,
5739                                                  LPFC_RSC_TYPE_FCOE_RPI);
5740                         } else
5741                                 return 0;
5742                 }
5743
5744                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5745                 if (unlikely(rc))
5746                         goto err_exit;
5747
5748                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5749                 if (unlikely(rc))
5750                         goto err_exit;
5751
5752                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5753                 if (unlikely(rc))
5754                         goto err_exit;
5755
5756                 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5757                 if (unlikely(rc))
5758                         goto err_exit;
5759                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5760                        LPFC_IDX_RSRC_RDY);
5761                 return rc;
5762         } else {
5763                 /*
5764                  * The port does not support resource extents.  The XRI, VPI,
5765                  * VFI, RPI resource ids were determined from READ_CONFIG.
5766                  * Just allocate the bitmasks and provision the resource id
5767                  * arrays.  If a port reset is active, the resources don't
5768                  * need any action - just exit.
5769                  */
5770                 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5771                     LPFC_IDX_RSRC_RDY) {
5772                         lpfc_sli4_dealloc_resource_identifiers(phba);
5773                         lpfc_sli4_remove_rpis(phba);
5774                 }
5775                 /* RPIs. */
5776                 count = phba->sli4_hba.max_cfg_param.max_rpi;
5777                 if (count <= 0) {
5778                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5779                                         "3279 Invalid provisioning of "
5780                                         "rpi:%d\n", count);
5781                         rc = -EINVAL;
5782                         goto err_exit;
5783                 }
5784                 base = phba->sli4_hba.max_cfg_param.rpi_base;
5785                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5786                 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5787                                                    sizeof(unsigned long),
5788                                                    GFP_KERNEL);
5789                 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5790                         rc = -ENOMEM;
5791                         goto err_exit;
5792                 }
5793                 phba->sli4_hba.rpi_ids = kzalloc(count *
5794                                                  sizeof(uint16_t),
5795                                                  GFP_KERNEL);
5796                 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5797                         rc = -ENOMEM;
5798                         goto free_rpi_bmask;
5799                 }
5800
5801                 for (i = 0; i < count; i++)
5802                         phba->sli4_hba.rpi_ids[i] = base + i;
5803
5804                 /* VPIs. */
5805                 count = phba->sli4_hba.max_cfg_param.max_vpi;
5806                 if (count <= 0) {
5807                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5808                                         "3280 Invalid provisioning of "
5809                                         "vpi:%d\n", count);
5810                         rc = -EINVAL;
5811                         goto free_rpi_ids;
5812                 }
5813                 base = phba->sli4_hba.max_cfg_param.vpi_base;
5814                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5815                 phba->vpi_bmask = kzalloc(longs *
5816                                           sizeof(unsigned long),
5817                                           GFP_KERNEL);
5818                 if (unlikely(!phba->vpi_bmask)) {
5819                         rc = -ENOMEM;
5820                         goto free_rpi_ids;
5821                 }
5822                 phba->vpi_ids = kzalloc(count *
5823                                         sizeof(uint16_t),
5824                                         GFP_KERNEL);
5825                 if (unlikely(!phba->vpi_ids)) {
5826                         rc = -ENOMEM;
5827                         goto free_vpi_bmask;
5828                 }
5829
5830                 for (i = 0; i < count; i++)
5831                         phba->vpi_ids[i] = base + i;
5832
5833                 /* XRIs. */
5834                 count = phba->sli4_hba.max_cfg_param.max_xri;
5835                 if (count <= 0) {
5836                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5837                                         "3281 Invalid provisioning of "
5838                                         "xri:%d\n", count);
5839                         rc = -EINVAL;
5840                         goto free_vpi_ids;
5841                 }
5842                 base = phba->sli4_hba.max_cfg_param.xri_base;
5843                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5844                 phba->sli4_hba.xri_bmask = kzalloc(longs *
5845                                                    sizeof(unsigned long),
5846                                                    GFP_KERNEL);
5847                 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5848                         rc = -ENOMEM;
5849                         goto free_vpi_ids;
5850                 }
5851                 phba->sli4_hba.max_cfg_param.xri_used = 0;
5852                 phba->sli4_hba.xri_ids = kzalloc(count *
5853                                                  sizeof(uint16_t),
5854                                                  GFP_KERNEL);
5855                 if (unlikely(!phba->sli4_hba.xri_ids)) {
5856                         rc = -ENOMEM;
5857                         goto free_xri_bmask;
5858                 }
5859
5860                 for (i = 0; i < count; i++)
5861                         phba->sli4_hba.xri_ids[i] = base + i;
5862
5863                 /* VFIs. */
5864                 count = phba->sli4_hba.max_cfg_param.max_vfi;
5865                 if (count <= 0) {
5866                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5867                                         "3282 Invalid provisioning of "
5868                                         "vfi:%d\n", count);
5869                         rc = -EINVAL;
5870                         goto free_xri_ids;
5871                 }
5872                 base = phba->sli4_hba.max_cfg_param.vfi_base;
5873                 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5874                 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5875                                                    sizeof(unsigned long),
5876                                                    GFP_KERNEL);
5877                 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5878                         rc = -ENOMEM;
5879                         goto free_xri_ids;
5880                 }
5881                 phba->sli4_hba.vfi_ids = kzalloc(count *
5882                                                  sizeof(uint16_t),
5883                                                  GFP_KERNEL);
5884                 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5885                         rc = -ENOMEM;
5886                         goto free_vfi_bmask;
5887                 }
5888
5889                 for (i = 0; i < count; i++)
5890                         phba->sli4_hba.vfi_ids[i] = base + i;
5891
5892                 /*
5893                  * Mark all resources ready.  An HBA reset doesn't need
5894                  * to reset the initialization.
5895                  */
5896                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5897                        LPFC_IDX_RSRC_RDY);
5898                 return 0;
5899         }
5900
5901  free_vfi_bmask:
5902         kfree(phba->sli4_hba.vfi_bmask);
5903         phba->sli4_hba.vfi_bmask = NULL;
5904  free_xri_ids:
5905         kfree(phba->sli4_hba.xri_ids);
5906         phba->sli4_hba.xri_ids = NULL;
5907  free_xri_bmask:
5908         kfree(phba->sli4_hba.xri_bmask);
5909         phba->sli4_hba.xri_bmask = NULL;
5910  free_vpi_ids:
5911         kfree(phba->vpi_ids);
5912         phba->vpi_ids = NULL;
5913  free_vpi_bmask:
5914         kfree(phba->vpi_bmask);
5915         phba->vpi_bmask = NULL;
5916  free_rpi_ids:
5917         kfree(phba->sli4_hba.rpi_ids);
5918         phba->sli4_hba.rpi_ids = NULL;
5919  free_rpi_bmask:
5920         kfree(phba->sli4_hba.rpi_bmask);
5921         phba->sli4_hba.rpi_bmask = NULL;
5922  err_exit:
5923         return rc;
5924 }
5925
5926 /**
5927  * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5928  * @phba: Pointer to HBA context object.
5929  *
5930  * This function allocates the number of elements for the specified
5931  * resource type.
5932  **/
5933 int
5934 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5935 {
5936         if (phba->sli4_hba.extents_in_use) {
5937                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5938                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5939                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5940                 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5941         } else {
5942                 kfree(phba->vpi_bmask);
5943                 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5944                 kfree(phba->vpi_ids);
5945                 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5946                 kfree(phba->sli4_hba.xri_bmask);
5947                 kfree(phba->sli4_hba.xri_ids);
5948                 kfree(phba->sli4_hba.vfi_bmask);
5949                 kfree(phba->sli4_hba.vfi_ids);
5950                 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5951                 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5952         }
5953
5954         return 0;
5955 }
5956
5957 /**
5958  * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5959  * @phba: Pointer to HBA context object.
5960  * @type: The resource extent type.
5961  * @extnt_count: buffer to hold port extent count response
5962  * @extnt_size: buffer to hold port extent size response.
5963  *
5964  * This function calls the port to read the host allocated extents
5965  * for a particular type.
5966  **/
5967 int
5968 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5969                                uint16_t *extnt_cnt, uint16_t *extnt_size)
5970 {
5971         bool emb;
5972         int rc = 0;
5973         uint16_t curr_blks = 0;
5974         uint32_t req_len, emb_len;
5975         uint32_t alloc_len, mbox_tmo;
5976         struct list_head *blk_list_head;
5977         struct lpfc_rsrc_blks *rsrc_blk;
5978         LPFC_MBOXQ_t *mbox;
5979         void *virtaddr = NULL;
5980         struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5981         struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5982         union  lpfc_sli4_cfg_shdr *shdr;
5983
5984         switch (type) {
5985         case LPFC_RSC_TYPE_FCOE_VPI:
5986                 blk_list_head = &phba->lpfc_vpi_blk_list;
5987                 break;
5988         case LPFC_RSC_TYPE_FCOE_XRI:
5989                 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5990                 break;
5991         case LPFC_RSC_TYPE_FCOE_VFI:
5992                 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5993                 break;
5994         case LPFC_RSC_TYPE_FCOE_RPI:
5995                 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5996                 break;
5997         default:
5998                 return -EIO;
5999         }
6000
6001         /* Count the number of extents currently allocatd for this type. */
6002         list_for_each_entry(rsrc_blk, blk_list_head, list) {
6003                 if (curr_blks == 0) {
6004                         /*
6005                          * The GET_ALLOCATED mailbox does not return the size,
6006                          * just the count.  The size should be just the size
6007                          * stored in the current allocated block and all sizes
6008                          * for an extent type are the same so set the return
6009                          * value now.
6010                          */
6011                         *extnt_size = rsrc_blk->rsrc_size;
6012                 }
6013                 curr_blks++;
6014         }
6015
6016         /*
6017          * Calculate the size of an embedded mailbox.  The uint32_t
6018          * accounts for extents-specific word.
6019          */
6020         emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6021                 sizeof(uint32_t);
6022
6023         /*
6024          * Presume the allocation and response will fit into an embedded
6025          * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6026          */
6027         emb = LPFC_SLI4_MBX_EMBED;
6028         req_len = emb_len;
6029         if (req_len > emb_len) {
6030                 req_len = curr_blks * sizeof(uint16_t) +
6031                         sizeof(union lpfc_sli4_cfg_shdr) +
6032                         sizeof(uint32_t);
6033                 emb = LPFC_SLI4_MBX_NEMBED;
6034         }
6035
6036         mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6037         if (!mbox)
6038                 return -ENOMEM;
6039         memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6040
6041         alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6042                                      LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6043                                      req_len, emb);
6044         if (alloc_len < req_len) {
6045                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6046                         "2983 Allocated DMA memory size (x%x) is "
6047                         "less than the requested DMA memory "
6048                         "size (x%x)\n", alloc_len, req_len);
6049                 rc = -ENOMEM;
6050                 goto err_exit;
6051         }
6052         rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6053         if (unlikely(rc)) {
6054                 rc = -EIO;
6055                 goto err_exit;
6056         }
6057
6058         if (!phba->sli4_hba.intr_enable)
6059                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6060         else {
6061                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6062                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6063         }
6064
6065         if (unlikely(rc)) {
6066                 rc = -EIO;
6067                 goto err_exit;
6068         }
6069
6070         /*
6071          * Figure out where the response is located.  Then get local pointers
6072          * to the response data.  The port does not guarantee to respond to
6073          * all extents counts request so update the local variable with the
6074          * allocated count from the port.
6075          */
6076         if (emb == LPFC_SLI4_MBX_EMBED) {
6077                 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6078                 shdr = &rsrc_ext->header.cfg_shdr;
6079                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6080         } else {
6081                 virtaddr = mbox->sge_array->addr[0];
6082                 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6083                 shdr = &n_rsrc->cfg_shdr;
6084                 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6085         }
6086
6087         if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6088                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6089                         "2984 Failed to read allocated resources "
6090                         "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6091                         type,
6092                         bf_get(lpfc_mbox_hdr_status, &shdr->response),
6093                         bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6094                 rc = -EIO;
6095                 goto err_exit;
6096         }
6097  err_exit:
6098         lpfc_sli4_mbox_cmd_free(phba, mbox);
6099         return rc;
6100 }
6101
6102 /**
6103  * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6104  * @phba: pointer to lpfc hba data structure.
6105  *
6106  * This routine walks the list of els buffers that have been allocated and
6107  * repost them to the port by using SGL block post. This is needed after a
6108  * pci_function_reset/warm_start or start. It attempts to construct blocks
6109  * of els buffer sgls which contains contiguous xris and uses the non-embedded
6110  * SGL block post mailbox commands to post them to the port. For single els
6111  * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6112  * mailbox command for posting.
6113  *
6114  * Returns: 0 = success, non-zero failure.
6115  **/
6116 static int
6117 lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6118 {
6119         struct lpfc_sglq *sglq_entry = NULL;
6120         struct lpfc_sglq *sglq_entry_next = NULL;
6121         struct lpfc_sglq *sglq_entry_first = NULL;
6122         int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
6123         int last_xritag = NO_XRI;
6124         struct lpfc_sli_ring *pring;
6125         LIST_HEAD(prep_sgl_list);
6126         LIST_HEAD(blck_sgl_list);
6127         LIST_HEAD(allc_sgl_list);
6128         LIST_HEAD(post_sgl_list);
6129         LIST_HEAD(free_sgl_list);
6130
6131         pring = &phba->sli.ring[LPFC_ELS_RING];
6132         spin_lock_irq(&phba->hbalock);
6133         spin_lock(&pring->ring_lock);
6134         list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
6135         spin_unlock(&pring->ring_lock);
6136         spin_unlock_irq(&phba->hbalock);
6137
6138         total_cnt = phba->sli4_hba.els_xri_cnt;
6139         list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6140                                  &allc_sgl_list, list) {
6141                 list_del_init(&sglq_entry->list);
6142                 block_cnt++;
6143                 if ((last_xritag != NO_XRI) &&
6144                     (sglq_entry->sli4_xritag != last_xritag + 1)) {
6145                         /* a hole in xri block, form a sgl posting block */
6146                         list_splice_init(&prep_sgl_list, &blck_sgl_list);
6147                         post_cnt = block_cnt - 1;
6148                         /* prepare list for next posting block */
6149                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6150                         block_cnt = 1;
6151                 } else {
6152                         /* prepare list for next posting block */
6153                         list_add_tail(&sglq_entry->list, &prep_sgl_list);
6154                         /* enough sgls for non-embed sgl mbox command */
6155                         if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6156                                 list_splice_init(&prep_sgl_list,
6157                                                  &blck_sgl_list);
6158                                 post_cnt = block_cnt;
6159                                 block_cnt = 0;
6160                         }
6161                 }
6162                 num_posted++;
6163
6164                 /* keep track of last sgl's xritag */
6165                 last_xritag = sglq_entry->sli4_xritag;
6166
6167                 /* end of repost sgl list condition for els buffers */
6168                 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6169                         if (post_cnt == 0) {
6170                                 list_splice_init(&prep_sgl_list,
6171                                                  &blck_sgl_list);
6172                                 post_cnt = block_cnt;
6173                         } else if (block_cnt == 1) {
6174                                 status = lpfc_sli4_post_sgl(phba,
6175                                                 sglq_entry->phys, 0,
6176                                                 sglq_entry->sli4_xritag);
6177                                 if (!status) {
6178                                         /* successful, put sgl to posted list */
6179                                         list_add_tail(&sglq_entry->list,
6180                                                       &post_sgl_list);
6181                                 } else {
6182                                         /* Failure, put sgl to free list */
6183                                         lpfc_printf_log(phba, KERN_WARNING,
6184                                                 LOG_SLI,
6185                                                 "3159 Failed to post els "
6186                                                 "sgl, xritag:x%x\n",
6187                                                 sglq_entry->sli4_xritag);
6188                                         list_add_tail(&sglq_entry->list,
6189                                                       &free_sgl_list);
6190                                         total_cnt--;
6191                                 }
6192                         }
6193                 }
6194
6195                 /* continue until a nembed page worth of sgls */
6196                 if (post_cnt == 0)
6197                         continue;
6198
6199                 /* post the els buffer list sgls as a block */
6200                 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6201                                                      post_cnt);
6202
6203                 if (!status) {
6204                         /* success, put sgl list to posted sgl list */
6205                         list_splice_init(&blck_sgl_list, &post_sgl_list);
6206                 } else {
6207                         /* Failure, put sgl list to free sgl list */
6208                         sglq_entry_first = list_first_entry(&blck_sgl_list,
6209                                                             struct lpfc_sglq,
6210                                                             list);
6211                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6212                                         "3160 Failed to post els sgl-list, "
6213                                         "xritag:x%x-x%x\n",
6214                                         sglq_entry_first->sli4_xritag,
6215                                         (sglq_entry_first->sli4_xritag +
6216                                          post_cnt - 1));
6217                         list_splice_init(&blck_sgl_list, &free_sgl_list);
6218                         total_cnt -= post_cnt;
6219                 }
6220
6221                 /* don't reset xirtag due to hole in xri block */
6222                 if (block_cnt == 0)
6223                         last_xritag = NO_XRI;
6224
6225                 /* reset els sgl post count for next round of posting */
6226                 post_cnt = 0;
6227         }
6228         /* update the number of XRIs posted for ELS */
6229         phba->sli4_hba.els_xri_cnt = total_cnt;
6230
6231         /* free the els sgls failed to post */
6232         lpfc_free_sgl_list(phba, &free_sgl_list);
6233
6234         /* push els sgls posted to the availble list */
6235         if (!list_empty(&post_sgl_list)) {
6236                 spin_lock_irq(&phba->hbalock);
6237                 spin_lock(&pring->ring_lock);
6238                 list_splice_init(&post_sgl_list,
6239                                  &phba->sli4_hba.lpfc_sgl_list);
6240                 spin_unlock(&pring->ring_lock);
6241                 spin_unlock_irq(&phba->hbalock);
6242         } else {
6243                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6244                                 "3161 Failure to post els sgl to port.\n");
6245                 return -EIO;
6246         }
6247         return 0;
6248 }
6249
6250 /**
6251  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6252  * @phba: Pointer to HBA context object.
6253  *
6254  * This function is the main SLI4 device intialization PCI function. This
6255  * function is called by the HBA intialization code, HBA reset code and
6256  * HBA error attention handler code. Caller is not required to hold any
6257  * locks.
6258  **/
6259 int
6260 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6261 {
6262         int rc;
6263         LPFC_MBOXQ_t *mboxq;
6264         struct lpfc_mqe *mqe;
6265         uint8_t *vpd;
6266         uint32_t vpd_size;
6267         uint32_t ftr_rsp = 0;
6268         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6269         struct lpfc_vport *vport = phba->pport;
6270         struct lpfc_dmabuf *mp;
6271
6272         /* Perform a PCI function reset to start from clean */
6273         rc = lpfc_pci_function_reset(phba);
6274         if (unlikely(rc))
6275                 return -ENODEV;
6276
6277         /* Check the HBA Host Status Register for readyness */
6278         rc = lpfc_sli4_post_status_check(phba);
6279         if (unlikely(rc))
6280                 return -ENODEV;
6281         else {
6282                 spin_lock_irq(&phba->hbalock);
6283                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6284                 spin_unlock_irq(&phba->hbalock);
6285         }
6286
6287         /*
6288          * Allocate a single mailbox container for initializing the
6289          * port.
6290          */
6291         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6292         if (!mboxq)
6293                 return -ENOMEM;
6294
6295         /* Issue READ_REV to collect vpd and FW information. */
6296         vpd_size = SLI4_PAGE_SIZE;
6297         vpd = kzalloc(vpd_size, GFP_KERNEL);
6298         if (!vpd) {
6299                 rc = -ENOMEM;
6300                 goto out_free_mbox;
6301         }
6302
6303         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6304         if (unlikely(rc)) {
6305                 kfree(vpd);
6306                 goto out_free_mbox;
6307         }
6308
6309         mqe = &mboxq->u.mqe;
6310         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6311         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
6312                 phba->hba_flag |= HBA_FCOE_MODE;
6313         else
6314                 phba->hba_flag &= ~HBA_FCOE_MODE;
6315
6316         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6317                 LPFC_DCBX_CEE_MODE)
6318                 phba->hba_flag |= HBA_FIP_SUPPORT;
6319         else
6320                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6321
6322         phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6323
6324         if (phba->sli_rev != LPFC_SLI_REV4) {
6325                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6326                         "0376 READ_REV Error. SLI Level %d "
6327                         "FCoE enabled %d\n",
6328                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6329                 rc = -EIO;
6330                 kfree(vpd);
6331                 goto out_free_mbox;
6332         }
6333
6334         /*
6335          * Continue initialization with default values even if driver failed
6336          * to read FCoE param config regions, only read parameters if the
6337          * board is FCoE
6338          */
6339         if (phba->hba_flag & HBA_FCOE_MODE &&
6340             lpfc_sli4_read_fcoe_params(phba))
6341                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6342                         "2570 Failed to read FCoE parameters\n");
6343
6344         /*
6345          * Retrieve sli4 device physical port name, failure of doing it
6346          * is considered as non-fatal.
6347          */
6348         rc = lpfc_sli4_retrieve_pport_name(phba);
6349         if (!rc)
6350                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6351                                 "3080 Successful retrieving SLI4 device "
6352                                 "physical port name: %s.\n", phba->Port);
6353
6354         /*
6355          * Evaluate the read rev and vpd data. Populate the driver
6356          * state with the results. If this routine fails, the failure
6357          * is not fatal as the driver will use generic values.
6358          */
6359         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6360         if (unlikely(!rc)) {
6361                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6362                                 "0377 Error %d parsing vpd. "
6363                                 "Using defaults.\n", rc);
6364                 rc = 0;
6365         }
6366         kfree(vpd);
6367
6368         /* Save information as VPD data */
6369         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6370         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6371         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6372         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6373                                          &mqe->un.read_rev);
6374         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6375                                        &mqe->un.read_rev);
6376         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6377                                             &mqe->un.read_rev);
6378         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6379                                            &mqe->un.read_rev);
6380         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6381         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6382         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6383         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6384         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6385         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6386         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6387                         "(%d):0380 READ_REV Status x%x "
6388                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6389                         mboxq->vport ? mboxq->vport->vpi : 0,
6390                         bf_get(lpfc_mqe_status, mqe),
6391                         phba->vpd.rev.opFwName,
6392                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6393                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6394
6395         /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
6396         rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6397         if (phba->pport->cfg_lun_queue_depth > rc) {
6398                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6399                                 "3362 LUN queue depth changed from %d to %d\n",
6400                                 phba->pport->cfg_lun_queue_depth, rc);
6401                 phba->pport->cfg_lun_queue_depth = rc;
6402         }
6403
6404
6405         /*
6406          * Discover the port's supported feature set and match it against the
6407          * hosts requests.
6408          */
6409         lpfc_request_features(phba, mboxq);
6410         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6411         if (unlikely(rc)) {
6412                 rc = -EIO;
6413                 goto out_free_mbox;
6414         }
6415
6416         /*
6417          * The port must support FCP initiator mode as this is the
6418          * only mode running in the host.
6419          */
6420         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6421                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6422                                 "0378 No support for fcpi mode.\n");
6423                 ftr_rsp++;
6424         }
6425         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6426                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6427         else
6428                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6429         /*
6430          * If the port cannot support the host's requested features
6431          * then turn off the global config parameters to disable the
6432          * feature in the driver.  This is not a fatal error.
6433          */
6434         phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6435         if (phba->cfg_enable_bg) {
6436                 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6437                         phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6438                 else
6439                         ftr_rsp++;
6440         }
6441
6442         if (phba->max_vpi && phba->cfg_enable_npiv &&
6443             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6444                 ftr_rsp++;
6445
6446         if (ftr_rsp) {
6447                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6448                                 "0379 Feature Mismatch Data: x%08x %08x "
6449                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6450                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6451                                 phba->cfg_enable_npiv, phba->max_vpi);
6452                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6453                         phba->cfg_enable_bg = 0;
6454                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6455                         phba->cfg_enable_npiv = 0;
6456         }
6457
6458         /* These SLI3 features are assumed in SLI4 */
6459         spin_lock_irq(&phba->hbalock);
6460         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6461         spin_unlock_irq(&phba->hbalock);
6462
6463         /*
6464          * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
6465          * calls depends on these resources to complete port setup.
6466          */
6467         rc = lpfc_sli4_alloc_resource_identifiers(phba);
6468         if (rc) {
6469                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6470                                 "2920 Failed to alloc Resource IDs "
6471                                 "rc = x%x\n", rc);
6472                 goto out_free_mbox;
6473         }
6474
6475         /* Read the port's service parameters. */
6476         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6477         if (rc) {
6478                 phba->link_state = LPFC_HBA_ERROR;
6479                 rc = -ENOMEM;
6480                 goto out_free_mbox;
6481         }
6482
6483         mboxq->vport = vport;
6484         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6485         mp = (struct lpfc_dmabuf *) mboxq->context1;
6486         if (rc == MBX_SUCCESS) {
6487                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6488                 rc = 0;
6489         }
6490
6491         /*
6492          * This memory was allocated by the lpfc_read_sparam routine. Release
6493          * it to the mbuf pool.
6494          */
6495         lpfc_mbuf_free(phba, mp->virt, mp->phys);
6496         kfree(mp);
6497         mboxq->context1 = NULL;
6498         if (unlikely(rc)) {
6499                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6500                                 "0382 READ_SPARAM command failed "
6501                                 "status %d, mbxStatus x%x\n",
6502                                 rc, bf_get(lpfc_mqe_status, mqe));
6503                 phba->link_state = LPFC_HBA_ERROR;
6504                 rc = -EIO;
6505                 goto out_free_mbox;
6506         }
6507
6508         lpfc_update_vport_wwn(vport);
6509
6510         /* Update the fc_host data structures with new wwn. */
6511         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6512         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6513
6514         /* update host els and scsi xri-sgl sizes and mappings */
6515         rc = lpfc_sli4_xri_sgl_update(phba);
6516         if (unlikely(rc)) {
6517                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6518                                 "1400 Failed to update xri-sgl size and "
6519                                 "mapping: %d\n", rc);
6520                 goto out_free_mbox;
6521         }
6522
6523         /* register the els sgl pool to the port */
6524         rc = lpfc_sli4_repost_els_sgl_list(phba);
6525         if (unlikely(rc)) {
6526                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6527                                 "0582 Error %d during els sgl post "
6528                                 "operation\n", rc);
6529                 rc = -ENODEV;
6530                 goto out_free_mbox;
6531         }
6532
6533         /* register the allocated scsi sgl pool to the port */
6534         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6535         if (unlikely(rc)) {
6536                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6537                                 "0383 Error %d during scsi sgl post "
6538                                 "operation\n", rc);
6539                 /* Some Scsi buffers were moved to the abort scsi list */
6540                 /* A pci function reset will repost them */
6541                 rc = -ENODEV;
6542                 goto out_free_mbox;
6543         }
6544
6545         /* Post the rpi header region to the device. */
6546         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6547         if (unlikely(rc)) {
6548                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6549                                 "0393 Error %d during rpi post operation\n",
6550                                 rc);
6551                 rc = -ENODEV;
6552                 goto out_free_mbox;
6553         }
6554         lpfc_sli4_node_prep(phba);
6555
6556         /* Create all the SLI4 queues */
6557         rc = lpfc_sli4_queue_create(phba);
6558         if (rc) {
6559                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6560                                 "3089 Failed to allocate queues\n");
6561                 rc = -ENODEV;
6562                 goto out_stop_timers;
6563         }
6564         /* Set up all the queues to the device */
6565         rc = lpfc_sli4_queue_setup(phba);
6566         if (unlikely(rc)) {
6567                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6568                                 "0381 Error %d during queue setup.\n ", rc);
6569                 goto out_destroy_queue;
6570         }
6571
6572         /* Arm the CQs and then EQs on device */
6573         lpfc_sli4_arm_cqeq_intr(phba);
6574
6575         /* Indicate device interrupt mode */
6576         phba->sli4_hba.intr_enable = 1;
6577
6578         /* Allow asynchronous mailbox command to go through */
6579         spin_lock_irq(&phba->hbalock);
6580         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6581         spin_unlock_irq(&phba->hbalock);
6582
6583         /* Post receive buffers to the device */
6584         lpfc_sli4_rb_setup(phba);
6585
6586         /* Reset HBA FCF states after HBA reset */
6587         phba->fcf.fcf_flag = 0;
6588         phba->fcf.current_rec.flag = 0;
6589
6590         /* Start the ELS watchdog timer */
6591         mod_timer(&vport->els_tmofunc,
6592                   jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
6593
6594         /* Start heart beat timer */
6595         mod_timer(&phba->hb_tmofunc,
6596                   jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
6597         phba->hb_outstanding = 0;
6598         phba->last_completion_time = jiffies;
6599
6600         /* Start error attention (ERATT) polling timer */
6601         mod_timer(&phba->eratt_poll,
6602                   jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
6603
6604         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6605         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6606                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6607                 if (!rc) {
6608                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6609                                         "2829 This device supports "
6610                                         "Advanced Error Reporting (AER)\n");
6611                         spin_lock_irq(&phba->hbalock);
6612                         phba->hba_flag |= HBA_AER_ENABLED;
6613                         spin_unlock_irq(&phba->hbalock);
6614                 } else {
6615                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6616                                         "2830 This device does not support "
6617                                         "Advanced Error Reporting (AER)\n");
6618                         phba->cfg_aer_support = 0;
6619                 }
6620                 rc = 0;
6621         }
6622
6623         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6624                 /*
6625                  * The FC Port needs to register FCFI (index 0)
6626                  */
6627                 lpfc_reg_fcfi(phba, mboxq);
6628                 mboxq->vport = phba->pport;
6629                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6630                 if (rc != MBX_SUCCESS)
6631                         goto out_unset_queue;
6632                 rc = 0;
6633                 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6634                                         &mboxq->u.mqe.un.reg_fcfi);
6635
6636                 /* Check if the port is configured to be disabled */
6637                 lpfc_sli_read_link_ste(phba);
6638         }
6639
6640         /*
6641          * The port is ready, set the host's link state to LINK_DOWN
6642          * in preparation for link interrupts.
6643          */
6644         spin_lock_irq(&phba->hbalock);
6645         phba->link_state = LPFC_LINK_DOWN;
6646         spin_unlock_irq(&phba->hbalock);
6647         if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6648             (phba->hba_flag & LINK_DISABLED)) {
6649                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6650                                 "3103 Adapter Link is disabled.\n");
6651                 lpfc_down_link(phba, mboxq);
6652                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6653                 if (rc != MBX_SUCCESS) {
6654                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6655                                         "3104 Adapter failed to issue "
6656                                         "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6657                         goto out_unset_queue;
6658                 }
6659         } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6660                 /* don't perform init_link on SLI4 FC port loopback test */
6661                 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6662                         rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6663                         if (rc)
6664                                 goto out_unset_queue;
6665                 }
6666         }
6667         mempool_free(mboxq, phba->mbox_mem_pool);
6668         return rc;
6669 out_unset_queue:
6670         /* Unset all the queues set up in this routine when error out */
6671         lpfc_sli4_queue_unset(phba);
6672 out_destroy_queue:
6673         lpfc_sli4_queue_destroy(phba);
6674 out_stop_timers:
6675         lpfc_stop_hba_timers(phba);
6676 out_free_mbox:
6677         mempool_free(mboxq, phba->mbox_mem_pool);
6678         return rc;
6679 }
6680
6681 /**
6682  * lpfc_mbox_timeout - Timeout call back function for mbox timer
6683  * @ptr: context object - pointer to hba structure.
6684  *
6685  * This is the callback function for mailbox timer. The mailbox
6686  * timer is armed when a new mailbox command is issued and the timer
6687  * is deleted when the mailbox complete. The function is called by
6688  * the kernel timer code when a mailbox does not complete within
6689  * expected time. This function wakes up the worker thread to
6690  * process the mailbox timeout and returns. All the processing is
6691  * done by the worker thread function lpfc_mbox_timeout_handler.
6692  **/
6693 void
6694 lpfc_mbox_timeout(unsigned long ptr)
6695 {
6696         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6697         unsigned long iflag;
6698         uint32_t tmo_posted;
6699
6700         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6701         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6702         if (!tmo_posted)
6703                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6704         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6705
6706         if (!tmo_posted)
6707                 lpfc_worker_wake_up(phba);
6708         return;
6709 }
6710
6711 /**
6712  * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6713  *                                    are pending
6714  * @phba: Pointer to HBA context object.
6715  *
6716  * This function checks if any mailbox completions are present on the mailbox
6717  * completion queue.
6718  **/
6719 static bool
6720 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6721 {
6722
6723         uint32_t idx;
6724         struct lpfc_queue *mcq;
6725         struct lpfc_mcqe *mcqe;
6726         bool pending_completions = false;
6727
6728         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6729                 return false;
6730
6731         /* Check for completions on mailbox completion queue */
6732
6733         mcq = phba->sli4_hba.mbx_cq;
6734         idx = mcq->hba_index;
6735         while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6736                 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6737                 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6738                     (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6739                         pending_completions = true;
6740                         break;
6741                 }
6742                 idx = (idx + 1) % mcq->entry_count;
6743                 if (mcq->hba_index == idx)
6744                         break;
6745         }
6746         return pending_completions;
6747
6748 }
6749
6750 /**
6751  * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6752  *                                            that were missed.
6753  * @phba: Pointer to HBA context object.
6754  *
6755  * For sli4, it is possible to miss an interrupt. As such mbox completions
6756  * maybe missed causing erroneous mailbox timeouts to occur. This function
6757  * checks to see if mbox completions are on the mailbox completion queue
6758  * and will process all the completions associated with the eq for the
6759  * mailbox completion queue.
6760  **/
6761 bool
6762 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6763 {
6764
6765         uint32_t eqidx;
6766         struct lpfc_queue *fpeq = NULL;
6767         struct lpfc_eqe *eqe;
6768         bool mbox_pending;
6769
6770         if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6771                 return false;
6772
6773         /* Find the eq associated with the mcq */
6774
6775         if (phba->sli4_hba.hba_eq)
6776                 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6777                         if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6778                             phba->sli4_hba.mbx_cq->assoc_qid) {
6779                                 fpeq = phba->sli4_hba.hba_eq[eqidx];
6780                                 break;
6781                         }
6782         if (!fpeq)
6783                 return false;
6784
6785         /* Turn off interrupts from this EQ */
6786
6787         lpfc_sli4_eq_clr_intr(fpeq);
6788
6789         /* Check to see if a mbox completion is pending */
6790
6791         mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6792
6793         /*
6794          * If a mbox completion is pending, process all the events on EQ
6795          * associated with the mbox completion queue (this could include
6796          * mailbox commands, async events, els commands, receive queue data
6797          * and fcp commands)
6798          */
6799
6800         if (mbox_pending)
6801                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6802                         lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6803                         fpeq->EQ_processed++;
6804                 }
6805
6806         /* Always clear and re-arm the EQ */
6807
6808         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6809
6810         return mbox_pending;
6811
6812 }
6813
6814 /**
6815  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6816  * @phba: Pointer to HBA context object.
6817  *
6818  * This function is called from worker thread when a mailbox command times out.
6819  * The caller is not required to hold any locks. This function will reset the
6820  * HBA and recover all the pending commands.
6821  **/
6822 void
6823 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6824 {
6825         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6826         MAILBOX_t *mb = NULL;
6827
6828         struct lpfc_sli *psli = &phba->sli;
6829
6830         /* If the mailbox completed, process the completion and return */
6831         if (lpfc_sli4_process_missed_mbox_completions(phba))
6832                 return;
6833
6834         if (pmbox != NULL)
6835                 mb = &pmbox->u.mb;
6836         /* Check the pmbox pointer first.  There is a race condition
6837          * between the mbox timeout handler getting executed in the
6838          * worklist and the mailbox actually completing. When this
6839          * race condition occurs, the mbox_active will be NULL.
6840          */
6841         spin_lock_irq(&phba->hbalock);
6842         if (pmbox == NULL) {
6843                 lpfc_printf_log(phba, KERN_WARNING,
6844                                 LOG_MBOX | LOG_SLI,
6845                                 "0353 Active Mailbox cleared - mailbox timeout "
6846                                 "exiting\n");
6847                 spin_unlock_irq(&phba->hbalock);
6848                 return;
6849         }
6850
6851         /* Mbox cmd <mbxCommand> timeout */
6852         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6853                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6854                         mb->mbxCommand,
6855                         phba->pport->port_state,
6856                         phba->sli.sli_flag,
6857                         phba->sli.mbox_active);
6858         spin_unlock_irq(&phba->hbalock);
6859
6860         /* Setting state unknown so lpfc_sli_abort_iocb_ring
6861          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6862          * it to fail all outstanding SCSI IO.
6863          */
6864         spin_lock_irq(&phba->pport->work_port_lock);
6865         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6866         spin_unlock_irq(&phba->pport->work_port_lock);
6867         spin_lock_irq(&phba->hbalock);
6868         phba->link_state = LPFC_LINK_UNKNOWN;
6869         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6870         spin_unlock_irq(&phba->hbalock);
6871
6872         lpfc_sli_abort_fcp_rings(phba);
6873
6874         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6875                         "0345 Resetting board due to mailbox timeout\n");
6876
6877         /* Reset the HBA device */
6878         lpfc_reset_hba(phba);
6879 }
6880
6881 /**
6882  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6883  * @phba: Pointer to HBA context object.
6884  * @pmbox: Pointer to mailbox object.
6885  * @flag: Flag indicating how the mailbox need to be processed.
6886  *
6887  * This function is called by discovery code and HBA management code
6888  * to submit a mailbox command to firmware with SLI-3 interface spec. This
6889  * function gets the hbalock to protect the data structures.
6890  * The mailbox command can be submitted in polling mode, in which case
6891  * this function will wait in a polling loop for the completion of the
6892  * mailbox.
6893  * If the mailbox is submitted in no_wait mode (not polling) the
6894  * function will submit the command and returns immediately without waiting
6895  * for the mailbox completion. The no_wait is supported only when HBA
6896  * is in SLI2/SLI3 mode - interrupts are enabled.
6897  * The SLI interface allows only one mailbox pending at a time. If the
6898  * mailbox is issued in polling mode and there is already a mailbox
6899  * pending, then the function will return an error. If the mailbox is issued
6900  * in NO_WAIT mode and there is a mailbox pending already, the function
6901  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6902  * The sli layer owns the mailbox object until the completion of mailbox
6903  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6904  * return codes the caller owns the mailbox command after the return of
6905  * the function.
6906  **/
6907 static int
6908 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6909                        uint32_t flag)
6910 {
6911         MAILBOX_t *mbx;
6912         struct lpfc_sli *psli = &phba->sli;
6913         uint32_t status, evtctr;
6914         uint32_t ha_copy, hc_copy;
6915         int i;
6916         unsigned long timeout;
6917         unsigned long drvr_flag = 0;
6918         uint32_t word0, ldata;
6919         void __iomem *to_slim;
6920         int processing_queue = 0;
6921
6922         spin_lock_irqsave(&phba->hbalock, drvr_flag);
6923         if (!pmbox) {
6924                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6925                 /* processing mbox queue from intr_handler */
6926                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6927                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6928                         return MBX_SUCCESS;
6929                 }
6930                 processing_queue = 1;
6931                 pmbox = lpfc_mbox_get(phba);
6932                 if (!pmbox) {
6933                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6934                         return MBX_SUCCESS;
6935                 }
6936         }
6937
6938         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6939                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6940                 if(!pmbox->vport) {
6941                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6942                         lpfc_printf_log(phba, KERN_ERR,
6943                                         LOG_MBOX | LOG_VPORT,
6944                                         "1806 Mbox x%x failed. No vport\n",
6945                                         pmbox->u.mb.mbxCommand);
6946                         dump_stack();
6947                         goto out_not_finished;
6948                 }
6949         }
6950
6951         /* If the PCI channel is in offline state, do not post mbox. */
6952         if (unlikely(pci_channel_offline(phba->pcidev))) {
6953                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6954                 goto out_not_finished;
6955         }
6956
6957         /* If HBA has a deferred error attention, fail the iocb. */
6958         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6959                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6960                 goto out_not_finished;
6961         }
6962
6963         psli = &phba->sli;
6964
6965         mbx = &pmbox->u.mb;
6966         status = MBX_SUCCESS;
6967
6968         if (phba->link_state == LPFC_HBA_ERROR) {
6969                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6970
6971                 /* Mbox command <mbxCommand> cannot issue */
6972                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6973                                 "(%d):0311 Mailbox command x%x cannot "
6974                                 "issue Data: x%x x%x\n",
6975                                 pmbox->vport ? pmbox->vport->vpi : 0,
6976                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6977                 goto out_not_finished;
6978         }
6979
6980         if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6981                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6982                         !(hc_copy & HC_MBINT_ENA)) {
6983                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6984                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6985                                 "(%d):2528 Mailbox command x%x cannot "
6986                                 "issue Data: x%x x%x\n",
6987                                 pmbox->vport ? pmbox->vport->vpi : 0,
6988                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6989                         goto out_not_finished;
6990                 }
6991         }
6992
6993         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6994                 /* Polling for a mbox command when another one is already active
6995                  * is not allowed in SLI. Also, the driver must have established
6996                  * SLI2 mode to queue and process multiple mbox commands.
6997                  */
6998
6999                 if (flag & MBX_POLL) {
7000                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7001
7002                         /* Mbox command <mbxCommand> cannot issue */
7003                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7004                                         "(%d):2529 Mailbox command x%x "
7005                                         "cannot issue Data: x%x x%x\n",
7006                                         pmbox->vport ? pmbox->vport->vpi : 0,
7007                                         pmbox->u.mb.mbxCommand,
7008                                         psli->sli_flag, flag);
7009                         goto out_not_finished;
7010                 }
7011
7012                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7013                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7014                         /* Mbox command <mbxCommand> cannot issue */
7015                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7016                                         "(%d):2530 Mailbox command x%x "
7017                                         "cannot issue Data: x%x x%x\n",
7018                                         pmbox->vport ? pmbox->vport->vpi : 0,
7019                                         pmbox->u.mb.mbxCommand,
7020                                         psli->sli_flag, flag);
7021                         goto out_not_finished;
7022                 }
7023
7024                 /* Another mailbox command is still being processed, queue this
7025                  * command to be processed later.
7026                  */
7027                 lpfc_mbox_put(phba, pmbox);
7028
7029                 /* Mbox cmd issue - BUSY */
7030                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7031                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
7032                                 "x%x x%x x%x x%x\n",
7033                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7034                                 mbx->mbxCommand, phba->pport->port_state,
7035                                 psli->sli_flag, flag);
7036
7037                 psli->slistat.mbox_busy++;
7038                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7039
7040                 if (pmbox->vport) {
7041                         lpfc_debugfs_disc_trc(pmbox->vport,
7042                                 LPFC_DISC_TRC_MBOX_VPORT,
7043                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
7044                                 (uint32_t)mbx->mbxCommand,
7045                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7046                 }
7047                 else {
7048                         lpfc_debugfs_disc_trc(phba->pport,
7049                                 LPFC_DISC_TRC_MBOX,
7050                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
7051                                 (uint32_t)mbx->mbxCommand,
7052                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7053                 }
7054
7055                 return MBX_BUSY;
7056         }
7057
7058         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7059
7060         /* If we are not polling, we MUST be in SLI2 mode */
7061         if (flag != MBX_POLL) {
7062                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7063                     (mbx->mbxCommand != MBX_KILL_BOARD)) {
7064                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7065                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7066                         /* Mbox command <mbxCommand> cannot issue */
7067                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7068                                         "(%d):2531 Mailbox command x%x "
7069                                         "cannot issue Data: x%x x%x\n",
7070                                         pmbox->vport ? pmbox->vport->vpi : 0,
7071                                         pmbox->u.mb.mbxCommand,
7072                                         psli->sli_flag, flag);
7073                         goto out_not_finished;
7074                 }
7075                 /* timeout active mbox command */
7076                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7077                                            1000);
7078                 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7079         }
7080
7081         /* Mailbox cmd <cmd> issue */
7082         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7083                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7084                         "x%x\n",
7085                         pmbox->vport ? pmbox->vport->vpi : 0,
7086                         mbx->mbxCommand, phba->pport->port_state,
7087                         psli->sli_flag, flag);
7088
7089         if (mbx->mbxCommand != MBX_HEARTBEAT) {
7090                 if (pmbox->vport) {
7091                         lpfc_debugfs_disc_trc(pmbox->vport,
7092                                 LPFC_DISC_TRC_MBOX_VPORT,
7093                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7094                                 (uint32_t)mbx->mbxCommand,
7095                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7096                 }
7097                 else {
7098                         lpfc_debugfs_disc_trc(phba->pport,
7099                                 LPFC_DISC_TRC_MBOX,
7100                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
7101                                 (uint32_t)mbx->mbxCommand,
7102                                 mbx->un.varWords[0], mbx->un.varWords[1]);
7103                 }
7104         }
7105
7106         psli->slistat.mbox_cmd++;
7107         evtctr = psli->slistat.mbox_event;
7108
7109         /* next set own bit for the adapter and copy over command word */
7110         mbx->mbxOwner = OWN_CHIP;
7111
7112         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7113                 /* Populate mbox extension offset word. */
7114                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7115                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7116                                 = (uint8_t *)phba->mbox_ext
7117                                   - (uint8_t *)phba->mbox;
7118                 }
7119
7120                 /* Copy the mailbox extension data */
7121                 if (pmbox->in_ext_byte_len && pmbox->context2) {
7122                         lpfc_sli_pcimem_bcopy(pmbox->context2,
7123                                 (uint8_t *)phba->mbox_ext,
7124                                 pmbox->in_ext_byte_len);
7125                 }
7126                 /* Copy command data to host SLIM area */
7127                 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7128         } else {
7129                 /* Populate mbox extension offset word. */
7130                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7131                         *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7132                                 = MAILBOX_HBA_EXT_OFFSET;
7133
7134                 /* Copy the mailbox extension data */
7135                 if (pmbox->in_ext_byte_len && pmbox->context2) {
7136                         lpfc_memcpy_to_slim(phba->MBslimaddr +
7137                                 MAILBOX_HBA_EXT_OFFSET,
7138                                 pmbox->context2, pmbox->in_ext_byte_len);
7139
7140                 }
7141                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7142                         /* copy command data into host mbox for cmpl */
7143                         lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7144                 }
7145
7146                 /* First copy mbox command data to HBA SLIM, skip past first
7147                    word */
7148                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7149                 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7150                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
7151
7152                 /* Next copy over first word, with mbxOwner set */
7153                 ldata = *((uint32_t *)mbx);
7154                 to_slim = phba->MBslimaddr;
7155                 writel(ldata, to_slim);
7156                 readl(to_slim); /* flush */
7157
7158                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7159                         /* switch over to host mailbox */
7160                         psli->sli_flag |= LPFC_SLI_ACTIVE;
7161                 }
7162         }
7163
7164         wmb();
7165
7166         switch (flag) {
7167         case MBX_NOWAIT:
7168                 /* Set up reference to mailbox command */
7169                 psli->mbox_active = pmbox;
7170                 /* Interrupt board to do it */
7171                 writel(CA_MBATT, phba->CAregaddr);
7172                 readl(phba->CAregaddr); /* flush */
7173                 /* Don't wait for it to finish, just return */
7174                 break;
7175
7176         case MBX_POLL:
7177                 /* Set up null reference to mailbox command */
7178                 psli->mbox_active = NULL;
7179                 /* Interrupt board to do it */
7180                 writel(CA_MBATT, phba->CAregaddr);
7181                 readl(phba->CAregaddr); /* flush */
7182
7183                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7184                         /* First read mbox status word */
7185                         word0 = *((uint32_t *)phba->mbox);
7186                         word0 = le32_to_cpu(word0);
7187                 } else {
7188                         /* First read mbox status word */
7189                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
7190                                 spin_unlock_irqrestore(&phba->hbalock,
7191                                                        drvr_flag);
7192                                 goto out_not_finished;
7193                         }
7194                 }
7195
7196                 /* Read the HBA Host Attention Register */
7197                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7198                         spin_unlock_irqrestore(&phba->hbalock,
7199                                                        drvr_flag);
7200                         goto out_not_finished;
7201                 }
7202                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7203                                                         1000) + jiffies;
7204                 i = 0;
7205                 /* Wait for command to complete */
7206                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7207                        (!(ha_copy & HA_MBATT) &&
7208                         (phba->link_state > LPFC_WARM_START))) {
7209                         if (time_after(jiffies, timeout)) {
7210                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7211                                 spin_unlock_irqrestore(&phba->hbalock,
7212                                                        drvr_flag);
7213                                 goto out_not_finished;
7214                         }
7215
7216                         /* Check if we took a mbox interrupt while we were
7217                            polling */
7218                         if (((word0 & OWN_CHIP) != OWN_CHIP)
7219                             && (evtctr != psli->slistat.mbox_event))
7220                                 break;
7221
7222                         if (i++ > 10) {
7223                                 spin_unlock_irqrestore(&phba->hbalock,
7224                                                        drvr_flag);
7225                                 msleep(1);
7226                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7227                         }
7228
7229                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7230                                 /* First copy command data */
7231                                 word0 = *((uint32_t *)phba->mbox);
7232                                 word0 = le32_to_cpu(word0);
7233                                 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7234                                         MAILBOX_t *slimmb;
7235                                         uint32_t slimword0;
7236                                         /* Check real SLIM for any errors */
7237                                         slimword0 = readl(phba->MBslimaddr);
7238                                         slimmb = (MAILBOX_t *) & slimword0;
7239                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7240                                             && slimmb->mbxStatus) {
7241                                                 psli->sli_flag &=
7242                                                     ~LPFC_SLI_ACTIVE;
7243                                                 word0 = slimword0;
7244                                         }
7245                                 }
7246                         } else {
7247                                 /* First copy command data */
7248                                 word0 = readl(phba->MBslimaddr);
7249                         }
7250                         /* Read the HBA Host Attention Register */
7251                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7252                                 spin_unlock_irqrestore(&phba->hbalock,
7253                                                        drvr_flag);
7254                                 goto out_not_finished;
7255                         }
7256                 }
7257
7258                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7259                         /* copy results back to user */
7260                         lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7261                         /* Copy the mailbox extension data */
7262                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7263                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7264                                                       pmbox->context2,
7265                                                       pmbox->out_ext_byte_len);
7266                         }
7267                 } else {
7268                         /* First copy command data */
7269                         lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7270                                                         MAILBOX_CMD_SIZE);
7271                         /* Copy the mailbox extension data */
7272                         if (pmbox->out_ext_byte_len && pmbox->context2) {
7273                                 lpfc_memcpy_from_slim(pmbox->context2,
7274                                         phba->MBslimaddr +
7275                                         MAILBOX_HBA_EXT_OFFSET,
7276                                         pmbox->out_ext_byte_len);
7277                         }
7278                 }
7279
7280                 writel(HA_MBATT, phba->HAregaddr);
7281                 readl(phba->HAregaddr); /* flush */
7282
7283                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7284                 status = mbx->mbxStatus;
7285         }
7286
7287         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7288         return status;
7289
7290 out_not_finished:
7291         if (processing_queue) {
7292                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7293                 lpfc_mbox_cmpl_put(phba, pmbox);
7294         }
7295         return MBX_NOT_FINISHED;
7296 }
7297
7298 /**
7299  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7300  * @phba: Pointer to HBA context object.
7301  *
7302  * The function blocks the posting of SLI4 asynchronous mailbox commands from
7303  * the driver internal pending mailbox queue. It will then try to wait out the
7304  * possible outstanding mailbox command before return.
7305  *
7306  * Returns:
7307  *      0 - the outstanding mailbox command completed; otherwise, the wait for
7308  *      the outstanding mailbox command timed out.
7309  **/
7310 static int
7311 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7312 {
7313         struct lpfc_sli *psli = &phba->sli;
7314         int rc = 0;
7315         unsigned long timeout = 0;
7316
7317         /* Mark the asynchronous mailbox command posting as blocked */
7318         spin_lock_irq(&phba->hbalock);
7319         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7320         /* Determine how long we might wait for the active mailbox
7321          * command to be gracefully completed by firmware.
7322          */
7323         if (phba->sli.mbox_active)
7324                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7325                                                 phba->sli.mbox_active) *
7326                                                 1000) + jiffies;
7327         spin_unlock_irq(&phba->hbalock);
7328
7329         /* Make sure the mailbox is really active */
7330         if (timeout)
7331                 lpfc_sli4_process_missed_mbox_completions(phba);
7332
7333         /* Wait for the outstnading mailbox command to complete */
7334         while (phba->sli.mbox_active) {
7335                 /* Check active mailbox complete status every 2ms */
7336                 msleep(2);
7337                 if (time_after(jiffies, timeout)) {
7338                         /* Timeout, marked the outstanding cmd not complete */
7339                         rc = 1;
7340                         break;
7341                 }
7342         }
7343
7344         /* Can not cleanly block async mailbox command, fails it */
7345         if (rc) {
7346                 spin_lock_irq(&phba->hbalock);
7347                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7348                 spin_unlock_irq(&phba->hbalock);
7349         }
7350         return rc;
7351 }
7352
7353 /**
7354  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7355  * @phba: Pointer to HBA context object.
7356  *
7357  * The function unblocks and resume posting of SLI4 asynchronous mailbox
7358  * commands from the driver internal pending mailbox queue. It makes sure
7359  * that there is no outstanding mailbox command before resuming posting
7360  * asynchronous mailbox commands. If, for any reason, there is outstanding
7361  * mailbox command, it will try to wait it out before resuming asynchronous
7362  * mailbox command posting.
7363  **/
7364 static void
7365 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7366 {
7367         struct lpfc_sli *psli = &phba->sli;
7368
7369         spin_lock_irq(&phba->hbalock);
7370         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7371                 /* Asynchronous mailbox posting is not blocked, do nothing */
7372                 spin_unlock_irq(&phba->hbalock);
7373                 return;
7374         }
7375
7376         /* Outstanding synchronous mailbox command is guaranteed to be done,
7377          * successful or timeout, after timing-out the outstanding mailbox
7378          * command shall always be removed, so just unblock posting async
7379          * mailbox command and resume
7380          */
7381         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7382         spin_unlock_irq(&phba->hbalock);
7383
7384         /* wake up worker thread to post asynchronlous mailbox command */
7385         lpfc_worker_wake_up(phba);
7386 }
7387
7388 /**
7389  * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7390  * @phba: Pointer to HBA context object.
7391  * @mboxq: Pointer to mailbox object.
7392  *
7393  * The function waits for the bootstrap mailbox register ready bit from
7394  * port for twice the regular mailbox command timeout value.
7395  *
7396  *      0 - no timeout on waiting for bootstrap mailbox register ready.
7397  *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7398  **/
7399 static int
7400 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7401 {
7402         uint32_t db_ready;
7403         unsigned long timeout;
7404         struct lpfc_register bmbx_reg;
7405
7406         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7407                                    * 1000) + jiffies;
7408
7409         do {
7410                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7411                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7412                 if (!db_ready)
7413                         msleep(2);
7414
7415                 if (time_after(jiffies, timeout))
7416                         return MBXERR_ERROR;
7417         } while (!db_ready);
7418
7419         return 0;
7420 }
7421
7422 /**
7423  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7424  * @phba: Pointer to HBA context object.
7425  * @mboxq: Pointer to mailbox object.
7426  *
7427  * The function posts a mailbox to the port.  The mailbox is expected
7428  * to be comletely filled in and ready for the port to operate on it.
7429  * This routine executes a synchronous completion operation on the
7430  * mailbox by polling for its completion.
7431  *
7432  * The caller must not be holding any locks when calling this routine.
7433  *
7434  * Returns:
7435  *      MBX_SUCCESS - mailbox posted successfully
7436  *      Any of the MBX error values.
7437  **/
7438 static int
7439 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7440 {
7441         int rc = MBX_SUCCESS;
7442         unsigned long iflag;
7443         uint32_t mcqe_status;
7444         uint32_t mbx_cmnd;
7445         struct lpfc_sli *psli = &phba->sli;
7446         struct lpfc_mqe *mb = &mboxq->u.mqe;
7447         struct lpfc_bmbx_create *mbox_rgn;
7448         struct dma_address *dma_address;
7449
7450         /*
7451          * Only one mailbox can be active to the bootstrap mailbox region
7452          * at a time and there is no queueing provided.
7453          */
7454         spin_lock_irqsave(&phba->hbalock, iflag);
7455         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7456                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7457                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7458                                 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7459                                 "cannot issue Data: x%x x%x\n",
7460                                 mboxq->vport ? mboxq->vport->vpi : 0,
7461                                 mboxq->u.mb.mbxCommand,
7462                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7463                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7464                                 psli->sli_flag, MBX_POLL);
7465                 return MBXERR_ERROR;
7466         }
7467         /* The server grabs the token and owns it until release */
7468         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7469         phba->sli.mbox_active = mboxq;
7470         spin_unlock_irqrestore(&phba->hbalock, iflag);
7471
7472         /* wait for bootstrap mbox register for readyness */
7473         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7474         if (rc)
7475                 goto exit;
7476
7477         /*
7478          * Initialize the bootstrap memory region to avoid stale data areas
7479          * in the mailbox post.  Then copy the caller's mailbox contents to
7480          * the bmbx mailbox region.
7481          */
7482         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7483         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7484         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7485                               sizeof(struct lpfc_mqe));
7486
7487         /* Post the high mailbox dma address to the port and wait for ready. */
7488         dma_address = &phba->sli4_hba.bmbx.dma_address;
7489         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7490
7491         /* wait for bootstrap mbox register for hi-address write done */
7492         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7493         if (rc)
7494                 goto exit;
7495
7496         /* Post the low mailbox dma address to the port. */
7497         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7498
7499         /* wait for bootstrap mbox register for low address write done */
7500         rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7501         if (rc)
7502                 goto exit;
7503
7504         /*
7505          * Read the CQ to ensure the mailbox has completed.
7506          * If so, update the mailbox status so that the upper layers
7507          * can complete the request normally.
7508          */
7509         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7510                               sizeof(struct lpfc_mqe));
7511         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7512         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7513                               sizeof(struct lpfc_mcqe));
7514         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7515         /*
7516          * When the CQE status indicates a failure and the mailbox status
7517          * indicates success then copy the CQE status into the mailbox status
7518          * (and prefix it with x4000).
7519          */
7520         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7521                 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7522                         bf_set(lpfc_mqe_status, mb,
7523                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
7524                 rc = MBXERR_ERROR;
7525         } else
7526                 lpfc_sli4_swap_str(phba, mboxq);
7527
7528         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7529                         "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7530                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7531                         " x%x x%x CQ: x%x x%x x%x x%x\n",
7532                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7533                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7534                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7535                         bf_get(lpfc_mqe_status, mb),
7536                         mb->un.mb_words[0], mb->un.mb_words[1],
7537                         mb->un.mb_words[2], mb->un.mb_words[3],
7538                         mb->un.mb_words[4], mb->un.mb_words[5],
7539                         mb->un.mb_words[6], mb->un.mb_words[7],
7540                         mb->un.mb_words[8], mb->un.mb_words[9],
7541                         mb->un.mb_words[10], mb->un.mb_words[11],
7542                         mb->un.mb_words[12], mboxq->mcqe.word0,
7543                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
7544                         mboxq->mcqe.trailer);
7545 exit:
7546         /* We are holding the token, no needed for lock when release */
7547         spin_lock_irqsave(&phba->hbalock, iflag);
7548         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7549         phba->sli.mbox_active = NULL;
7550         spin_unlock_irqrestore(&phba->hbalock, iflag);
7551         return rc;
7552 }
7553
7554 /**
7555  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7556  * @phba: Pointer to HBA context object.
7557  * @pmbox: Pointer to mailbox object.
7558  * @flag: Flag indicating how the mailbox need to be processed.
7559  *
7560  * This function is called by discovery code and HBA management code to submit
7561  * a mailbox command to firmware with SLI-4 interface spec.
7562  *
7563  * Return codes the caller owns the mailbox command after the return of the
7564  * function.
7565  **/
7566 static int
7567 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7568                        uint32_t flag)
7569 {
7570         struct lpfc_sli *psli = &phba->sli;
7571         unsigned long iflags;
7572         int rc;
7573
7574         /* dump from issue mailbox command if setup */
7575         lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7576
7577         rc = lpfc_mbox_dev_check(phba);
7578         if (unlikely(rc)) {
7579                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7580                                 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7581                                 "cannot issue Data: x%x x%x\n",
7582                                 mboxq->vport ? mboxq->vport->vpi : 0,
7583                                 mboxq->u.mb.mbxCommand,
7584                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7585                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7586                                 psli->sli_flag, flag);
7587                 goto out_not_finished;
7588         }
7589
7590         /* Detect polling mode and jump to a handler */
7591         if (!phba->sli4_hba.intr_enable) {
7592                 if (flag == MBX_POLL)
7593                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7594                 else
7595                         rc = -EIO;
7596                 if (rc != MBX_SUCCESS)
7597                         lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7598                                         "(%d):2541 Mailbox command x%x "
7599                                         "(x%x/x%x) failure: "
7600                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
7601                                         "Data: x%x x%x\n,",
7602                                         mboxq->vport ? mboxq->vport->vpi : 0,
7603                                         mboxq->u.mb.mbxCommand,
7604                                         lpfc_sli_config_mbox_subsys_get(phba,
7605                                                                         mboxq),
7606                                         lpfc_sli_config_mbox_opcode_get(phba,
7607                                                                         mboxq),
7608                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7609                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7610                                         bf_get(lpfc_mcqe_ext_status,
7611                                                &mboxq->mcqe),
7612                                         psli->sli_flag, flag);
7613                 return rc;
7614         } else if (flag == MBX_POLL) {
7615                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7616                                 "(%d):2542 Try to issue mailbox command "
7617                                 "x%x (x%x/x%x) synchronously ahead of async"
7618                                 "mailbox command queue: x%x x%x\n",
7619                                 mboxq->vport ? mboxq->vport->vpi : 0,
7620                                 mboxq->u.mb.mbxCommand,
7621                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7622                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7623                                 psli->sli_flag, flag);
7624                 /* Try to block the asynchronous mailbox posting */
7625                 rc = lpfc_sli4_async_mbox_block(phba);
7626                 if (!rc) {
7627                         /* Successfully blocked, now issue sync mbox cmd */
7628                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7629                         if (rc != MBX_SUCCESS)
7630                                 lpfc_printf_log(phba, KERN_WARNING,
7631                                         LOG_MBOX | LOG_SLI,
7632                                         "(%d):2597 Sync Mailbox command "
7633                                         "x%x (x%x/x%x) failure: "
7634                                         "mqe_sta: x%x mcqe_sta: x%x/x%x "
7635                                         "Data: x%x x%x\n,",
7636                                         mboxq->vport ? mboxq->vport->vpi : 0,
7637                                         mboxq->u.mb.mbxCommand,
7638                                         lpfc_sli_config_mbox_subsys_get(phba,
7639                                                                         mboxq),
7640                                         lpfc_sli_config_mbox_opcode_get(phba,
7641                                                                         mboxq),
7642                                         bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7643                                         bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7644                                         bf_get(lpfc_mcqe_ext_status,
7645                                                &mboxq->mcqe),
7646                                         psli->sli_flag, flag);
7647                         /* Unblock the async mailbox posting afterward */
7648                         lpfc_sli4_async_mbox_unblock(phba);
7649                 }
7650                 return rc;
7651         }
7652
7653         /* Now, interrupt mode asynchrous mailbox command */
7654         rc = lpfc_mbox_cmd_check(phba, mboxq);
7655         if (rc) {
7656                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7657                                 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7658                                 "cannot issue Data: x%x x%x\n",
7659                                 mboxq->vport ? mboxq->vport->vpi : 0,
7660                                 mboxq->u.mb.mbxCommand,
7661                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7662                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7663                                 psli->sli_flag, flag);
7664                 goto out_not_finished;
7665         }
7666
7667         /* Put the mailbox command to the driver internal FIFO */
7668         psli->slistat.mbox_busy++;
7669         spin_lock_irqsave(&phba->hbalock, iflags);
7670         lpfc_mbox_put(phba, mboxq);
7671         spin_unlock_irqrestore(&phba->hbalock, iflags);
7672         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7673                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
7674                         "x%x (x%x/x%x) x%x x%x x%x\n",
7675                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7676                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7677                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7678                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7679                         phba->pport->port_state,
7680                         psli->sli_flag, MBX_NOWAIT);
7681         /* Wake up worker thread to transport mailbox command from head */
7682         lpfc_worker_wake_up(phba);
7683
7684         return MBX_BUSY;
7685
7686 out_not_finished:
7687         return MBX_NOT_FINISHED;
7688 }
7689
7690 /**
7691  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7692  * @phba: Pointer to HBA context object.
7693  *
7694  * This function is called by worker thread to send a mailbox command to
7695  * SLI4 HBA firmware.
7696  *
7697  **/
7698 int
7699 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7700 {
7701         struct lpfc_sli *psli = &phba->sli;
7702         LPFC_MBOXQ_t *mboxq;
7703         int rc = MBX_SUCCESS;
7704         unsigned long iflags;
7705         struct lpfc_mqe *mqe;
7706         uint32_t mbx_cmnd;
7707
7708         /* Check interrupt mode before post async mailbox command */
7709         if (unlikely(!phba->sli4_hba.intr_enable))
7710                 return MBX_NOT_FINISHED;
7711
7712         /* Check for mailbox command service token */
7713         spin_lock_irqsave(&phba->hbalock, iflags);
7714         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7715                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7716                 return MBX_NOT_FINISHED;
7717         }
7718         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7719                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7720                 return MBX_NOT_FINISHED;
7721         }
7722         if (unlikely(phba->sli.mbox_active)) {
7723                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7724                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7725                                 "0384 There is pending active mailbox cmd\n");
7726                 return MBX_NOT_FINISHED;
7727         }
7728         /* Take the mailbox command service token */
7729         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7730
7731         /* Get the next mailbox command from head of queue */
7732         mboxq = lpfc_mbox_get(phba);
7733
7734         /* If no more mailbox command waiting for post, we're done */
7735         if (!mboxq) {
7736                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7737                 spin_unlock_irqrestore(&phba->hbalock, iflags);
7738                 return MBX_SUCCESS;
7739         }
7740         phba->sli.mbox_active = mboxq;
7741         spin_unlock_irqrestore(&phba->hbalock, iflags);
7742
7743         /* Check device readiness for posting mailbox command */
7744         rc = lpfc_mbox_dev_check(phba);
7745         if (unlikely(rc))
7746                 /* Driver clean routine will clean up pending mailbox */
7747                 goto out_not_finished;
7748
7749         /* Prepare the mbox command to be posted */
7750         mqe = &mboxq->u.mqe;
7751         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7752
7753         /* Start timer for the mbox_tmo and log some mailbox post messages */
7754         mod_timer(&psli->mbox_tmo, (jiffies +
7755                   msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
7756
7757         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7758                         "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7759                         "x%x x%x\n",
7760                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7761                         lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7762                         lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7763                         phba->pport->port_state, psli->sli_flag);
7764
7765         if (mbx_cmnd != MBX_HEARTBEAT) {
7766                 if (mboxq->vport) {
7767                         lpfc_debugfs_disc_trc(mboxq->vport,
7768                                 LPFC_DISC_TRC_MBOX_VPORT,
7769                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7770                                 mbx_cmnd, mqe->un.mb_words[0],
7771                                 mqe->un.mb_words[1]);
7772                 } else {
7773                         lpfc_debugfs_disc_trc(phba->pport,
7774                                 LPFC_DISC_TRC_MBOX,
7775                                 "MBOX Send: cmd:x%x mb:x%x x%x",
7776                                 mbx_cmnd, mqe->un.mb_words[0],
7777                                 mqe->un.mb_words[1]);
7778                 }
7779         }
7780         psli->slistat.mbox_cmd++;
7781
7782         /* Post the mailbox command to the port */
7783         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7784         if (rc != MBX_SUCCESS) {
7785                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7786                                 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7787                                 "cannot issue Data: x%x x%x\n",
7788                                 mboxq->vport ? mboxq->vport->vpi : 0,
7789                                 mboxq->u.mb.mbxCommand,
7790                                 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7791                                 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7792                                 psli->sli_flag, MBX_NOWAIT);
7793                 goto out_not_finished;
7794         }
7795
7796         return rc;
7797
7798 out_not_finished:
7799         spin_lock_irqsave(&phba->hbalock, iflags);
7800         if (phba->sli.mbox_active) {
7801                 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7802                 __lpfc_mbox_cmpl_put(phba, mboxq);
7803                 /* Release the token */
7804                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7805                 phba->sli.mbox_active = NULL;
7806         }
7807         spin_unlock_irqrestore(&phba->hbalock, iflags);
7808
7809         return MBX_NOT_FINISHED;
7810 }
7811
7812 /**
7813  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7814  * @phba: Pointer to HBA context object.
7815  * @pmbox: Pointer to mailbox object.
7816  * @flag: Flag indicating how the mailbox need to be processed.
7817  *
7818  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7819  * the API jump table function pointer from the lpfc_hba struct.
7820  *
7821  * Return codes the caller owns the mailbox command after the return of the
7822  * function.
7823  **/
7824 int
7825 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7826 {
7827         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7828 }
7829
7830 /**
7831  * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7832  * @phba: The hba struct for which this call is being executed.
7833  * @dev_grp: The HBA PCI-Device group number.
7834  *
7835  * This routine sets up the mbox interface API function jump table in @phba
7836  * struct.
7837  * Returns: 0 - success, -ENODEV - failure.
7838  **/
7839 int
7840 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7841 {
7842
7843         switch (dev_grp) {
7844         case LPFC_PCI_DEV_LP:
7845                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7846                 phba->lpfc_sli_handle_slow_ring_event =
7847                                 lpfc_sli_handle_slow_ring_event_s3;
7848                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7849                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7850                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7851                 break;
7852         case LPFC_PCI_DEV_OC:
7853                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7854                 phba->lpfc_sli_handle_slow_ring_event =
7855                                 lpfc_sli_handle_slow_ring_event_s4;
7856                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7857                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7858                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7859                 break;
7860         default:
7861                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7862                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
7863                                 dev_grp);
7864                 return -ENODEV;
7865                 break;
7866         }
7867         return 0;
7868 }
7869
7870 /**
7871  * __lpfc_sli_ringtx_put - Add an iocb to the txq
7872  * @phba: Pointer to HBA context object.
7873  * @pring: Pointer to driver SLI ring object.
7874  * @piocb: Pointer to address of newly added command iocb.
7875  *
7876  * This function is called with hbalock held to add a command
7877  * iocb to the txq when SLI layer cannot submit the command iocb
7878  * to the ring.
7879  **/
7880 void
7881 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7882                     struct lpfc_iocbq *piocb)
7883 {
7884         /* Insert the caller's iocb in the txq tail for later processing. */
7885         list_add_tail(&piocb->list, &pring->txq);
7886 }
7887
7888 /**
7889  * lpfc_sli_next_iocb - Get the next iocb in the txq
7890  * @phba: Pointer to HBA context object.
7891  * @pring: Pointer to driver SLI ring object.
7892  * @piocb: Pointer to address of newly added command iocb.
7893  *
7894  * This function is called with hbalock held before a new
7895  * iocb is submitted to the firmware. This function checks
7896  * txq to flush the iocbs in txq to Firmware before
7897  * submitting new iocbs to the Firmware.
7898  * If there are iocbs in the txq which need to be submitted
7899  * to firmware, lpfc_sli_next_iocb returns the first element
7900  * of the txq after dequeuing it from txq.
7901  * If there is no iocb in the txq then the function will return
7902  * *piocb and *piocb is set to NULL. Caller needs to check
7903  * *piocb to find if there are more commands in the txq.
7904  **/
7905 static struct lpfc_iocbq *
7906 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7907                    struct lpfc_iocbq **piocb)
7908 {
7909         struct lpfc_iocbq * nextiocb;
7910
7911         nextiocb = lpfc_sli_ringtx_get(phba, pring);
7912         if (!nextiocb) {
7913                 nextiocb = *piocb;
7914                 *piocb = NULL;
7915         }
7916
7917         return nextiocb;
7918 }
7919
7920 /**
7921  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7922  * @phba: Pointer to HBA context object.
7923  * @ring_number: SLI ring number to issue iocb on.
7924  * @piocb: Pointer to command iocb.
7925  * @flag: Flag indicating if this command can be put into txq.
7926  *
7927  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7928  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7929  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7930  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7931  * this function allows only iocbs for posting buffers. This function finds
7932  * next available slot in the command ring and posts the command to the
7933  * available slot and writes the port attention register to request HBA start
7934  * processing new iocb. If there is no slot available in the ring and
7935  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7936  * the function returns IOCB_BUSY.
7937  *
7938  * This function is called with hbalock held. The function will return success
7939  * after it successfully submit the iocb to firmware or after adding to the
7940  * txq.
7941  **/
7942 static int
7943 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
7944                     struct lpfc_iocbq *piocb, uint32_t flag)
7945 {
7946         struct lpfc_iocbq *nextiocb;
7947         IOCB_t *iocb;
7948         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7949
7950         if (piocb->iocb_cmpl && (!piocb->vport) &&
7951            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7952            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7953                 lpfc_printf_log(phba, KERN_ERR,
7954                                 LOG_SLI | LOG_VPORT,
7955                                 "1807 IOCB x%x failed. No vport\n",
7956                                 piocb->iocb.ulpCommand);
7957                 dump_stack();
7958                 return IOCB_ERROR;
7959         }
7960
7961
7962         /* If the PCI channel is in offline state, do not post iocbs. */
7963         if (unlikely(pci_channel_offline(phba->pcidev)))
7964                 return IOCB_ERROR;
7965
7966         /* If HBA has a deferred error attention, fail the iocb. */
7967         if (unlikely(phba->hba_flag & DEFER_ERATT))
7968                 return IOCB_ERROR;
7969
7970         /*
7971          * We should never get an IOCB if we are in a < LINK_DOWN state
7972          */
7973         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7974                 return IOCB_ERROR;
7975
7976         /*
7977          * Check to see if we are blocking IOCB processing because of a
7978          * outstanding event.
7979          */
7980         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7981                 goto iocb_busy;
7982
7983         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7984                 /*
7985                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7986                  * can be issued if the link is not up.
7987                  */
7988                 switch (piocb->iocb.ulpCommand) {
7989                 case CMD_GEN_REQUEST64_CR:
7990                 case CMD_GEN_REQUEST64_CX:
7991                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7992                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7993                                         FC_RCTL_DD_UNSOL_CMD) ||
7994                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7995                                         MENLO_TRANSPORT_TYPE))
7996
7997                                 goto iocb_busy;
7998                         break;
7999                 case CMD_QUE_RING_BUF_CN:
8000                 case CMD_QUE_RING_BUF64_CN:
8001                         /*
8002                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8003                          * completion, iocb_cmpl MUST be 0.
8004                          */
8005                         if (piocb->iocb_cmpl)
8006                                 piocb->iocb_cmpl = NULL;
8007                         /*FALLTHROUGH*/
8008                 case CMD_CREATE_XRI_CR:
8009                 case CMD_CLOSE_XRI_CN:
8010                 case CMD_CLOSE_XRI_CX:
8011                         break;
8012                 default:
8013                         goto iocb_busy;
8014                 }
8015
8016         /*
8017          * For FCP commands, we must be in a state where we can process link
8018          * attention events.
8019          */
8020         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
8021                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8022                 goto iocb_busy;
8023         }
8024
8025         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8026                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8027                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8028
8029         if (iocb)
8030                 lpfc_sli_update_ring(phba, pring);
8031         else
8032                 lpfc_sli_update_full_ring(phba, pring);
8033
8034         if (!piocb)
8035                 return IOCB_SUCCESS;
8036
8037         goto out_busy;
8038
8039  iocb_busy:
8040         pring->stats.iocb_cmd_delay++;
8041
8042  out_busy:
8043
8044         if (!(flag & SLI_IOCB_RET_IOCB)) {
8045                 __lpfc_sli_ringtx_put(phba, pring, piocb);
8046                 return IOCB_SUCCESS;
8047         }
8048
8049         return IOCB_BUSY;
8050 }
8051
8052 /**
8053  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8054  * @phba: Pointer to HBA context object.
8055  * @piocb: Pointer to command iocb.
8056  * @sglq: Pointer to the scatter gather queue object.
8057  *
8058  * This routine converts the bpl or bde that is in the IOCB
8059  * to a sgl list for the sli4 hardware. The physical address
8060  * of the bpl/bde is converted back to a virtual address.
8061  * If the IOCB contains a BPL then the list of BDE's is
8062  * converted to sli4_sge's. If the IOCB contains a single
8063  * BDE then it is converted to a single sli_sge.
8064  * The IOCB is still in cpu endianess so the contents of
8065  * the bpl can be used without byte swapping.
8066  *
8067  * Returns valid XRI = Success, NO_XRI = Failure.
8068 **/
8069 static uint16_t
8070 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8071                 struct lpfc_sglq *sglq)
8072 {
8073         uint16_t xritag = NO_XRI;
8074         struct ulp_bde64 *bpl = NULL;
8075         struct ulp_bde64 bde;
8076         struct sli4_sge *sgl  = NULL;
8077         struct lpfc_dmabuf *dmabuf;
8078         IOCB_t *icmd;
8079         int numBdes = 0;
8080         int i = 0;
8081         uint32_t offset = 0; /* accumulated offset in the sg request list */
8082         int inbound = 0; /* number of sg reply entries inbound from firmware */
8083
8084         if (!piocbq || !sglq)
8085                 return xritag;
8086
8087         sgl  = (struct sli4_sge *)sglq->sgl;
8088         icmd = &piocbq->iocb;
8089         if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8090                 return sglq->sli4_xritag;
8091         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8092                 numBdes = icmd->un.genreq64.bdl.bdeSize /
8093                                 sizeof(struct ulp_bde64);
8094                 /* The addrHigh and addrLow fields within the IOCB
8095                  * have not been byteswapped yet so there is no
8096                  * need to swap them back.
8097                  */
8098                 if (piocbq->context3)
8099                         dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8100                 else
8101                         return xritag;
8102
8103                 bpl  = (struct ulp_bde64 *)dmabuf->virt;
8104                 if (!bpl)
8105                         return xritag;
8106
8107                 for (i = 0; i < numBdes; i++) {
8108                         /* Should already be byte swapped. */
8109                         sgl->addr_hi = bpl->addrHigh;
8110                         sgl->addr_lo = bpl->addrLow;
8111
8112                         sgl->word2 = le32_to_cpu(sgl->word2);
8113                         if ((i+1) == numBdes)
8114                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
8115                         else
8116                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
8117                         /* swap the size field back to the cpu so we
8118                          * can assign it to the sgl.
8119                          */
8120                         bde.tus.w = le32_to_cpu(bpl->tus.w);
8121                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8122                         /* The offsets in the sgl need to be accumulated
8123                          * separately for the request and reply lists.
8124                          * The request is always first, the reply follows.
8125                          */
8126                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8127                                 /* add up the reply sg entries */
8128                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8129                                         inbound++;
8130                                 /* first inbound? reset the offset */
8131                                 if (inbound == 1)
8132                                         offset = 0;
8133                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8134                                 bf_set(lpfc_sli4_sge_type, sgl,
8135                                         LPFC_SGE_TYPE_DATA);
8136                                 offset += bde.tus.f.bdeSize;
8137                         }
8138                         sgl->word2 = cpu_to_le32(sgl->word2);
8139                         bpl++;
8140                         sgl++;
8141                 }
8142         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8143                         /* The addrHigh and addrLow fields of the BDE have not
8144                          * been byteswapped yet so they need to be swapped
8145                          * before putting them in the sgl.
8146                          */
8147                         sgl->addr_hi =
8148                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8149                         sgl->addr_lo =
8150                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8151                         sgl->word2 = le32_to_cpu(sgl->word2);
8152                         bf_set(lpfc_sli4_sge_last, sgl, 1);
8153                         sgl->word2 = cpu_to_le32(sgl->word2);
8154                         sgl->sge_len =
8155                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8156         }
8157         return sglq->sli4_xritag;
8158 }
8159
8160 /**
8161  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8162  * @phba: Pointer to HBA context object.
8163  * @piocb: Pointer to command iocb.
8164  * @wqe: Pointer to the work queue entry.
8165  *
8166  * This routine converts the iocb command to its Work Queue Entry
8167  * equivalent. The wqe pointer should not have any fields set when
8168  * this routine is called because it will memcpy over them.
8169  * This routine does not set the CQ_ID or the WQEC bits in the
8170  * wqe.
8171  *
8172  * Returns: 0 = Success, IOCB_ERROR = Failure.
8173  **/
8174 static int
8175 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8176                 union lpfc_wqe *wqe)
8177 {
8178         uint32_t xmit_len = 0, total_len = 0;
8179         uint8_t ct = 0;
8180         uint32_t fip;
8181         uint32_t abort_tag;
8182         uint8_t command_type = ELS_COMMAND_NON_FIP;
8183         uint8_t cmnd;
8184         uint16_t xritag;
8185         uint16_t abrt_iotag;
8186         struct lpfc_iocbq *abrtiocbq;
8187         struct ulp_bde64 *bpl = NULL;
8188         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8189         int numBdes, i;
8190         struct ulp_bde64 bde;
8191         struct lpfc_nodelist *ndlp;
8192         uint32_t *pcmd;
8193         uint32_t if_type;
8194
8195         fip = phba->hba_flag & HBA_FIP_SUPPORT;
8196         /* The fcp commands will set command type */
8197         if (iocbq->iocb_flag &  LPFC_IO_FCP)
8198                 command_type = FCP_COMMAND;
8199         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8200                 command_type = ELS_COMMAND_FIP;
8201         else
8202                 command_type = ELS_COMMAND_NON_FIP;
8203
8204         /* Some of the fields are in the right position already */
8205         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8206         abort_tag = (uint32_t) iocbq->iotag;
8207         xritag = iocbq->sli4_xritag;
8208         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8209         wqe->generic.wqe_com.word10 = 0;
8210         /* words0-2 bpl convert bde */
8211         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8212                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8213                                 sizeof(struct ulp_bde64);
8214                 bpl  = (struct ulp_bde64 *)
8215                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8216                 if (!bpl)
8217                         return IOCB_ERROR;
8218
8219                 /* Should already be byte swapped. */
8220                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
8221                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
8222                 /* swap the size field back to the cpu so we
8223                  * can assign it to the sgl.
8224                  */
8225                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
8226                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8227                 total_len = 0;
8228                 for (i = 0; i < numBdes; i++) {
8229                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
8230                         total_len += bde.tus.f.bdeSize;
8231                 }
8232         } else
8233                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8234
8235         iocbq->iocb.ulpIoTag = iocbq->iotag;
8236         cmnd = iocbq->iocb.ulpCommand;
8237
8238         switch (iocbq->iocb.ulpCommand) {
8239         case CMD_ELS_REQUEST64_CR:
8240                 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8241                         ndlp = iocbq->context_un.ndlp;
8242                 else
8243                         ndlp = (struct lpfc_nodelist *)iocbq->context1;
8244                 if (!iocbq->iocb.ulpLe) {
8245                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8246                                 "2007 Only Limited Edition cmd Format"
8247                                 " supported 0x%x\n",
8248                                 iocbq->iocb.ulpCommand);
8249                         return IOCB_ERROR;
8250                 }
8251
8252                 wqe->els_req.payload_len = xmit_len;
8253                 /* Els_reguest64 has a TMO */
8254                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8255                         iocbq->iocb.ulpTimeout);
8256                 /* Need a VF for word 4 set the vf bit*/
8257                 bf_set(els_req64_vf, &wqe->els_req, 0);
8258                 /* And a VFID for word 12 */
8259                 bf_set(els_req64_vfid, &wqe->els_req, 0);
8260                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8261                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8262                        iocbq->iocb.ulpContext);
8263                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8264                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8265                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8266                 if (command_type == ELS_COMMAND_FIP)
8267                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8268                                         >> LPFC_FIP_ELS_ID_SHIFT);
8269                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8270                                         iocbq->context2)->virt);
8271                 if_type = bf_get(lpfc_sli_intf_if_type,
8272                                         &phba->sli4_hba.sli_intf);
8273                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8274                         if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8275                                 *pcmd == ELS_CMD_SCR ||
8276                                 *pcmd == ELS_CMD_FDISC ||
8277                                 *pcmd == ELS_CMD_LOGO ||
8278                                 *pcmd == ELS_CMD_PLOGI)) {
8279                                 bf_set(els_req64_sp, &wqe->els_req, 1);
8280                                 bf_set(els_req64_sid, &wqe->els_req,
8281                                         iocbq->vport->fc_myDID);
8282                                 if ((*pcmd == ELS_CMD_FLOGI) &&
8283                                         !(phba->fc_topology ==
8284                                                 LPFC_TOPOLOGY_LOOP))
8285                                         bf_set(els_req64_sid, &wqe->els_req, 0);
8286                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8287                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8288                                         phba->vpi_ids[iocbq->vport->vpi]);
8289                         } else if (pcmd && iocbq->context1) {
8290                                 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8291                                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8292                                         phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8293                         }
8294                 }
8295                 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8296                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8297                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8298                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8299                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8300                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8301                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8302                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8303                 wqe->els_req.max_response_payload_len = total_len - xmit_len;
8304                 break;
8305         case CMD_XMIT_SEQUENCE64_CX:
8306                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8307                        iocbq->iocb.un.ulpWord[3]);
8308                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8309                        iocbq->iocb.unsli3.rcvsli3.ox_id);
8310                 /* The entire sequence is transmitted for this IOCB */
8311                 xmit_len = total_len;
8312                 cmnd = CMD_XMIT_SEQUENCE64_CR;
8313                 if (phba->link_flag & LS_LOOPBACK_MODE)
8314                         bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8315         case CMD_XMIT_SEQUENCE64_CR:
8316                 /* word3 iocb=io_tag32 wqe=reserved */
8317                 wqe->xmit_sequence.rsvd3 = 0;
8318                 /* word4 relative_offset memcpy */
8319                 /* word5 r_ctl/df_ctl memcpy */
8320                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8321                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8322                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8323                        LPFC_WQE_IOD_WRITE);
8324                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8325                        LPFC_WQE_LENLOC_WORD12);
8326                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8327                 wqe->xmit_sequence.xmit_len = xmit_len;
8328                 command_type = OTHER_COMMAND;
8329                 break;
8330         case CMD_XMIT_BCAST64_CN:
8331                 /* word3 iocb=iotag32 wqe=seq_payload_len */
8332                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8333                 /* word4 iocb=rsvd wqe=rsvd */
8334                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8335                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8336                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8337                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8338                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8339                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8340                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8341                        LPFC_WQE_LENLOC_WORD3);
8342                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8343                 break;
8344         case CMD_FCP_IWRITE64_CR:
8345                 command_type = FCP_COMMAND_DATA_OUT;
8346                 /* word3 iocb=iotag wqe=payload_offset_len */
8347                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8348                 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8349                        xmit_len + sizeof(struct fcp_rsp));
8350                 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8351                        0);
8352                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8353                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8354                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8355                        iocbq->iocb.ulpFCP2Rcvy);
8356                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8357                 /* Always open the exchange */
8358                 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
8359                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8360                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8361                        LPFC_WQE_LENLOC_WORD4);
8362                 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8363                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8364                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8365                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8366                         bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8367                         if (phba->cfg_XLanePriority) {
8368                                 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8369                                 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8370                                        (phba->cfg_XLanePriority << 1));
8371                         }
8372                 }
8373                 break;
8374         case CMD_FCP_IREAD64_CR:
8375                 /* word3 iocb=iotag wqe=payload_offset_len */
8376                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8377                 bf_set(payload_offset_len, &wqe->fcp_iread,
8378                        xmit_len + sizeof(struct fcp_rsp));
8379                 bf_set(cmd_buff_len, &wqe->fcp_iread,
8380                        0);
8381                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8382                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8383                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8384                        iocbq->iocb.ulpFCP2Rcvy);
8385                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8386                 /* Always open the exchange */
8387                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
8388                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8389                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8390                        LPFC_WQE_LENLOC_WORD4);
8391                 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8392                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8393                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8394                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8395                         bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8396                         if (phba->cfg_XLanePriority) {
8397                                 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8398                                 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8399                                        (phba->cfg_XLanePriority << 1));
8400                         }
8401                 }
8402                 break;
8403         case CMD_FCP_ICMND64_CR:
8404                 /* word3 iocb=iotag wqe=payload_offset_len */
8405                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8406                 bf_set(payload_offset_len, &wqe->fcp_icmd,
8407                        xmit_len + sizeof(struct fcp_rsp));
8408                 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8409                        0);
8410                 /* word3 iocb=IO_TAG wqe=reserved */
8411                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8412                 /* Always open the exchange */
8413                 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8414                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8415                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8416                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8417                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8418                        LPFC_WQE_LENLOC_NONE);
8419                 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
8420                 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8421                        iocbq->iocb.ulpFCP2Rcvy);
8422                 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8423                         bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8424                         if (phba->cfg_XLanePriority) {
8425                                 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8426                                 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8427                                        (phba->cfg_XLanePriority << 1));
8428                         }
8429                 }
8430                 break;
8431         case CMD_GEN_REQUEST64_CR:
8432                 /* For this command calculate the xmit length of the
8433                  * request bde.
8434                  */
8435                 xmit_len = 0;
8436                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8437                         sizeof(struct ulp_bde64);
8438                 for (i = 0; i < numBdes; i++) {
8439                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8440                         if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8441                                 break;
8442                         xmit_len += bde.tus.f.bdeSize;
8443                 }
8444                 /* word3 iocb=IO_TAG wqe=request_payload_len */
8445                 wqe->gen_req.request_payload_len = xmit_len;
8446                 /* word4 iocb=parameter wqe=relative_offset memcpy */
8447                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8448                 /* word6 context tag copied in memcpy */
8449                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
8450                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8451                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8452                                 "2015 Invalid CT %x command 0x%x\n",
8453                                 ct, iocbq->iocb.ulpCommand);
8454                         return IOCB_ERROR;
8455                 }
8456                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8457                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8458                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8459                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8460                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8461                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8462                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8463                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8464                 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
8465                 command_type = OTHER_COMMAND;
8466                 break;
8467         case CMD_XMIT_ELS_RSP64_CX:
8468                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8469                 /* words0-2 BDE memcpy */
8470                 /* word3 iocb=iotag32 wqe=response_payload_len */
8471                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8472                 /* word4 */
8473                 wqe->xmit_els_rsp.word4 = 0;
8474                 /* word5 iocb=rsvd wge=did */
8475                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8476                          iocbq->iocb.un.xseq64.xmit_els_remoteID);
8477
8478                 if_type = bf_get(lpfc_sli_intf_if_type,
8479                                         &phba->sli4_hba.sli_intf);
8480                 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8481                         if (iocbq->vport->fc_flag & FC_PT2PT) {
8482                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8483                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8484                                         iocbq->vport->fc_myDID);
8485                                 if (iocbq->vport->fc_myDID == Fabric_DID) {
8486                                         bf_set(wqe_els_did,
8487                                                 &wqe->xmit_els_rsp.wqe_dest, 0);
8488                                 }
8489                         }
8490                 }
8491                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8492                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8493                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8494                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8495                        iocbq->iocb.unsli3.rcvsli3.ox_id);
8496                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8497                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8498                                phba->vpi_ids[iocbq->vport->vpi]);
8499                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8500                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8501                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8502                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8503                        LPFC_WQE_LENLOC_WORD3);
8504                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
8505                 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8506                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8507                 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8508                                         iocbq->context2)->virt);
8509                 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8510                                 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8511                                 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8512                                         iocbq->vport->fc_myDID);
8513                                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8514                                 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8515                                         phba->vpi_ids[phba->pport->vpi]);
8516                 }
8517                 command_type = OTHER_COMMAND;
8518                 break;
8519         case CMD_CLOSE_XRI_CN:
8520         case CMD_ABORT_XRI_CN:
8521         case CMD_ABORT_XRI_CX:
8522                 /* words 0-2 memcpy should be 0 rserved */
8523                 /* port will send abts */
8524                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8525                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8526                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8527                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8528                 } else
8529                         fip = 0;
8530
8531                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
8532                         /*
8533                          * The link is down, or the command was ELS_FIP
8534                          * so the fw does not need to send abts
8535                          * on the wire.
8536                          */
8537                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8538                 else
8539                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8540                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8541                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8542                 wqe->abort_cmd.rsrvd5 = 0;
8543                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
8544                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8545                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
8546                 /*
8547                  * The abort handler will send us CMD_ABORT_XRI_CN or
8548                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8549                  */
8550                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8551                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8552                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8553                        LPFC_WQE_LENLOC_NONE);
8554                 cmnd = CMD_ABORT_XRI_CX;
8555                 command_type = OTHER_COMMAND;
8556                 xritag = 0;
8557                 break;
8558         case CMD_XMIT_BLS_RSP64_CX:
8559                 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8560                 /* As BLS ABTS RSP WQE is very different from other WQEs,
8561                  * we re-construct this WQE here based on information in
8562                  * iocbq from scratch.
8563                  */
8564                 memset(wqe, 0, sizeof(union lpfc_wqe));
8565                 /* OX_ID is invariable to who sent ABTS to CT exchange */
8566                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
8567                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8568                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
8569                     LPFC_ABTS_UNSOL_INT) {
8570                         /* ABTS sent by initiator to CT exchange, the
8571                          * RX_ID field will be filled with the newly
8572                          * allocated responder XRI.
8573                          */
8574                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8575                                iocbq->sli4_xritag);
8576                 } else {
8577                         /* ABTS sent by responder to CT exchange, the
8578                          * RX_ID field will be filled with the responder
8579                          * RX_ID from ABTS.
8580                          */
8581                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8582                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8583                 }
8584                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8585                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8586
8587                 /* Use CT=VPI */
8588                 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8589                         ndlp->nlp_DID);
8590                 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8591                         iocbq->iocb.ulpContext);
8592                 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8593                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8594                         phba->vpi_ids[phba->pport->vpi]);
8595                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8596                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8597                        LPFC_WQE_LENLOC_NONE);
8598                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8599                 command_type = OTHER_COMMAND;
8600                 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8601                         bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8602                                bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8603                         bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8604                                bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8605                         bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8606                                bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8607                 }
8608
8609                 break;
8610         case CMD_XRI_ABORTED_CX:
8611         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8612         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8613         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8614         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8615         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8616         default:
8617                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8618                                 "2014 Invalid command 0x%x\n",
8619                                 iocbq->iocb.ulpCommand);
8620                 return IOCB_ERROR;
8621                 break;
8622         }
8623
8624         if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8625                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8626         else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8627                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8628         else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8629                 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8630         iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8631                               LPFC_IO_DIF_INSERT);
8632         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8633         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8634         wqe->generic.wqe_com.abort_tag = abort_tag;
8635         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8636         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8637         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8638         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8639         return 0;
8640 }
8641
8642 /**
8643  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8644  * @phba: Pointer to HBA context object.
8645  * @ring_number: SLI ring number to issue iocb on.
8646  * @piocb: Pointer to command iocb.
8647  * @flag: Flag indicating if this command can be put into txq.
8648  *
8649  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8650  * an iocb command to an HBA with SLI-4 interface spec.
8651  *
8652  * This function is called with hbalock held. The function will return success
8653  * after it successfully submit the iocb to firmware or after adding to the
8654  * txq.
8655  **/
8656 static int
8657 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8658                          struct lpfc_iocbq *piocb, uint32_t flag)
8659 {
8660         struct lpfc_sglq *sglq;
8661         union lpfc_wqe wqe;
8662         struct lpfc_queue *wq;
8663         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8664
8665         if (piocb->sli4_xritag == NO_XRI) {
8666                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8667                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8668                         sglq = NULL;
8669                 else {
8670                         if (!list_empty(&pring->txq)) {
8671                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
8672                                         __lpfc_sli_ringtx_put(phba,
8673                                                 pring, piocb);
8674                                         return IOCB_SUCCESS;
8675                                 } else {
8676                                         return IOCB_BUSY;
8677                                 }
8678                         } else {
8679                                 sglq = __lpfc_sli_get_sglq(phba, piocb);
8680                                 if (!sglq) {
8681                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
8682                                                 __lpfc_sli_ringtx_put(phba,
8683                                                                 pring,
8684                                                                 piocb);
8685                                                 return IOCB_SUCCESS;
8686                                         } else
8687                                                 return IOCB_BUSY;
8688                                 }
8689                         }
8690                 }
8691         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
8692                 /* These IO's already have an XRI and a mapped sgl. */
8693                 sglq = NULL;
8694         } else {
8695                 /*
8696                  * This is a continuation of a commandi,(CX) so this
8697                  * sglq is on the active list
8698                  */
8699                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
8700                 if (!sglq)
8701                         return IOCB_ERROR;
8702         }
8703
8704         if (sglq) {
8705                 piocb->sli4_lxritag = sglq->sli4_lxritag;
8706                 piocb->sli4_xritag = sglq->sli4_xritag;
8707                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8708                         return IOCB_ERROR;
8709         }
8710
8711         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8712                 return IOCB_ERROR;
8713
8714         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8715             (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8716                 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
8717                         wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8718                 } else {
8719                         wq = phba->sli4_hba.oas_wq;
8720                 }
8721                 if (lpfc_sli4_wq_put(wq, &wqe))
8722                         return IOCB_ERROR;
8723         } else {
8724                 if (unlikely(!phba->sli4_hba.els_wq))
8725                         return IOCB_ERROR;
8726                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8727                         return IOCB_ERROR;
8728         }
8729         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8730
8731         return 0;
8732 }
8733
8734 /**
8735  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8736  *
8737  * This routine wraps the actual lockless version for issusing IOCB function
8738  * pointer from the lpfc_hba struct.
8739  *
8740  * Return codes:
8741  *      IOCB_ERROR - Error
8742  *      IOCB_SUCCESS - Success
8743  *      IOCB_BUSY - Busy
8744  **/
8745 int
8746 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8747                 struct lpfc_iocbq *piocb, uint32_t flag)
8748 {
8749         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8750 }
8751
8752 /**
8753  * lpfc_sli_api_table_setup - Set up sli api function jump table
8754  * @phba: The hba struct for which this call is being executed.
8755  * @dev_grp: The HBA PCI-Device group number.
8756  *
8757  * This routine sets up the SLI interface API function jump table in @phba
8758  * struct.
8759  * Returns: 0 - success, -ENODEV - failure.
8760  **/
8761 int
8762 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8763 {
8764
8765         switch (dev_grp) {
8766         case LPFC_PCI_DEV_LP:
8767                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8768                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8769                 break;
8770         case LPFC_PCI_DEV_OC:
8771                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8772                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8773                 break;
8774         default:
8775                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8776                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
8777                                 dev_grp);
8778                 return -ENODEV;
8779                 break;
8780         }
8781         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8782         return 0;
8783 }
8784
8785 /**
8786  * lpfc_sli_calc_ring - Calculates which ring to use
8787  * @phba: Pointer to HBA context object.
8788  * @ring_number: Initial ring
8789  * @piocb: Pointer to command iocb.
8790  *
8791  * For SLI4, FCP IO can deferred to one fo many WQs, based on
8792  * fcp_wqidx, thus we need to calculate the corresponding ring.
8793  * Since ABORTS must go on the same WQ of the command they are
8794  * aborting, we use command's fcp_wqidx.
8795  */
8796 int
8797 lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8798                     struct lpfc_iocbq *piocb)
8799 {
8800         if (phba->sli_rev < LPFC_SLI_REV4)
8801                 return ring_number;
8802
8803         if (piocb->iocb_flag &  (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8804                 if (!(phba->cfg_fof) ||
8805                                 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8806                         if (unlikely(!phba->sli4_hba.fcp_wq))
8807                                 return LPFC_HBA_ERROR;
8808                         /*
8809                          * for abort iocb fcp_wqidx should already
8810                          * be setup based on what work queue we used.
8811                          */
8812                         if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
8813                                 piocb->fcp_wqidx =
8814                                         lpfc_sli4_scmd_to_wqidx_distr(phba,
8815                                                               piocb->context1);
8816                         ring_number = MAX_SLI3_CONFIGURED_RINGS +
8817                                 piocb->fcp_wqidx;
8818                 } else {
8819                         if (unlikely(!phba->sli4_hba.oas_wq))
8820                                 return LPFC_HBA_ERROR;
8821                         piocb->fcp_wqidx = 0;
8822                         ring_number =  LPFC_FCP_OAS_RING;
8823                 }
8824         }
8825         return ring_number;
8826 }
8827
8828 /**
8829  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8830  * @phba: Pointer to HBA context object.
8831  * @pring: Pointer to driver SLI ring object.
8832  * @piocb: Pointer to command iocb.
8833  * @flag: Flag indicating if this command can be put into txq.
8834  *
8835  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8836  * function. This function gets the hbalock and calls
8837  * __lpfc_sli_issue_iocb function and will return the error returned
8838  * by __lpfc_sli_issue_iocb function. This wrapper is used by
8839  * functions which do not hold hbalock.
8840  **/
8841 int
8842 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8843                     struct lpfc_iocbq *piocb, uint32_t flag)
8844 {
8845         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
8846         struct lpfc_sli_ring *pring;
8847         struct lpfc_queue *fpeq;
8848         struct lpfc_eqe *eqe;
8849         unsigned long iflags;
8850         int rc, idx;
8851
8852         if (phba->sli_rev == LPFC_SLI_REV4) {
8853                 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
8854                 if (unlikely(ring_number == LPFC_HBA_ERROR))
8855                         return IOCB_ERROR;
8856                 idx = piocb->fcp_wqidx;
8857
8858                 pring = &phba->sli.ring[ring_number];
8859                 spin_lock_irqsave(&pring->ring_lock, iflags);
8860                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8861                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8862
8863                 if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
8864                         fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8865
8866                         if (atomic_dec_and_test(&fcp_eq_hdl->
8867                                 fcp_eq_in_use)) {
8868
8869                                 /* Get associated EQ with this index */
8870                                 fpeq = phba->sli4_hba.hba_eq[idx];
8871
8872                                 /* Turn off interrupts from this EQ */
8873                                 lpfc_sli4_eq_clr_intr(fpeq);
8874
8875                                 /*
8876                                  * Process all the events on FCP EQ
8877                                  */
8878                                 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8879                                         lpfc_sli4_hba_handle_eqe(phba,
8880                                                 eqe, idx);
8881                                         fpeq->EQ_processed++;
8882                                 }
8883
8884                                 /* Always clear and re-arm the EQ */
8885                                 lpfc_sli4_eq_release(fpeq,
8886                                         LPFC_QUEUE_REARM);
8887                         }
8888                         atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8889                 }
8890         } else {
8891                 /* For now, SLI2/3 will still use hbalock */
8892                 spin_lock_irqsave(&phba->hbalock, iflags);
8893                 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8894                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8895         }
8896         return rc;
8897 }
8898
8899 /**
8900  * lpfc_extra_ring_setup - Extra ring setup function
8901  * @phba: Pointer to HBA context object.
8902  *
8903  * This function is called while driver attaches with the
8904  * HBA to setup the extra ring. The extra ring is used
8905  * only when driver needs to support target mode functionality
8906  * or IP over FC functionalities.
8907  *
8908  * This function is called with no lock held.
8909  **/
8910 static int
8911 lpfc_extra_ring_setup( struct lpfc_hba *phba)
8912 {
8913         struct lpfc_sli *psli;
8914         struct lpfc_sli_ring *pring;
8915
8916         psli = &phba->sli;
8917
8918         /* Adjust cmd/rsp ring iocb entries more evenly */
8919
8920         /* Take some away from the FCP ring */
8921         pring = &psli->ring[psli->fcp_ring];
8922         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8923         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8924         pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8925         pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8926
8927         /* and give them to the extra ring */
8928         pring = &psli->ring[psli->extra_ring];
8929
8930         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8931         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8932         pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8933         pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8934
8935         /* Setup default profile for this ring */
8936         pring->iotag_max = 4096;
8937         pring->num_mask = 1;
8938         pring->prt[0].profile = 0;      /* Mask 0 */
8939         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8940         pring->prt[0].type = phba->cfg_multi_ring_type;
8941         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8942         return 0;
8943 }
8944
8945 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8946  * @phba: Pointer to HBA context object.
8947  * @iocbq: Pointer to iocb object.
8948  *
8949  * The async_event handler calls this routine when it receives
8950  * an ASYNC_STATUS_CN event from the port.  The port generates
8951  * this event when an Abort Sequence request to an rport fails
8952  * twice in succession.  The abort could be originated by the
8953  * driver or by the port.  The ABTS could have been for an ELS
8954  * or FCP IO.  The port only generates this event when an ABTS
8955  * fails to complete after one retry.
8956  */
8957 static void
8958 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8959                           struct lpfc_iocbq *iocbq)
8960 {
8961         struct lpfc_nodelist *ndlp = NULL;
8962         uint16_t rpi = 0, vpi = 0;
8963         struct lpfc_vport *vport = NULL;
8964
8965         /* The rpi in the ulpContext is vport-sensitive. */
8966         vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8967         rpi = iocbq->iocb.ulpContext;
8968
8969         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8970                         "3092 Port generated ABTS async event "
8971                         "on vpi %d rpi %d status 0x%x\n",
8972                         vpi, rpi, iocbq->iocb.ulpStatus);
8973
8974         vport = lpfc_find_vport_by_vpid(phba, vpi);
8975         if (!vport)
8976                 goto err_exit;
8977         ndlp = lpfc_findnode_rpi(vport, rpi);
8978         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8979                 goto err_exit;
8980
8981         if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8982                 lpfc_sli_abts_recover_port(vport, ndlp);
8983         return;
8984
8985  err_exit:
8986         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8987                         "3095 Event Context not found, no "
8988                         "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8989                         iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8990                         vpi, rpi);
8991 }
8992
8993 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8994  * @phba: pointer to HBA context object.
8995  * @ndlp: nodelist pointer for the impacted rport.
8996  * @axri: pointer to the wcqe containing the failed exchange.
8997  *
8998  * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8999  * port.  The port generates this event when an abort exchange request to an
9000  * rport fails twice in succession with no reply.  The abort could be originated
9001  * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
9002  */
9003 void
9004 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9005                            struct lpfc_nodelist *ndlp,
9006                            struct sli4_wcqe_xri_aborted *axri)
9007 {
9008         struct lpfc_vport *vport;
9009         uint32_t ext_status = 0;
9010
9011         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9012                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9013                                 "3115 Node Context not found, driver "
9014                                 "ignoring abts err event\n");
9015                 return;
9016         }
9017
9018         vport = ndlp->vport;
9019         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9020                         "3116 Port generated FCP XRI ABORT event on "
9021                         "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9022                         ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9023                         bf_get(lpfc_wcqe_xa_xri, axri),
9024                         bf_get(lpfc_wcqe_xa_status, axri),
9025                         axri->parameter);
9026
9027         /*
9028          * Catch the ABTS protocol failure case.  Older OCe FW releases returned
9029          * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9030          * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9031          */
9032         ext_status = axri->parameter & IOERR_PARAM_MASK;
9033         if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9034             ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9035                 lpfc_sli_abts_recover_port(vport, ndlp);
9036 }
9037
9038 /**
9039  * lpfc_sli_async_event_handler - ASYNC iocb handler function
9040  * @phba: Pointer to HBA context object.
9041  * @pring: Pointer to driver SLI ring object.
9042  * @iocbq: Pointer to iocb object.
9043  *
9044  * This function is called by the slow ring event handler
9045  * function when there is an ASYNC event iocb in the ring.
9046  * This function is called with no lock held.
9047  * Currently this function handles only temperature related
9048  * ASYNC events. The function decodes the temperature sensor
9049  * event message and posts events for the management applications.
9050  **/
9051 static void
9052 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9053         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9054 {
9055         IOCB_t *icmd;
9056         uint16_t evt_code;
9057         struct temp_event temp_event_data;
9058         struct Scsi_Host *shost;
9059         uint32_t *iocb_w;
9060
9061         icmd = &iocbq->iocb;
9062         evt_code = icmd->un.asyncstat.evt_code;
9063
9064         switch (evt_code) {
9065         case ASYNC_TEMP_WARN:
9066         case ASYNC_TEMP_SAFE:
9067                 temp_event_data.data = (uint32_t) icmd->ulpContext;
9068                 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9069                 if (evt_code == ASYNC_TEMP_WARN) {
9070                         temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9071                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9072                                 "0347 Adapter is very hot, please take "
9073                                 "corrective action. temperature : %d Celsius\n",
9074                                 (uint32_t) icmd->ulpContext);
9075                 } else {
9076                         temp_event_data.event_code = LPFC_NORMAL_TEMP;
9077                         lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9078                                 "0340 Adapter temperature is OK now. "
9079                                 "temperature : %d Celsius\n",
9080                                 (uint32_t) icmd->ulpContext);
9081                 }
9082
9083                 /* Send temperature change event to applications */
9084                 shost = lpfc_shost_from_vport(phba->pport);
9085                 fc_host_post_vendor_event(shost, fc_get_event_number(),
9086                         sizeof(temp_event_data), (char *) &temp_event_data,
9087                         LPFC_NL_VENDOR_ID);
9088                 break;
9089         case ASYNC_STATUS_CN:
9090                 lpfc_sli_abts_err_handler(phba, iocbq);
9091                 break;
9092         default:
9093                 iocb_w = (uint32_t *) icmd;
9094                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9095                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
9096                         " evt_code 0x%x\n"
9097                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
9098                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
9099                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
9100                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9101                         pring->ringno, icmd->un.asyncstat.evt_code,
9102                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9103                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9104                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9105                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9106
9107                 break;
9108         }
9109 }
9110
9111
9112 /**
9113  * lpfc_sli_setup - SLI ring setup function
9114  * @phba: Pointer to HBA context object.
9115  *
9116  * lpfc_sli_setup sets up rings of the SLI interface with
9117  * number of iocbs per ring and iotags. This function is
9118  * called while driver attach to the HBA and before the
9119  * interrupts are enabled. So there is no need for locking.
9120  *
9121  * This function always returns 0.
9122  **/
9123 int
9124 lpfc_sli_setup(struct lpfc_hba *phba)
9125 {
9126         int i, totiocbsize = 0;
9127         struct lpfc_sli *psli = &phba->sli;
9128         struct lpfc_sli_ring *pring;
9129
9130         psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9131         if (phba->sli_rev == LPFC_SLI_REV4)
9132                 psli->num_rings += phba->cfg_fcp_io_channel;
9133         psli->sli_flag = 0;
9134         psli->fcp_ring = LPFC_FCP_RING;
9135         psli->next_ring = LPFC_FCP_NEXT_RING;
9136         psli->extra_ring = LPFC_EXTRA_RING;
9137
9138         psli->iocbq_lookup = NULL;
9139         psli->iocbq_lookup_len = 0;
9140         psli->last_iotag = 0;
9141
9142         for (i = 0; i < psli->num_rings; i++) {
9143                 pring = &psli->ring[i];
9144                 switch (i) {
9145                 case LPFC_FCP_RING:     /* ring 0 - FCP */
9146                         /* numCiocb and numRiocb are used in config_port */
9147                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9148                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9149                         pring->sli.sli3.numCiocb +=
9150                                 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9151                         pring->sli.sli3.numRiocb +=
9152                                 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9153                         pring->sli.sli3.numCiocb +=
9154                                 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9155                         pring->sli.sli3.numRiocb +=
9156                                 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9157                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9158                                                         SLI3_IOCB_CMD_SIZE :
9159                                                         SLI2_IOCB_CMD_SIZE;
9160                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9161                                                         SLI3_IOCB_RSP_SIZE :
9162                                                         SLI2_IOCB_RSP_SIZE;
9163                         pring->iotag_ctr = 0;
9164                         pring->iotag_max =
9165                             (phba->cfg_hba_queue_depth * 2);
9166                         pring->fast_iotag = pring->iotag_max;
9167                         pring->num_mask = 0;
9168                         break;
9169                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
9170                         /* numCiocb and numRiocb are used in config_port */
9171                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9172                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9173                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9174                                                         SLI3_IOCB_CMD_SIZE :
9175                                                         SLI2_IOCB_CMD_SIZE;
9176                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9177                                                         SLI3_IOCB_RSP_SIZE :
9178                                                         SLI2_IOCB_RSP_SIZE;
9179                         pring->iotag_max = phba->cfg_hba_queue_depth;
9180                         pring->num_mask = 0;
9181                         break;
9182                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
9183                         /* numCiocb and numRiocb are used in config_port */
9184                         pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9185                         pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9186                         pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9187                                                         SLI3_IOCB_CMD_SIZE :
9188                                                         SLI2_IOCB_CMD_SIZE;
9189                         pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9190                                                         SLI3_IOCB_RSP_SIZE :
9191                                                         SLI2_IOCB_RSP_SIZE;
9192                         pring->fast_iotag = 0;
9193                         pring->iotag_ctr = 0;
9194                         pring->iotag_max = 4096;
9195                         pring->lpfc_sli_rcv_async_status =
9196                                 lpfc_sli_async_event_handler;
9197                         pring->num_mask = LPFC_MAX_RING_MASK;
9198                         pring->prt[0].profile = 0;      /* Mask 0 */
9199                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9200                         pring->prt[0].type = FC_TYPE_ELS;
9201                         pring->prt[0].lpfc_sli_rcv_unsol_event =
9202                             lpfc_els_unsol_event;
9203                         pring->prt[1].profile = 0;      /* Mask 1 */
9204                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
9205                         pring->prt[1].type = FC_TYPE_ELS;
9206                         pring->prt[1].lpfc_sli_rcv_unsol_event =
9207                             lpfc_els_unsol_event;
9208                         pring->prt[2].profile = 0;      /* Mask 2 */
9209                         /* NameServer Inquiry */
9210                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9211                         /* NameServer */
9212                         pring->prt[2].type = FC_TYPE_CT;
9213                         pring->prt[2].lpfc_sli_rcv_unsol_event =
9214                             lpfc_ct_unsol_event;
9215                         pring->prt[3].profile = 0;      /* Mask 3 */
9216                         /* NameServer response */
9217                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9218                         /* NameServer */
9219                         pring->prt[3].type = FC_TYPE_CT;
9220                         pring->prt[3].lpfc_sli_rcv_unsol_event =
9221                             lpfc_ct_unsol_event;
9222                         break;
9223                 }
9224                 totiocbsize += (pring->sli.sli3.numCiocb *
9225                         pring->sli.sli3.sizeCiocb) +
9226                         (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
9227         }
9228         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
9229                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
9230                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9231                        "SLI2 SLIM Data: x%x x%lx\n",
9232                        phba->brd_no, totiocbsize,
9233                        (unsigned long) MAX_SLIM_IOCB_SIZE);
9234         }
9235         if (phba->cfg_multi_ring_support == 2)
9236                 lpfc_extra_ring_setup(phba);
9237
9238         return 0;
9239 }
9240
9241 /**
9242  * lpfc_sli_queue_setup - Queue initialization function
9243  * @phba: Pointer to HBA context object.
9244  *
9245  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9246  * ring. This function also initializes ring indices of each ring.
9247  * This function is called during the initialization of the SLI
9248  * interface of an HBA.
9249  * This function is called with no lock held and always returns
9250  * 1.
9251  **/
9252 int
9253 lpfc_sli_queue_setup(struct lpfc_hba *phba)
9254 {
9255         struct lpfc_sli *psli;
9256         struct lpfc_sli_ring *pring;
9257         int i;
9258
9259         psli = &phba->sli;
9260         spin_lock_irq(&phba->hbalock);
9261         INIT_LIST_HEAD(&psli->mboxq);
9262         INIT_LIST_HEAD(&psli->mboxq_cmpl);
9263         /* Initialize list headers for txq and txcmplq as double linked lists */
9264         for (i = 0; i < psli->num_rings; i++) {
9265                 pring = &psli->ring[i];
9266                 pring->ringno = i;
9267                 pring->sli.sli3.next_cmdidx  = 0;
9268                 pring->sli.sli3.local_getidx = 0;
9269                 pring->sli.sli3.cmdidx = 0;
9270                 pring->flag = 0;
9271                 INIT_LIST_HEAD(&pring->txq);
9272                 INIT_LIST_HEAD(&pring->txcmplq);
9273                 INIT_LIST_HEAD(&pring->iocb_continueq);
9274                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9275                 INIT_LIST_HEAD(&pring->postbufq);
9276                 spin_lock_init(&pring->ring_lock);
9277         }
9278         spin_unlock_irq(&phba->hbalock);
9279         return 1;
9280 }
9281
9282 /**
9283  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9284  * @phba: Pointer to HBA context object.
9285  *
9286  * This routine flushes the mailbox command subsystem. It will unconditionally
9287  * flush all the mailbox commands in the three possible stages in the mailbox
9288  * command sub-system: pending mailbox command queue; the outstanding mailbox
9289  * command; and completed mailbox command queue. It is caller's responsibility
9290  * to make sure that the driver is in the proper state to flush the mailbox
9291  * command sub-system. Namely, the posting of mailbox commands into the
9292  * pending mailbox command queue from the various clients must be stopped;
9293  * either the HBA is in a state that it will never works on the outstanding
9294  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9295  * mailbox command has been completed.
9296  **/
9297 static void
9298 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9299 {
9300         LIST_HEAD(completions);
9301         struct lpfc_sli *psli = &phba->sli;
9302         LPFC_MBOXQ_t *pmb;
9303         unsigned long iflag;
9304
9305         /* Flush all the mailbox commands in the mbox system */
9306         spin_lock_irqsave(&phba->hbalock, iflag);
9307         /* The pending mailbox command queue */
9308         list_splice_init(&phba->sli.mboxq, &completions);
9309         /* The outstanding active mailbox command */
9310         if (psli->mbox_active) {
9311                 list_add_tail(&psli->mbox_active->list, &completions);
9312                 psli->mbox_active = NULL;
9313                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9314         }
9315         /* The completed mailbox command queue */
9316         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9317         spin_unlock_irqrestore(&phba->hbalock, iflag);
9318
9319         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9320         while (!list_empty(&completions)) {
9321                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9322                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9323                 if (pmb->mbox_cmpl)
9324                         pmb->mbox_cmpl(phba, pmb);
9325         }
9326 }
9327
9328 /**
9329  * lpfc_sli_host_down - Vport cleanup function
9330  * @vport: Pointer to virtual port object.
9331  *
9332  * lpfc_sli_host_down is called to clean up the resources
9333  * associated with a vport before destroying virtual
9334  * port data structures.
9335  * This function does following operations:
9336  * - Free discovery resources associated with this virtual
9337  *   port.
9338  * - Free iocbs associated with this virtual port in
9339  *   the txq.
9340  * - Send abort for all iocb commands associated with this
9341  *   vport in txcmplq.
9342  *
9343  * This function is called with no lock held and always returns 1.
9344  **/
9345 int
9346 lpfc_sli_host_down(struct lpfc_vport *vport)
9347 {
9348         LIST_HEAD(completions);
9349         struct lpfc_hba *phba = vport->phba;
9350         struct lpfc_sli *psli = &phba->sli;
9351         struct lpfc_sli_ring *pring;
9352         struct lpfc_iocbq *iocb, *next_iocb;
9353         int i;
9354         unsigned long flags = 0;
9355         uint16_t prev_pring_flag;
9356
9357         lpfc_cleanup_discovery_resources(vport);
9358
9359         spin_lock_irqsave(&phba->hbalock, flags);
9360         for (i = 0; i < psli->num_rings; i++) {
9361                 pring = &psli->ring[i];
9362                 prev_pring_flag = pring->flag;
9363                 /* Only slow rings */
9364                 if (pring->ringno == LPFC_ELS_RING) {
9365                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
9366                         /* Set the lpfc data pending flag */
9367                         set_bit(LPFC_DATA_READY, &phba->data_flags);
9368                 }
9369                 /*
9370                  * Error everything on the txq since these iocbs have not been
9371                  * given to the FW yet.
9372                  */
9373                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9374                         if (iocb->vport != vport)
9375                                 continue;
9376                         list_move_tail(&iocb->list, &completions);
9377                 }
9378
9379                 /* Next issue ABTS for everything on the txcmplq */
9380                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9381                                                                         list) {
9382                         if (iocb->vport != vport)
9383                                 continue;
9384                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9385                 }
9386
9387                 pring->flag = prev_pring_flag;
9388         }
9389
9390         spin_unlock_irqrestore(&phba->hbalock, flags);
9391
9392         /* Cancel all the IOCBs from the completions list */
9393         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9394                               IOERR_SLI_DOWN);
9395         return 1;
9396 }
9397
9398 /**
9399  * lpfc_sli_hba_down - Resource cleanup function for the HBA
9400  * @phba: Pointer to HBA context object.
9401  *
9402  * This function cleans up all iocb, buffers, mailbox commands
9403  * while shutting down the HBA. This function is called with no
9404  * lock held and always returns 1.
9405  * This function does the following to cleanup driver resources:
9406  * - Free discovery resources for each virtual port
9407  * - Cleanup any pending fabric iocbs
9408  * - Iterate through the iocb txq and free each entry
9409  *   in the list.
9410  * - Free up any buffer posted to the HBA
9411  * - Free mailbox commands in the mailbox queue.
9412  **/
9413 int
9414 lpfc_sli_hba_down(struct lpfc_hba *phba)
9415 {
9416         LIST_HEAD(completions);
9417         struct lpfc_sli *psli = &phba->sli;
9418         struct lpfc_sli_ring *pring;
9419         struct lpfc_dmabuf *buf_ptr;
9420         unsigned long flags = 0;
9421         int i;
9422
9423         /* Shutdown the mailbox command sub-system */
9424         lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
9425
9426         lpfc_hba_down_prep(phba);
9427
9428         lpfc_fabric_abort_hba(phba);
9429
9430         spin_lock_irqsave(&phba->hbalock, flags);
9431         for (i = 0; i < psli->num_rings; i++) {
9432                 pring = &psli->ring[i];
9433                 /* Only slow rings */
9434                 if (pring->ringno == LPFC_ELS_RING) {
9435                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
9436                         /* Set the lpfc data pending flag */
9437                         set_bit(LPFC_DATA_READY, &phba->data_flags);
9438                 }
9439
9440                 /*
9441                  * Error everything on the txq since these iocbs have not been
9442                  * given to the FW yet.
9443                  */
9444                 list_splice_init(&pring->txq, &completions);
9445         }
9446         spin_unlock_irqrestore(&phba->hbalock, flags);
9447
9448         /* Cancel all the IOCBs from the completions list */
9449         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9450                               IOERR_SLI_DOWN);
9451
9452         spin_lock_irqsave(&phba->hbalock, flags);
9453         list_splice_init(&phba->elsbuf, &completions);
9454         phba->elsbuf_cnt = 0;
9455         phba->elsbuf_prev_cnt = 0;
9456         spin_unlock_irqrestore(&phba->hbalock, flags);
9457
9458         while (!list_empty(&completions)) {
9459                 list_remove_head(&completions, buf_ptr,
9460                         struct lpfc_dmabuf, list);
9461                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9462                 kfree(buf_ptr);
9463         }
9464
9465         /* Return any active mbox cmds */
9466         del_timer_sync(&psli->mbox_tmo);
9467
9468         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
9469         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9470         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
9471
9472         return 1;
9473 }
9474
9475 /**
9476  * lpfc_sli_pcimem_bcopy - SLI memory copy function
9477  * @srcp: Source memory pointer.
9478  * @destp: Destination memory pointer.
9479  * @cnt: Number of words required to be copied.
9480  *
9481  * This function is used for copying data between driver memory
9482  * and the SLI memory. This function also changes the endianness
9483  * of each word if native endianness is different from SLI
9484  * endianness. This function can be called with or without
9485  * lock.
9486  **/
9487 void
9488 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9489 {
9490         uint32_t *src = srcp;
9491         uint32_t *dest = destp;
9492         uint32_t ldata;
9493         int i;
9494
9495         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9496                 ldata = *src;
9497                 ldata = le32_to_cpu(ldata);
9498                 *dest = ldata;
9499                 src++;
9500                 dest++;
9501         }
9502 }
9503
9504
9505 /**
9506  * lpfc_sli_bemem_bcopy - SLI memory copy function
9507  * @srcp: Source memory pointer.
9508  * @destp: Destination memory pointer.
9509  * @cnt: Number of words required to be copied.
9510  *
9511  * This function is used for copying data between a data structure
9512  * with big endian representation to local endianness.
9513  * This function can be called with or without lock.
9514  **/
9515 void
9516 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9517 {
9518         uint32_t *src = srcp;
9519         uint32_t *dest = destp;
9520         uint32_t ldata;
9521         int i;
9522
9523         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9524                 ldata = *src;
9525                 ldata = be32_to_cpu(ldata);
9526                 *dest = ldata;
9527                 src++;
9528                 dest++;
9529         }
9530 }
9531
9532 /**
9533  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
9534  * @phba: Pointer to HBA context object.
9535  * @pring: Pointer to driver SLI ring object.
9536  * @mp: Pointer to driver buffer object.
9537  *
9538  * This function is called with no lock held.
9539  * It always return zero after adding the buffer to the postbufq
9540  * buffer list.
9541  **/
9542 int
9543 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9544                          struct lpfc_dmabuf *mp)
9545 {
9546         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9547            later */
9548         spin_lock_irq(&phba->hbalock);
9549         list_add_tail(&mp->list, &pring->postbufq);
9550         pring->postbufq_cnt++;
9551         spin_unlock_irq(&phba->hbalock);
9552         return 0;
9553 }
9554
9555 /**
9556  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
9557  * @phba: Pointer to HBA context object.
9558  *
9559  * When HBQ is enabled, buffers are searched based on tags. This function
9560  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9561  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9562  * does not conflict with tags of buffer posted for unsolicited events.
9563  * The function returns the allocated tag. The function is called with
9564  * no locks held.
9565  **/
9566 uint32_t
9567 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9568 {
9569         spin_lock_irq(&phba->hbalock);
9570         phba->buffer_tag_count++;
9571         /*
9572          * Always set the QUE_BUFTAG_BIT to distiguish between
9573          * a tag assigned by HBQ.
9574          */
9575         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9576         spin_unlock_irq(&phba->hbalock);
9577         return phba->buffer_tag_count;
9578 }
9579
9580 /**
9581  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
9582  * @phba: Pointer to HBA context object.
9583  * @pring: Pointer to driver SLI ring object.
9584  * @tag: Buffer tag.
9585  *
9586  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9587  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9588  * iocb is posted to the response ring with the tag of the buffer.
9589  * This function searches the pring->postbufq list using the tag
9590  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9591  * iocb. If the buffer is found then lpfc_dmabuf object of the
9592  * buffer is returned to the caller else NULL is returned.
9593  * This function is called with no lock held.
9594  **/
9595 struct lpfc_dmabuf *
9596 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9597                         uint32_t tag)
9598 {
9599         struct lpfc_dmabuf *mp, *next_mp;
9600         struct list_head *slp = &pring->postbufq;
9601
9602         /* Search postbufq, from the beginning, looking for a match on tag */
9603         spin_lock_irq(&phba->hbalock);
9604         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9605                 if (mp->buffer_tag == tag) {
9606                         list_del_init(&mp->list);
9607                         pring->postbufq_cnt--;
9608                         spin_unlock_irq(&phba->hbalock);
9609                         return mp;
9610                 }
9611         }
9612
9613         spin_unlock_irq(&phba->hbalock);
9614         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9615                         "0402 Cannot find virtual addr for buffer tag on "
9616                         "ring %d Data x%lx x%p x%p x%x\n",
9617                         pring->ringno, (unsigned long) tag,
9618                         slp->next, slp->prev, pring->postbufq_cnt);
9619
9620         return NULL;
9621 }
9622
9623 /**
9624  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
9625  * @phba: Pointer to HBA context object.
9626  * @pring: Pointer to driver SLI ring object.
9627  * @phys: DMA address of the buffer.
9628  *
9629  * This function searches the buffer list using the dma_address
9630  * of unsolicited event to find the driver's lpfc_dmabuf object
9631  * corresponding to the dma_address. The function returns the
9632  * lpfc_dmabuf object if a buffer is found else it returns NULL.
9633  * This function is called by the ct and els unsolicited event
9634  * handlers to get the buffer associated with the unsolicited
9635  * event.
9636  *
9637  * This function is called with no lock held.
9638  **/
9639 struct lpfc_dmabuf *
9640 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9641                          dma_addr_t phys)
9642 {
9643         struct lpfc_dmabuf *mp, *next_mp;
9644         struct list_head *slp = &pring->postbufq;
9645
9646         /* Search postbufq, from the beginning, looking for a match on phys */
9647         spin_lock_irq(&phba->hbalock);
9648         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9649                 if (mp->phys == phys) {
9650                         list_del_init(&mp->list);
9651                         pring->postbufq_cnt--;
9652                         spin_unlock_irq(&phba->hbalock);
9653                         return mp;
9654                 }
9655         }
9656
9657         spin_unlock_irq(&phba->hbalock);
9658         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9659                         "0410 Cannot find virtual addr for mapped buf on "
9660                         "ring %d Data x%llx x%p x%p x%x\n",
9661                         pring->ringno, (unsigned long long)phys,
9662                         slp->next, slp->prev, pring->postbufq_cnt);
9663         return NULL;
9664 }
9665
9666 /**
9667  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9668  * @phba: Pointer to HBA context object.
9669  * @cmdiocb: Pointer to driver command iocb object.
9670  * @rspiocb: Pointer to driver response iocb object.
9671  *
9672  * This function is the completion handler for the abort iocbs for
9673  * ELS commands. This function is called from the ELS ring event
9674  * handler with no lock held. This function frees memory resources
9675  * associated with the abort iocb.
9676  **/
9677 static void
9678 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9679                         struct lpfc_iocbq *rspiocb)
9680 {
9681         IOCB_t *irsp = &rspiocb->iocb;
9682         uint16_t abort_iotag, abort_context;
9683         struct lpfc_iocbq *abort_iocb = NULL;
9684
9685         if (irsp->ulpStatus) {
9686
9687                 /*
9688                  * Assume that the port already completed and returned, or
9689                  * will return the iocb. Just Log the message.
9690                  */
9691                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9692                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9693
9694                 spin_lock_irq(&phba->hbalock);
9695                 if (phba->sli_rev < LPFC_SLI_REV4) {
9696                         if (abort_iotag != 0 &&
9697                                 abort_iotag <= phba->sli.last_iotag)
9698                                 abort_iocb =
9699                                         phba->sli.iocbq_lookup[abort_iotag];
9700                 } else
9701                         /* For sli4 the abort_tag is the XRI,
9702                          * so the abort routine puts the iotag  of the iocb
9703                          * being aborted in the context field of the abort
9704                          * IOCB.
9705                          */
9706                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
9707
9708                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9709                                 "0327 Cannot abort els iocb %p "
9710                                 "with tag %x context %x, abort status %x, "
9711                                 "abort code %x\n",
9712                                 abort_iocb, abort_iotag, abort_context,
9713                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
9714
9715                 spin_unlock_irq(&phba->hbalock);
9716         }
9717         lpfc_sli_release_iocbq(phba, cmdiocb);
9718         return;
9719 }
9720
9721 /**
9722  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9723  * @phba: Pointer to HBA context object.
9724  * @cmdiocb: Pointer to driver command iocb object.
9725  * @rspiocb: Pointer to driver response iocb object.
9726  *
9727  * The function is called from SLI ring event handler with no
9728  * lock held. This function is the completion handler for ELS commands
9729  * which are aborted. The function frees memory resources used for
9730  * the aborted ELS commands.
9731  **/
9732 static void
9733 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9734                      struct lpfc_iocbq *rspiocb)
9735 {
9736         IOCB_t *irsp = &rspiocb->iocb;
9737
9738         /* ELS cmd tag <ulpIoTag> completes */
9739         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9740                         "0139 Ignoring ELS cmd tag x%x completion Data: "
9741                         "x%x x%x x%x\n",
9742                         irsp->ulpIoTag, irsp->ulpStatus,
9743                         irsp->un.ulpWord[4], irsp->ulpTimeout);
9744         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9745                 lpfc_ct_free_iocb(phba, cmdiocb);
9746         else
9747                 lpfc_els_free_iocb(phba, cmdiocb);
9748         return;
9749 }
9750
9751 /**
9752  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9753  * @phba: Pointer to HBA context object.
9754  * @pring: Pointer to driver SLI ring object.
9755  * @cmdiocb: Pointer to driver command iocb object.
9756  *
9757  * This function issues an abort iocb for the provided command iocb down to
9758  * the port. Other than the case the outstanding command iocb is an abort
9759  * request, this function issues abort out unconditionally. This function is
9760  * called with hbalock held. The function returns 0 when it fails due to
9761  * memory allocation failure or when the command iocb is an abort request.
9762  **/
9763 static int
9764 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9765                            struct lpfc_iocbq *cmdiocb)
9766 {
9767         struct lpfc_vport *vport = cmdiocb->vport;
9768         struct lpfc_iocbq *abtsiocbp;
9769         IOCB_t *icmd = NULL;
9770         IOCB_t *iabt = NULL;
9771         int ring_number;
9772         int retval;
9773         unsigned long iflags;
9774
9775         /*
9776          * There are certain command types we don't want to abort.  And we
9777          * don't want to abort commands that are already in the process of
9778          * being aborted.
9779          */
9780         icmd = &cmdiocb->iocb;
9781         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9782             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9783             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9784                 return 0;
9785
9786         /* issue ABTS for this IOCB based on iotag */
9787         abtsiocbp = __lpfc_sli_get_iocbq(phba);
9788         if (abtsiocbp == NULL)
9789                 return 0;
9790
9791         /* This signals the response to set the correct status
9792          * before calling the completion handler
9793          */
9794         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9795
9796         iabt = &abtsiocbp->iocb;
9797         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9798         iabt->un.acxri.abortContextTag = icmd->ulpContext;
9799         if (phba->sli_rev == LPFC_SLI_REV4) {
9800                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
9801                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9802         }
9803         else
9804                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
9805         iabt->ulpLe = 1;
9806         iabt->ulpClass = icmd->ulpClass;
9807
9808         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9809         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9810         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9811                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9812         if (cmdiocb->iocb_flag & LPFC_IO_FOF)
9813                 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
9814
9815         if (phba->link_state >= LPFC_LINK_UP)
9816                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9817         else
9818                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9819
9820         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
9821         abtsiocbp->vport = vport;
9822
9823         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9824                          "0339 Abort xri x%x, original iotag x%x, "
9825                          "abort cmd iotag x%x\n",
9826                          iabt->un.acxri.abortIoTag,
9827                          iabt->un.acxri.abortContextTag,
9828                          abtsiocbp->iotag);
9829
9830         if (phba->sli_rev == LPFC_SLI_REV4) {
9831                 ring_number =
9832                         lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
9833                 if (unlikely(ring_number == LPFC_HBA_ERROR))
9834                         return 0;
9835                 pring = &phba->sli.ring[ring_number];
9836                 /* Note: both hbalock and ring_lock need to be set here */
9837                 spin_lock_irqsave(&pring->ring_lock, iflags);
9838                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9839                         abtsiocbp, 0);
9840                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9841         } else {
9842                 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9843                         abtsiocbp, 0);
9844         }
9845
9846         if (retval)
9847                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
9848
9849         /*
9850          * Caller to this routine should check for IOCB_ERROR
9851          * and handle it properly.  This routine no longer removes
9852          * iocb off txcmplq and call compl in case of IOCB_ERROR.
9853          */
9854         return retval;
9855 }
9856
9857 /**
9858  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9859  * @phba: Pointer to HBA context object.
9860  * @pring: Pointer to driver SLI ring object.
9861  * @cmdiocb: Pointer to driver command iocb object.
9862  *
9863  * This function issues an abort iocb for the provided command iocb. In case
9864  * of unloading, the abort iocb will not be issued to commands on the ELS
9865  * ring. Instead, the callback function shall be changed to those commands
9866  * so that nothing happens when them finishes. This function is called with
9867  * hbalock held. The function returns 0 when the command iocb is an abort
9868  * request.
9869  **/
9870 int
9871 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9872                            struct lpfc_iocbq *cmdiocb)
9873 {
9874         struct lpfc_vport *vport = cmdiocb->vport;
9875         int retval = IOCB_ERROR;
9876         IOCB_t *icmd = NULL;
9877
9878         /*
9879          * There are certain command types we don't want to abort.  And we
9880          * don't want to abort commands that are already in the process of
9881          * being aborted.
9882          */
9883         icmd = &cmdiocb->iocb;
9884         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9885             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9886             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9887                 return 0;
9888
9889         /*
9890          * If we're unloading, don't abort iocb on the ELS ring, but change
9891          * the callback so that nothing happens when it finishes.
9892          */
9893         if ((vport->load_flag & FC_UNLOADING) &&
9894             (pring->ringno == LPFC_ELS_RING)) {
9895                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9896                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9897                 else
9898                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9899                 goto abort_iotag_exit;
9900         }
9901
9902         /* Now, we try to issue the abort to the cmdiocb out */
9903         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9904
9905 abort_iotag_exit:
9906         /*
9907          * Caller to this routine should check for IOCB_ERROR
9908          * and handle it properly.  This routine no longer removes
9909          * iocb off txcmplq and call compl in case of IOCB_ERROR.
9910          */
9911         return retval;
9912 }
9913
9914 /**
9915  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9916  * @phba: pointer to lpfc HBA data structure.
9917  *
9918  * This routine will abort all pending and outstanding iocbs to an HBA.
9919  **/
9920 void
9921 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9922 {
9923         struct lpfc_sli *psli = &phba->sli;
9924         struct lpfc_sli_ring *pring;
9925         int i;
9926
9927         for (i = 0; i < psli->num_rings; i++) {
9928                 pring = &psli->ring[i];
9929                 lpfc_sli_abort_iocb_ring(phba, pring);
9930         }
9931 }
9932
9933 /**
9934  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
9935  * @iocbq: Pointer to driver iocb object.
9936  * @vport: Pointer to driver virtual port object.
9937  * @tgt_id: SCSI ID of the target.
9938  * @lun_id: LUN ID of the scsi device.
9939  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9940  *
9941  * This function acts as an iocb filter for functions which abort or count
9942  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9943  * 0 if the filtering criteria is met for the given iocb and will return
9944  * 1 if the filtering criteria is not met.
9945  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9946  * given iocb is for the SCSI device specified by vport, tgt_id and
9947  * lun_id parameter.
9948  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
9949  * given iocb is for the SCSI target specified by vport and tgt_id
9950  * parameters.
9951  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9952  * given iocb is for the SCSI host associated with the given vport.
9953  * This function is called with no locks held.
9954  **/
9955 static int
9956 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9957                            uint16_t tgt_id, uint64_t lun_id,
9958                            lpfc_ctx_cmd ctx_cmd)
9959 {
9960         struct lpfc_scsi_buf *lpfc_cmd;
9961         int rc = 1;
9962
9963         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
9964                 return rc;
9965
9966         if (iocbq->vport != vport)
9967                 return rc;
9968
9969         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
9970
9971         if (lpfc_cmd->pCmd == NULL)
9972                 return rc;
9973
9974         switch (ctx_cmd) {
9975         case LPFC_CTX_LUN:
9976                 if ((lpfc_cmd->rdata->pnode) &&
9977                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9978                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
9979                         rc = 0;
9980                 break;
9981         case LPFC_CTX_TGT:
9982                 if ((lpfc_cmd->rdata->pnode) &&
9983                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
9984                         rc = 0;
9985                 break;
9986         case LPFC_CTX_HOST:
9987                 rc = 0;
9988                 break;
9989         default:
9990                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
9991                         __func__, ctx_cmd);
9992                 break;
9993         }
9994
9995         return rc;
9996 }
9997
9998 /**
9999  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
10000  * @vport: Pointer to virtual port.
10001  * @tgt_id: SCSI ID of the target.
10002  * @lun_id: LUN ID of the scsi device.
10003  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10004  *
10005  * This function returns number of FCP commands pending for the vport.
10006  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10007  * commands pending on the vport associated with SCSI device specified
10008  * by tgt_id and lun_id parameters.
10009  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10010  * commands pending on the vport associated with SCSI target specified
10011  * by tgt_id parameter.
10012  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10013  * commands pending on the vport.
10014  * This function returns the number of iocbs which satisfy the filter.
10015  * This function is called without any lock held.
10016  **/
10017 int
10018 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10019                   lpfc_ctx_cmd ctx_cmd)
10020 {
10021         struct lpfc_hba *phba = vport->phba;
10022         struct lpfc_iocbq *iocbq;
10023         int sum, i;
10024
10025         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10026                 iocbq = phba->sli.iocbq_lookup[i];
10027
10028                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10029                                                 ctx_cmd) == 0)
10030                         sum++;
10031         }
10032
10033         return sum;
10034 }
10035
10036 /**
10037  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
10038  * @phba: Pointer to HBA context object
10039  * @cmdiocb: Pointer to command iocb object.
10040  * @rspiocb: Pointer to response iocb object.
10041  *
10042  * This function is called when an aborted FCP iocb completes. This
10043  * function is called by the ring event handler with no lock held.
10044  * This function frees the iocb.
10045  **/
10046 void
10047 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10048                         struct lpfc_iocbq *rspiocb)
10049 {
10050         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10051                         "3096 ABORT_XRI_CN completing on rpi x%x "
10052                         "original iotag x%x, abort cmd iotag x%x "
10053                         "status 0x%x, reason 0x%x\n",
10054                         cmdiocb->iocb.un.acxri.abortContextTag,
10055                         cmdiocb->iocb.un.acxri.abortIoTag,
10056                         cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10057                         rspiocb->iocb.un.ulpWord[4]);
10058         lpfc_sli_release_iocbq(phba, cmdiocb);
10059         return;
10060 }
10061
10062 /**
10063  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
10064  * @vport: Pointer to virtual port.
10065  * @pring: Pointer to driver SLI ring object.
10066  * @tgt_id: SCSI ID of the target.
10067  * @lun_id: LUN ID of the scsi device.
10068  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10069  *
10070  * This function sends an abort command for every SCSI command
10071  * associated with the given virtual port pending on the ring
10072  * filtered by lpfc_sli_validate_fcp_iocb function.
10073  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10074  * FCP iocbs associated with lun specified by tgt_id and lun_id
10075  * parameters
10076  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10077  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10078  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10079  * FCP iocbs associated with virtual port.
10080  * This function returns number of iocbs it failed to abort.
10081  * This function is called with no locks held.
10082  **/
10083 int
10084 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10085                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
10086 {
10087         struct lpfc_hba *phba = vport->phba;
10088         struct lpfc_iocbq *iocbq;
10089         struct lpfc_iocbq *abtsiocb;
10090         IOCB_t *cmd = NULL;
10091         int errcnt = 0, ret_val = 0;
10092         int i;
10093
10094         for (i = 1; i <= phba->sli.last_iotag; i++) {
10095                 iocbq = phba->sli.iocbq_lookup[i];
10096
10097                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10098                                                abort_cmd) != 0)
10099                         continue;
10100
10101                 /*
10102                  * If the iocbq is already being aborted, don't take a second
10103                  * action, but do count it.
10104                  */
10105                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10106                         continue;
10107
10108                 /* issue ABTS for this IOCB based on iotag */
10109                 abtsiocb = lpfc_sli_get_iocbq(phba);
10110                 if (abtsiocb == NULL) {
10111                         errcnt++;
10112                         continue;
10113                 }
10114
10115                 /* indicate the IO is being aborted by the driver. */
10116                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10117
10118                 cmd = &iocbq->iocb;
10119                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10120                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
10121                 if (phba->sli_rev == LPFC_SLI_REV4)
10122                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10123                 else
10124                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
10125                 abtsiocb->iocb.ulpLe = 1;
10126                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
10127                 abtsiocb->vport = vport;
10128
10129                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10130                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
10131                 if (iocbq->iocb_flag & LPFC_IO_FCP)
10132                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
10133                 if (iocbq->iocb_flag & LPFC_IO_FOF)
10134                         abtsiocb->iocb_flag |= LPFC_IO_FOF;
10135
10136                 if (lpfc_is_link_up(phba))
10137                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10138                 else
10139                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10140
10141                 /* Setup callback routine and issue the command. */
10142                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10143                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10144                                               abtsiocb, 0);
10145                 if (ret_val == IOCB_ERROR) {
10146                         lpfc_sli_release_iocbq(phba, abtsiocb);
10147                         errcnt++;
10148                         continue;
10149                 }
10150         }
10151
10152         return errcnt;
10153 }
10154
10155 /**
10156  * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10157  * @vport: Pointer to virtual port.
10158  * @pring: Pointer to driver SLI ring object.
10159  * @tgt_id: SCSI ID of the target.
10160  * @lun_id: LUN ID of the scsi device.
10161  * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10162  *
10163  * This function sends an abort command for every SCSI command
10164  * associated with the given virtual port pending on the ring
10165  * filtered by lpfc_sli_validate_fcp_iocb function.
10166  * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10167  * FCP iocbs associated with lun specified by tgt_id and lun_id
10168  * parameters
10169  * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10170  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10171  * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10172  * FCP iocbs associated with virtual port.
10173  * This function returns number of iocbs it aborted .
10174  * This function is called with no locks held right after a taskmgmt
10175  * command is sent.
10176  **/
10177 int
10178 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10179                         uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10180 {
10181         struct lpfc_hba *phba = vport->phba;
10182         struct lpfc_scsi_buf *lpfc_cmd;
10183         struct lpfc_iocbq *abtsiocbq;
10184         struct lpfc_nodelist *ndlp;
10185         struct lpfc_iocbq *iocbq;
10186         IOCB_t *icmd;
10187         int sum, i, ret_val;
10188         unsigned long iflags;
10189         struct lpfc_sli_ring *pring_s4;
10190         uint32_t ring_number;
10191
10192         spin_lock_irq(&phba->hbalock);
10193
10194         /* all I/Os are in process of being flushed */
10195         if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10196                 spin_unlock_irq(&phba->hbalock);
10197                 return 0;
10198         }
10199         sum = 0;
10200
10201         for (i = 1; i <= phba->sli.last_iotag; i++) {
10202                 iocbq = phba->sli.iocbq_lookup[i];
10203
10204                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10205                                                cmd) != 0)
10206                         continue;
10207
10208                 /*
10209                  * If the iocbq is already being aborted, don't take a second
10210                  * action, but do count it.
10211                  */
10212                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10213                         continue;
10214
10215                 /* issue ABTS for this IOCB based on iotag */
10216                 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10217                 if (abtsiocbq == NULL)
10218                         continue;
10219
10220                 icmd = &iocbq->iocb;
10221                 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10222                 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10223                 if (phba->sli_rev == LPFC_SLI_REV4)
10224                         abtsiocbq->iocb.un.acxri.abortIoTag =
10225                                                          iocbq->sli4_xritag;
10226                 else
10227                         abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10228                 abtsiocbq->iocb.ulpLe = 1;
10229                 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10230                 abtsiocbq->vport = vport;
10231
10232                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10233                 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10234                 if (iocbq->iocb_flag & LPFC_IO_FCP)
10235                         abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
10236                 if (iocbq->iocb_flag & LPFC_IO_FOF)
10237                         abtsiocbq->iocb_flag |= LPFC_IO_FOF;
10238
10239                 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10240                 ndlp = lpfc_cmd->rdata->pnode;
10241
10242                 if (lpfc_is_link_up(phba) &&
10243                     (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
10244                         abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10245                 else
10246                         abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10247
10248                 /* Setup callback routine and issue the command. */
10249                 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10250
10251                 /*
10252                  * Indicate the IO is being aborted by the driver and set
10253                  * the caller's flag into the aborted IO.
10254                  */
10255                 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10256
10257                 if (phba->sli_rev == LPFC_SLI_REV4) {
10258                         ring_number = MAX_SLI3_CONFIGURED_RINGS +
10259                                          iocbq->fcp_wqidx;
10260                         pring_s4 = &phba->sli.ring[ring_number];
10261                         /* Note: both hbalock and ring_lock must be set here */
10262                         spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10263                         ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10264                                                         abtsiocbq, 0);
10265                         spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10266                 } else {
10267                         ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10268                                                         abtsiocbq, 0);
10269                 }
10270
10271
10272                 if (ret_val == IOCB_ERROR)
10273                         __lpfc_sli_release_iocbq(phba, abtsiocbq);
10274                 else
10275                         sum++;
10276         }
10277         spin_unlock_irq(&phba->hbalock);
10278         return sum;
10279 }
10280
10281 /**
10282  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
10283  * @phba: Pointer to HBA context object.
10284  * @cmdiocbq: Pointer to command iocb.
10285  * @rspiocbq: Pointer to response iocb.
10286  *
10287  * This function is the completion handler for iocbs issued using
10288  * lpfc_sli_issue_iocb_wait function. This function is called by the
10289  * ring event handler function without any lock held. This function
10290  * can be called from both worker thread context and interrupt
10291  * context. This function also can be called from other thread which
10292  * cleans up the SLI layer objects.
10293  * This function copy the contents of the response iocb to the
10294  * response iocb memory object provided by the caller of
10295  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10296  * sleeps for the iocb completion.
10297  **/
10298 static void
10299 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10300                         struct lpfc_iocbq *cmdiocbq,
10301                         struct lpfc_iocbq *rspiocbq)
10302 {
10303         wait_queue_head_t *pdone_q;
10304         unsigned long iflags;
10305         struct lpfc_scsi_buf *lpfc_cmd;
10306
10307         spin_lock_irqsave(&phba->hbalock, iflags);
10308         if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10309
10310                 /*
10311                  * A time out has occurred for the iocb.  If a time out
10312                  * completion handler has been supplied, call it.  Otherwise,
10313                  * just free the iocbq.
10314                  */
10315
10316                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10317                 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10318                 cmdiocbq->wait_iocb_cmpl = NULL;
10319                 if (cmdiocbq->iocb_cmpl)
10320                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10321                 else
10322                         lpfc_sli_release_iocbq(phba, cmdiocbq);
10323                 return;
10324         }
10325
10326         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10327         if (cmdiocbq->context2 && rspiocbq)
10328                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10329                        &rspiocbq->iocb, sizeof(IOCB_t));
10330
10331         /* Set the exchange busy flag for task management commands */
10332         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10333                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10334                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10335                         cur_iocbq);
10336                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10337         }
10338
10339         pdone_q = cmdiocbq->context_un.wait_queue;
10340         if (pdone_q)
10341                 wake_up(pdone_q);
10342         spin_unlock_irqrestore(&phba->hbalock, iflags);
10343         return;
10344 }
10345
10346 /**
10347  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10348  * @phba: Pointer to HBA context object..
10349  * @piocbq: Pointer to command iocb.
10350  * @flag: Flag to test.
10351  *
10352  * This routine grabs the hbalock and then test the iocb_flag to
10353  * see if the passed in flag is set.
10354  * Returns:
10355  * 1 if flag is set.
10356  * 0 if flag is not set.
10357  **/
10358 static int
10359 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10360                  struct lpfc_iocbq *piocbq, uint32_t flag)
10361 {
10362         unsigned long iflags;
10363         int ret;
10364
10365         spin_lock_irqsave(&phba->hbalock, iflags);
10366         ret = piocbq->iocb_flag & flag;
10367         spin_unlock_irqrestore(&phba->hbalock, iflags);
10368         return ret;
10369
10370 }
10371
10372 /**
10373  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
10374  * @phba: Pointer to HBA context object..
10375  * @pring: Pointer to sli ring.
10376  * @piocb: Pointer to command iocb.
10377  * @prspiocbq: Pointer to response iocb.
10378  * @timeout: Timeout in number of seconds.
10379  *
10380  * This function issues the iocb to firmware and waits for the
10381  * iocb to complete. The iocb_cmpl field of the shall be used
10382  * to handle iocbs which time out. If the field is NULL, the
10383  * function shall free the iocbq structure.  If more clean up is
10384  * needed, the caller is expected to provide a completion function
10385  * that will provide the needed clean up.  If the iocb command is
10386  * not completed within timeout seconds, the function will either
10387  * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10388  * completion function set in the iocb_cmpl field and then return
10389  * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
10390  * resources if this function returns IOCB_TIMEDOUT.
10391  * The function waits for the iocb completion using an
10392  * non-interruptible wait.
10393  * This function will sleep while waiting for iocb completion.
10394  * So, this function should not be called from any context which
10395  * does not allow sleeping. Due to the same reason, this function
10396  * cannot be called with interrupt disabled.
10397  * This function assumes that the iocb completions occur while
10398  * this function sleep. So, this function cannot be called from
10399  * the thread which process iocb completion for this ring.
10400  * This function clears the iocb_flag of the iocb object before
10401  * issuing the iocb and the iocb completion handler sets this
10402  * flag and wakes this thread when the iocb completes.
10403  * The contents of the response iocb will be copied to prspiocbq
10404  * by the completion handler when the command completes.
10405  * This function returns IOCB_SUCCESS when success.
10406  * This function is called with no lock held.
10407  **/
10408 int
10409 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
10410                          uint32_t ring_number,
10411                          struct lpfc_iocbq *piocb,
10412                          struct lpfc_iocbq *prspiocbq,
10413                          uint32_t timeout)
10414 {
10415         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10416         long timeleft, timeout_req = 0;
10417         int retval = IOCB_SUCCESS;
10418         uint32_t creg_val;
10419         struct lpfc_iocbq *iocb;
10420         int txq_cnt = 0;
10421         int txcmplq_cnt = 0;
10422         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10423         unsigned long iflags;
10424         bool iocb_completed = true;
10425
10426         /*
10427          * If the caller has provided a response iocbq buffer, then context2
10428          * is NULL or its an error.
10429          */
10430         if (prspiocbq) {
10431                 if (piocb->context2)
10432                         return IOCB_ERROR;
10433                 piocb->context2 = prspiocbq;
10434         }
10435
10436         piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
10437         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10438         piocb->context_un.wait_queue = &done_q;
10439         piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
10440
10441         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10442                 if (lpfc_readl(phba->HCregaddr, &creg_val))
10443                         return IOCB_ERROR;
10444                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10445                 writel(creg_val, phba->HCregaddr);
10446                 readl(phba->HCregaddr); /* flush */
10447         }
10448
10449         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10450                                      SLI_IOCB_RET_IOCB);
10451         if (retval == IOCB_SUCCESS) {
10452                 timeout_req = msecs_to_jiffies(timeout * 1000);
10453                 timeleft = wait_event_timeout(done_q,
10454                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
10455                                 timeout_req);
10456                 spin_lock_irqsave(&phba->hbalock, iflags);
10457                 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
10458
10459                         /*
10460                          * IOCB timed out.  Inform the wake iocb wait
10461                          * completion function and set local status
10462                          */
10463
10464                         iocb_completed = false;
10465                         piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10466                 }
10467                 spin_unlock_irqrestore(&phba->hbalock, iflags);
10468                 if (iocb_completed) {
10469                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10470                                         "0331 IOCB wake signaled\n");
10471                         /* Note: we are not indicating if the IOCB has a success
10472                          * status or not - that's for the caller to check.
10473                          * IOCB_SUCCESS means just that the command was sent and
10474                          * completed. Not that it completed successfully.
10475                          * */
10476                 } else if (timeleft == 0) {
10477                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10478                                         "0338 IOCB wait timeout error - no "
10479                                         "wake response Data x%x\n", timeout);
10480                         retval = IOCB_TIMEDOUT;
10481                 } else {
10482                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10483                                         "0330 IOCB wake NOT set, "
10484                                         "Data x%x x%lx\n",
10485                                         timeout, (timeleft / jiffies));
10486                         retval = IOCB_TIMEDOUT;
10487                 }
10488         } else if (retval == IOCB_BUSY) {
10489                 if (phba->cfg_log_verbose & LOG_SLI) {
10490                         list_for_each_entry(iocb, &pring->txq, list) {
10491                                 txq_cnt++;
10492                         }
10493                         list_for_each_entry(iocb, &pring->txcmplq, list) {
10494                                 txcmplq_cnt++;
10495                         }
10496                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10497                                 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10498                                 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10499                 }
10500                 return retval;
10501         } else {
10502                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10503                                 "0332 IOCB wait issue failed, Data x%x\n",
10504                                 retval);
10505                 retval = IOCB_ERROR;
10506         }
10507
10508         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10509                 if (lpfc_readl(phba->HCregaddr, &creg_val))
10510                         return IOCB_ERROR;
10511                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10512                 writel(creg_val, phba->HCregaddr);
10513                 readl(phba->HCregaddr); /* flush */
10514         }
10515
10516         if (prspiocbq)
10517                 piocb->context2 = NULL;
10518
10519         piocb->context_un.wait_queue = NULL;
10520         piocb->iocb_cmpl = NULL;
10521         return retval;
10522 }
10523
10524 /**
10525  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
10526  * @phba: Pointer to HBA context object.
10527  * @pmboxq: Pointer to driver mailbox object.
10528  * @timeout: Timeout in number of seconds.
10529  *
10530  * This function issues the mailbox to firmware and waits for the
10531  * mailbox command to complete. If the mailbox command is not
10532  * completed within timeout seconds, it returns MBX_TIMEOUT.
10533  * The function waits for the mailbox completion using an
10534  * interruptible wait. If the thread is woken up due to a
10535  * signal, MBX_TIMEOUT error is returned to the caller. Caller
10536  * should not free the mailbox resources, if this function returns
10537  * MBX_TIMEOUT.
10538  * This function will sleep while waiting for mailbox completion.
10539  * So, this function should not be called from any context which
10540  * does not allow sleeping. Due to the same reason, this function
10541  * cannot be called with interrupt disabled.
10542  * This function assumes that the mailbox completion occurs while
10543  * this function sleep. So, this function cannot be called from
10544  * the worker thread which processes mailbox completion.
10545  * This function is called in the context of HBA management
10546  * applications.
10547  * This function returns MBX_SUCCESS when successful.
10548  * This function is called with no lock held.
10549  **/
10550 int
10551 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
10552                          uint32_t timeout)
10553 {
10554         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10555         MAILBOX_t *mb = NULL;
10556         int retval;
10557         unsigned long flag;
10558
10559         /* The caller might set context1 for extended buffer */
10560         if (pmboxq->context1)
10561                 mb = (MAILBOX_t *)pmboxq->context1;
10562
10563         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
10564         /* setup wake call as IOCB callback */
10565         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10566         /* setup context field to pass wait_queue pointer to wake function  */
10567         pmboxq->context1 = &done_q;
10568
10569         /* now issue the command */
10570         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
10571         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
10572                 wait_event_interruptible_timeout(done_q,
10573                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10574                                 msecs_to_jiffies(timeout * 1000));
10575
10576                 spin_lock_irqsave(&phba->hbalock, flag);
10577                 /* restore the possible extended buffer for free resource */
10578                 pmboxq->context1 = (uint8_t *)mb;
10579                 /*
10580                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
10581                  * else do not free the resources.
10582                  */
10583                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
10584                         retval = MBX_SUCCESS;
10585                 } else {
10586                         retval = MBX_TIMEOUT;
10587                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10588                 }
10589                 spin_unlock_irqrestore(&phba->hbalock, flag);
10590         } else {
10591                 /* restore the possible extended buffer for free resource */
10592                 pmboxq->context1 = (uint8_t *)mb;
10593         }
10594
10595         return retval;
10596 }
10597
10598 /**
10599  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
10600  * @phba: Pointer to HBA context.
10601  *
10602  * This function is called to shutdown the driver's mailbox sub-system.
10603  * It first marks the mailbox sub-system is in a block state to prevent
10604  * the asynchronous mailbox command from issued off the pending mailbox
10605  * command queue. If the mailbox command sub-system shutdown is due to
10606  * HBA error conditions such as EEH or ERATT, this routine shall invoke
10607  * the mailbox sub-system flush routine to forcefully bring down the
10608  * mailbox sub-system. Otherwise, if it is due to normal condition (such
10609  * as with offline or HBA function reset), this routine will wait for the
10610  * outstanding mailbox command to complete before invoking the mailbox
10611  * sub-system flush routine to gracefully bring down mailbox sub-system.
10612  **/
10613 void
10614 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
10615 {
10616         struct lpfc_sli *psli = &phba->sli;
10617         unsigned long timeout;
10618
10619         if (mbx_action == LPFC_MBX_NO_WAIT) {
10620                 /* delay 100ms for port state */
10621                 msleep(100);
10622                 lpfc_sli_mbox_sys_flush(phba);
10623                 return;
10624         }
10625         timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
10626
10627         spin_lock_irq(&phba->hbalock);
10628         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10629
10630         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
10631                 /* Determine how long we might wait for the active mailbox
10632                  * command to be gracefully completed by firmware.
10633                  */
10634                 if (phba->sli.mbox_active)
10635                         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10636                                                 phba->sli.mbox_active) *
10637                                                 1000) + jiffies;
10638                 spin_unlock_irq(&phba->hbalock);
10639
10640                 while (phba->sli.mbox_active) {
10641                         /* Check active mailbox complete status every 2ms */
10642                         msleep(2);
10643                         if (time_after(jiffies, timeout))
10644                                 /* Timeout, let the mailbox flush routine to
10645                                  * forcefully release active mailbox command
10646                                  */
10647                                 break;
10648                 }
10649         } else
10650                 spin_unlock_irq(&phba->hbalock);
10651
10652         lpfc_sli_mbox_sys_flush(phba);
10653 }
10654
10655 /**
10656  * lpfc_sli_eratt_read - read sli-3 error attention events
10657  * @phba: Pointer to HBA context.
10658  *
10659  * This function is called to read the SLI3 device error attention registers
10660  * for possible error attention events. The caller must hold the hostlock
10661  * with spin_lock_irq().
10662  *
10663  * This function returns 1 when there is Error Attention in the Host Attention
10664  * Register and returns 0 otherwise.
10665  **/
10666 static int
10667 lpfc_sli_eratt_read(struct lpfc_hba *phba)
10668 {
10669         uint32_t ha_copy;
10670
10671         /* Read chip Host Attention (HA) register */
10672         if (lpfc_readl(phba->HAregaddr, &ha_copy))
10673                 goto unplug_err;
10674
10675         if (ha_copy & HA_ERATT) {
10676                 /* Read host status register to retrieve error event */
10677                 if (lpfc_sli_read_hs(phba))
10678                         goto unplug_err;
10679
10680                 /* Check if there is a deferred error condition is active */
10681                 if ((HS_FFER1 & phba->work_hs) &&
10682                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10683                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
10684                         phba->hba_flag |= DEFER_ERATT;
10685                         /* Clear all interrupt enable conditions */
10686                         writel(0, phba->HCregaddr);
10687                         readl(phba->HCregaddr);
10688                 }
10689
10690                 /* Set the driver HA work bitmap */
10691                 phba->work_ha |= HA_ERATT;
10692                 /* Indicate polling handles this ERATT */
10693                 phba->hba_flag |= HBA_ERATT_HANDLED;
10694                 return 1;
10695         }
10696         return 0;
10697
10698 unplug_err:
10699         /* Set the driver HS work bitmap */
10700         phba->work_hs |= UNPLUG_ERR;
10701         /* Set the driver HA work bitmap */
10702         phba->work_ha |= HA_ERATT;
10703         /* Indicate polling handles this ERATT */
10704         phba->hba_flag |= HBA_ERATT_HANDLED;
10705         return 1;
10706 }
10707
10708 /**
10709  * lpfc_sli4_eratt_read - read sli-4 error attention events
10710  * @phba: Pointer to HBA context.
10711  *
10712  * This function is called to read the SLI4 device error attention registers
10713  * for possible error attention events. The caller must hold the hostlock
10714  * with spin_lock_irq().
10715  *
10716  * This function returns 1 when there is Error Attention in the Host Attention
10717  * Register and returns 0 otherwise.
10718  **/
10719 static int
10720 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10721 {
10722         uint32_t uerr_sta_hi, uerr_sta_lo;
10723         uint32_t if_type, portsmphr;
10724         struct lpfc_register portstat_reg;
10725
10726         /*
10727          * For now, use the SLI4 device internal unrecoverable error
10728          * registers for error attention. This can be changed later.
10729          */
10730         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10731         switch (if_type) {
10732         case LPFC_SLI_INTF_IF_TYPE_0:
10733                 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10734                         &uerr_sta_lo) ||
10735                         lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10736                         &uerr_sta_hi)) {
10737                         phba->work_hs |= UNPLUG_ERR;
10738                         phba->work_ha |= HA_ERATT;
10739                         phba->hba_flag |= HBA_ERATT_HANDLED;
10740                         return 1;
10741                 }
10742                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10743                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10744                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10745                                         "1423 HBA Unrecoverable error: "
10746                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10747                                         "ue_mask_lo_reg=0x%x, "
10748                                         "ue_mask_hi_reg=0x%x\n",
10749                                         uerr_sta_lo, uerr_sta_hi,
10750                                         phba->sli4_hba.ue_mask_lo,
10751                                         phba->sli4_hba.ue_mask_hi);
10752                         phba->work_status[0] = uerr_sta_lo;
10753                         phba->work_status[1] = uerr_sta_hi;
10754                         phba->work_ha |= HA_ERATT;
10755                         phba->hba_flag |= HBA_ERATT_HANDLED;
10756                         return 1;
10757                 }
10758                 break;
10759         case LPFC_SLI_INTF_IF_TYPE_2:
10760                 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10761                         &portstat_reg.word0) ||
10762                         lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10763                         &portsmphr)){
10764                         phba->work_hs |= UNPLUG_ERR;
10765                         phba->work_ha |= HA_ERATT;
10766                         phba->hba_flag |= HBA_ERATT_HANDLED;
10767                         return 1;
10768                 }
10769                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10770                         phba->work_status[0] =
10771                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10772                         phba->work_status[1] =
10773                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10774                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10775                                         "2885 Port Status Event: "
10776                                         "port status reg 0x%x, "
10777                                         "port smphr reg 0x%x, "
10778                                         "error 1=0x%x, error 2=0x%x\n",
10779                                         portstat_reg.word0,
10780                                         portsmphr,
10781                                         phba->work_status[0],
10782                                         phba->work_status[1]);
10783                         phba->work_ha |= HA_ERATT;
10784                         phba->hba_flag |= HBA_ERATT_HANDLED;
10785                         return 1;
10786                 }
10787                 break;
10788         case LPFC_SLI_INTF_IF_TYPE_1:
10789         default:
10790                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10791                                 "2886 HBA Error Attention on unsupported "
10792                                 "if type %d.", if_type);
10793                 return 1;
10794         }
10795
10796         return 0;
10797 }
10798
10799 /**
10800  * lpfc_sli_check_eratt - check error attention events
10801  * @phba: Pointer to HBA context.
10802  *
10803  * This function is called from timer soft interrupt context to check HBA's
10804  * error attention register bit for error attention events.
10805  *
10806  * This function returns 1 when there is Error Attention in the Host Attention
10807  * Register and returns 0 otherwise.
10808  **/
10809 int
10810 lpfc_sli_check_eratt(struct lpfc_hba *phba)
10811 {
10812         uint32_t ha_copy;
10813
10814         /* If somebody is waiting to handle an eratt, don't process it
10815          * here. The brdkill function will do this.
10816          */
10817         if (phba->link_flag & LS_IGNORE_ERATT)
10818                 return 0;
10819
10820         /* Check if interrupt handler handles this ERATT */
10821         spin_lock_irq(&phba->hbalock);
10822         if (phba->hba_flag & HBA_ERATT_HANDLED) {
10823                 /* Interrupt handler has handled ERATT */
10824                 spin_unlock_irq(&phba->hbalock);
10825                 return 0;
10826         }
10827
10828         /*
10829          * If there is deferred error attention, do not check for error
10830          * attention
10831          */
10832         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10833                 spin_unlock_irq(&phba->hbalock);
10834                 return 0;
10835         }
10836
10837         /* If PCI channel is offline, don't process it */
10838         if (unlikely(pci_channel_offline(phba->pcidev))) {
10839                 spin_unlock_irq(&phba->hbalock);
10840                 return 0;
10841         }
10842
10843         switch (phba->sli_rev) {
10844         case LPFC_SLI_REV2:
10845         case LPFC_SLI_REV3:
10846                 /* Read chip Host Attention (HA) register */
10847                 ha_copy = lpfc_sli_eratt_read(phba);
10848                 break;
10849         case LPFC_SLI_REV4:
10850                 /* Read device Uncoverable Error (UERR) registers */
10851                 ha_copy = lpfc_sli4_eratt_read(phba);
10852                 break;
10853         default:
10854                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10855                                 "0299 Invalid SLI revision (%d)\n",
10856                                 phba->sli_rev);
10857                 ha_copy = 0;
10858                 break;
10859         }
10860         spin_unlock_irq(&phba->hbalock);
10861
10862         return ha_copy;
10863 }
10864
10865 /**
10866  * lpfc_intr_state_check - Check device state for interrupt handling
10867  * @phba: Pointer to HBA context.
10868  *
10869  * This inline routine checks whether a device or its PCI slot is in a state
10870  * that the interrupt should be handled.
10871  *
10872  * This function returns 0 if the device or the PCI slot is in a state that
10873  * interrupt should be handled, otherwise -EIO.
10874  */
10875 static inline int
10876 lpfc_intr_state_check(struct lpfc_hba *phba)
10877 {
10878         /* If the pci channel is offline, ignore all the interrupts */
10879         if (unlikely(pci_channel_offline(phba->pcidev)))
10880                 return -EIO;
10881
10882         /* Update device level interrupt statistics */
10883         phba->sli.slistat.sli_intr++;
10884
10885         /* Ignore all interrupts during initialization. */
10886         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10887                 return -EIO;
10888
10889         return 0;
10890 }
10891
10892 /**
10893  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
10894  * @irq: Interrupt number.
10895  * @dev_id: The device context pointer.
10896  *
10897  * This function is directly called from the PCI layer as an interrupt
10898  * service routine when device with SLI-3 interface spec is enabled with
10899  * MSI-X multi-message interrupt mode and there are slow-path events in
10900  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10901  * interrupt mode, this function is called as part of the device-level
10902  * interrupt handler. When the PCI slot is in error recovery or the HBA
10903  * is undergoing initialization, the interrupt handler will not process
10904  * the interrupt. The link attention and ELS ring attention events are
10905  * handled by the worker thread. The interrupt handler signals the worker
10906  * thread and returns for these events. This function is called without
10907  * any lock held. It gets the hbalock to access and update SLI data
10908  * structures.
10909  *
10910  * This function returns IRQ_HANDLED when interrupt is handled else it
10911  * returns IRQ_NONE.
10912  **/
10913 irqreturn_t
10914 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
10915 {
10916         struct lpfc_hba  *phba;
10917         uint32_t ha_copy, hc_copy;
10918         uint32_t work_ha_copy;
10919         unsigned long status;
10920         unsigned long iflag;
10921         uint32_t control;
10922
10923         MAILBOX_t *mbox, *pmbox;
10924         struct lpfc_vport *vport;
10925         struct lpfc_nodelist *ndlp;
10926         struct lpfc_dmabuf *mp;
10927         LPFC_MBOXQ_t *pmb;
10928         int rc;
10929
10930         /*
10931          * Get the driver's phba structure from the dev_id and
10932          * assume the HBA is not interrupting.
10933          */
10934         phba = (struct lpfc_hba *)dev_id;
10935
10936         if (unlikely(!phba))
10937                 return IRQ_NONE;
10938
10939         /*
10940          * Stuff needs to be attented to when this function is invoked as an
10941          * individual interrupt handler in MSI-X multi-message interrupt mode
10942          */
10943         if (phba->intr_type == MSIX) {
10944                 /* Check device state for handling interrupt */
10945                 if (lpfc_intr_state_check(phba))
10946                         return IRQ_NONE;
10947                 /* Need to read HA REG for slow-path events */
10948                 spin_lock_irqsave(&phba->hbalock, iflag);
10949                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10950                         goto unplug_error;
10951                 /* If somebody is waiting to handle an eratt don't process it
10952                  * here. The brdkill function will do this.
10953                  */
10954                 if (phba->link_flag & LS_IGNORE_ERATT)
10955                         ha_copy &= ~HA_ERATT;
10956                 /* Check the need for handling ERATT in interrupt handler */
10957                 if (ha_copy & HA_ERATT) {
10958                         if (phba->hba_flag & HBA_ERATT_HANDLED)
10959                                 /* ERATT polling has handled ERATT */
10960                                 ha_copy &= ~HA_ERATT;
10961                         else
10962                                 /* Indicate interrupt handler handles ERATT */
10963                                 phba->hba_flag |= HBA_ERATT_HANDLED;
10964                 }
10965
10966                 /*
10967                  * If there is deferred error attention, do not check for any
10968                  * interrupt.
10969                  */
10970                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10971                         spin_unlock_irqrestore(&phba->hbalock, iflag);
10972                         return IRQ_NONE;
10973                 }
10974
10975                 /* Clear up only attention source related to slow-path */
10976                 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10977                         goto unplug_error;
10978
10979                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10980                         HC_LAINT_ENA | HC_ERINT_ENA),
10981                         phba->HCregaddr);
10982                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10983                         phba->HAregaddr);
10984                 writel(hc_copy, phba->HCregaddr);
10985                 readl(phba->HAregaddr); /* flush */
10986                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10987         } else
10988                 ha_copy = phba->ha_copy;
10989
10990         work_ha_copy = ha_copy & phba->work_ha_mask;
10991
10992         if (work_ha_copy) {
10993                 if (work_ha_copy & HA_LATT) {
10994                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10995                                 /*
10996                                  * Turn off Link Attention interrupts
10997                                  * until CLEAR_LA done
10998                                  */
10999                                 spin_lock_irqsave(&phba->hbalock, iflag);
11000                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
11001                                 if (lpfc_readl(phba->HCregaddr, &control))
11002                                         goto unplug_error;
11003                                 control &= ~HC_LAINT_ENA;
11004                                 writel(control, phba->HCregaddr);
11005                                 readl(phba->HCregaddr); /* flush */
11006                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11007                         }
11008                         else
11009                                 work_ha_copy &= ~HA_LATT;
11010                 }
11011
11012                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
11013                         /*
11014                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11015                          * the only slow ring.
11016                          */
11017                         status = (work_ha_copy &
11018                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
11019                         status >>= (4*LPFC_ELS_RING);
11020                         if (status & HA_RXMASK) {
11021                                 spin_lock_irqsave(&phba->hbalock, iflag);
11022                                 if (lpfc_readl(phba->HCregaddr, &control))
11023                                         goto unplug_error;
11024
11025                                 lpfc_debugfs_slow_ring_trc(phba,
11026                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
11027                                 control, status,
11028                                 (uint32_t)phba->sli.slistat.sli_intr);
11029
11030                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
11031                                         lpfc_debugfs_slow_ring_trc(phba,
11032                                                 "ISR Disable ring:"
11033                                                 "pwork:x%x hawork:x%x wait:x%x",
11034                                                 phba->work_ha, work_ha_copy,
11035                                                 (uint32_t)((unsigned long)
11036                                                 &phba->work_waitq));
11037
11038                                         control &=
11039                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
11040                                         writel(control, phba->HCregaddr);
11041                                         readl(phba->HCregaddr); /* flush */
11042                                 }
11043                                 else {
11044                                         lpfc_debugfs_slow_ring_trc(phba,
11045                                                 "ISR slow ring:   pwork:"
11046                                                 "x%x hawork:x%x wait:x%x",
11047                                                 phba->work_ha, work_ha_copy,
11048                                                 (uint32_t)((unsigned long)
11049                                                 &phba->work_waitq));
11050                                 }
11051                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11052                         }
11053                 }
11054                 spin_lock_irqsave(&phba->hbalock, iflag);
11055                 if (work_ha_copy & HA_ERATT) {
11056                         if (lpfc_sli_read_hs(phba))
11057                                 goto unplug_error;
11058                         /*
11059                          * Check if there is a deferred error condition
11060                          * is active
11061                          */
11062                         if ((HS_FFER1 & phba->work_hs) &&
11063                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11064                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
11065                                   phba->work_hs)) {
11066                                 phba->hba_flag |= DEFER_ERATT;
11067                                 /* Clear all interrupt enable conditions */
11068                                 writel(0, phba->HCregaddr);
11069                                 readl(phba->HCregaddr);
11070                         }
11071                 }
11072
11073                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
11074                         pmb = phba->sli.mbox_active;
11075                         pmbox = &pmb->u.mb;
11076                         mbox = phba->mbox;
11077                         vport = pmb->vport;
11078
11079                         /* First check out the status word */
11080                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11081                         if (pmbox->mbxOwner != OWN_HOST) {
11082                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11083                                 /*
11084                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
11085                                  * mbxStatus <status>
11086                                  */
11087                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11088                                                 LOG_SLI,
11089                                                 "(%d):0304 Stray Mailbox "
11090                                                 "Interrupt mbxCommand x%x "
11091                                                 "mbxStatus x%x\n",
11092                                                 (vport ? vport->vpi : 0),
11093                                                 pmbox->mbxCommand,
11094                                                 pmbox->mbxStatus);
11095                                 /* clear mailbox attention bit */
11096                                 work_ha_copy &= ~HA_MBATT;
11097                         } else {
11098                                 phba->sli.mbox_active = NULL;
11099                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11100                                 phba->last_completion_time = jiffies;
11101                                 del_timer(&phba->sli.mbox_tmo);
11102                                 if (pmb->mbox_cmpl) {
11103                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
11104                                                         MAILBOX_CMD_SIZE);
11105                                         if (pmb->out_ext_byte_len &&
11106                                                 pmb->context2)
11107                                                 lpfc_sli_pcimem_bcopy(
11108                                                 phba->mbox_ext,
11109                                                 pmb->context2,
11110                                                 pmb->out_ext_byte_len);
11111                                 }
11112                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11113                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11114
11115                                         lpfc_debugfs_disc_trc(vport,
11116                                                 LPFC_DISC_TRC_MBOX_VPORT,
11117                                                 "MBOX dflt rpi: : "
11118                                                 "status:x%x rpi:x%x",
11119                                                 (uint32_t)pmbox->mbxStatus,
11120                                                 pmbox->un.varWords[0], 0);
11121
11122                                         if (!pmbox->mbxStatus) {
11123                                                 mp = (struct lpfc_dmabuf *)
11124                                                         (pmb->context1);
11125                                                 ndlp = (struct lpfc_nodelist *)
11126                                                         pmb->context2;
11127
11128                                                 /* Reg_LOGIN of dflt RPI was
11129                                                  * successful. new lets get
11130                                                  * rid of the RPI using the
11131                                                  * same mbox buffer.
11132                                                  */
11133                                                 lpfc_unreg_login(phba,
11134                                                         vport->vpi,
11135                                                         pmbox->un.varWords[0],
11136                                                         pmb);
11137                                                 pmb->mbox_cmpl =
11138                                                         lpfc_mbx_cmpl_dflt_rpi;
11139                                                 pmb->context1 = mp;
11140                                                 pmb->context2 = ndlp;
11141                                                 pmb->vport = vport;
11142                                                 rc = lpfc_sli_issue_mbox(phba,
11143                                                                 pmb,
11144                                                                 MBX_NOWAIT);
11145                                                 if (rc != MBX_BUSY)
11146                                                         lpfc_printf_log(phba,
11147                                                         KERN_ERR,
11148                                                         LOG_MBOX | LOG_SLI,
11149                                                         "0350 rc should have"
11150                                                         "been MBX_BUSY\n");
11151                                                 if (rc != MBX_NOT_FINISHED)
11152                                                         goto send_current_mbox;
11153                                         }
11154                                 }
11155                                 spin_lock_irqsave(
11156                                                 &phba->pport->work_port_lock,
11157                                                 iflag);
11158                                 phba->pport->work_port_events &=
11159                                         ~WORKER_MBOX_TMO;
11160                                 spin_unlock_irqrestore(
11161                                                 &phba->pport->work_port_lock,
11162                                                 iflag);
11163                                 lpfc_mbox_cmpl_put(phba, pmb);
11164                         }
11165                 } else
11166                         spin_unlock_irqrestore(&phba->hbalock, iflag);
11167
11168                 if ((work_ha_copy & HA_MBATT) &&
11169                     (phba->sli.mbox_active == NULL)) {
11170 send_current_mbox:
11171                         /* Process next mailbox command if there is one */
11172                         do {
11173                                 rc = lpfc_sli_issue_mbox(phba, NULL,
11174                                                          MBX_NOWAIT);
11175                         } while (rc == MBX_NOT_FINISHED);
11176                         if (rc != MBX_SUCCESS)
11177                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11178                                                 LOG_SLI, "0349 rc should be "
11179                                                 "MBX_SUCCESS\n");
11180                 }
11181
11182                 spin_lock_irqsave(&phba->hbalock, iflag);
11183                 phba->work_ha |= work_ha_copy;
11184                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11185                 lpfc_worker_wake_up(phba);
11186         }
11187         return IRQ_HANDLED;
11188 unplug_error:
11189         spin_unlock_irqrestore(&phba->hbalock, iflag);
11190         return IRQ_HANDLED;
11191
11192 } /* lpfc_sli_sp_intr_handler */
11193
11194 /**
11195  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
11196  * @irq: Interrupt number.
11197  * @dev_id: The device context pointer.
11198  *
11199  * This function is directly called from the PCI layer as an interrupt
11200  * service routine when device with SLI-3 interface spec is enabled with
11201  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11202  * ring event in the HBA. However, when the device is enabled with either
11203  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11204  * device-level interrupt handler. When the PCI slot is in error recovery
11205  * or the HBA is undergoing initialization, the interrupt handler will not
11206  * process the interrupt. The SCSI FCP fast-path ring event are handled in
11207  * the intrrupt context. This function is called without any lock held.
11208  * It gets the hbalock to access and update SLI data structures.
11209  *
11210  * This function returns IRQ_HANDLED when interrupt is handled else it
11211  * returns IRQ_NONE.
11212  **/
11213 irqreturn_t
11214 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
11215 {
11216         struct lpfc_hba  *phba;
11217         uint32_t ha_copy;
11218         unsigned long status;
11219         unsigned long iflag;
11220
11221         /* Get the driver's phba structure from the dev_id and
11222          * assume the HBA is not interrupting.
11223          */
11224         phba = (struct lpfc_hba *) dev_id;
11225
11226         if (unlikely(!phba))
11227                 return IRQ_NONE;
11228
11229         /*
11230          * Stuff needs to be attented to when this function is invoked as an
11231          * individual interrupt handler in MSI-X multi-message interrupt mode
11232          */
11233         if (phba->intr_type == MSIX) {
11234                 /* Check device state for handling interrupt */
11235                 if (lpfc_intr_state_check(phba))
11236                         return IRQ_NONE;
11237                 /* Need to read HA REG for FCP ring and other ring events */
11238                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11239                         return IRQ_HANDLED;
11240                 /* Clear up only attention source related to fast-path */
11241                 spin_lock_irqsave(&phba->hbalock, iflag);
11242                 /*
11243                  * If there is deferred error attention, do not check for
11244                  * any interrupt.
11245                  */
11246                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11247                         spin_unlock_irqrestore(&phba->hbalock, iflag);
11248                         return IRQ_NONE;
11249                 }
11250                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11251                         phba->HAregaddr);
11252                 readl(phba->HAregaddr); /* flush */
11253                 spin_unlock_irqrestore(&phba->hbalock, iflag);
11254         } else
11255                 ha_copy = phba->ha_copy;
11256
11257         /*
11258          * Process all events on FCP ring. Take the optimized path for FCP IO.
11259          */
11260         ha_copy &= ~(phba->work_ha_mask);
11261
11262         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11263         status >>= (4*LPFC_FCP_RING);
11264         if (status & HA_RXMASK)
11265                 lpfc_sli_handle_fast_ring_event(phba,
11266                                                 &phba->sli.ring[LPFC_FCP_RING],
11267                                                 status);
11268
11269         if (phba->cfg_multi_ring_support == 2) {
11270                 /*
11271                  * Process all events on extra ring. Take the optimized path
11272                  * for extra ring IO.
11273                  */
11274                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11275                 status >>= (4*LPFC_EXTRA_RING);
11276                 if (status & HA_RXMASK) {
11277                         lpfc_sli_handle_fast_ring_event(phba,
11278                                         &phba->sli.ring[LPFC_EXTRA_RING],
11279                                         status);
11280                 }
11281         }
11282         return IRQ_HANDLED;
11283 }  /* lpfc_sli_fp_intr_handler */
11284
11285 /**
11286  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
11287  * @irq: Interrupt number.
11288  * @dev_id: The device context pointer.
11289  *
11290  * This function is the HBA device-level interrupt handler to device with
11291  * SLI-3 interface spec, called from the PCI layer when either MSI or
11292  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11293  * requires driver attention. This function invokes the slow-path interrupt
11294  * attention handling function and fast-path interrupt attention handling
11295  * function in turn to process the relevant HBA attention events. This
11296  * function is called without any lock held. It gets the hbalock to access
11297  * and update SLI data structures.
11298  *
11299  * This function returns IRQ_HANDLED when interrupt is handled, else it
11300  * returns IRQ_NONE.
11301  **/
11302 irqreturn_t
11303 lpfc_sli_intr_handler(int irq, void *dev_id)
11304 {
11305         struct lpfc_hba  *phba;
11306         irqreturn_t sp_irq_rc, fp_irq_rc;
11307         unsigned long status1, status2;
11308         uint32_t hc_copy;
11309
11310         /*
11311          * Get the driver's phba structure from the dev_id and
11312          * assume the HBA is not interrupting.
11313          */
11314         phba = (struct lpfc_hba *) dev_id;
11315
11316         if (unlikely(!phba))
11317                 return IRQ_NONE;
11318
11319         /* Check device state for handling interrupt */
11320         if (lpfc_intr_state_check(phba))
11321                 return IRQ_NONE;
11322
11323         spin_lock(&phba->hbalock);
11324         if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11325                 spin_unlock(&phba->hbalock);
11326                 return IRQ_HANDLED;
11327         }
11328
11329         if (unlikely(!phba->ha_copy)) {
11330                 spin_unlock(&phba->hbalock);
11331                 return IRQ_NONE;
11332         } else if (phba->ha_copy & HA_ERATT) {
11333                 if (phba->hba_flag & HBA_ERATT_HANDLED)
11334                         /* ERATT polling has handled ERATT */
11335                         phba->ha_copy &= ~HA_ERATT;
11336                 else
11337                         /* Indicate interrupt handler handles ERATT */
11338                         phba->hba_flag |= HBA_ERATT_HANDLED;
11339         }
11340
11341         /*
11342          * If there is deferred error attention, do not check for any interrupt.
11343          */
11344         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11345                 spin_unlock(&phba->hbalock);
11346                 return IRQ_NONE;
11347         }
11348
11349         /* Clear attention sources except link and error attentions */
11350         if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11351                 spin_unlock(&phba->hbalock);
11352                 return IRQ_HANDLED;
11353         }
11354         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11355                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11356                 phba->HCregaddr);
11357         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
11358         writel(hc_copy, phba->HCregaddr);
11359         readl(phba->HAregaddr); /* flush */
11360         spin_unlock(&phba->hbalock);
11361
11362         /*
11363          * Invokes slow-path host attention interrupt handling as appropriate.
11364          */
11365
11366         /* status of events with mailbox and link attention */
11367         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11368
11369         /* status of events with ELS ring */
11370         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
11371         status2 >>= (4*LPFC_ELS_RING);
11372
11373         if (status1 || (status2 & HA_RXMASK))
11374                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
11375         else
11376                 sp_irq_rc = IRQ_NONE;
11377
11378         /*
11379          * Invoke fast-path host attention interrupt handling as appropriate.
11380          */
11381
11382         /* status of events with FCP ring */
11383         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11384         status1 >>= (4*LPFC_FCP_RING);
11385
11386         /* status of events with extra ring */
11387         if (phba->cfg_multi_ring_support == 2) {
11388                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11389                 status2 >>= (4*LPFC_EXTRA_RING);
11390         } else
11391                 status2 = 0;
11392
11393         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
11394                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
11395         else
11396                 fp_irq_rc = IRQ_NONE;
11397
11398         /* Return device-level interrupt handling status */
11399         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
11400 }  /* lpfc_sli_intr_handler */
11401
11402 /**
11403  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11404  * @phba: pointer to lpfc hba data structure.
11405  *
11406  * This routine is invoked by the worker thread to process all the pending
11407  * SLI4 FCP abort XRI events.
11408  **/
11409 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11410 {
11411         struct lpfc_cq_event *cq_event;
11412
11413         /* First, declare the fcp xri abort event has been handled */
11414         spin_lock_irq(&phba->hbalock);
11415         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11416         spin_unlock_irq(&phba->hbalock);
11417         /* Now, handle all the fcp xri abort events */
11418         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11419                 /* Get the first event from the head of the event queue */
11420                 spin_lock_irq(&phba->hbalock);
11421                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11422                                  cq_event, struct lpfc_cq_event, list);
11423                 spin_unlock_irq(&phba->hbalock);
11424                 /* Notify aborted XRI for FCP work queue */
11425                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11426                 /* Free the event processed back to the free pool */
11427                 lpfc_sli4_cq_event_release(phba, cq_event);
11428         }
11429 }
11430
11431 /**
11432  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11433  * @phba: pointer to lpfc hba data structure.
11434  *
11435  * This routine is invoked by the worker thread to process all the pending
11436  * SLI4 els abort xri events.
11437  **/
11438 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11439 {
11440         struct lpfc_cq_event *cq_event;
11441
11442         /* First, declare the els xri abort event has been handled */
11443         spin_lock_irq(&phba->hbalock);
11444         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11445         spin_unlock_irq(&phba->hbalock);
11446         /* Now, handle all the els xri abort events */
11447         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11448                 /* Get the first event from the head of the event queue */
11449                 spin_lock_irq(&phba->hbalock);
11450                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11451                                  cq_event, struct lpfc_cq_event, list);
11452                 spin_unlock_irq(&phba->hbalock);
11453                 /* Notify aborted XRI for ELS work queue */
11454                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11455                 /* Free the event processed back to the free pool */
11456                 lpfc_sli4_cq_event_release(phba, cq_event);
11457         }
11458 }
11459
11460 /**
11461  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11462  * @phba: pointer to lpfc hba data structure
11463  * @pIocbIn: pointer to the rspiocbq
11464  * @pIocbOut: pointer to the cmdiocbq
11465  * @wcqe: pointer to the complete wcqe
11466  *
11467  * This routine transfers the fields of a command iocbq to a response iocbq
11468  * by copying all the IOCB fields from command iocbq and transferring the
11469  * completion status information from the complete wcqe.
11470  **/
11471 static void
11472 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11473                               struct lpfc_iocbq *pIocbIn,
11474                               struct lpfc_iocbq *pIocbOut,
11475                               struct lpfc_wcqe_complete *wcqe)
11476 {
11477         int numBdes, i;
11478         unsigned long iflags;
11479         uint32_t status, max_response;
11480         struct lpfc_dmabuf *dmabuf;
11481         struct ulp_bde64 *bpl, bde;
11482         size_t offset = offsetof(struct lpfc_iocbq, iocb);
11483
11484         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11485                sizeof(struct lpfc_iocbq) - offset);
11486         /* Map WCQE parameters into irspiocb parameters */
11487         status = bf_get(lpfc_wcqe_c_status, wcqe);
11488         pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
11489         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11490                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11491                         pIocbIn->iocb.un.fcpi.fcpi_parm =
11492                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
11493                                         wcqe->total_data_placed;
11494                 else
11495                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11496         else {
11497                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11498                 switch (pIocbOut->iocb.ulpCommand) {
11499                 case CMD_ELS_REQUEST64_CR:
11500                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11501                         bpl  = (struct ulp_bde64 *)dmabuf->virt;
11502                         bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11503                         max_response = bde.tus.f.bdeSize;
11504                         break;
11505                 case CMD_GEN_REQUEST64_CR:
11506                         max_response = 0;
11507                         if (!pIocbOut->context3)
11508                                 break;
11509                         numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11510                                         sizeof(struct ulp_bde64);
11511                         dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11512                         bpl = (struct ulp_bde64 *)dmabuf->virt;
11513                         for (i = 0; i < numBdes; i++) {
11514                                 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11515                                 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11516                                         max_response += bde.tus.f.bdeSize;
11517                         }
11518                         break;
11519                 default:
11520                         max_response = wcqe->total_data_placed;
11521                         break;
11522                 }
11523                 if (max_response < wcqe->total_data_placed)
11524                         pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11525                 else
11526                         pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11527                                 wcqe->total_data_placed;
11528         }
11529
11530         /* Convert BG errors for completion status */
11531         if (status == CQE_STATUS_DI_ERROR) {
11532                 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11533
11534                 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11535                         pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11536                 else
11537                         pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11538
11539                 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11540                 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11541                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11542                                 BGS_GUARD_ERR_MASK;
11543                 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11544                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11545                                 BGS_APPTAG_ERR_MASK;
11546                 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11547                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11548                                 BGS_REFTAG_ERR_MASK;
11549
11550                 /* Check to see if there was any good data before the error */
11551                 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11552                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11553                                 BGS_HI_WATER_MARK_PRESENT_MASK;
11554                         pIocbIn->iocb.unsli3.sli3_bg.bghm =
11555                                 wcqe->total_data_placed;
11556                 }
11557
11558                 /*
11559                 * Set ALL the error bits to indicate we don't know what
11560                 * type of error it is.
11561                 */
11562                 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11563                         pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11564                                 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11565                                 BGS_GUARD_ERR_MASK);
11566         }
11567
11568         /* Pick up HBA exchange busy condition */
11569         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11570                 spin_lock_irqsave(&phba->hbalock, iflags);
11571                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11572                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11573         }
11574 }
11575
11576 /**
11577  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11578  * @phba: Pointer to HBA context object.
11579  * @wcqe: Pointer to work-queue completion queue entry.
11580  *
11581  * This routine handles an ELS work-queue completion event and construct
11582  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11583  * discovery engine to handle.
11584  *
11585  * Return: Pointer to the receive IOCBQ, NULL otherwise.
11586  **/
11587 static struct lpfc_iocbq *
11588 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11589                                struct lpfc_iocbq *irspiocbq)
11590 {
11591         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11592         struct lpfc_iocbq *cmdiocbq;
11593         struct lpfc_wcqe_complete *wcqe;
11594         unsigned long iflags;
11595
11596         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
11597         spin_lock_irqsave(&pring->ring_lock, iflags);
11598         pring->stats.iocb_event++;
11599         /* Look up the ELS command IOCB and create pseudo response IOCB */
11600         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11601                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11602         spin_unlock_irqrestore(&pring->ring_lock, iflags);
11603
11604         if (unlikely(!cmdiocbq)) {
11605                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11606                                 "0386 ELS complete with no corresponding "
11607                                 "cmdiocb: iotag (%d)\n",
11608                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11609                 lpfc_sli_release_iocbq(phba, irspiocbq);
11610                 return NULL;
11611         }
11612
11613         /* Fake the irspiocbq and copy necessary response information */
11614         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
11615
11616         return irspiocbq;
11617 }
11618
11619 /**
11620  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11621  * @phba: Pointer to HBA context object.
11622  * @cqe: Pointer to mailbox completion queue entry.
11623  *
11624  * This routine process a mailbox completion queue entry with asynchrous
11625  * event.
11626  *
11627  * Return: true if work posted to worker thread, otherwise false.
11628  **/
11629 static bool
11630 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11631 {
11632         struct lpfc_cq_event *cq_event;
11633         unsigned long iflags;
11634
11635         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11636                         "0392 Async Event: word0:x%x, word1:x%x, "
11637                         "word2:x%x, word3:x%x\n", mcqe->word0,
11638                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11639
11640         /* Allocate a new internal CQ_EVENT entry */
11641         cq_event = lpfc_sli4_cq_event_alloc(phba);
11642         if (!cq_event) {
11643                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11644                                 "0394 Failed to allocate CQ_EVENT entry\n");
11645                 return false;
11646         }
11647
11648         /* Move the CQE into an asynchronous event entry */
11649         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11650         spin_lock_irqsave(&phba->hbalock, iflags);
11651         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11652         /* Set the async event flag */
11653         phba->hba_flag |= ASYNC_EVENT;
11654         spin_unlock_irqrestore(&phba->hbalock, iflags);
11655
11656         return true;
11657 }
11658
11659 /**
11660  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11661  * @phba: Pointer to HBA context object.
11662  * @cqe: Pointer to mailbox completion queue entry.
11663  *
11664  * This routine process a mailbox completion queue entry with mailbox
11665  * completion event.
11666  *
11667  * Return: true if work posted to worker thread, otherwise false.
11668  **/
11669 static bool
11670 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11671 {
11672         uint32_t mcqe_status;
11673         MAILBOX_t *mbox, *pmbox;
11674         struct lpfc_mqe *mqe;
11675         struct lpfc_vport *vport;
11676         struct lpfc_nodelist *ndlp;
11677         struct lpfc_dmabuf *mp;
11678         unsigned long iflags;
11679         LPFC_MBOXQ_t *pmb;
11680         bool workposted = false;
11681         int rc;
11682
11683         /* If not a mailbox complete MCQE, out by checking mailbox consume */
11684         if (!bf_get(lpfc_trailer_completed, mcqe))
11685                 goto out_no_mqe_complete;
11686
11687         /* Get the reference to the active mbox command */
11688         spin_lock_irqsave(&phba->hbalock, iflags);
11689         pmb = phba->sli.mbox_active;
11690         if (unlikely(!pmb)) {
11691                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11692                                 "1832 No pending MBOX command to handle\n");
11693                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11694                 goto out_no_mqe_complete;
11695         }
11696         spin_unlock_irqrestore(&phba->hbalock, iflags);
11697         mqe = &pmb->u.mqe;
11698         pmbox = (MAILBOX_t *)&pmb->u.mqe;
11699         mbox = phba->mbox;
11700         vport = pmb->vport;
11701
11702         /* Reset heartbeat timer */
11703         phba->last_completion_time = jiffies;
11704         del_timer(&phba->sli.mbox_tmo);
11705
11706         /* Move mbox data to caller's mailbox region, do endian swapping */
11707         if (pmb->mbox_cmpl && mbox)
11708                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
11709
11710         /*
11711          * For mcqe errors, conditionally move a modified error code to
11712          * the mbox so that the error will not be missed.
11713          */
11714         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11715         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11716                 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11717                         bf_set(lpfc_mqe_status, mqe,
11718                                (LPFC_MBX_ERROR_RANGE | mcqe_status));
11719         }
11720         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11721                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11722                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11723                                       "MBOX dflt rpi: status:x%x rpi:x%x",
11724                                       mcqe_status,
11725                                       pmbox->un.varWords[0], 0);
11726                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11727                         mp = (struct lpfc_dmabuf *)(pmb->context1);
11728                         ndlp = (struct lpfc_nodelist *)pmb->context2;
11729                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
11730                          * RID of the PPI using the same mbox buffer.
11731                          */
11732                         lpfc_unreg_login(phba, vport->vpi,
11733                                          pmbox->un.varWords[0], pmb);
11734                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11735                         pmb->context1 = mp;
11736                         pmb->context2 = ndlp;
11737                         pmb->vport = vport;
11738                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11739                         if (rc != MBX_BUSY)
11740                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11741                                                 LOG_SLI, "0385 rc should "
11742                                                 "have been MBX_BUSY\n");
11743                         if (rc != MBX_NOT_FINISHED)
11744                                 goto send_current_mbox;
11745                 }
11746         }
11747         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11748         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11749         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11750
11751         /* There is mailbox completion work to do */
11752         spin_lock_irqsave(&phba->hbalock, iflags);
11753         __lpfc_mbox_cmpl_put(phba, pmb);
11754         phba->work_ha |= HA_MBATT;
11755         spin_unlock_irqrestore(&phba->hbalock, iflags);
11756         workposted = true;
11757
11758 send_current_mbox:
11759         spin_lock_irqsave(&phba->hbalock, iflags);
11760         /* Release the mailbox command posting token */
11761         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11762         /* Setting active mailbox pointer need to be in sync to flag clear */
11763         phba->sli.mbox_active = NULL;
11764         if (bf_get(lpfc_trailer_consumed, mcqe))
11765                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11766         spin_unlock_irqrestore(&phba->hbalock, iflags);
11767         /* Wake up worker thread to post the next pending mailbox command */
11768         lpfc_worker_wake_up(phba);
11769         return workposted;
11770
11771 out_no_mqe_complete:
11772         spin_lock_irqsave(&phba->hbalock, iflags);
11773         if (bf_get(lpfc_trailer_consumed, mcqe))
11774                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11775         spin_unlock_irqrestore(&phba->hbalock, iflags);
11776         return false;
11777 }
11778
11779 /**
11780  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11781  * @phba: Pointer to HBA context object.
11782  * @cqe: Pointer to mailbox completion queue entry.
11783  *
11784  * This routine process a mailbox completion queue entry, it invokes the
11785  * proper mailbox complete handling or asynchrous event handling routine
11786  * according to the MCQE's async bit.
11787  *
11788  * Return: true if work posted to worker thread, otherwise false.
11789  **/
11790 static bool
11791 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11792 {
11793         struct lpfc_mcqe mcqe;
11794         bool workposted;
11795
11796         /* Copy the mailbox MCQE and convert endian order as needed */
11797         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11798
11799         /* Invoke the proper event handling routine */
11800         if (!bf_get(lpfc_trailer_async, &mcqe))
11801                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11802         else
11803                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11804         return workposted;
11805 }
11806
11807 /**
11808  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11809  * @phba: Pointer to HBA context object.
11810  * @cq: Pointer to associated CQ
11811  * @wcqe: Pointer to work-queue completion queue entry.
11812  *
11813  * This routine handles an ELS work-queue completion event.
11814  *
11815  * Return: true if work posted to worker thread, otherwise false.
11816  **/
11817 static bool
11818 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11819                              struct lpfc_wcqe_complete *wcqe)
11820 {
11821         struct lpfc_iocbq *irspiocbq;
11822         unsigned long iflags;
11823         struct lpfc_sli_ring *pring = cq->pring;
11824         int txq_cnt = 0;
11825         int txcmplq_cnt = 0;
11826         int fcp_txcmplq_cnt = 0;
11827
11828         /* Get an irspiocbq for later ELS response processing use */
11829         irspiocbq = lpfc_sli_get_iocbq(phba);
11830         if (!irspiocbq) {
11831                 if (!list_empty(&pring->txq))
11832                         txq_cnt++;
11833                 if (!list_empty(&pring->txcmplq))
11834                         txcmplq_cnt++;
11835                 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11836                         fcp_txcmplq_cnt++;
11837                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11838                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11839                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11840                         txq_cnt, phba->iocb_cnt,
11841                         fcp_txcmplq_cnt,
11842                         txcmplq_cnt);
11843                 return false;
11844         }
11845
11846         /* Save off the slow-path queue event for work thread to process */
11847         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
11848         spin_lock_irqsave(&phba->hbalock, iflags);
11849         list_add_tail(&irspiocbq->cq_event.list,
11850                       &phba->sli4_hba.sp_queue_event);
11851         phba->hba_flag |= HBA_SP_QUEUE_EVT;
11852         spin_unlock_irqrestore(&phba->hbalock, iflags);
11853
11854         return true;
11855 }
11856
11857 /**
11858  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11859  * @phba: Pointer to HBA context object.
11860  * @wcqe: Pointer to work-queue completion queue entry.
11861  *
11862  * This routine handles slow-path WQ entry comsumed event by invoking the
11863  * proper WQ release routine to the slow-path WQ.
11864  **/
11865 static void
11866 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11867                              struct lpfc_wcqe_release *wcqe)
11868 {
11869         /* sanity check on queue memory */
11870         if (unlikely(!phba->sli4_hba.els_wq))
11871                 return;
11872         /* Check for the slow-path ELS work queue */
11873         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11874                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11875                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11876         else
11877                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11878                                 "2579 Slow-path wqe consume event carries "
11879                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11880                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11881                                 phba->sli4_hba.els_wq->queue_id);
11882 }
11883
11884 /**
11885  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11886  * @phba: Pointer to HBA context object.
11887  * @cq: Pointer to a WQ completion queue.
11888  * @wcqe: Pointer to work-queue completion queue entry.
11889  *
11890  * This routine handles an XRI abort event.
11891  *
11892  * Return: true if work posted to worker thread, otherwise false.
11893  **/
11894 static bool
11895 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11896                                    struct lpfc_queue *cq,
11897                                    struct sli4_wcqe_xri_aborted *wcqe)
11898 {
11899         bool workposted = false;
11900         struct lpfc_cq_event *cq_event;
11901         unsigned long iflags;
11902
11903         /* Allocate a new internal CQ_EVENT entry */
11904         cq_event = lpfc_sli4_cq_event_alloc(phba);
11905         if (!cq_event) {
11906                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11907                                 "0602 Failed to allocate CQ_EVENT entry\n");
11908                 return false;
11909         }
11910
11911         /* Move the CQE into the proper xri abort event list */
11912         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11913         switch (cq->subtype) {
11914         case LPFC_FCP:
11915                 spin_lock_irqsave(&phba->hbalock, iflags);
11916                 list_add_tail(&cq_event->list,
11917                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11918                 /* Set the fcp xri abort event flag */
11919                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11920                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11921                 workposted = true;
11922                 break;
11923         case LPFC_ELS:
11924                 spin_lock_irqsave(&phba->hbalock, iflags);
11925                 list_add_tail(&cq_event->list,
11926                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11927                 /* Set the els xri abort event flag */
11928                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11929                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11930                 workposted = true;
11931                 break;
11932         default:
11933                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11934                                 "0603 Invalid work queue CQE subtype (x%x)\n",
11935                                 cq->subtype);
11936                 workposted = false;
11937                 break;
11938         }
11939         return workposted;
11940 }
11941
11942 /**
11943  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11944  * @phba: Pointer to HBA context object.
11945  * @rcqe: Pointer to receive-queue completion queue entry.
11946  *
11947  * This routine process a receive-queue completion queue entry.
11948  *
11949  * Return: true if work posted to worker thread, otherwise false.
11950  **/
11951 static bool
11952 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11953 {
11954         bool workposted = false;
11955         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11956         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11957         struct hbq_dmabuf *dma_buf;
11958         uint32_t status, rq_id;
11959         unsigned long iflags;
11960
11961         /* sanity check on queue memory */
11962         if (unlikely(!hrq) || unlikely(!drq))
11963                 return workposted;
11964
11965         if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11966                 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11967         else
11968                 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11969         if (rq_id != hrq->queue_id)
11970                 goto out;
11971
11972         status = bf_get(lpfc_rcqe_status, rcqe);
11973         switch (status) {
11974         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11975                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11976                                 "2537 Receive Frame Truncated!!\n");
11977                 hrq->RQ_buf_trunc++;
11978         case FC_STATUS_RQ_SUCCESS:
11979                 lpfc_sli4_rq_release(hrq, drq);
11980                 spin_lock_irqsave(&phba->hbalock, iflags);
11981                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11982                 if (!dma_buf) {
11983                         hrq->RQ_no_buf_found++;
11984                         spin_unlock_irqrestore(&phba->hbalock, iflags);
11985                         goto out;
11986                 }
11987                 hrq->RQ_rcv_buf++;
11988                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11989                 /* save off the frame for the word thread to process */
11990                 list_add_tail(&dma_buf->cq_event.list,
11991                               &phba->sli4_hba.sp_queue_event);
11992                 /* Frame received */
11993                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11994                 spin_unlock_irqrestore(&phba->hbalock, iflags);
11995                 workposted = true;
11996                 break;
11997         case FC_STATUS_INSUFF_BUF_NEED_BUF:
11998         case FC_STATUS_INSUFF_BUF_FRM_DISC:
11999                 hrq->RQ_no_posted_buf++;
12000                 /* Post more buffers if possible */
12001                 spin_lock_irqsave(&phba->hbalock, iflags);
12002                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12003                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12004                 workposted = true;
12005                 break;
12006         }
12007 out:
12008         return workposted;
12009 }
12010
12011 /**
12012  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12013  * @phba: Pointer to HBA context object.
12014  * @cq: Pointer to the completion queue.
12015  * @wcqe: Pointer to a completion queue entry.
12016  *
12017  * This routine process a slow-path work-queue or receive queue completion queue
12018  * entry.
12019  *
12020  * Return: true if work posted to worker thread, otherwise false.
12021  **/
12022 static bool
12023 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12024                          struct lpfc_cqe *cqe)
12025 {
12026         struct lpfc_cqe cqevt;
12027         bool workposted = false;
12028
12029         /* Copy the work queue CQE and convert endian order if needed */
12030         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
12031
12032         /* Check and process for different type of WCQE and dispatch */
12033         switch (bf_get(lpfc_cqe_code, &cqevt)) {
12034         case CQE_CODE_COMPL_WQE:
12035                 /* Process the WQ/RQ complete event */
12036                 phba->last_completion_time = jiffies;
12037                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
12038                                 (struct lpfc_wcqe_complete *)&cqevt);
12039                 break;
12040         case CQE_CODE_RELEASE_WQE:
12041                 /* Process the WQ release event */
12042                 lpfc_sli4_sp_handle_rel_wcqe(phba,
12043                                 (struct lpfc_wcqe_release *)&cqevt);
12044                 break;
12045         case CQE_CODE_XRI_ABORTED:
12046                 /* Process the WQ XRI abort event */
12047                 phba->last_completion_time = jiffies;
12048                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12049                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
12050                 break;
12051         case CQE_CODE_RECEIVE:
12052         case CQE_CODE_RECEIVE_V1:
12053                 /* Process the RQ event */
12054                 phba->last_completion_time = jiffies;
12055                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
12056                                 (struct lpfc_rcqe *)&cqevt);
12057                 break;
12058         default:
12059                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12060                                 "0388 Not a valid WCQE code: x%x\n",
12061                                 bf_get(lpfc_cqe_code, &cqevt));
12062                 break;
12063         }
12064         return workposted;
12065 }
12066
12067 /**
12068  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12069  * @phba: Pointer to HBA context object.
12070  * @eqe: Pointer to fast-path event queue entry.
12071  *
12072  * This routine process a event queue entry from the slow-path event queue.
12073  * It will check the MajorCode and MinorCode to determine this is for a
12074  * completion event on a completion queue, if not, an error shall be logged
12075  * and just return. Otherwise, it will get to the corresponding completion
12076  * queue and process all the entries on that completion queue, rearm the
12077  * completion queue, and then return.
12078  *
12079  **/
12080 static void
12081 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12082         struct lpfc_queue *speq)
12083 {
12084         struct lpfc_queue *cq = NULL, *childq;
12085         struct lpfc_cqe *cqe;
12086         bool workposted = false;
12087         int ecount = 0;
12088         uint16_t cqid;
12089
12090         /* Get the reference to the corresponding CQ */
12091         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12092
12093         list_for_each_entry(childq, &speq->child_list, list) {
12094                 if (childq->queue_id == cqid) {
12095                         cq = childq;
12096                         break;
12097                 }
12098         }
12099         if (unlikely(!cq)) {
12100                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12101                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12102                                         "0365 Slow-path CQ identifier "
12103                                         "(%d) does not exist\n", cqid);
12104                 return;
12105         }
12106
12107         /* Process all the entries to the CQ */
12108         switch (cq->type) {
12109         case LPFC_MCQ:
12110                 while ((cqe = lpfc_sli4_cq_get(cq))) {
12111                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
12112                         if (!(++ecount % cq->entry_repost))
12113                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12114                         cq->CQ_mbox++;
12115                 }
12116                 break;
12117         case LPFC_WCQ:
12118                 while ((cqe = lpfc_sli4_cq_get(cq))) {
12119                         if (cq->subtype == LPFC_FCP)
12120                                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12121                                                                        cqe);
12122                         else
12123                                 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12124                                                                       cqe);
12125                         if (!(++ecount % cq->entry_repost))
12126                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12127                 }
12128
12129                 /* Track the max number of CQEs processed in 1 EQ */
12130                 if (ecount > cq->CQ_max_cqe)
12131                         cq->CQ_max_cqe = ecount;
12132                 break;
12133         default:
12134                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12135                                 "0370 Invalid completion queue type (%d)\n",
12136                                 cq->type);
12137                 return;
12138         }
12139
12140         /* Catch the no cq entry condition, log an error */
12141         if (unlikely(ecount == 0))
12142                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12143                                 "0371 No entry from the CQ: identifier "
12144                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12145
12146         /* In any case, flash and re-arm the RCQ */
12147         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12148
12149         /* wake up worker thread if there are works to be done */
12150         if (workposted)
12151                 lpfc_worker_wake_up(phba);
12152 }
12153
12154 /**
12155  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
12156  * @phba: Pointer to HBA context object.
12157  * @cq: Pointer to associated CQ
12158  * @wcqe: Pointer to work-queue completion queue entry.
12159  *
12160  * This routine process a fast-path work queue completion entry from fast-path
12161  * event queue for FCP command response completion.
12162  **/
12163 static void
12164 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12165                              struct lpfc_wcqe_complete *wcqe)
12166 {
12167         struct lpfc_sli_ring *pring = cq->pring;
12168         struct lpfc_iocbq *cmdiocbq;
12169         struct lpfc_iocbq irspiocbq;
12170         unsigned long iflags;
12171
12172         /* Check for response status */
12173         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12174                 /* If resource errors reported from HBA, reduce queue
12175                  * depth of the SCSI device.
12176                  */
12177                 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12178                      IOSTAT_LOCAL_REJECT)) &&
12179                     ((wcqe->parameter & IOERR_PARAM_MASK) ==
12180                      IOERR_NO_RESOURCES))
12181                         phba->lpfc_rampdown_queue_depth(phba);
12182
12183                 /* Log the error status */
12184                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12185                                 "0373 FCP complete error: status=x%x, "
12186                                 "hw_status=x%x, total_data_specified=%d, "
12187                                 "parameter=x%x, word3=x%x\n",
12188                                 bf_get(lpfc_wcqe_c_status, wcqe),
12189                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12190                                 wcqe->total_data_placed, wcqe->parameter,
12191                                 wcqe->word3);
12192         }
12193
12194         /* Look up the FCP command IOCB and create pseudo response IOCB */
12195         spin_lock_irqsave(&pring->ring_lock, iflags);
12196         pring->stats.iocb_event++;
12197         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12198                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12199         spin_unlock_irqrestore(&pring->ring_lock, iflags);
12200         if (unlikely(!cmdiocbq)) {
12201                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12202                                 "0374 FCP complete with no corresponding "
12203                                 "cmdiocb: iotag (%d)\n",
12204                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12205                 return;
12206         }
12207         if (unlikely(!cmdiocbq->iocb_cmpl)) {
12208                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12209                                 "0375 FCP cmdiocb not callback function "
12210                                 "iotag: (%d)\n",
12211                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12212                 return;
12213         }
12214
12215         /* Fake the irspiocb and copy necessary response information */
12216         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
12217
12218         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12219                 spin_lock_irqsave(&phba->hbalock, iflags);
12220                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12221                 spin_unlock_irqrestore(&phba->hbalock, iflags);
12222         }
12223
12224         /* Pass the cmd_iocb and the rsp state to the upper layer */
12225         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12226 }
12227
12228 /**
12229  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12230  * @phba: Pointer to HBA context object.
12231  * @cq: Pointer to completion queue.
12232  * @wcqe: Pointer to work-queue completion queue entry.
12233  *
12234  * This routine handles an fast-path WQ entry comsumed event by invoking the
12235  * proper WQ release routine to the slow-path WQ.
12236  **/
12237 static void
12238 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12239                              struct lpfc_wcqe_release *wcqe)
12240 {
12241         struct lpfc_queue *childwq;
12242         bool wqid_matched = false;
12243         uint16_t fcp_wqid;
12244
12245         /* Check for fast-path FCP work queue release */
12246         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12247         list_for_each_entry(childwq, &cq->child_list, list) {
12248                 if (childwq->queue_id == fcp_wqid) {
12249                         lpfc_sli4_wq_release(childwq,
12250                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12251                         wqid_matched = true;
12252                         break;
12253                 }
12254         }
12255         /* Report warning log message if no match found */
12256         if (wqid_matched != true)
12257                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12258                                 "2580 Fast-path wqe consume event carries "
12259                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12260 }
12261
12262 /**
12263  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12264  * @cq: Pointer to the completion queue.
12265  * @eqe: Pointer to fast-path completion queue entry.
12266  *
12267  * This routine process a fast-path work queue completion entry from fast-path
12268  * event queue for FCP command response completion.
12269  **/
12270 static int
12271 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12272                          struct lpfc_cqe *cqe)
12273 {
12274         struct lpfc_wcqe_release wcqe;
12275         bool workposted = false;
12276
12277         /* Copy the work queue CQE and convert endian order if needed */
12278         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12279
12280         /* Check and process for different type of WCQE and dispatch */
12281         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12282         case CQE_CODE_COMPL_WQE:
12283                 cq->CQ_wq++;
12284                 /* Process the WQ complete event */
12285                 phba->last_completion_time = jiffies;
12286                 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
12287                                 (struct lpfc_wcqe_complete *)&wcqe);
12288                 break;
12289         case CQE_CODE_RELEASE_WQE:
12290                 cq->CQ_release_wqe++;
12291                 /* Process the WQ release event */
12292                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12293                                 (struct lpfc_wcqe_release *)&wcqe);
12294                 break;
12295         case CQE_CODE_XRI_ABORTED:
12296                 cq->CQ_xri_aborted++;
12297                 /* Process the WQ XRI abort event */
12298                 phba->last_completion_time = jiffies;
12299                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12300                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
12301                 break;
12302         default:
12303                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12304                                 "0144 Not a valid WCQE code: x%x\n",
12305                                 bf_get(lpfc_wcqe_c_code, &wcqe));
12306                 break;
12307         }
12308         return workposted;
12309 }
12310
12311 /**
12312  * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
12313  * @phba: Pointer to HBA context object.
12314  * @eqe: Pointer to fast-path event queue entry.
12315  *
12316  * This routine process a event queue entry from the fast-path event queue.
12317  * It will check the MajorCode and MinorCode to determine this is for a
12318  * completion event on a completion queue, if not, an error shall be logged
12319  * and just return. Otherwise, it will get to the corresponding completion
12320  * queue and process all the entries on the completion queue, rearm the
12321  * completion queue, and then return.
12322  **/
12323 static void
12324 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12325                         uint32_t qidx)
12326 {
12327         struct lpfc_queue *cq;
12328         struct lpfc_cqe *cqe;
12329         bool workposted = false;
12330         uint16_t cqid;
12331         int ecount = 0;
12332
12333         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12334                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12335                                 "0366 Not a valid completion "
12336                                 "event: majorcode=x%x, minorcode=x%x\n",
12337                                 bf_get_le32(lpfc_eqe_major_code, eqe),
12338                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
12339                 return;
12340         }
12341
12342         /* Get the reference to the corresponding CQ */
12343         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12344
12345         /* Check if this is a Slow path event */
12346         if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12347                 lpfc_sli4_sp_handle_eqe(phba, eqe,
12348                         phba->sli4_hba.hba_eq[qidx]);
12349                 return;
12350         }
12351
12352         if (unlikely(!phba->sli4_hba.fcp_cq)) {
12353                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12354                                 "3146 Fast-path completion queues "
12355                                 "does not exist\n");
12356                 return;
12357         }
12358         cq = phba->sli4_hba.fcp_cq[qidx];
12359         if (unlikely(!cq)) {
12360                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12361                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12362                                         "0367 Fast-path completion queue "
12363                                         "(%d) does not exist\n", qidx);
12364                 return;
12365         }
12366
12367         if (unlikely(cqid != cq->queue_id)) {
12368                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12369                                 "0368 Miss-matched fast-path completion "
12370                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12371                                 cqid, cq->queue_id);
12372                 return;
12373         }
12374
12375         /* Process all the entries to the CQ */
12376         while ((cqe = lpfc_sli4_cq_get(cq))) {
12377                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12378                 if (!(++ecount % cq->entry_repost))
12379                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12380         }
12381
12382         /* Track the max number of CQEs processed in 1 EQ */
12383         if (ecount > cq->CQ_max_cqe)
12384                 cq->CQ_max_cqe = ecount;
12385
12386         /* Catch the no cq entry condition */
12387         if (unlikely(ecount == 0))
12388                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12389                                 "0369 No entry from fast-path completion "
12390                                 "queue fcpcqid=%d\n", cq->queue_id);
12391
12392         /* In any case, flash and re-arm the CQ */
12393         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12394
12395         /* wake up worker thread if there are works to be done */
12396         if (workposted)
12397                 lpfc_worker_wake_up(phba);
12398 }
12399
12400 static void
12401 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12402 {
12403         struct lpfc_eqe *eqe;
12404
12405         /* walk all the EQ entries and drop on the floor */
12406         while ((eqe = lpfc_sli4_eq_get(eq)))
12407                 ;
12408
12409         /* Clear and re-arm the EQ */
12410         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12411 }
12412
12413
12414 /**
12415  * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12416  *                           entry
12417  * @phba: Pointer to HBA context object.
12418  * @eqe: Pointer to fast-path event queue entry.
12419  *
12420  * This routine process a event queue entry from the Flash Optimized Fabric
12421  * event queue.  It will check the MajorCode and MinorCode to determine this
12422  * is for a completion event on a completion queue, if not, an error shall be
12423  * logged and just return. Otherwise, it will get to the corresponding
12424  * completion queue and process all the entries on the completion queue, rearm
12425  * the completion queue, and then return.
12426  **/
12427 static void
12428 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12429 {
12430         struct lpfc_queue *cq;
12431         struct lpfc_cqe *cqe;
12432         bool workposted = false;
12433         uint16_t cqid;
12434         int ecount = 0;
12435
12436         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12437                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12438                                 "9147 Not a valid completion "
12439                                 "event: majorcode=x%x, minorcode=x%x\n",
12440                                 bf_get_le32(lpfc_eqe_major_code, eqe),
12441                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
12442                 return;
12443         }
12444
12445         /* Get the reference to the corresponding CQ */
12446         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12447
12448         /* Next check for OAS */
12449         cq = phba->sli4_hba.oas_cq;
12450         if (unlikely(!cq)) {
12451                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12452                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12453                                         "9148 OAS completion queue "
12454                                         "does not exist\n");
12455                 return;
12456         }
12457
12458         if (unlikely(cqid != cq->queue_id)) {
12459                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12460                                 "9149 Miss-matched fast-path compl "
12461                                 "queue id: eqcqid=%d, fcpcqid=%d\n",
12462                                 cqid, cq->queue_id);
12463                 return;
12464         }
12465
12466         /* Process all the entries to the OAS CQ */
12467         while ((cqe = lpfc_sli4_cq_get(cq))) {
12468                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12469                 if (!(++ecount % cq->entry_repost))
12470                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12471         }
12472
12473         /* Track the max number of CQEs processed in 1 EQ */
12474         if (ecount > cq->CQ_max_cqe)
12475                 cq->CQ_max_cqe = ecount;
12476
12477         /* Catch the no cq entry condition */
12478         if (unlikely(ecount == 0))
12479                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12480                                 "9153 No entry from fast-path completion "
12481                                 "queue fcpcqid=%d\n", cq->queue_id);
12482
12483         /* In any case, flash and re-arm the CQ */
12484         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12485
12486         /* wake up worker thread if there are works to be done */
12487         if (workposted)
12488                 lpfc_worker_wake_up(phba);
12489 }
12490
12491 /**
12492  * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12493  * @irq: Interrupt number.
12494  * @dev_id: The device context pointer.
12495  *
12496  * This function is directly called from the PCI layer as an interrupt
12497  * service routine when device with SLI-4 interface spec is enabled with
12498  * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12499  * IOCB ring event in the HBA. However, when the device is enabled with either
12500  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12501  * device-level interrupt handler. When the PCI slot is in error recovery
12502  * or the HBA is undergoing initialization, the interrupt handler will not
12503  * process the interrupt. The Flash Optimized Fabric ring event are handled in
12504  * the intrrupt context. This function is called without any lock held.
12505  * It gets the hbalock to access and update SLI data structures. Note that,
12506  * the EQ to CQ are one-to-one map such that the EQ index is
12507  * equal to that of CQ index.
12508  *
12509  * This function returns IRQ_HANDLED when interrupt is handled else it
12510  * returns IRQ_NONE.
12511  **/
12512 irqreturn_t
12513 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12514 {
12515         struct lpfc_hba *phba;
12516         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12517         struct lpfc_queue *eq;
12518         struct lpfc_eqe *eqe;
12519         unsigned long iflag;
12520         int ecount = 0;
12521
12522         /* Get the driver's phba structure from the dev_id */
12523         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12524         phba = fcp_eq_hdl->phba;
12525
12526         if (unlikely(!phba))
12527                 return IRQ_NONE;
12528
12529         /* Get to the EQ struct associated with this vector */
12530         eq = phba->sli4_hba.fof_eq;
12531         if (unlikely(!eq))
12532                 return IRQ_NONE;
12533
12534         /* Check device state for handling interrupt */
12535         if (unlikely(lpfc_intr_state_check(phba))) {
12536                 eq->EQ_badstate++;
12537                 /* Check again for link_state with lock held */
12538                 spin_lock_irqsave(&phba->hbalock, iflag);
12539                 if (phba->link_state < LPFC_LINK_DOWN)
12540                         /* Flush, clear interrupt, and rearm the EQ */
12541                         lpfc_sli4_eq_flush(phba, eq);
12542                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12543                 return IRQ_NONE;
12544         }
12545
12546         /*
12547          * Process all the event on FCP fast-path EQ
12548          */
12549         while ((eqe = lpfc_sli4_eq_get(eq))) {
12550                 lpfc_sli4_fof_handle_eqe(phba, eqe);
12551                 if (!(++ecount % eq->entry_repost))
12552                         lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12553                 eq->EQ_processed++;
12554         }
12555
12556         /* Track the max number of EQEs processed in 1 intr */
12557         if (ecount > eq->EQ_max_eqe)
12558                 eq->EQ_max_eqe = ecount;
12559
12560
12561         if (unlikely(ecount == 0)) {
12562                 eq->EQ_no_entry++;
12563
12564                 if (phba->intr_type == MSIX)
12565                         /* MSI-X treated interrupt served as no EQ share INT */
12566                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12567                                         "9145 MSI-X interrupt with no EQE\n");
12568                 else {
12569                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12570                                         "9146 ISR interrupt with no EQE\n");
12571                         /* Non MSI-X treated on interrupt as EQ share INT */
12572                         return IRQ_NONE;
12573                 }
12574         }
12575         /* Always clear and re-arm the fast-path EQ */
12576         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12577         return IRQ_HANDLED;
12578 }
12579
12580 /**
12581  * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
12582  * @irq: Interrupt number.
12583  * @dev_id: The device context pointer.
12584  *
12585  * This function is directly called from the PCI layer as an interrupt
12586  * service routine when device with SLI-4 interface spec is enabled with
12587  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12588  * ring event in the HBA. However, when the device is enabled with either
12589  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12590  * device-level interrupt handler. When the PCI slot is in error recovery
12591  * or the HBA is undergoing initialization, the interrupt handler will not
12592  * process the interrupt. The SCSI FCP fast-path ring event are handled in
12593  * the intrrupt context. This function is called without any lock held.
12594  * It gets the hbalock to access and update SLI data structures. Note that,
12595  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12596  * equal to that of FCP CQ index.
12597  *
12598  * The link attention and ELS ring attention events are handled
12599  * by the worker thread. The interrupt handler signals the worker thread
12600  * and returns for these events. This function is called without any lock
12601  * held. It gets the hbalock to access and update SLI data structures.
12602  *
12603  * This function returns IRQ_HANDLED when interrupt is handled else it
12604  * returns IRQ_NONE.
12605  **/
12606 irqreturn_t
12607 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
12608 {
12609         struct lpfc_hba *phba;
12610         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12611         struct lpfc_queue *fpeq;
12612         struct lpfc_eqe *eqe;
12613         unsigned long iflag;
12614         int ecount = 0;
12615         int fcp_eqidx;
12616
12617         /* Get the driver's phba structure from the dev_id */
12618         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12619         phba = fcp_eq_hdl->phba;
12620         fcp_eqidx = fcp_eq_hdl->idx;
12621
12622         if (unlikely(!phba))
12623                 return IRQ_NONE;
12624         if (unlikely(!phba->sli4_hba.hba_eq))
12625                 return IRQ_NONE;
12626
12627         /* Get to the EQ struct associated with this vector */
12628         fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
12629         if (unlikely(!fpeq))
12630                 return IRQ_NONE;
12631
12632         if (lpfc_fcp_look_ahead) {
12633                 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12634                         lpfc_sli4_eq_clr_intr(fpeq);
12635                 else {
12636                         atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12637                         return IRQ_NONE;
12638                 }
12639         }
12640
12641         /* Check device state for handling interrupt */
12642         if (unlikely(lpfc_intr_state_check(phba))) {
12643                 fpeq->EQ_badstate++;
12644                 /* Check again for link_state with lock held */
12645                 spin_lock_irqsave(&phba->hbalock, iflag);
12646                 if (phba->link_state < LPFC_LINK_DOWN)
12647                         /* Flush, clear interrupt, and rearm the EQ */
12648                         lpfc_sli4_eq_flush(phba, fpeq);
12649                 spin_unlock_irqrestore(&phba->hbalock, iflag);
12650                 if (lpfc_fcp_look_ahead)
12651                         atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12652                 return IRQ_NONE;
12653         }
12654
12655         /*
12656          * Process all the event on FCP fast-path EQ
12657          */
12658         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
12659                 if (eqe == NULL)
12660                         break;
12661
12662                 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
12663                 if (!(++ecount % fpeq->entry_repost))
12664                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
12665                 fpeq->EQ_processed++;
12666         }
12667
12668         /* Track the max number of EQEs processed in 1 intr */
12669         if (ecount > fpeq->EQ_max_eqe)
12670                 fpeq->EQ_max_eqe = ecount;
12671
12672         /* Always clear and re-arm the fast-path EQ */
12673         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12674
12675         if (unlikely(ecount == 0)) {
12676                 fpeq->EQ_no_entry++;
12677
12678                 if (lpfc_fcp_look_ahead) {
12679                         atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12680                         return IRQ_NONE;
12681                 }
12682
12683                 if (phba->intr_type == MSIX)
12684                         /* MSI-X treated interrupt served as no EQ share INT */
12685                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12686                                         "0358 MSI-X interrupt with no EQE\n");
12687                 else
12688                         /* Non MSI-X treated on interrupt as EQ share INT */
12689                         return IRQ_NONE;
12690         }
12691
12692         if (lpfc_fcp_look_ahead)
12693                 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12694         return IRQ_HANDLED;
12695 } /* lpfc_sli4_fp_intr_handler */
12696
12697 /**
12698  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12699  * @irq: Interrupt number.
12700  * @dev_id: The device context pointer.
12701  *
12702  * This function is the device-level interrupt handler to device with SLI-4
12703  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12704  * interrupt mode is enabled and there is an event in the HBA which requires
12705  * driver attention. This function invokes the slow-path interrupt attention
12706  * handling function and fast-path interrupt attention handling function in
12707  * turn to process the relevant HBA attention events. This function is called
12708  * without any lock held. It gets the hbalock to access and update SLI data
12709  * structures.
12710  *
12711  * This function returns IRQ_HANDLED when interrupt is handled, else it
12712  * returns IRQ_NONE.
12713  **/
12714 irqreturn_t
12715 lpfc_sli4_intr_handler(int irq, void *dev_id)
12716 {
12717         struct lpfc_hba  *phba;
12718         irqreturn_t hba_irq_rc;
12719         bool hba_handled = false;
12720         int fcp_eqidx;
12721
12722         /* Get the driver's phba structure from the dev_id */
12723         phba = (struct lpfc_hba *)dev_id;
12724
12725         if (unlikely(!phba))
12726                 return IRQ_NONE;
12727
12728         /*
12729          * Invoke fast-path host attention interrupt handling as appropriate.
12730          */
12731         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12732                 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
12733                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12734                 if (hba_irq_rc == IRQ_HANDLED)
12735                         hba_handled |= true;
12736         }
12737
12738         if (phba->cfg_fof) {
12739                 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12740                                         &phba->sli4_hba.fcp_eq_hdl[0]);
12741                 if (hba_irq_rc == IRQ_HANDLED)
12742                         hba_handled |= true;
12743         }
12744
12745         return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
12746 } /* lpfc_sli4_intr_handler */
12747
12748 /**
12749  * lpfc_sli4_queue_free - free a queue structure and associated memory
12750  * @queue: The queue structure to free.
12751  *
12752  * This function frees a queue structure and the DMAable memory used for
12753  * the host resident queue. This function must be called after destroying the
12754  * queue on the HBA.
12755  **/
12756 void
12757 lpfc_sli4_queue_free(struct lpfc_queue *queue)
12758 {
12759         struct lpfc_dmabuf *dmabuf;
12760
12761         if (!queue)
12762                 return;
12763
12764         while (!list_empty(&queue->page_list)) {
12765                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12766                                  list);
12767                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
12768                                   dmabuf->virt, dmabuf->phys);
12769                 kfree(dmabuf);
12770         }
12771         kfree(queue);
12772         return;
12773 }
12774
12775 /**
12776  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12777  * @phba: The HBA that this queue is being created on.
12778  * @entry_size: The size of each queue entry for this queue.
12779  * @entry count: The number of entries that this queue will handle.
12780  *
12781  * This function allocates a queue structure and the DMAable memory used for
12782  * the host resident queue. This function must be called before creating the
12783  * queue on the HBA.
12784  **/
12785 struct lpfc_queue *
12786 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12787                       uint32_t entry_count)
12788 {
12789         struct lpfc_queue *queue;
12790         struct lpfc_dmabuf *dmabuf;
12791         int x, total_qe_count;
12792         void *dma_pointer;
12793         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12794
12795         if (!phba->sli4_hba.pc_sli4_params.supported)
12796                 hw_page_size = SLI4_PAGE_SIZE;
12797
12798         queue = kzalloc(sizeof(struct lpfc_queue) +
12799                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12800         if (!queue)
12801                 return NULL;
12802         queue->page_count = (ALIGN(entry_size * entry_count,
12803                         hw_page_size))/hw_page_size;
12804         INIT_LIST_HEAD(&queue->list);
12805         INIT_LIST_HEAD(&queue->page_list);
12806         INIT_LIST_HEAD(&queue->child_list);
12807         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12808                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12809                 if (!dmabuf)
12810                         goto out_fail;
12811                 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
12812                                                    hw_page_size, &dmabuf->phys,
12813                                                    GFP_KERNEL);
12814                 if (!dmabuf->virt) {
12815                         kfree(dmabuf);
12816                         goto out_fail;
12817                 }
12818                 dmabuf->buffer_tag = x;
12819                 list_add_tail(&dmabuf->list, &queue->page_list);
12820                 /* initialize queue's entry array */
12821                 dma_pointer = dmabuf->virt;
12822                 for (; total_qe_count < entry_count &&
12823                      dma_pointer < (hw_page_size + dmabuf->virt);
12824                      total_qe_count++, dma_pointer += entry_size) {
12825                         queue->qe[total_qe_count].address = dma_pointer;
12826                 }
12827         }
12828         queue->entry_size = entry_size;
12829         queue->entry_count = entry_count;
12830
12831         /*
12832          * entry_repost is calculated based on the number of entries in the
12833          * queue. This works out except for RQs. If buffers are NOT initially
12834          * posted for every RQE, entry_repost should be adjusted accordingly.
12835          */
12836         queue->entry_repost = (entry_count >> 3);
12837         if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12838                 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
12839         queue->phba = phba;
12840
12841         return queue;
12842 out_fail:
12843         lpfc_sli4_queue_free(queue);
12844         return NULL;
12845 }
12846
12847 /**
12848  * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12849  * @phba: HBA structure that indicates port to create a queue on.
12850  * @pci_barset: PCI BAR set flag.
12851  *
12852  * This function shall perform iomap of the specified PCI BAR address to host
12853  * memory address if not already done so and return it. The returned host
12854  * memory address can be NULL.
12855  */
12856 static void __iomem *
12857 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12858 {
12859         if (!phba->pcidev)
12860                 return NULL;
12861
12862         switch (pci_barset) {
12863         case WQ_PCI_BAR_0_AND_1:
12864                 return phba->pci_bar0_memmap_p;
12865         case WQ_PCI_BAR_2_AND_3:
12866                 return phba->pci_bar2_memmap_p;
12867         case WQ_PCI_BAR_4_AND_5:
12868                 return phba->pci_bar4_memmap_p;
12869         default:
12870                 break;
12871         }
12872         return NULL;
12873 }
12874
12875 /**
12876  * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12877  * @phba: HBA structure that indicates port to create a queue on.
12878  * @startq: The starting FCP EQ to modify
12879  *
12880  * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12881  *
12882  * The @phba struct is used to send mailbox command to HBA. The @startq
12883  * is used to get the starting FCP EQ to change.
12884  * This function is asynchronous and will wait for the mailbox
12885  * command to finish before continuing.
12886  *
12887  * On success this function will return a zero. If unable to allocate enough
12888  * memory this function will return -ENOMEM. If the queue create mailbox command
12889  * fails this function will return -ENXIO.
12890  **/
12891 int
12892 lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
12893 {
12894         struct lpfc_mbx_modify_eq_delay *eq_delay;
12895         LPFC_MBOXQ_t *mbox;
12896         struct lpfc_queue *eq;
12897         int cnt, rc, length, status = 0;
12898         uint32_t shdr_status, shdr_add_status;
12899         uint32_t result;
12900         int fcp_eqidx;
12901         union lpfc_sli4_cfg_shdr *shdr;
12902         uint16_t dmult;
12903
12904         if (startq >= phba->cfg_fcp_io_channel)
12905                 return 0;
12906
12907         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12908         if (!mbox)
12909                 return -ENOMEM;
12910         length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12911                   sizeof(struct lpfc_sli4_cfg_mhdr));
12912         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12913                          LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12914                          length, LPFC_SLI4_MBX_EMBED);
12915         eq_delay = &mbox->u.mqe.un.eq_delay;
12916
12917         /* Calculate delay multiper from maximum interrupt per second */
12918         result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12919         if (result > LPFC_DMULT_CONST)
12920                 dmult = 0;
12921         else
12922                 dmult = LPFC_DMULT_CONST/result - 1;
12923
12924         cnt = 0;
12925         for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
12926             fcp_eqidx++) {
12927                 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
12928                 if (!eq)
12929                         continue;
12930                 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12931                 eq_delay->u.request.eq[cnt].phase = 0;
12932                 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12933                 cnt++;
12934                 if (cnt >= LPFC_MAX_EQ_DELAY)
12935                         break;
12936         }
12937         eq_delay->u.request.num_eq = cnt;
12938
12939         mbox->vport = phba->pport;
12940         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12941         mbox->context1 = NULL;
12942         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12943         shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12944         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12945         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12946         if (shdr_status || shdr_add_status || rc) {
12947                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12948                                 "2512 MODIFY_EQ_DELAY mailbox failed with "
12949                                 "status x%x add_status x%x, mbx status x%x\n",
12950                                 shdr_status, shdr_add_status, rc);
12951                 status = -ENXIO;
12952         }
12953         mempool_free(mbox, phba->mbox_mem_pool);
12954         return status;
12955 }
12956
12957 /**
12958  * lpfc_eq_create - Create an Event Queue on the HBA
12959  * @phba: HBA structure that indicates port to create a queue on.
12960  * @eq: The queue structure to use to create the event queue.
12961  * @imax: The maximum interrupt per second limit.
12962  *
12963  * This function creates an event queue, as detailed in @eq, on a port,
12964  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12965  *
12966  * The @phba struct is used to send mailbox command to HBA. The @eq struct
12967  * is used to get the entry count and entry size that are necessary to
12968  * determine the number of pages to allocate and use for this queue. This
12969  * function will send the EQ_CREATE mailbox command to the HBA to setup the
12970  * event queue. This function is asynchronous and will wait for the mailbox
12971  * command to finish before continuing.
12972  *
12973  * On success this function will return a zero. If unable to allocate enough
12974  * memory this function will return -ENOMEM. If the queue create mailbox command
12975  * fails this function will return -ENXIO.
12976  **/
12977 int
12978 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
12979 {
12980         struct lpfc_mbx_eq_create *eq_create;
12981         LPFC_MBOXQ_t *mbox;
12982         int rc, length, status = 0;
12983         struct lpfc_dmabuf *dmabuf;
12984         uint32_t shdr_status, shdr_add_status;
12985         union lpfc_sli4_cfg_shdr *shdr;
12986         uint16_t dmult;
12987         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12988
12989         /* sanity check on queue memory */
12990         if (!eq)
12991                 return -ENODEV;
12992         if (!phba->sli4_hba.pc_sli4_params.supported)
12993                 hw_page_size = SLI4_PAGE_SIZE;
12994
12995         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12996         if (!mbox)
12997                 return -ENOMEM;
12998         length = (sizeof(struct lpfc_mbx_eq_create) -
12999                   sizeof(struct lpfc_sli4_cfg_mhdr));
13000         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13001                          LPFC_MBOX_OPCODE_EQ_CREATE,
13002                          length, LPFC_SLI4_MBX_EMBED);
13003         eq_create = &mbox->u.mqe.un.eq_create;
13004         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13005                eq->page_count);
13006         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13007                LPFC_EQE_SIZE);
13008         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
13009         /* don't setup delay multiplier using EQ_CREATE */
13010         dmult = 0;
13011         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13012                dmult);
13013         switch (eq->entry_count) {
13014         default:
13015                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13016                                 "0360 Unsupported EQ count. (%d)\n",
13017                                 eq->entry_count);
13018                 if (eq->entry_count < 256)
13019                         return -EINVAL;
13020                 /* otherwise default to smallest count (drop through) */
13021         case 256:
13022                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13023                        LPFC_EQ_CNT_256);
13024                 break;
13025         case 512:
13026                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13027                        LPFC_EQ_CNT_512);
13028                 break;
13029         case 1024:
13030                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13031                        LPFC_EQ_CNT_1024);
13032                 break;
13033         case 2048:
13034                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13035                        LPFC_EQ_CNT_2048);
13036                 break;
13037         case 4096:
13038                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13039                        LPFC_EQ_CNT_4096);
13040                 break;
13041         }
13042         list_for_each_entry(dmabuf, &eq->page_list, list) {
13043                 memset(dmabuf->virt, 0, hw_page_size);
13044                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13045                                         putPaddrLow(dmabuf->phys);
13046                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13047                                         putPaddrHigh(dmabuf->phys);
13048         }
13049         mbox->vport = phba->pport;
13050         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13051         mbox->context1 = NULL;
13052         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13053         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13054         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13055         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13056         if (shdr_status || shdr_add_status || rc) {
13057                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13058                                 "2500 EQ_CREATE mailbox failed with "
13059                                 "status x%x add_status x%x, mbx status x%x\n",
13060                                 shdr_status, shdr_add_status, rc);
13061                 status = -ENXIO;
13062         }
13063         eq->type = LPFC_EQ;
13064         eq->subtype = LPFC_NONE;
13065         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13066         if (eq->queue_id == 0xFFFF)
13067                 status = -ENXIO;
13068         eq->host_index = 0;
13069         eq->hba_index = 0;
13070
13071         mempool_free(mbox, phba->mbox_mem_pool);
13072         return status;
13073 }
13074
13075 /**
13076  * lpfc_cq_create - Create a Completion Queue on the HBA
13077  * @phba: HBA structure that indicates port to create a queue on.
13078  * @cq: The queue structure to use to create the completion queue.
13079  * @eq: The event queue to bind this completion queue to.
13080  *
13081  * This function creates a completion queue, as detailed in @wq, on a port,
13082  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13083  *
13084  * The @phba struct is used to send mailbox command to HBA. The @cq struct
13085  * is used to get the entry count and entry size that are necessary to
13086  * determine the number of pages to allocate and use for this queue. The @eq
13087  * is used to indicate which event queue to bind this completion queue to. This
13088  * function will send the CQ_CREATE mailbox command to the HBA to setup the
13089  * completion queue. This function is asynchronous and will wait for the mailbox
13090  * command to finish before continuing.
13091  *
13092  * On success this function will return a zero. If unable to allocate enough
13093  * memory this function will return -ENOMEM. If the queue create mailbox command
13094  * fails this function will return -ENXIO.
13095  **/
13096 int
13097 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13098                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13099 {
13100         struct lpfc_mbx_cq_create *cq_create;
13101         struct lpfc_dmabuf *dmabuf;
13102         LPFC_MBOXQ_t *mbox;
13103         int rc, length, status = 0;
13104         uint32_t shdr_status, shdr_add_status;
13105         union lpfc_sli4_cfg_shdr *shdr;
13106         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13107
13108         /* sanity check on queue memory */
13109         if (!cq || !eq)
13110                 return -ENODEV;
13111         if (!phba->sli4_hba.pc_sli4_params.supported)
13112                 hw_page_size = SLI4_PAGE_SIZE;
13113
13114         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13115         if (!mbox)
13116                 return -ENOMEM;
13117         length = (sizeof(struct lpfc_mbx_cq_create) -
13118                   sizeof(struct lpfc_sli4_cfg_mhdr));
13119         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13120                          LPFC_MBOX_OPCODE_CQ_CREATE,
13121                          length, LPFC_SLI4_MBX_EMBED);
13122         cq_create = &mbox->u.mqe.un.cq_create;
13123         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
13124         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13125                     cq->page_count);
13126         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13127         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
13128         bf_set(lpfc_mbox_hdr_version, &shdr->request,
13129                phba->sli4_hba.pc_sli4_params.cqv);
13130         if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
13131                 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13132                 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
13133                 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13134                        eq->queue_id);
13135         } else {
13136                 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13137                        eq->queue_id);
13138         }
13139         switch (cq->entry_count) {
13140         default:
13141                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13142                                 "0361 Unsupported CQ count. (%d)\n",
13143                                 cq->entry_count);
13144                 if (cq->entry_count < 256) {
13145                         status = -EINVAL;
13146                         goto out;
13147                 }
13148                 /* otherwise default to smallest count (drop through) */
13149         case 256:
13150                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13151                        LPFC_CQ_CNT_256);
13152                 break;
13153         case 512:
13154                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13155                        LPFC_CQ_CNT_512);
13156                 break;
13157         case 1024:
13158                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13159                        LPFC_CQ_CNT_1024);
13160                 break;
13161         }
13162         list_for_each_entry(dmabuf, &cq->page_list, list) {
13163                 memset(dmabuf->virt, 0, hw_page_size);
13164                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13165                                         putPaddrLow(dmabuf->phys);
13166                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13167                                         putPaddrHigh(dmabuf->phys);
13168         }
13169         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13170
13171         /* The IOCTL status is embedded in the mailbox subheader. */
13172         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13173         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13174         if (shdr_status || shdr_add_status || rc) {
13175                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13176                                 "2501 CQ_CREATE mailbox failed with "
13177                                 "status x%x add_status x%x, mbx status x%x\n",
13178                                 shdr_status, shdr_add_status, rc);
13179                 status = -ENXIO;
13180                 goto out;
13181         }
13182         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13183         if (cq->queue_id == 0xFFFF) {
13184                 status = -ENXIO;
13185                 goto out;
13186         }
13187         /* link the cq onto the parent eq child list */
13188         list_add_tail(&cq->list, &eq->child_list);
13189         /* Set up completion queue's type and subtype */
13190         cq->type = type;
13191         cq->subtype = subtype;
13192         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13193         cq->assoc_qid = eq->queue_id;
13194         cq->host_index = 0;
13195         cq->hba_index = 0;
13196
13197 out:
13198         mempool_free(mbox, phba->mbox_mem_pool);
13199         return status;
13200 }
13201
13202 /**
13203  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
13204  * @phba: HBA structure that indicates port to create a queue on.
13205  * @mq: The queue structure to use to create the mailbox queue.
13206  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13207  * @cq: The completion queue to associate with this cq.
13208  *
13209  * This function provides failback (fb) functionality when the
13210  * mq_create_ext fails on older FW generations.  It's purpose is identical
13211  * to mq_create_ext otherwise.
13212  *
13213  * This routine cannot fail as all attributes were previously accessed and
13214  * initialized in mq_create_ext.
13215  **/
13216 static void
13217 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13218                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
13219 {
13220         struct lpfc_mbx_mq_create *mq_create;
13221         struct lpfc_dmabuf *dmabuf;
13222         int length;
13223
13224         length = (sizeof(struct lpfc_mbx_mq_create) -
13225                   sizeof(struct lpfc_sli4_cfg_mhdr));
13226         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13227                          LPFC_MBOX_OPCODE_MQ_CREATE,
13228                          length, LPFC_SLI4_MBX_EMBED);
13229         mq_create = &mbox->u.mqe.un.mq_create;
13230         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
13231                mq->page_count);
13232         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
13233                cq->queue_id);
13234         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13235         switch (mq->entry_count) {
13236         case 16:
13237                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13238                        LPFC_MQ_RING_SIZE_16);
13239                 break;
13240         case 32:
13241                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13242                        LPFC_MQ_RING_SIZE_32);
13243                 break;
13244         case 64:
13245                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13246                        LPFC_MQ_RING_SIZE_64);
13247                 break;
13248         case 128:
13249                 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13250                        LPFC_MQ_RING_SIZE_128);
13251                 break;
13252         }
13253         list_for_each_entry(dmabuf, &mq->page_list, list) {
13254                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13255                         putPaddrLow(dmabuf->phys);
13256                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13257                         putPaddrHigh(dmabuf->phys);
13258         }
13259 }
13260
13261 /**
13262  * lpfc_mq_create - Create a mailbox Queue on the HBA
13263  * @phba: HBA structure that indicates port to create a queue on.
13264  * @mq: The queue structure to use to create the mailbox queue.
13265  * @cq: The completion queue to associate with this cq.
13266  * @subtype: The queue's subtype.
13267  *
13268  * This function creates a mailbox queue, as detailed in @mq, on a port,
13269  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13270  *
13271  * The @phba struct is used to send mailbox command to HBA. The @cq struct
13272  * is used to get the entry count and entry size that are necessary to
13273  * determine the number of pages to allocate and use for this queue. This
13274  * function will send the MQ_CREATE mailbox command to the HBA to setup the
13275  * mailbox queue. This function is asynchronous and will wait for the mailbox
13276  * command to finish before continuing.
13277  *
13278  * On success this function will return a zero. If unable to allocate enough
13279  * memory this function will return -ENOMEM. If the queue create mailbox command
13280  * fails this function will return -ENXIO.
13281  **/
13282 int32_t
13283 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13284                struct lpfc_queue *cq, uint32_t subtype)
13285 {
13286         struct lpfc_mbx_mq_create *mq_create;
13287         struct lpfc_mbx_mq_create_ext *mq_create_ext;
13288         struct lpfc_dmabuf *dmabuf;
13289         LPFC_MBOXQ_t *mbox;
13290         int rc, length, status = 0;
13291         uint32_t shdr_status, shdr_add_status;
13292         union lpfc_sli4_cfg_shdr *shdr;
13293         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13294
13295         /* sanity check on queue memory */
13296         if (!mq || !cq)
13297                 return -ENODEV;
13298         if (!phba->sli4_hba.pc_sli4_params.supported)
13299                 hw_page_size = SLI4_PAGE_SIZE;
13300
13301         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13302         if (!mbox)
13303                 return -ENOMEM;
13304         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13305                   sizeof(struct lpfc_sli4_cfg_mhdr));
13306         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13307                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13308                          length, LPFC_SLI4_MBX_EMBED);
13309
13310         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
13311         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
13312         bf_set(lpfc_mbx_mq_create_ext_num_pages,
13313                &mq_create_ext->u.request, mq->page_count);
13314         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13315                &mq_create_ext->u.request, 1);
13316         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
13317                &mq_create_ext->u.request, 1);
13318         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13319                &mq_create_ext->u.request, 1);
13320         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13321                &mq_create_ext->u.request, 1);
13322         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13323                &mq_create_ext->u.request, 1);
13324         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
13325         bf_set(lpfc_mbox_hdr_version, &shdr->request,
13326                phba->sli4_hba.pc_sli4_params.mqv);
13327         if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13328                 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13329                        cq->queue_id);
13330         else
13331                 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13332                        cq->queue_id);
13333         switch (mq->entry_count) {
13334         default:
13335                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13336                                 "0362 Unsupported MQ count. (%d)\n",
13337                                 mq->entry_count);
13338                 if (mq->entry_count < 16) {
13339                         status = -EINVAL;
13340                         goto out;
13341                 }
13342                 /* otherwise default to smallest count (drop through) */
13343         case 16:
13344                 bf_set(lpfc_mq_context_ring_size,
13345                        &mq_create_ext->u.request.context,
13346                        LPFC_MQ_RING_SIZE_16);
13347                 break;
13348         case 32:
13349                 bf_set(lpfc_mq_context_ring_size,
13350                        &mq_create_ext->u.request.context,
13351                        LPFC_MQ_RING_SIZE_32);
13352                 break;
13353         case 64:
13354                 bf_set(lpfc_mq_context_ring_size,
13355                        &mq_create_ext->u.request.context,
13356                        LPFC_MQ_RING_SIZE_64);
13357                 break;
13358         case 128:
13359                 bf_set(lpfc_mq_context_ring_size,
13360                        &mq_create_ext->u.request.context,
13361                        LPFC_MQ_RING_SIZE_128);
13362                 break;
13363         }
13364         list_for_each_entry(dmabuf, &mq->page_list, list) {
13365                 memset(dmabuf->virt, 0, hw_page_size);
13366                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13367                                         putPaddrLow(dmabuf->phys);
13368                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
13369                                         putPaddrHigh(dmabuf->phys);
13370         }
13371         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13372         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13373                               &mq_create_ext->u.response);
13374         if (rc != MBX_SUCCESS) {
13375                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13376                                 "2795 MQ_CREATE_EXT failed with "
13377                                 "status x%x. Failback to MQ_CREATE.\n",
13378                                 rc);
13379                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13380                 mq_create = &mbox->u.mqe.un.mq_create;
13381                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13382                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13383                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13384                                       &mq_create->u.response);
13385         }
13386
13387         /* The IOCTL status is embedded in the mailbox subheader. */
13388         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13389         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13390         if (shdr_status || shdr_add_status || rc) {
13391                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13392                                 "2502 MQ_CREATE mailbox failed with "
13393                                 "status x%x add_status x%x, mbx status x%x\n",
13394                                 shdr_status, shdr_add_status, rc);
13395                 status = -ENXIO;
13396                 goto out;
13397         }
13398         if (mq->queue_id == 0xFFFF) {
13399                 status = -ENXIO;
13400                 goto out;
13401         }
13402         mq->type = LPFC_MQ;
13403         mq->assoc_qid = cq->queue_id;
13404         mq->subtype = subtype;
13405         mq->host_index = 0;
13406         mq->hba_index = 0;
13407
13408         /* link the mq onto the parent cq child list */
13409         list_add_tail(&mq->list, &cq->child_list);
13410 out:
13411         mempool_free(mbox, phba->mbox_mem_pool);
13412         return status;
13413 }
13414
13415 /**
13416  * lpfc_wq_create - Create a Work Queue on the HBA
13417  * @phba: HBA structure that indicates port to create a queue on.
13418  * @wq: The queue structure to use to create the work queue.
13419  * @cq: The completion queue to bind this work queue to.
13420  * @subtype: The subtype of the work queue indicating its functionality.
13421  *
13422  * This function creates a work queue, as detailed in @wq, on a port, described
13423  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13424  *
13425  * The @phba struct is used to send mailbox command to HBA. The @wq struct
13426  * is used to get the entry count and entry size that are necessary to
13427  * determine the number of pages to allocate and use for this queue. The @cq
13428  * is used to indicate which completion queue to bind this work queue to. This
13429  * function will send the WQ_CREATE mailbox command to the HBA to setup the
13430  * work queue. This function is asynchronous and will wait for the mailbox
13431  * command to finish before continuing.
13432  *
13433  * On success this function will return a zero. If unable to allocate enough
13434  * memory this function will return -ENOMEM. If the queue create mailbox command
13435  * fails this function will return -ENXIO.
13436  **/
13437 int
13438 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13439                struct lpfc_queue *cq, uint32_t subtype)
13440 {
13441         struct lpfc_mbx_wq_create *wq_create;
13442         struct lpfc_dmabuf *dmabuf;
13443         LPFC_MBOXQ_t *mbox;
13444         int rc, length, status = 0;
13445         uint32_t shdr_status, shdr_add_status;
13446         union lpfc_sli4_cfg_shdr *shdr;
13447         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13448         struct dma_address *page;
13449         void __iomem *bar_memmap_p;
13450         uint32_t db_offset;
13451         uint16_t pci_barset;
13452
13453         /* sanity check on queue memory */
13454         if (!wq || !cq)
13455                 return -ENODEV;
13456         if (!phba->sli4_hba.pc_sli4_params.supported)
13457                 hw_page_size = SLI4_PAGE_SIZE;
13458
13459         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13460         if (!mbox)
13461                 return -ENOMEM;
13462         length = (sizeof(struct lpfc_mbx_wq_create) -
13463                   sizeof(struct lpfc_sli4_cfg_mhdr));
13464         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13465                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13466                          length, LPFC_SLI4_MBX_EMBED);
13467         wq_create = &mbox->u.mqe.un.wq_create;
13468         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
13469         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13470                     wq->page_count);
13471         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13472                     cq->queue_id);
13473
13474         /* wqv is the earliest version supported, NOT the latest */
13475         bf_set(lpfc_mbox_hdr_version, &shdr->request,
13476                phba->sli4_hba.pc_sli4_params.wqv);
13477
13478         switch (phba->sli4_hba.pc_sli4_params.wqv) {
13479         case LPFC_Q_CREATE_VERSION_0:
13480                 switch (wq->entry_size) {
13481                 default:
13482                 case 64:
13483                         /* Nothing to do, version 0 ONLY supports 64 byte */
13484                         page = wq_create->u.request.page;
13485                         break;
13486                 case 128:
13487                         if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13488                             LPFC_WQ_SZ128_SUPPORT)) {
13489                                 status = -ERANGE;
13490                                 goto out;
13491                         }
13492                         /* If we get here the HBA MUST also support V1 and
13493                          * we MUST use it
13494                          */
13495                         bf_set(lpfc_mbox_hdr_version, &shdr->request,
13496                                LPFC_Q_CREATE_VERSION_1);
13497
13498                         bf_set(lpfc_mbx_wq_create_wqe_count,
13499                                &wq_create->u.request_1, wq->entry_count);
13500                         bf_set(lpfc_mbx_wq_create_wqe_size,
13501                                &wq_create->u.request_1,
13502                                LPFC_WQ_WQE_SIZE_128);
13503                         bf_set(lpfc_mbx_wq_create_page_size,
13504                                &wq_create->u.request_1,
13505                                LPFC_WQ_PAGE_SIZE_4096);
13506                         page = wq_create->u.request_1.page;
13507                         break;
13508                 }
13509                 break;
13510         case LPFC_Q_CREATE_VERSION_1:
13511                 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13512                        wq->entry_count);
13513                 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13514                        LPFC_Q_CREATE_VERSION_1);
13515
13516                 switch (wq->entry_size) {
13517                 default:
13518                 case 64:
13519                         bf_set(lpfc_mbx_wq_create_wqe_size,
13520                                &wq_create->u.request_1,
13521                                LPFC_WQ_WQE_SIZE_64);
13522                         break;
13523                 case 128:
13524                         if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13525                                 LPFC_WQ_SZ128_SUPPORT)) {
13526                                 status = -ERANGE;
13527                                 goto out;
13528                         }
13529                         bf_set(lpfc_mbx_wq_create_wqe_size,
13530                                &wq_create->u.request_1,
13531                                LPFC_WQ_WQE_SIZE_128);
13532                         break;
13533                 }
13534                 bf_set(lpfc_mbx_wq_create_page_size,
13535                        &wq_create->u.request_1,
13536                        LPFC_WQ_PAGE_SIZE_4096);
13537                 page = wq_create->u.request_1.page;
13538                 break;
13539         default:
13540                 status = -ERANGE;
13541                 goto out;
13542         }
13543
13544         list_for_each_entry(dmabuf, &wq->page_list, list) {
13545                 memset(dmabuf->virt, 0, hw_page_size);
13546                 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13547                 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
13548         }
13549
13550         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13551                 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13552
13553         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13554         /* The IOCTL status is embedded in the mailbox subheader. */
13555         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13556         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13557         if (shdr_status || shdr_add_status || rc) {
13558                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13559                                 "2503 WQ_CREATE mailbox failed with "
13560                                 "status x%x add_status x%x, mbx status x%x\n",
13561                                 shdr_status, shdr_add_status, rc);
13562                 status = -ENXIO;
13563                 goto out;
13564         }
13565         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13566         if (wq->queue_id == 0xFFFF) {
13567                 status = -ENXIO;
13568                 goto out;
13569         }
13570         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13571                 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13572                                        &wq_create->u.response);
13573                 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13574                     (wq->db_format != LPFC_DB_RING_FORMAT)) {
13575                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13576                                         "3265 WQ[%d] doorbell format not "
13577                                         "supported: x%x\n", wq->queue_id,
13578                                         wq->db_format);
13579                         status = -EINVAL;
13580                         goto out;
13581                 }
13582                 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13583                                     &wq_create->u.response);
13584                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13585                 if (!bar_memmap_p) {
13586                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13587                                         "3263 WQ[%d] failed to memmap pci "
13588                                         "barset:x%x\n", wq->queue_id,
13589                                         pci_barset);
13590                         status = -ENOMEM;
13591                         goto out;
13592                 }
13593                 db_offset = wq_create->u.response.doorbell_offset;
13594                 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13595                     (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13596                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13597                                         "3252 WQ[%d] doorbell offset not "
13598                                         "supported: x%x\n", wq->queue_id,
13599                                         db_offset);
13600                         status = -EINVAL;
13601                         goto out;
13602                 }
13603                 wq->db_regaddr = bar_memmap_p + db_offset;
13604                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13605                                 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13606                                 "format:x%x\n", wq->queue_id, pci_barset,
13607                                 db_offset, wq->db_format);
13608         } else {
13609                 wq->db_format = LPFC_DB_LIST_FORMAT;
13610                 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13611         }
13612         wq->type = LPFC_WQ;
13613         wq->assoc_qid = cq->queue_id;
13614         wq->subtype = subtype;
13615         wq->host_index = 0;
13616         wq->hba_index = 0;
13617         wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
13618
13619         /* link the wq onto the parent cq child list */
13620         list_add_tail(&wq->list, &cq->child_list);
13621 out:
13622         mempool_free(mbox, phba->mbox_mem_pool);
13623         return status;
13624 }
13625
13626 /**
13627  * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13628  * @phba: HBA structure that indicates port to create a queue on.
13629  * @rq:   The queue structure to use for the receive queue.
13630  * @qno:  The associated HBQ number
13631  *
13632  *
13633  * For SLI4 we need to adjust the RQ repost value based on
13634  * the number of buffers that are initially posted to the RQ.
13635  */
13636 void
13637 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13638 {
13639         uint32_t cnt;
13640
13641         /* sanity check on queue memory */
13642         if (!rq)
13643                 return;
13644         cnt = lpfc_hbq_defs[qno]->entry_count;
13645
13646         /* Recalc repost for RQs based on buffers initially posted */
13647         cnt = (cnt >> 3);
13648         if (cnt < LPFC_QUEUE_MIN_REPOST)
13649                 cnt = LPFC_QUEUE_MIN_REPOST;
13650
13651         rq->entry_repost = cnt;
13652 }
13653
13654 /**
13655  * lpfc_rq_create - Create a Receive Queue on the HBA
13656  * @phba: HBA structure that indicates port to create a queue on.
13657  * @hrq: The queue structure to use to create the header receive queue.
13658  * @drq: The queue structure to use to create the data receive queue.
13659  * @cq: The completion queue to bind this work queue to.
13660  *
13661  * This function creates a receive buffer queue pair , as detailed in @hrq and
13662  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13663  * to the HBA.
13664  *
13665  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13666  * struct is used to get the entry count that is necessary to determine the
13667  * number of pages to use for this queue. The @cq is used to indicate which
13668  * completion queue to bind received buffers that are posted to these queues to.
13669  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13670  * receive queue pair. This function is asynchronous and will wait for the
13671  * mailbox command to finish before continuing.
13672  *
13673  * On success this function will return a zero. If unable to allocate enough
13674  * memory this function will return -ENOMEM. If the queue create mailbox command
13675  * fails this function will return -ENXIO.
13676  **/
13677 int
13678 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13679                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13680 {
13681         struct lpfc_mbx_rq_create *rq_create;
13682         struct lpfc_dmabuf *dmabuf;
13683         LPFC_MBOXQ_t *mbox;
13684         int rc, length, status = 0;
13685         uint32_t shdr_status, shdr_add_status;
13686         union lpfc_sli4_cfg_shdr *shdr;
13687         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13688         void __iomem *bar_memmap_p;
13689         uint32_t db_offset;
13690         uint16_t pci_barset;
13691
13692         /* sanity check on queue memory */
13693         if (!hrq || !drq || !cq)
13694                 return -ENODEV;
13695         if (!phba->sli4_hba.pc_sli4_params.supported)
13696                 hw_page_size = SLI4_PAGE_SIZE;
13697
13698         if (hrq->entry_count != drq->entry_count)
13699                 return -EINVAL;
13700         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13701         if (!mbox)
13702                 return -ENOMEM;
13703         length = (sizeof(struct lpfc_mbx_rq_create) -
13704                   sizeof(struct lpfc_sli4_cfg_mhdr));
13705         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13706                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13707                          length, LPFC_SLI4_MBX_EMBED);
13708         rq_create = &mbox->u.mqe.un.rq_create;
13709         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13710         bf_set(lpfc_mbox_hdr_version, &shdr->request,
13711                phba->sli4_hba.pc_sli4_params.rqv);
13712         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13713                 bf_set(lpfc_rq_context_rqe_count_1,
13714                        &rq_create->u.request.context,
13715                        hrq->entry_count);
13716                 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
13717                 bf_set(lpfc_rq_context_rqe_size,
13718                        &rq_create->u.request.context,
13719                        LPFC_RQE_SIZE_8);
13720                 bf_set(lpfc_rq_context_page_size,
13721                        &rq_create->u.request.context,
13722                        LPFC_RQ_PAGE_SIZE_4096);
13723         } else {
13724                 switch (hrq->entry_count) {
13725                 default:
13726                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13727                                         "2535 Unsupported RQ count. (%d)\n",
13728                                         hrq->entry_count);
13729                         if (hrq->entry_count < 512) {
13730                                 status = -EINVAL;
13731                                 goto out;
13732                         }
13733                         /* otherwise default to smallest count (drop through) */
13734                 case 512:
13735                         bf_set(lpfc_rq_context_rqe_count,
13736                                &rq_create->u.request.context,
13737                                LPFC_RQ_RING_SIZE_512);
13738                         break;
13739                 case 1024:
13740                         bf_set(lpfc_rq_context_rqe_count,
13741                                &rq_create->u.request.context,
13742                                LPFC_RQ_RING_SIZE_1024);
13743                         break;
13744                 case 2048:
13745                         bf_set(lpfc_rq_context_rqe_count,
13746                                &rq_create->u.request.context,
13747                                LPFC_RQ_RING_SIZE_2048);
13748                         break;
13749                 case 4096:
13750                         bf_set(lpfc_rq_context_rqe_count,
13751                                &rq_create->u.request.context,
13752                                LPFC_RQ_RING_SIZE_4096);
13753                         break;
13754                 }
13755                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13756                        LPFC_HDR_BUF_SIZE);
13757         }
13758         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13759                cq->queue_id);
13760         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13761                hrq->page_count);
13762         list_for_each_entry(dmabuf, &hrq->page_list, list) {
13763                 memset(dmabuf->virt, 0, hw_page_size);
13764                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13765                                         putPaddrLow(dmabuf->phys);
13766                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13767                                         putPaddrHigh(dmabuf->phys);
13768         }
13769         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13770                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13771
13772         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13773         /* The IOCTL status is embedded in the mailbox subheader. */
13774         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13775         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13776         if (shdr_status || shdr_add_status || rc) {
13777                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13778                                 "2504 RQ_CREATE mailbox failed with "
13779                                 "status x%x add_status x%x, mbx status x%x\n",
13780                                 shdr_status, shdr_add_status, rc);
13781                 status = -ENXIO;
13782                 goto out;
13783         }
13784         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13785         if (hrq->queue_id == 0xFFFF) {
13786                 status = -ENXIO;
13787                 goto out;
13788         }
13789
13790         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13791                 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13792                                         &rq_create->u.response);
13793                 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13794                     (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13795                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13796                                         "3262 RQ [%d] doorbell format not "
13797                                         "supported: x%x\n", hrq->queue_id,
13798                                         hrq->db_format);
13799                         status = -EINVAL;
13800                         goto out;
13801                 }
13802
13803                 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13804                                     &rq_create->u.response);
13805                 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13806                 if (!bar_memmap_p) {
13807                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13808                                         "3269 RQ[%d] failed to memmap pci "
13809                                         "barset:x%x\n", hrq->queue_id,
13810                                         pci_barset);
13811                         status = -ENOMEM;
13812                         goto out;
13813                 }
13814
13815                 db_offset = rq_create->u.response.doorbell_offset;
13816                 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13817                     (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13818                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13819                                         "3270 RQ[%d] doorbell offset not "
13820                                         "supported: x%x\n", hrq->queue_id,
13821                                         db_offset);
13822                         status = -EINVAL;
13823                         goto out;
13824                 }
13825                 hrq->db_regaddr = bar_memmap_p + db_offset;
13826                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13827                                 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13828                                 "format:x%x\n", hrq->queue_id, pci_barset,
13829                                 db_offset, hrq->db_format);
13830         } else {
13831                 hrq->db_format = LPFC_DB_RING_FORMAT;
13832                 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13833         }
13834         hrq->type = LPFC_HRQ;
13835         hrq->assoc_qid = cq->queue_id;
13836         hrq->subtype = subtype;
13837         hrq->host_index = 0;
13838         hrq->hba_index = 0;
13839
13840         /* now create the data queue */
13841         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13842                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13843                          length, LPFC_SLI4_MBX_EMBED);
13844         bf_set(lpfc_mbox_hdr_version, &shdr->request,
13845                phba->sli4_hba.pc_sli4_params.rqv);
13846         if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13847                 bf_set(lpfc_rq_context_rqe_count_1,
13848                        &rq_create->u.request.context, hrq->entry_count);
13849                 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
13850                 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13851                        LPFC_RQE_SIZE_8);
13852                 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13853                        (PAGE_SIZE/SLI4_PAGE_SIZE));
13854         } else {
13855                 switch (drq->entry_count) {
13856                 default:
13857                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13858                                         "2536 Unsupported RQ count. (%d)\n",
13859                                         drq->entry_count);
13860                         if (drq->entry_count < 512) {
13861                                 status = -EINVAL;
13862                                 goto out;
13863                         }
13864                         /* otherwise default to smallest count (drop through) */
13865                 case 512:
13866                         bf_set(lpfc_rq_context_rqe_count,
13867                                &rq_create->u.request.context,
13868                                LPFC_RQ_RING_SIZE_512);
13869                         break;
13870                 case 1024:
13871                         bf_set(lpfc_rq_context_rqe_count,
13872                                &rq_create->u.request.context,
13873                                LPFC_RQ_RING_SIZE_1024);
13874                         break;
13875                 case 2048:
13876                         bf_set(lpfc_rq_context_rqe_count,
13877                                &rq_create->u.request.context,
13878                                LPFC_RQ_RING_SIZE_2048);
13879                         break;
13880                 case 4096:
13881                         bf_set(lpfc_rq_context_rqe_count,
13882                                &rq_create->u.request.context,
13883                                LPFC_RQ_RING_SIZE_4096);
13884                         break;
13885                 }
13886                 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13887                        LPFC_DATA_BUF_SIZE);
13888         }
13889         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13890                cq->queue_id);
13891         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13892                drq->page_count);
13893         list_for_each_entry(dmabuf, &drq->page_list, list) {
13894                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13895                                         putPaddrLow(dmabuf->phys);
13896                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13897                                         putPaddrHigh(dmabuf->phys);
13898         }
13899         if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13900                 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13901         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13902         /* The IOCTL status is embedded in the mailbox subheader. */
13903         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13904         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13905         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13906         if (shdr_status || shdr_add_status || rc) {
13907                 status = -ENXIO;
13908                 goto out;
13909         }
13910         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13911         if (drq->queue_id == 0xFFFF) {
13912                 status = -ENXIO;
13913                 goto out;
13914         }
13915         drq->type = LPFC_DRQ;
13916         drq->assoc_qid = cq->queue_id;
13917         drq->subtype = subtype;
13918         drq->host_index = 0;
13919         drq->hba_index = 0;
13920
13921         /* link the header and data RQs onto the parent cq child list */
13922         list_add_tail(&hrq->list, &cq->child_list);
13923         list_add_tail(&drq->list, &cq->child_list);
13924
13925 out:
13926         mempool_free(mbox, phba->mbox_mem_pool);
13927         return status;
13928 }
13929
13930 /**
13931  * lpfc_eq_destroy - Destroy an event Queue on the HBA
13932  * @eq: The queue structure associated with the queue to destroy.
13933  *
13934  * This function destroys a queue, as detailed in @eq by sending an mailbox
13935  * command, specific to the type of queue, to the HBA.
13936  *
13937  * The @eq struct is used to get the queue ID of the queue to destroy.
13938  *
13939  * On success this function will return a zero. If the queue destroy mailbox
13940  * command fails this function will return -ENXIO.
13941  **/
13942 int
13943 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13944 {
13945         LPFC_MBOXQ_t *mbox;
13946         int rc, length, status = 0;
13947         uint32_t shdr_status, shdr_add_status;
13948         union lpfc_sli4_cfg_shdr *shdr;
13949
13950         /* sanity check on queue memory */
13951         if (!eq)
13952                 return -ENODEV;
13953         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13954         if (!mbox)
13955                 return -ENOMEM;
13956         length = (sizeof(struct lpfc_mbx_eq_destroy) -
13957                   sizeof(struct lpfc_sli4_cfg_mhdr));
13958         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13959                          LPFC_MBOX_OPCODE_EQ_DESTROY,
13960                          length, LPFC_SLI4_MBX_EMBED);
13961         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13962                eq->queue_id);
13963         mbox->vport = eq->phba->pport;
13964         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13965
13966         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13967         /* The IOCTL status is embedded in the mailbox subheader. */
13968         shdr = (union lpfc_sli4_cfg_shdr *)
13969                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13970         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13971         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13972         if (shdr_status || shdr_add_status || rc) {
13973                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13974                                 "2505 EQ_DESTROY mailbox failed with "
13975                                 "status x%x add_status x%x, mbx status x%x\n",
13976                                 shdr_status, shdr_add_status, rc);
13977                 status = -ENXIO;
13978         }
13979
13980         /* Remove eq from any list */
13981         list_del_init(&eq->list);
13982         mempool_free(mbox, eq->phba->mbox_mem_pool);
13983         return status;
13984 }
13985
13986 /**
13987  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13988  * @cq: The queue structure associated with the queue to destroy.
13989  *
13990  * This function destroys a queue, as detailed in @cq by sending an mailbox
13991  * command, specific to the type of queue, to the HBA.
13992  *
13993  * The @cq struct is used to get the queue ID of the queue to destroy.
13994  *
13995  * On success this function will return a zero. If the queue destroy mailbox
13996  * command fails this function will return -ENXIO.
13997  **/
13998 int
13999 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14000 {
14001         LPFC_MBOXQ_t *mbox;
14002         int rc, length, status = 0;
14003         uint32_t shdr_status, shdr_add_status;
14004         union lpfc_sli4_cfg_shdr *shdr;
14005
14006         /* sanity check on queue memory */
14007         if (!cq)
14008                 return -ENODEV;
14009         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14010         if (!mbox)
14011                 return -ENOMEM;
14012         length = (sizeof(struct lpfc_mbx_cq_destroy) -
14013                   sizeof(struct lpfc_sli4_cfg_mhdr));
14014         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14015                          LPFC_MBOX_OPCODE_CQ_DESTROY,
14016                          length, LPFC_SLI4_MBX_EMBED);
14017         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14018                cq->queue_id);
14019         mbox->vport = cq->phba->pport;
14020         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14021         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14022         /* The IOCTL status is embedded in the mailbox subheader. */
14023         shdr = (union lpfc_sli4_cfg_shdr *)
14024                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14025         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14026         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14027         if (shdr_status || shdr_add_status || rc) {
14028                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14029                                 "2506 CQ_DESTROY mailbox failed with "
14030                                 "status x%x add_status x%x, mbx status x%x\n",
14031                                 shdr_status, shdr_add_status, rc);
14032                 status = -ENXIO;
14033         }
14034         /* Remove cq from any list */
14035         list_del_init(&cq->list);
14036         mempool_free(mbox, cq->phba->mbox_mem_pool);
14037         return status;
14038 }
14039
14040 /**
14041  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14042  * @qm: The queue structure associated with the queue to destroy.
14043  *
14044  * This function destroys a queue, as detailed in @mq by sending an mailbox
14045  * command, specific to the type of queue, to the HBA.
14046  *
14047  * The @mq struct is used to get the queue ID of the queue to destroy.
14048  *
14049  * On success this function will return a zero. If the queue destroy mailbox
14050  * command fails this function will return -ENXIO.
14051  **/
14052 int
14053 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14054 {
14055         LPFC_MBOXQ_t *mbox;
14056         int rc, length, status = 0;
14057         uint32_t shdr_status, shdr_add_status;
14058         union lpfc_sli4_cfg_shdr *shdr;
14059
14060         /* sanity check on queue memory */
14061         if (!mq)
14062                 return -ENODEV;
14063         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14064         if (!mbox)
14065                 return -ENOMEM;
14066         length = (sizeof(struct lpfc_mbx_mq_destroy) -
14067                   sizeof(struct lpfc_sli4_cfg_mhdr));
14068         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14069                          LPFC_MBOX_OPCODE_MQ_DESTROY,
14070                          length, LPFC_SLI4_MBX_EMBED);
14071         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14072                mq->queue_id);
14073         mbox->vport = mq->phba->pport;
14074         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14075         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14076         /* The IOCTL status is embedded in the mailbox subheader. */
14077         shdr = (union lpfc_sli4_cfg_shdr *)
14078                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14079         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14080         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14081         if (shdr_status || shdr_add_status || rc) {
14082                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14083                                 "2507 MQ_DESTROY mailbox failed with "
14084                                 "status x%x add_status x%x, mbx status x%x\n",
14085                                 shdr_status, shdr_add_status, rc);
14086                 status = -ENXIO;
14087         }
14088         /* Remove mq from any list */
14089         list_del_init(&mq->list);
14090         mempool_free(mbox, mq->phba->mbox_mem_pool);
14091         return status;
14092 }
14093
14094 /**
14095  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14096  * @wq: The queue structure associated with the queue to destroy.
14097  *
14098  * This function destroys a queue, as detailed in @wq by sending an mailbox
14099  * command, specific to the type of queue, to the HBA.
14100  *
14101  * The @wq struct is used to get the queue ID of the queue to destroy.
14102  *
14103  * On success this function will return a zero. If the queue destroy mailbox
14104  * command fails this function will return -ENXIO.
14105  **/
14106 int
14107 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14108 {
14109         LPFC_MBOXQ_t *mbox;
14110         int rc, length, status = 0;
14111         uint32_t shdr_status, shdr_add_status;
14112         union lpfc_sli4_cfg_shdr *shdr;
14113
14114         /* sanity check on queue memory */
14115         if (!wq)
14116                 return -ENODEV;
14117         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14118         if (!mbox)
14119                 return -ENOMEM;
14120         length = (sizeof(struct lpfc_mbx_wq_destroy) -
14121                   sizeof(struct lpfc_sli4_cfg_mhdr));
14122         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14123                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14124                          length, LPFC_SLI4_MBX_EMBED);
14125         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14126                wq->queue_id);
14127         mbox->vport = wq->phba->pport;
14128         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14129         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14130         shdr = (union lpfc_sli4_cfg_shdr *)
14131                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14132         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14133         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14134         if (shdr_status || shdr_add_status || rc) {
14135                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14136                                 "2508 WQ_DESTROY mailbox failed with "
14137                                 "status x%x add_status x%x, mbx status x%x\n",
14138                                 shdr_status, shdr_add_status, rc);
14139                 status = -ENXIO;
14140         }
14141         /* Remove wq from any list */
14142         list_del_init(&wq->list);
14143         mempool_free(mbox, wq->phba->mbox_mem_pool);
14144         return status;
14145 }
14146
14147 /**
14148  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14149  * @rq: The queue structure associated with the queue to destroy.
14150  *
14151  * This function destroys a queue, as detailed in @rq by sending an mailbox
14152  * command, specific to the type of queue, to the HBA.
14153  *
14154  * The @rq struct is used to get the queue ID of the queue to destroy.
14155  *
14156  * On success this function will return a zero. If the queue destroy mailbox
14157  * command fails this function will return -ENXIO.
14158  **/
14159 int
14160 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14161                 struct lpfc_queue *drq)
14162 {
14163         LPFC_MBOXQ_t *mbox;
14164         int rc, length, status = 0;
14165         uint32_t shdr_status, shdr_add_status;
14166         union lpfc_sli4_cfg_shdr *shdr;
14167
14168         /* sanity check on queue memory */
14169         if (!hrq || !drq)
14170                 return -ENODEV;
14171         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14172         if (!mbox)
14173                 return -ENOMEM;
14174         length = (sizeof(struct lpfc_mbx_rq_destroy) -
14175                   sizeof(struct lpfc_sli4_cfg_mhdr));
14176         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14177                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14178                          length, LPFC_SLI4_MBX_EMBED);
14179         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14180                hrq->queue_id);
14181         mbox->vport = hrq->phba->pport;
14182         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14183         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14184         /* The IOCTL status is embedded in the mailbox subheader. */
14185         shdr = (union lpfc_sli4_cfg_shdr *)
14186                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14187         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14188         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14189         if (shdr_status || shdr_add_status || rc) {
14190                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14191                                 "2509 RQ_DESTROY mailbox failed with "
14192                                 "status x%x add_status x%x, mbx status x%x\n",
14193                                 shdr_status, shdr_add_status, rc);
14194                 if (rc != MBX_TIMEOUT)
14195                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
14196                 return -ENXIO;
14197         }
14198         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14199                drq->queue_id);
14200         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14201         shdr = (union lpfc_sli4_cfg_shdr *)
14202                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14203         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14204         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14205         if (shdr_status || shdr_add_status || rc) {
14206                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14207                                 "2510 RQ_DESTROY mailbox failed with "
14208                                 "status x%x add_status x%x, mbx status x%x\n",
14209                                 shdr_status, shdr_add_status, rc);
14210                 status = -ENXIO;
14211         }
14212         list_del_init(&hrq->list);
14213         list_del_init(&drq->list);
14214         mempool_free(mbox, hrq->phba->mbox_mem_pool);
14215         return status;
14216 }
14217
14218 /**
14219  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14220  * @phba: The virtual port for which this call being executed.
14221  * @pdma_phys_addr0: Physical address of the 1st SGL page.
14222  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14223  * @xritag: the xritag that ties this io to the SGL pages.
14224  *
14225  * This routine will post the sgl pages for the IO that has the xritag
14226  * that is in the iocbq structure. The xritag is assigned during iocbq
14227  * creation and persists for as long as the driver is loaded.
14228  * if the caller has fewer than 256 scatter gather segments to map then
14229  * pdma_phys_addr1 should be 0.
14230  * If the caller needs to map more than 256 scatter gather segment then
14231  * pdma_phys_addr1 should be a valid physical address.
14232  * physical address for SGLs must be 64 byte aligned.
14233  * If you are going to map 2 SGL's then the first one must have 256 entries
14234  * the second sgl can have between 1 and 256 entries.
14235  *
14236  * Return codes:
14237  *      0 - Success
14238  *      -ENXIO, -ENOMEM - Failure
14239  **/
14240 int
14241 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14242                 dma_addr_t pdma_phys_addr0,
14243                 dma_addr_t pdma_phys_addr1,
14244                 uint16_t xritag)
14245 {
14246         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14247         LPFC_MBOXQ_t *mbox;
14248         int rc;
14249         uint32_t shdr_status, shdr_add_status;
14250         uint32_t mbox_tmo;
14251         union lpfc_sli4_cfg_shdr *shdr;
14252
14253         if (xritag == NO_XRI) {
14254                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14255                                 "0364 Invalid param:\n");
14256                 return -EINVAL;
14257         }
14258
14259         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14260         if (!mbox)
14261                 return -ENOMEM;
14262
14263         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14264                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14265                         sizeof(struct lpfc_mbx_post_sgl_pages) -
14266                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
14267
14268         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14269                                 &mbox->u.mqe.un.post_sgl_pages;
14270         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14271         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14272
14273         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14274                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14275         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14276                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14277
14278         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14279                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14280         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14281                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14282         if (!phba->sli4_hba.intr_enable)
14283                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14284         else {
14285                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14286                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14287         }
14288         /* The IOCTL status is embedded in the mailbox subheader. */
14289         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14290         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14291         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14292         if (rc != MBX_TIMEOUT)
14293                 mempool_free(mbox, phba->mbox_mem_pool);
14294         if (shdr_status || shdr_add_status || rc) {
14295                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14296                                 "2511 POST_SGL mailbox failed with "
14297                                 "status x%x add_status x%x, mbx status x%x\n",
14298                                 shdr_status, shdr_add_status, rc);
14299         }
14300         return 0;
14301 }
14302
14303 /**
14304  * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
14305  * @phba: pointer to lpfc hba data structure.
14306  *
14307  * This routine is invoked to post rpi header templates to the
14308  * HBA consistent with the SLI-4 interface spec.  This routine
14309  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14310  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14311  *
14312  * Returns
14313  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14314  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
14315  **/
14316 static uint16_t
14317 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14318 {
14319         unsigned long xri;
14320
14321         /*
14322          * Fetch the next logical xri.  Because this index is logical,
14323          * the driver starts at 0 each time.
14324          */
14325         spin_lock_irq(&phba->hbalock);
14326         xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14327                                  phba->sli4_hba.max_cfg_param.max_xri, 0);
14328         if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14329                 spin_unlock_irq(&phba->hbalock);
14330                 return NO_XRI;
14331         } else {
14332                 set_bit(xri, phba->sli4_hba.xri_bmask);
14333                 phba->sli4_hba.max_cfg_param.xri_used++;
14334         }
14335         spin_unlock_irq(&phba->hbalock);
14336         return xri;
14337 }
14338
14339 /**
14340  * lpfc_sli4_free_xri - Release an xri for reuse.
14341  * @phba: pointer to lpfc hba data structure.
14342  *
14343  * This routine is invoked to release an xri to the pool of
14344  * available rpis maintained by the driver.
14345  **/
14346 static void
14347 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14348 {
14349         if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
14350                 phba->sli4_hba.max_cfg_param.xri_used--;
14351         }
14352 }
14353
14354 /**
14355  * lpfc_sli4_free_xri - Release an xri for reuse.
14356  * @phba: pointer to lpfc hba data structure.
14357  *
14358  * This routine is invoked to release an xri to the pool of
14359  * available rpis maintained by the driver.
14360  **/
14361 void
14362 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14363 {
14364         spin_lock_irq(&phba->hbalock);
14365         __lpfc_sli4_free_xri(phba, xri);
14366         spin_unlock_irq(&phba->hbalock);
14367 }
14368
14369 /**
14370  * lpfc_sli4_next_xritag - Get an xritag for the io
14371  * @phba: Pointer to HBA context object.
14372  *
14373  * This function gets an xritag for the iocb. If there is no unused xritag
14374  * it will return 0xffff.
14375  * The function returns the allocated xritag if successful, else returns zero.
14376  * Zero is not a valid xritag.
14377  * The caller is not required to hold any lock.
14378  **/
14379 uint16_t
14380 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14381 {
14382         uint16_t xri_index;
14383
14384         xri_index = lpfc_sli4_alloc_xri(phba);
14385         if (xri_index == NO_XRI)
14386                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14387                                 "2004 Failed to allocate XRI.last XRITAG is %d"
14388                                 " Max XRI is %d, Used XRI is %d\n",
14389                                 xri_index,
14390                                 phba->sli4_hba.max_cfg_param.max_xri,
14391                                 phba->sli4_hba.max_cfg_param.xri_used);
14392         return xri_index;
14393 }
14394
14395 /**
14396  * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
14397  * @phba: pointer to lpfc hba data structure.
14398  * @post_sgl_list: pointer to els sgl entry list.
14399  * @count: number of els sgl entries on the list.
14400  *
14401  * This routine is invoked to post a block of driver's sgl pages to the
14402  * HBA using non-embedded mailbox command. No Lock is held. This routine
14403  * is only called when the driver is loading and after all IO has been
14404  * stopped.
14405  **/
14406 static int
14407 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14408                             struct list_head *post_sgl_list,
14409                             int post_cnt)
14410 {
14411         struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
14412         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14413         struct sgl_page_pairs *sgl_pg_pairs;
14414         void *viraddr;
14415         LPFC_MBOXQ_t *mbox;
14416         uint32_t reqlen, alloclen, pg_pairs;
14417         uint32_t mbox_tmo;
14418         uint16_t xritag_start = 0;
14419         int rc = 0;
14420         uint32_t shdr_status, shdr_add_status;
14421         union lpfc_sli4_cfg_shdr *shdr;
14422
14423         reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
14424                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
14425         if (reqlen > SLI4_PAGE_SIZE) {
14426                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14427                                 "2559 Block sgl registration required DMA "
14428                                 "size (%d) great than a page\n", reqlen);
14429                 return -ENOMEM;
14430         }
14431         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14432         if (!mbox)
14433                 return -ENOMEM;
14434
14435         /* Allocate DMA memory and set up the non-embedded mailbox command */
14436         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14437                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14438                          LPFC_SLI4_MBX_NEMBED);
14439
14440         if (alloclen < reqlen) {
14441                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14442                                 "0285 Allocated DMA memory size (%d) is "
14443                                 "less than the requested DMA memory "
14444                                 "size (%d)\n", alloclen, reqlen);
14445                 lpfc_sli4_mbox_cmd_free(phba, mbox);
14446                 return -ENOMEM;
14447         }
14448         /* Set up the SGL pages in the non-embedded DMA pages */
14449         viraddr = mbox->sge_array->addr[0];
14450         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14451         sgl_pg_pairs = &sgl->sgl_pg_pairs;
14452
14453         pg_pairs = 0;
14454         list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
14455                 /* Set up the sge entry */
14456                 sgl_pg_pairs->sgl_pg0_addr_lo =
14457                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14458                 sgl_pg_pairs->sgl_pg0_addr_hi =
14459                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14460                 sgl_pg_pairs->sgl_pg1_addr_lo =
14461                                 cpu_to_le32(putPaddrLow(0));
14462                 sgl_pg_pairs->sgl_pg1_addr_hi =
14463                                 cpu_to_le32(putPaddrHigh(0));
14464
14465                 /* Keep the first xritag on the list */
14466                 if (pg_pairs == 0)
14467                         xritag_start = sglq_entry->sli4_xritag;
14468                 sgl_pg_pairs++;
14469                 pg_pairs++;
14470         }
14471
14472         /* Complete initialization and perform endian conversion. */
14473         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14474         bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
14475         sgl->word0 = cpu_to_le32(sgl->word0);
14476         if (!phba->sli4_hba.intr_enable)
14477                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14478         else {
14479                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14480                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14481         }
14482         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14483         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14484         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14485         if (rc != MBX_TIMEOUT)
14486                 lpfc_sli4_mbox_cmd_free(phba, mbox);
14487         if (shdr_status || shdr_add_status || rc) {
14488                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14489                                 "2513 POST_SGL_BLOCK mailbox command failed "
14490                                 "status x%x add_status x%x mbx status x%x\n",
14491                                 shdr_status, shdr_add_status, rc);
14492                 rc = -ENXIO;
14493         }
14494         return rc;
14495 }
14496
14497 /**
14498  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14499  * @phba: pointer to lpfc hba data structure.
14500  * @sblist: pointer to scsi buffer list.
14501  * @count: number of scsi buffers on the list.
14502  *
14503  * This routine is invoked to post a block of @count scsi sgl pages from a
14504  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14505  * No Lock is held.
14506  *
14507  **/
14508 int
14509 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14510                               struct list_head *sblist,
14511                               int count)
14512 {
14513         struct lpfc_scsi_buf *psb;
14514         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14515         struct sgl_page_pairs *sgl_pg_pairs;
14516         void *viraddr;
14517         LPFC_MBOXQ_t *mbox;
14518         uint32_t reqlen, alloclen, pg_pairs;
14519         uint32_t mbox_tmo;
14520         uint16_t xritag_start = 0;
14521         int rc = 0;
14522         uint32_t shdr_status, shdr_add_status;
14523         dma_addr_t pdma_phys_bpl1;
14524         union lpfc_sli4_cfg_shdr *shdr;
14525
14526         /* Calculate the requested length of the dma memory */
14527         reqlen = count * sizeof(struct sgl_page_pairs) +
14528                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
14529         if (reqlen > SLI4_PAGE_SIZE) {
14530                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14531                                 "0217 Block sgl registration required DMA "
14532                                 "size (%d) great than a page\n", reqlen);
14533                 return -ENOMEM;
14534         }
14535         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14536         if (!mbox) {
14537                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14538                                 "0283 Failed to allocate mbox cmd memory\n");
14539                 return -ENOMEM;
14540         }
14541
14542         /* Allocate DMA memory and set up the non-embedded mailbox command */
14543         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14544                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14545                                 LPFC_SLI4_MBX_NEMBED);
14546
14547         if (alloclen < reqlen) {
14548                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14549                                 "2561 Allocated DMA memory size (%d) is "
14550                                 "less than the requested DMA memory "
14551                                 "size (%d)\n", alloclen, reqlen);
14552                 lpfc_sli4_mbox_cmd_free(phba, mbox);
14553                 return -ENOMEM;
14554         }
14555
14556         /* Get the first SGE entry from the non-embedded DMA memory */
14557         viraddr = mbox->sge_array->addr[0];
14558
14559         /* Set up the SGL pages in the non-embedded DMA pages */
14560         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14561         sgl_pg_pairs = &sgl->sgl_pg_pairs;
14562
14563         pg_pairs = 0;
14564         list_for_each_entry(psb, sblist, list) {
14565                 /* Set up the sge entry */
14566                 sgl_pg_pairs->sgl_pg0_addr_lo =
14567                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14568                 sgl_pg_pairs->sgl_pg0_addr_hi =
14569                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14570                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14571                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14572                 else
14573                         pdma_phys_bpl1 = 0;
14574                 sgl_pg_pairs->sgl_pg1_addr_lo =
14575                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14576                 sgl_pg_pairs->sgl_pg1_addr_hi =
14577                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14578                 /* Keep the first xritag on the list */
14579                 if (pg_pairs == 0)
14580                         xritag_start = psb->cur_iocbq.sli4_xritag;
14581                 sgl_pg_pairs++;
14582                 pg_pairs++;
14583         }
14584         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14585         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14586         /* Perform endian conversion if necessary */
14587         sgl->word0 = cpu_to_le32(sgl->word0);
14588
14589         if (!phba->sli4_hba.intr_enable)
14590                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14591         else {
14592                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14593                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14594         }
14595         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14596         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14597         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14598         if (rc != MBX_TIMEOUT)
14599                 lpfc_sli4_mbox_cmd_free(phba, mbox);
14600         if (shdr_status || shdr_add_status || rc) {
14601                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14602                                 "2564 POST_SGL_BLOCK mailbox command failed "
14603                                 "status x%x add_status x%x mbx status x%x\n",
14604                                 shdr_status, shdr_add_status, rc);
14605                 rc = -ENXIO;
14606         }
14607         return rc;
14608 }
14609
14610 /**
14611  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14612  * @phba: pointer to lpfc_hba struct that the frame was received on
14613  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14614  *
14615  * This function checks the fields in the @fc_hdr to see if the FC frame is a
14616  * valid type of frame that the LPFC driver will handle. This function will
14617  * return a zero if the frame is a valid frame or a non zero value when the
14618  * frame does not pass the check.
14619  **/
14620 static int
14621 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14622 {
14623         /*  make rctl_names static to save stack space */
14624         static char *rctl_names[] = FC_RCTL_NAMES_INIT;
14625         char *type_names[] = FC_TYPE_NAMES_INIT;
14626         struct fc_vft_header *fc_vft_hdr;
14627         uint32_t *header = (uint32_t *) fc_hdr;
14628
14629         switch (fc_hdr->fh_r_ctl) {
14630         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
14631         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
14632         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
14633         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
14634         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
14635         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
14636         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
14637         case FC_RCTL_DD_CMD_STATUS:     /* command status */
14638         case FC_RCTL_ELS_REQ:   /* extended link services request */
14639         case FC_RCTL_ELS_REP:   /* extended link services reply */
14640         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
14641         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
14642         case FC_RCTL_BA_NOP:    /* basic link service NOP */
14643         case FC_RCTL_BA_ABTS:   /* basic link service abort */
14644         case FC_RCTL_BA_RMC:    /* remove connection */
14645         case FC_RCTL_BA_ACC:    /* basic accept */
14646         case FC_RCTL_BA_RJT:    /* basic reject */
14647         case FC_RCTL_BA_PRMT:
14648         case FC_RCTL_ACK_1:     /* acknowledge_1 */
14649         case FC_RCTL_ACK_0:     /* acknowledge_0 */
14650         case FC_RCTL_P_RJT:     /* port reject */
14651         case FC_RCTL_F_RJT:     /* fabric reject */
14652         case FC_RCTL_P_BSY:     /* port busy */
14653         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
14654         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
14655         case FC_RCTL_LCR:       /* link credit reset */
14656         case FC_RCTL_END:       /* end */
14657                 break;
14658         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
14659                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14660                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14661                 return lpfc_fc_frame_check(phba, fc_hdr);
14662         default:
14663                 goto drop;
14664         }
14665         switch (fc_hdr->fh_type) {
14666         case FC_TYPE_BLS:
14667         case FC_TYPE_ELS:
14668         case FC_TYPE_FCP:
14669         case FC_TYPE_CT:
14670                 break;
14671         case FC_TYPE_IP:
14672         case FC_TYPE_ILS:
14673         default:
14674                 goto drop;
14675         }
14676
14677         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
14678                         "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14679                         "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14680                         rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14681                         type_names[fc_hdr->fh_type], fc_hdr->fh_type,
14682                         be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14683                         be32_to_cpu(header[2]), be32_to_cpu(header[3]),
14684                         be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14685                         be32_to_cpu(header[6]));
14686         return 0;
14687 drop:
14688         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14689                         "2539 Dropped frame rctl:%s type:%s\n",
14690                         rctl_names[fc_hdr->fh_r_ctl],
14691                         type_names[fc_hdr->fh_type]);
14692         return 1;
14693 }
14694
14695 /**
14696  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14697  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14698  *
14699  * This function processes the FC header to retrieve the VFI from the VF
14700  * header, if one exists. This function will return the VFI if one exists
14701  * or 0 if no VSAN Header exists.
14702  **/
14703 static uint32_t
14704 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14705 {
14706         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14707
14708         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14709                 return 0;
14710         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14711 }
14712
14713 /**
14714  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14715  * @phba: Pointer to the HBA structure to search for the vport on
14716  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14717  * @fcfi: The FC Fabric ID that the frame came from
14718  *
14719  * This function searches the @phba for a vport that matches the content of the
14720  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14721  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14722  * returns the matching vport pointer or NULL if unable to match frame to a
14723  * vport.
14724  **/
14725 static struct lpfc_vport *
14726 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14727                        uint16_t fcfi)
14728 {
14729         struct lpfc_vport **vports;
14730         struct lpfc_vport *vport = NULL;
14731         int i;
14732         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14733                         fc_hdr->fh_d_id[1] << 8 |
14734                         fc_hdr->fh_d_id[2]);
14735
14736         if (did == Fabric_DID)
14737                 return phba->pport;
14738         if ((phba->pport->fc_flag & FC_PT2PT) &&
14739                 !(phba->link_state == LPFC_HBA_READY))
14740                 return phba->pport;
14741
14742         vports = lpfc_create_vport_work_array(phba);
14743         if (vports != NULL)
14744                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14745                         if (phba->fcf.fcfi == fcfi &&
14746                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14747                             vports[i]->fc_myDID == did) {
14748                                 vport = vports[i];
14749                                 break;
14750                         }
14751                 }
14752         lpfc_destroy_vport_work_array(phba, vports);
14753         return vport;
14754 }
14755
14756 /**
14757  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14758  * @vport: The vport to work on.
14759  *
14760  * This function updates the receive sequence time stamp for this vport. The
14761  * receive sequence time stamp indicates the time that the last frame of the
14762  * the sequence that has been idle for the longest amount of time was received.
14763  * the driver uses this time stamp to indicate if any received sequences have
14764  * timed out.
14765  **/
14766 static void
14767 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14768 {
14769         struct lpfc_dmabuf *h_buf;
14770         struct hbq_dmabuf *dmabuf = NULL;
14771
14772         /* get the oldest sequence on the rcv list */
14773         h_buf = list_get_first(&vport->rcv_buffer_list,
14774                                struct lpfc_dmabuf, list);
14775         if (!h_buf)
14776                 return;
14777         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14778         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14779 }
14780
14781 /**
14782  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14783  * @vport: The vport that the received sequences were sent to.
14784  *
14785  * This function cleans up all outstanding received sequences. This is called
14786  * by the driver when a link event or user action invalidates all the received
14787  * sequences.
14788  **/
14789 void
14790 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14791 {
14792         struct lpfc_dmabuf *h_buf, *hnext;
14793         struct lpfc_dmabuf *d_buf, *dnext;
14794         struct hbq_dmabuf *dmabuf = NULL;
14795
14796         /* start with the oldest sequence on the rcv list */
14797         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14798                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14799                 list_del_init(&dmabuf->hbuf.list);
14800                 list_for_each_entry_safe(d_buf, dnext,
14801                                          &dmabuf->dbuf.list, list) {
14802                         list_del_init(&d_buf->list);
14803                         lpfc_in_buf_free(vport->phba, d_buf);
14804                 }
14805                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14806         }
14807 }
14808
14809 /**
14810  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14811  * @vport: The vport that the received sequences were sent to.
14812  *
14813  * This function determines whether any received sequences have timed out by
14814  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14815  * indicates that there is at least one timed out sequence this routine will
14816  * go through the received sequences one at a time from most inactive to most
14817  * active to determine which ones need to be cleaned up. Once it has determined
14818  * that a sequence needs to be cleaned up it will simply free up the resources
14819  * without sending an abort.
14820  **/
14821 void
14822 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14823 {
14824         struct lpfc_dmabuf *h_buf, *hnext;
14825         struct lpfc_dmabuf *d_buf, *dnext;
14826         struct hbq_dmabuf *dmabuf = NULL;
14827         unsigned long timeout;
14828         int abort_count = 0;
14829
14830         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14831                    vport->rcv_buffer_time_stamp);
14832         if (list_empty(&vport->rcv_buffer_list) ||
14833             time_before(jiffies, timeout))
14834                 return;
14835         /* start with the oldest sequence on the rcv list */
14836         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14837                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14838                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14839                            dmabuf->time_stamp);
14840                 if (time_before(jiffies, timeout))
14841                         break;
14842                 abort_count++;
14843                 list_del_init(&dmabuf->hbuf.list);
14844                 list_for_each_entry_safe(d_buf, dnext,
14845                                          &dmabuf->dbuf.list, list) {
14846                         list_del_init(&d_buf->list);
14847                         lpfc_in_buf_free(vport->phba, d_buf);
14848                 }
14849                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14850         }
14851         if (abort_count)
14852                 lpfc_update_rcv_time_stamp(vport);
14853 }
14854
14855 /**
14856  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14857  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14858  *
14859  * This function searches through the existing incomplete sequences that have
14860  * been sent to this @vport. If the frame matches one of the incomplete
14861  * sequences then the dbuf in the @dmabuf is added to the list of frames that
14862  * make up that sequence. If no sequence is found that matches this frame then
14863  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14864  * This function returns a pointer to the first dmabuf in the sequence list that
14865  * the frame was linked to.
14866  **/
14867 static struct hbq_dmabuf *
14868 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14869 {
14870         struct fc_frame_header *new_hdr;
14871         struct fc_frame_header *temp_hdr;
14872         struct lpfc_dmabuf *d_buf;
14873         struct lpfc_dmabuf *h_buf;
14874         struct hbq_dmabuf *seq_dmabuf = NULL;
14875         struct hbq_dmabuf *temp_dmabuf = NULL;
14876         uint8_t found = 0;
14877
14878         INIT_LIST_HEAD(&dmabuf->dbuf.list);
14879         dmabuf->time_stamp = jiffies;
14880         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14881
14882         /* Use the hdr_buf to find the sequence that this frame belongs to */
14883         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14884                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14885                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14886                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14887                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14888                         continue;
14889                 /* found a pending sequence that matches this frame */
14890                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14891                 break;
14892         }
14893         if (!seq_dmabuf) {
14894                 /*
14895                  * This indicates first frame received for this sequence.
14896                  * Queue the buffer on the vport's rcv_buffer_list.
14897                  */
14898                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14899                 lpfc_update_rcv_time_stamp(vport);
14900                 return dmabuf;
14901         }
14902         temp_hdr = seq_dmabuf->hbuf.virt;
14903         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14904                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14905                 list_del_init(&seq_dmabuf->hbuf.list);
14906                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14907                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14908                 lpfc_update_rcv_time_stamp(vport);
14909                 return dmabuf;
14910         }
14911         /* move this sequence to the tail to indicate a young sequence */
14912         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14913         seq_dmabuf->time_stamp = jiffies;
14914         lpfc_update_rcv_time_stamp(vport);
14915         if (list_empty(&seq_dmabuf->dbuf.list)) {
14916                 temp_hdr = dmabuf->hbuf.virt;
14917                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14918                 return seq_dmabuf;
14919         }
14920         /* find the correct place in the sequence to insert this frame */
14921         d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
14922         while (!found) {
14923                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14924                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14925                 /*
14926                  * If the frame's sequence count is greater than the frame on
14927                  * the list then insert the frame right after this frame
14928                  */
14929                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14930                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14931                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14932                         found = 1;
14933                         break;
14934                 }
14935
14936                 if (&d_buf->list == &seq_dmabuf->dbuf.list)
14937                         break;
14938                 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
14939         }
14940
14941         if (found)
14942                 return seq_dmabuf;
14943         return NULL;
14944 }
14945
14946 /**
14947  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14948  * @vport: pointer to a vitural port
14949  * @dmabuf: pointer to a dmabuf that describes the FC sequence
14950  *
14951  * This function tries to abort from the partially assembed sequence, described
14952  * by the information from basic abbort @dmabuf. It checks to see whether such
14953  * partially assembled sequence held by the driver. If so, it shall free up all
14954  * the frames from the partially assembled sequence.
14955  *
14956  * Return
14957  * true  -- if there is matching partially assembled sequence present and all
14958  *          the frames freed with the sequence;
14959  * false -- if there is no matching partially assembled sequence present so
14960  *          nothing got aborted in the lower layer driver
14961  **/
14962 static bool
14963 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14964                             struct hbq_dmabuf *dmabuf)
14965 {
14966         struct fc_frame_header *new_hdr;
14967         struct fc_frame_header *temp_hdr;
14968         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14969         struct hbq_dmabuf *seq_dmabuf = NULL;
14970
14971         /* Use the hdr_buf to find the sequence that matches this frame */
14972         INIT_LIST_HEAD(&dmabuf->dbuf.list);
14973         INIT_LIST_HEAD(&dmabuf->hbuf.list);
14974         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14975         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14976                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14977                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14978                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14979                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14980                         continue;
14981                 /* found a pending sequence that matches this frame */
14982                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14983                 break;
14984         }
14985
14986         /* Free up all the frames from the partially assembled sequence */
14987         if (seq_dmabuf) {
14988                 list_for_each_entry_safe(d_buf, n_buf,
14989                                          &seq_dmabuf->dbuf.list, list) {
14990                         list_del_init(&d_buf->list);
14991                         lpfc_in_buf_free(vport->phba, d_buf);
14992                 }
14993                 return true;
14994         }
14995         return false;
14996 }
14997
14998 /**
14999  * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15000  * @vport: pointer to a vitural port
15001  * @dmabuf: pointer to a dmabuf that describes the FC sequence
15002  *
15003  * This function tries to abort from the assembed sequence from upper level
15004  * protocol, described by the information from basic abbort @dmabuf. It
15005  * checks to see whether such pending context exists at upper level protocol.
15006  * If so, it shall clean up the pending context.
15007  *
15008  * Return
15009  * true  -- if there is matching pending context of the sequence cleaned
15010  *          at ulp;
15011  * false -- if there is no matching pending context of the sequence present
15012  *          at ulp.
15013  **/
15014 static bool
15015 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15016 {
15017         struct lpfc_hba *phba = vport->phba;
15018         int handled;
15019
15020         /* Accepting abort at ulp with SLI4 only */
15021         if (phba->sli_rev < LPFC_SLI_REV4)
15022                 return false;
15023
15024         /* Register all caring upper level protocols to attend abort */
15025         handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15026         if (handled)
15027                 return true;
15028
15029         return false;
15030 }
15031
15032 /**
15033  * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
15034  * @phba: Pointer to HBA context object.
15035  * @cmd_iocbq: pointer to the command iocbq structure.
15036  * @rsp_iocbq: pointer to the response iocbq structure.
15037  *
15038  * This function handles the sequence abort response iocb command complete
15039  * event. It properly releases the memory allocated to the sequence abort
15040  * accept iocb.
15041  **/
15042 static void
15043 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
15044                              struct lpfc_iocbq *cmd_iocbq,
15045                              struct lpfc_iocbq *rsp_iocbq)
15046 {
15047         struct lpfc_nodelist *ndlp;
15048
15049         if (cmd_iocbq) {
15050                 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15051                 lpfc_nlp_put(ndlp);
15052                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
15053         }
15054
15055         /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15056         if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15057                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15058                         "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
15059                         rsp_iocbq->iocb.ulpStatus,
15060                         rsp_iocbq->iocb.un.ulpWord[4]);
15061 }
15062
15063 /**
15064  * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15065  * @phba: Pointer to HBA context object.
15066  * @xri: xri id in transaction.
15067  *
15068  * This function validates the xri maps to the known range of XRIs allocated an
15069  * used by the driver.
15070  **/
15071 uint16_t
15072 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15073                       uint16_t xri)
15074 {
15075         uint16_t i;
15076
15077         for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15078                 if (xri == phba->sli4_hba.xri_ids[i])
15079                         return i;
15080         }
15081         return NO_XRI;
15082 }
15083
15084 /**
15085  * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
15086  * @phba: Pointer to HBA context object.
15087  * @fc_hdr: pointer to a FC frame header.
15088  *
15089  * This function sends a basic response to a previous unsol sequence abort
15090  * event after aborting the sequence handling.
15091  **/
15092 static void
15093 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15094                         struct fc_frame_header *fc_hdr, bool aborted)
15095 {
15096         struct lpfc_hba *phba = vport->phba;
15097         struct lpfc_iocbq *ctiocb = NULL;
15098         struct lpfc_nodelist *ndlp;
15099         uint16_t oxid, rxid, xri, lxri;
15100         uint32_t sid, fctl;
15101         IOCB_t *icmd;
15102         int rc;
15103
15104         if (!lpfc_is_link_up(phba))
15105                 return;
15106
15107         sid = sli4_sid_from_fc_hdr(fc_hdr);
15108         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
15109         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
15110
15111         ndlp = lpfc_findnode_did(vport, sid);
15112         if (!ndlp) {
15113                 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15114                 if (!ndlp) {
15115                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15116                                          "1268 Failed to allocate ndlp for "
15117                                          "oxid:x%x SID:x%x\n", oxid, sid);
15118                         return;
15119                 }
15120                 lpfc_nlp_init(vport, ndlp, sid);
15121                 /* Put ndlp onto pport node list */
15122                 lpfc_enqueue_node(vport, ndlp);
15123         } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15124                 /* re-setup ndlp without removing from node list */
15125                 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15126                 if (!ndlp) {
15127                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15128                                          "3275 Failed to active ndlp found "
15129                                          "for oxid:x%x SID:x%x\n", oxid, sid);
15130                         return;
15131                 }
15132         }
15133
15134         /* Allocate buffer for rsp iocb */
15135         ctiocb = lpfc_sli_get_iocbq(phba);
15136         if (!ctiocb)
15137                 return;
15138
15139         /* Extract the F_CTL field from FC_HDR */
15140         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15141
15142         icmd = &ctiocb->iocb;
15143         icmd->un.xseq64.bdl.bdeSize = 0;
15144         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
15145         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15146         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15147         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15148
15149         /* Fill in the rest of iocb fields */
15150         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15151         icmd->ulpBdeCount = 0;
15152         icmd->ulpLe = 1;
15153         icmd->ulpClass = CLASS3;
15154         icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
15155         ctiocb->context1 = lpfc_nlp_get(ndlp);
15156
15157         ctiocb->iocb_cmpl = NULL;
15158         ctiocb->vport = phba->pport;
15159         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
15160         ctiocb->sli4_lxritag = NO_XRI;
15161         ctiocb->sli4_xritag = NO_XRI;
15162
15163         if (fctl & FC_FC_EX_CTX)
15164                 /* Exchange responder sent the abort so we
15165                  * own the oxid.
15166                  */
15167                 xri = oxid;
15168         else
15169                 xri = rxid;
15170         lxri = lpfc_sli4_xri_inrange(phba, xri);
15171         if (lxri != NO_XRI)
15172                 lpfc_set_rrq_active(phba, ndlp, lxri,
15173                         (xri == oxid) ? rxid : oxid, 0);
15174         /* For BA_ABTS from exchange responder, if the logical xri with
15175          * the oxid maps to the FCP XRI range, the port no longer has
15176          * that exchange context, send a BLS_RJT. Override the IOCB for
15177          * a BA_RJT.
15178          */
15179         if ((fctl & FC_FC_EX_CTX) &&
15180             (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15181                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15182                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15183                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15184                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15185         }
15186
15187         /* If BA_ABTS failed to abort a partially assembled receive sequence,
15188          * the driver no longer has that exchange, send a BLS_RJT. Override
15189          * the IOCB for a BA_RJT.
15190          */
15191         if (aborted == false) {
15192                 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15193                 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15194                 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15195                 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15196         }
15197
15198         if (fctl & FC_FC_EX_CTX) {
15199                 /* ABTS sent by responder to CT exchange, construction
15200                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15201                  * field and RX_ID from ABTS for RX_ID field.
15202                  */
15203                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
15204         } else {
15205                 /* ABTS sent by initiator to CT exchange, construction
15206                  * of BA_ACC will need to allocate a new XRI as for the
15207                  * XRI_TAG field.
15208                  */
15209                 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
15210         }
15211         bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
15212         bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
15213
15214         /* Xmit CT abts response on exchange <xid> */
15215         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15216                          "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15217                          icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
15218
15219         rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15220         if (rc == IOCB_ERROR) {
15221                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15222                                  "2925 Failed to issue CT ABTS RSP x%x on "
15223                                  "xri x%x, Data x%x\n",
15224                                  icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15225                                  phba->link_state);
15226                 lpfc_nlp_put(ndlp);
15227                 ctiocb->context1 = NULL;
15228                 lpfc_sli_release_iocbq(phba, ctiocb);
15229         }
15230 }
15231
15232 /**
15233  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15234  * @vport: Pointer to the vport on which this sequence was received
15235  * @dmabuf: pointer to a dmabuf that describes the FC sequence
15236  *
15237  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15238  * receive sequence is only partially assembed by the driver, it shall abort
15239  * the partially assembled frames for the sequence. Otherwise, if the
15240  * unsolicited receive sequence has been completely assembled and passed to
15241  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15242  * unsolicited sequence has been aborted. After that, it will issue a basic
15243  * accept to accept the abort.
15244  **/
15245 static void
15246 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15247                              struct hbq_dmabuf *dmabuf)
15248 {
15249         struct lpfc_hba *phba = vport->phba;
15250         struct fc_frame_header fc_hdr;
15251         uint32_t fctl;
15252         bool aborted;
15253
15254         /* Make a copy of fc_hdr before the dmabuf being released */
15255         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
15256         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
15257
15258         if (fctl & FC_FC_EX_CTX) {
15259                 /* ABTS by responder to exchange, no cleanup needed */
15260                 aborted = true;
15261         } else {
15262                 /* ABTS by initiator to exchange, need to do cleanup */
15263                 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15264                 if (aborted == false)
15265                         aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
15266         }
15267         lpfc_in_buf_free(phba, &dmabuf->dbuf);
15268
15269         /* Respond with BA_ACC or BA_RJT accordingly */
15270         lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
15271 }
15272
15273 /**
15274  * lpfc_seq_complete - Indicates if a sequence is complete
15275  * @dmabuf: pointer to a dmabuf that describes the FC sequence
15276  *
15277  * This function checks the sequence, starting with the frame described by
15278  * @dmabuf, to see if all the frames associated with this sequence are present.
15279  * the frames associated with this sequence are linked to the @dmabuf using the
15280  * dbuf list. This function looks for two major things. 1) That the first frame
15281  * has a sequence count of zero. 2) There is a frame with last frame of sequence
15282  * set. 3) That there are no holes in the sequence count. The function will
15283  * return 1 when the sequence is complete, otherwise it will return 0.
15284  **/
15285 static int
15286 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15287 {
15288         struct fc_frame_header *hdr;
15289         struct lpfc_dmabuf *d_buf;
15290         struct hbq_dmabuf *seq_dmabuf;
15291         uint32_t fctl;
15292         int seq_count = 0;
15293
15294         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15295         /* make sure first fame of sequence has a sequence count of zero */
15296         if (hdr->fh_seq_cnt != seq_count)
15297                 return 0;
15298         fctl = (hdr->fh_f_ctl[0] << 16 |
15299                 hdr->fh_f_ctl[1] << 8 |
15300                 hdr->fh_f_ctl[2]);
15301         /* If last frame of sequence we can return success. */
15302         if (fctl & FC_FC_END_SEQ)
15303                 return 1;
15304         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15305                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15306                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15307                 /* If there is a hole in the sequence count then fail. */
15308                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
15309                         return 0;
15310                 fctl = (hdr->fh_f_ctl[0] << 16 |
15311                         hdr->fh_f_ctl[1] << 8 |
15312                         hdr->fh_f_ctl[2]);
15313                 /* If last frame of sequence we can return success. */
15314                 if (fctl & FC_FC_END_SEQ)
15315                         return 1;
15316         }
15317         return 0;
15318 }
15319
15320 /**
15321  * lpfc_prep_seq - Prep sequence for ULP processing
15322  * @vport: Pointer to the vport on which this sequence was received
15323  * @dmabuf: pointer to a dmabuf that describes the FC sequence
15324  *
15325  * This function takes a sequence, described by a list of frames, and creates
15326  * a list of iocbq structures to describe the sequence. This iocbq list will be
15327  * used to issue to the generic unsolicited sequence handler. This routine
15328  * returns a pointer to the first iocbq in the list. If the function is unable
15329  * to allocate an iocbq then it throw out the received frames that were not
15330  * able to be described and return a pointer to the first iocbq. If unable to
15331  * allocate any iocbqs (including the first) this function will return NULL.
15332  **/
15333 static struct lpfc_iocbq *
15334 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15335 {
15336         struct hbq_dmabuf *hbq_buf;
15337         struct lpfc_dmabuf *d_buf, *n_buf;
15338         struct lpfc_iocbq *first_iocbq, *iocbq;
15339         struct fc_frame_header *fc_hdr;
15340         uint32_t sid;
15341         uint32_t len, tot_len;
15342         struct ulp_bde64 *pbde;
15343
15344         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15345         /* remove from receive buffer list */
15346         list_del_init(&seq_dmabuf->hbuf.list);
15347         lpfc_update_rcv_time_stamp(vport);
15348         /* get the Remote Port's SID */
15349         sid = sli4_sid_from_fc_hdr(fc_hdr);
15350         tot_len = 0;
15351         /* Get an iocbq struct to fill in. */
15352         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15353         if (first_iocbq) {
15354                 /* Initialize the first IOCB. */
15355                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
15356                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
15357
15358                 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15359                 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15360                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15361                         first_iocbq->iocb.un.rcvels.parmRo =
15362                                 sli4_did_from_fc_hdr(fc_hdr);
15363                         first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15364                 } else
15365                         first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
15366                 first_iocbq->iocb.ulpContext = NO_XRI;
15367                 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15368                         be16_to_cpu(fc_hdr->fh_ox_id);
15369                 /* iocbq is prepped for internal consumption.  Physical vpi. */
15370                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15371                         vport->phba->vpi_ids[vport->vpi];
15372                 /* put the first buffer into the first IOCBq */
15373                 tot_len = bf_get(lpfc_rcqe_length,
15374                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15375
15376                 first_iocbq->context2 = &seq_dmabuf->dbuf;
15377                 first_iocbq->context3 = NULL;
15378                 first_iocbq->iocb.ulpBdeCount = 1;
15379                 if (tot_len > LPFC_DATA_BUF_SIZE)
15380                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15381                                                         LPFC_DATA_BUF_SIZE;
15382                 else
15383                         first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15384
15385                 first_iocbq->iocb.un.rcvels.remoteID = sid;
15386
15387                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15388         }
15389         iocbq = first_iocbq;
15390         /*
15391          * Each IOCBq can have two Buffers assigned, so go through the list
15392          * of buffers for this sequence and save two buffers in each IOCBq
15393          */
15394         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15395                 if (!iocbq) {
15396                         lpfc_in_buf_free(vport->phba, d_buf);
15397                         continue;
15398                 }
15399                 if (!iocbq->context3) {
15400                         iocbq->context3 = d_buf;
15401                         iocbq->iocb.ulpBdeCount++;
15402                         /* We need to get the size out of the right CQE */
15403                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15404                         len = bf_get(lpfc_rcqe_length,
15405                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
15406                         pbde = (struct ulp_bde64 *)
15407                                         &iocbq->iocb.unsli3.sli3Words[4];
15408                         if (len > LPFC_DATA_BUF_SIZE)
15409                                 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15410                         else
15411                                 pbde->tus.f.bdeSize = len;
15412
15413                         iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15414                         tot_len += len;
15415                 } else {
15416                         iocbq = lpfc_sli_get_iocbq(vport->phba);
15417                         if (!iocbq) {
15418                                 if (first_iocbq) {
15419                                         first_iocbq->iocb.ulpStatus =
15420                                                         IOSTAT_FCP_RSP_ERROR;
15421                                         first_iocbq->iocb.un.ulpWord[4] =
15422                                                         IOERR_NO_RESOURCES;
15423                                 }
15424                                 lpfc_in_buf_free(vport->phba, d_buf);
15425                                 continue;
15426                         }
15427                         /* We need to get the size out of the right CQE */
15428                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15429                         len = bf_get(lpfc_rcqe_length,
15430                                        &hbq_buf->cq_event.cqe.rcqe_cmpl);
15431                         iocbq->context2 = d_buf;
15432                         iocbq->context3 = NULL;
15433                         iocbq->iocb.ulpBdeCount = 1;
15434                         if (len > LPFC_DATA_BUF_SIZE)
15435                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15436                                                         LPFC_DATA_BUF_SIZE;
15437                         else
15438                                 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15439
15440                         tot_len += len;
15441                         iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15442
15443                         iocbq->iocb.un.rcvels.remoteID = sid;
15444                         list_add_tail(&iocbq->list, &first_iocbq->list);
15445                 }
15446         }
15447         /* Free the sequence's header buffer */
15448         if (!first_iocbq)
15449                 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
15450
15451         return first_iocbq;
15452 }
15453
15454 static void
15455 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15456                           struct hbq_dmabuf *seq_dmabuf)
15457 {
15458         struct fc_frame_header *fc_hdr;
15459         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15460         struct lpfc_hba *phba = vport->phba;
15461
15462         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15463         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15464         if (!iocbq) {
15465                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15466                                 "2707 Ring %d handler: Failed to allocate "
15467                                 "iocb Rctl x%x Type x%x received\n",
15468                                 LPFC_ELS_RING,
15469                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15470                 return;
15471         }
15472         if (!lpfc_complete_unsol_iocb(phba,
15473                                       &phba->sli.ring[LPFC_ELS_RING],
15474                                       iocbq, fc_hdr->fh_r_ctl,
15475                                       fc_hdr->fh_type))
15476                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15477                                 "2540 Ring %d handler: unexpected Rctl "
15478                                 "x%x Type x%x received\n",
15479                                 LPFC_ELS_RING,
15480                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15481
15482         /* Free iocb created in lpfc_prep_seq */
15483         list_for_each_entry_safe(curr_iocb, next_iocb,
15484                 &iocbq->list, list) {
15485                 list_del_init(&curr_iocb->list);
15486                 lpfc_sli_release_iocbq(phba, curr_iocb);
15487         }
15488         lpfc_sli_release_iocbq(phba, iocbq);
15489 }
15490
15491 /**
15492  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15493  * @phba: Pointer to HBA context object.
15494  *
15495  * This function is called with no lock held. This function processes all
15496  * the received buffers and gives it to upper layers when a received buffer
15497  * indicates that it is the final frame in the sequence. The interrupt
15498  * service routine processes received buffers at interrupt contexts and adds
15499  * received dma buffers to the rb_pend_list queue and signals the worker thread.
15500  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15501  * appropriate receive function when the final frame in a sequence is received.
15502  **/
15503 void
15504 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15505                                  struct hbq_dmabuf *dmabuf)
15506 {
15507         struct hbq_dmabuf *seq_dmabuf;
15508         struct fc_frame_header *fc_hdr;
15509         struct lpfc_vport *vport;
15510         uint32_t fcfi;
15511         uint32_t did;
15512
15513         /* Process each received buffer */
15514         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15515         /* check to see if this a valid type of frame */
15516         if (lpfc_fc_frame_check(phba, fc_hdr)) {
15517                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15518                 return;
15519         }
15520         if ((bf_get(lpfc_cqe_code,
15521                     &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15522                 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15523                               &dmabuf->cq_event.cqe.rcqe_cmpl);
15524         else
15525                 fcfi = bf_get(lpfc_rcqe_fcf_id,
15526                               &dmabuf->cq_event.cqe.rcqe_cmpl);
15527
15528         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
15529         if (!vport) {
15530                 /* throw out the frame */
15531                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15532                 return;
15533         }
15534
15535         /* d_id this frame is directed to */
15536         did = sli4_did_from_fc_hdr(fc_hdr);
15537
15538         /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15539         if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15540                 (did != Fabric_DID)) {
15541                 /*
15542                  * Throw out the frame if we are not pt2pt.
15543                  * The pt2pt protocol allows for discovery frames
15544                  * to be received without a registered VPI.
15545                  */
15546                 if (!(vport->fc_flag & FC_PT2PT) ||
15547                         (phba->link_state == LPFC_HBA_READY)) {
15548                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
15549                         return;
15550                 }
15551         }
15552
15553         /* Handle the basic abort sequence (BA_ABTS) event */
15554         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15555                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15556                 return;
15557         }
15558
15559         /* Link this frame */
15560         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15561         if (!seq_dmabuf) {
15562                 /* unable to add frame to vport - throw it out */
15563                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15564                 return;
15565         }
15566         /* If not last frame in sequence continue processing frames. */
15567         if (!lpfc_seq_complete(seq_dmabuf))
15568                 return;
15569
15570         /* Send the complete sequence to the upper layer protocol */
15571         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
15572 }
15573
15574 /**
15575  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15576  * @phba: pointer to lpfc hba data structure.
15577  *
15578  * This routine is invoked to post rpi header templates to the
15579  * HBA consistent with the SLI-4 interface spec.  This routine
15580  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15581  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15582  *
15583  * This routine does not require any locks.  It's usage is expected
15584  * to be driver load or reset recovery when the driver is
15585  * sequential.
15586  *
15587  * Return codes
15588  *      0 - successful
15589  *      -EIO - The mailbox failed to complete successfully.
15590  *      When this error occurs, the driver is not guaranteed
15591  *      to have any rpi regions posted to the device and
15592  *      must either attempt to repost the regions or take a
15593  *      fatal error.
15594  **/
15595 int
15596 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15597 {
15598         struct lpfc_rpi_hdr *rpi_page;
15599         uint32_t rc = 0;
15600         uint16_t lrpi = 0;
15601
15602         /* SLI4 ports that support extents do not require RPI headers. */
15603         if (!phba->sli4_hba.rpi_hdrs_in_use)
15604                 goto exit;
15605         if (phba->sli4_hba.extents_in_use)
15606                 return -EIO;
15607
15608         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
15609                 /*
15610                  * Assign the rpi headers a physical rpi only if the driver
15611                  * has not initialized those resources.  A port reset only
15612                  * needs the headers posted.
15613                  */
15614                 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15615                     LPFC_RPI_RSRC_RDY)
15616                         rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15617
15618                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15619                 if (rc != MBX_SUCCESS) {
15620                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15621                                         "2008 Error %d posting all rpi "
15622                                         "headers\n", rc);
15623                         rc = -EIO;
15624                         break;
15625                 }
15626         }
15627
15628  exit:
15629         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15630                LPFC_RPI_RSRC_RDY);
15631         return rc;
15632 }
15633
15634 /**
15635  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15636  * @phba: pointer to lpfc hba data structure.
15637  * @rpi_page:  pointer to the rpi memory region.
15638  *
15639  * This routine is invoked to post a single rpi header to the
15640  * HBA consistent with the SLI-4 interface spec.  This memory region
15641  * maps up to 64 rpi context regions.
15642  *
15643  * Return codes
15644  *      0 - successful
15645  *      -ENOMEM - No available memory
15646  *      -EIO - The mailbox failed to complete successfully.
15647  **/
15648 int
15649 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15650 {
15651         LPFC_MBOXQ_t *mboxq;
15652         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15653         uint32_t rc = 0;
15654         uint32_t shdr_status, shdr_add_status;
15655         union lpfc_sli4_cfg_shdr *shdr;
15656
15657         /* SLI4 ports that support extents do not require RPI headers. */
15658         if (!phba->sli4_hba.rpi_hdrs_in_use)
15659                 return rc;
15660         if (phba->sli4_hba.extents_in_use)
15661                 return -EIO;
15662
15663         /* The port is notified of the header region via a mailbox command. */
15664         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15665         if (!mboxq) {
15666                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15667                                 "2001 Unable to allocate memory for issuing "
15668                                 "SLI_CONFIG_SPECIAL mailbox command\n");
15669                 return -ENOMEM;
15670         }
15671
15672         /* Post all rpi memory regions to the port. */
15673         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
15674         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15675                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15676                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
15677                          sizeof(struct lpfc_sli4_cfg_mhdr),
15678                          LPFC_SLI4_MBX_EMBED);
15679
15680
15681         /* Post the physical rpi to the port for this rpi header. */
15682         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15683                rpi_page->start_rpi);
15684         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15685                hdr_tmpl, rpi_page->page_count);
15686
15687         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15688         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
15689         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15690         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15691         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15692         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15693         if (rc != MBX_TIMEOUT)
15694                 mempool_free(mboxq, phba->mbox_mem_pool);
15695         if (shdr_status || shdr_add_status || rc) {
15696                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15697                                 "2514 POST_RPI_HDR mailbox failed with "
15698                                 "status x%x add_status x%x, mbx status x%x\n",
15699                                 shdr_status, shdr_add_status, rc);
15700                 rc = -ENXIO;
15701         }
15702         return rc;
15703 }
15704
15705 /**
15706  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15707  * @phba: pointer to lpfc hba data structure.
15708  *
15709  * This routine is invoked to post rpi header templates to the
15710  * HBA consistent with the SLI-4 interface spec.  This routine
15711  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15712  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15713  *
15714  * Returns
15715  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15716  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
15717  **/
15718 int
15719 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15720 {
15721         unsigned long rpi;
15722         uint16_t max_rpi, rpi_limit;
15723         uint16_t rpi_remaining, lrpi = 0;
15724         struct lpfc_rpi_hdr *rpi_hdr;
15725         unsigned long iflag;
15726
15727         /*
15728          * Fetch the next logical rpi.  Because this index is logical,
15729          * the  driver starts at 0 each time.
15730          */
15731         spin_lock_irqsave(&phba->hbalock, iflag);
15732         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15733         rpi_limit = phba->sli4_hba.next_rpi;
15734
15735         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15736         if (rpi >= rpi_limit)
15737                 rpi = LPFC_RPI_ALLOC_ERROR;
15738         else {
15739                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15740                 phba->sli4_hba.max_cfg_param.rpi_used++;
15741                 phba->sli4_hba.rpi_count++;
15742         }
15743         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15744                         "0001 rpi:%x max:%x lim:%x\n",
15745                         (int) rpi, max_rpi, rpi_limit);
15746
15747         /*
15748          * Don't try to allocate more rpi header regions if the device limit
15749          * has been exhausted.
15750          */
15751         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15752             (phba->sli4_hba.rpi_count >= max_rpi)) {
15753                 spin_unlock_irqrestore(&phba->hbalock, iflag);
15754                 return rpi;
15755         }
15756
15757         /*
15758          * RPI header postings are not required for SLI4 ports capable of
15759          * extents.
15760          */
15761         if (!phba->sli4_hba.rpi_hdrs_in_use) {
15762                 spin_unlock_irqrestore(&phba->hbalock, iflag);
15763                 return rpi;
15764         }
15765
15766         /*
15767          * If the driver is running low on rpi resources, allocate another
15768          * page now.  Note that the next_rpi value is used because
15769          * it represents how many are actually in use whereas max_rpi notes
15770          * how many are supported max by the device.
15771          */
15772         rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
15773         spin_unlock_irqrestore(&phba->hbalock, iflag);
15774         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15775                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15776                 if (!rpi_hdr) {
15777                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15778                                         "2002 Error Could not grow rpi "
15779                                         "count\n");
15780                 } else {
15781                         lrpi = rpi_hdr->start_rpi;
15782                         rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15783                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15784                 }
15785         }
15786
15787         return rpi;
15788 }
15789
15790 /**
15791  * lpfc_sli4_free_rpi - Release an rpi for reuse.
15792  * @phba: pointer to lpfc hba data structure.
15793  *
15794  * This routine is invoked to release an rpi to the pool of
15795  * available rpis maintained by the driver.
15796  **/
15797 static void
15798 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15799 {
15800         /*
15801          * if the rpi value indicates a prior unreg has already
15802          * been done, skip the unreg.
15803          */
15804         if (rpi == LPFC_RPI_ALLOC_ERROR)
15805                 return;
15806
15807         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15808                 phba->sli4_hba.rpi_count--;
15809                 phba->sli4_hba.max_cfg_param.rpi_used--;
15810         }
15811 }
15812
15813 /**
15814  * lpfc_sli4_free_rpi - Release an rpi for reuse.
15815  * @phba: pointer to lpfc hba data structure.
15816  *
15817  * This routine is invoked to release an rpi to the pool of
15818  * available rpis maintained by the driver.
15819  **/
15820 void
15821 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15822 {
15823         spin_lock_irq(&phba->hbalock);
15824         __lpfc_sli4_free_rpi(phba, rpi);
15825         spin_unlock_irq(&phba->hbalock);
15826 }
15827
15828 /**
15829  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15830  * @phba: pointer to lpfc hba data structure.
15831  *
15832  * This routine is invoked to remove the memory region that
15833  * provided rpi via a bitmask.
15834  **/
15835 void
15836 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15837 {
15838         kfree(phba->sli4_hba.rpi_bmask);
15839         kfree(phba->sli4_hba.rpi_ids);
15840         bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
15841 }
15842
15843 /**
15844  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15845  * @phba: pointer to lpfc hba data structure.
15846  *
15847  * This routine is invoked to remove the memory region that
15848  * provided rpi via a bitmask.
15849  **/
15850 int
15851 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15852         void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
15853 {
15854         LPFC_MBOXQ_t *mboxq;
15855         struct lpfc_hba *phba = ndlp->phba;
15856         int rc;
15857
15858         /* The port is notified of the header region via a mailbox command. */
15859         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15860         if (!mboxq)
15861                 return -ENOMEM;
15862
15863         /* Post all rpi memory regions to the port. */
15864         lpfc_resume_rpi(mboxq, ndlp);
15865         if (cmpl) {
15866                 mboxq->mbox_cmpl = cmpl;
15867                 mboxq->context1 = arg;
15868                 mboxq->context2 = ndlp;
15869         } else
15870                 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15871         mboxq->vport = ndlp->vport;
15872         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15873         if (rc == MBX_NOT_FINISHED) {
15874                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15875                                 "2010 Resume RPI Mailbox failed "
15876                                 "status %d, mbxStatus x%x\n", rc,
15877                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15878                 mempool_free(mboxq, phba->mbox_mem_pool);
15879                 return -EIO;
15880         }
15881         return 0;
15882 }
15883
15884 /**
15885  * lpfc_sli4_init_vpi - Initialize a vpi with the port
15886  * @vport: Pointer to the vport for which the vpi is being initialized
15887  *
15888  * This routine is invoked to activate a vpi with the port.
15889  *
15890  * Returns:
15891  *    0 success
15892  *    -Evalue otherwise
15893  **/
15894 int
15895 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
15896 {
15897         LPFC_MBOXQ_t *mboxq;
15898         int rc = 0;
15899         int retval = MBX_SUCCESS;
15900         uint32_t mbox_tmo;
15901         struct lpfc_hba *phba = vport->phba;
15902         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15903         if (!mboxq)
15904                 return -ENOMEM;
15905         lpfc_init_vpi(phba, mboxq, vport->vpi);
15906         mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
15907         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
15908         if (rc != MBX_SUCCESS) {
15909                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
15910                                 "2022 INIT VPI Mailbox failed "
15911                                 "status %d, mbxStatus x%x\n", rc,
15912                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15913                 retval = -EIO;
15914         }
15915         if (rc != MBX_TIMEOUT)
15916                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
15917
15918         return retval;
15919 }
15920
15921 /**
15922  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15923  * @phba: pointer to lpfc hba data structure.
15924  * @mboxq: Pointer to mailbox object.
15925  *
15926  * This routine is invoked to manually add a single FCF record. The caller
15927  * must pass a completely initialized FCF_Record.  This routine takes
15928  * care of the nonembedded mailbox operations.
15929  **/
15930 static void
15931 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15932 {
15933         void *virt_addr;
15934         union lpfc_sli4_cfg_shdr *shdr;
15935         uint32_t shdr_status, shdr_add_status;
15936
15937         virt_addr = mboxq->sge_array->addr[0];
15938         /* The IOCTL status is embedded in the mailbox subheader. */
15939         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15940         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15941         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15942
15943         if ((shdr_status || shdr_add_status) &&
15944                 (shdr_status != STATUS_FCF_IN_USE))
15945                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15946                         "2558 ADD_FCF_RECORD mailbox failed with "
15947                         "status x%x add_status x%x\n",
15948                         shdr_status, shdr_add_status);
15949
15950         lpfc_sli4_mbox_cmd_free(phba, mboxq);
15951 }
15952
15953 /**
15954  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15955  * @phba: pointer to lpfc hba data structure.
15956  * @fcf_record:  pointer to the initialized fcf record to add.
15957  *
15958  * This routine is invoked to manually add a single FCF record. The caller
15959  * must pass a completely initialized FCF_Record.  This routine takes
15960  * care of the nonembedded mailbox operations.
15961  **/
15962 int
15963 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15964 {
15965         int rc = 0;
15966         LPFC_MBOXQ_t *mboxq;
15967         uint8_t *bytep;
15968         void *virt_addr;
15969         struct lpfc_mbx_sge sge;
15970         uint32_t alloc_len, req_len;
15971         uint32_t fcfindex;
15972
15973         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15974         if (!mboxq) {
15975                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15976                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15977                 return -ENOMEM;
15978         }
15979
15980         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15981                   sizeof(uint32_t);
15982
15983         /* Allocate DMA memory and set up the non-embedded mailbox command */
15984         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15985                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15986                                      req_len, LPFC_SLI4_MBX_NEMBED);
15987         if (alloc_len < req_len) {
15988                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15989                         "2523 Allocated DMA memory size (x%x) is "
15990                         "less than the requested DMA memory "
15991                         "size (x%x)\n", alloc_len, req_len);
15992                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15993                 return -ENOMEM;
15994         }
15995
15996         /*
15997          * Get the first SGE entry from the non-embedded DMA memory.  This
15998          * routine only uses a single SGE.
15999          */
16000         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
16001         virt_addr = mboxq->sge_array->addr[0];
16002         /*
16003          * Configure the FCF record for FCFI 0.  This is the driver's
16004          * hardcoded default and gets used in nonFIP mode.
16005          */
16006         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16007         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16008         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16009
16010         /*
16011          * Copy the fcf_index and the FCF Record Data. The data starts after
16012          * the FCoE header plus word10. The data copy needs to be endian
16013          * correct.
16014          */
16015         bytep += sizeof(uint32_t);
16016         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16017         mboxq->vport = phba->pport;
16018         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16019         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16020         if (rc == MBX_NOT_FINISHED) {
16021                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16022                         "2515 ADD_FCF_RECORD mailbox failed with "
16023                         "status 0x%x\n", rc);
16024                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16025                 rc = -EIO;
16026         } else
16027                 rc = 0;
16028
16029         return rc;
16030 }
16031
16032 /**
16033  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16034  * @phba: pointer to lpfc hba data structure.
16035  * @fcf_record:  pointer to the fcf record to write the default data.
16036  * @fcf_index: FCF table entry index.
16037  *
16038  * This routine is invoked to build the driver's default FCF record.  The
16039  * values used are hardcoded.  This routine handles memory initialization.
16040  *
16041  **/
16042 void
16043 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16044                                 struct fcf_record *fcf_record,
16045                                 uint16_t fcf_index)
16046 {
16047         memset(fcf_record, 0, sizeof(struct fcf_record));
16048         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16049         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16050         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16051         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16052         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16053         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16054         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16055         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16056         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16057         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16058         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16059         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16060         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
16061         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
16062         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16063         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16064                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16065         /* Set the VLAN bit map */
16066         if (phba->valid_vlan) {
16067                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16068                         = 1 << (phba->vlan_id % 8);
16069         }
16070 }
16071
16072 /**
16073  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
16074  * @phba: pointer to lpfc hba data structure.
16075  * @fcf_index: FCF table entry offset.
16076  *
16077  * This routine is invoked to scan the entire FCF table by reading FCF
16078  * record and processing it one at a time starting from the @fcf_index
16079  * for initial FCF discovery or fast FCF failover rediscovery.
16080  *
16081  * Return 0 if the mailbox command is submitted successfully, none 0
16082  * otherwise.
16083  **/
16084 int
16085 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16086 {
16087         int rc = 0, error;
16088         LPFC_MBOXQ_t *mboxq;
16089
16090         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
16091         phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
16092         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16093         if (!mboxq) {
16094                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16095                                 "2000 Failed to allocate mbox for "
16096                                 "READ_FCF cmd\n");
16097                 error = -ENOMEM;
16098                 goto fail_fcf_scan;
16099         }
16100         /* Construct the read FCF record mailbox command */
16101         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16102         if (rc) {
16103                 error = -EINVAL;
16104                 goto fail_fcf_scan;
16105         }
16106         /* Issue the mailbox command asynchronously */
16107         mboxq->vport = phba->pport;
16108         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
16109
16110         spin_lock_irq(&phba->hbalock);
16111         phba->hba_flag |= FCF_TS_INPROG;
16112         spin_unlock_irq(&phba->hbalock);
16113
16114         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16115         if (rc == MBX_NOT_FINISHED)
16116                 error = -EIO;
16117         else {
16118                 /* Reset eligible FCF count for new scan */
16119                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
16120                         phba->fcf.eligible_fcf_cnt = 0;
16121                 error = 0;
16122         }
16123 fail_fcf_scan:
16124         if (error) {
16125                 if (mboxq)
16126                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
16127                 /* FCF scan failed, clear FCF_TS_INPROG flag */
16128                 spin_lock_irq(&phba->hbalock);
16129                 phba->hba_flag &= ~FCF_TS_INPROG;
16130                 spin_unlock_irq(&phba->hbalock);
16131         }
16132         return error;
16133 }
16134
16135 /**
16136  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
16137  * @phba: pointer to lpfc hba data structure.
16138  * @fcf_index: FCF table entry offset.
16139  *
16140  * This routine is invoked to read an FCF record indicated by @fcf_index
16141  * and to use it for FLOGI roundrobin FCF failover.
16142  *
16143  * Return 0 if the mailbox command is submitted successfully, none 0
16144  * otherwise.
16145  **/
16146 int
16147 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16148 {
16149         int rc = 0, error;
16150         LPFC_MBOXQ_t *mboxq;
16151
16152         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16153         if (!mboxq) {
16154                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16155                                 "2763 Failed to allocate mbox for "
16156                                 "READ_FCF cmd\n");
16157                 error = -ENOMEM;
16158                 goto fail_fcf_read;
16159         }
16160         /* Construct the read FCF record mailbox command */
16161         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16162         if (rc) {
16163                 error = -EINVAL;
16164                 goto fail_fcf_read;
16165         }
16166         /* Issue the mailbox command asynchronously */
16167         mboxq->vport = phba->pport;
16168         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16169         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16170         if (rc == MBX_NOT_FINISHED)
16171                 error = -EIO;
16172         else
16173                 error = 0;
16174
16175 fail_fcf_read:
16176         if (error && mboxq)
16177                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16178         return error;
16179 }
16180
16181 /**
16182  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16183  * @phba: pointer to lpfc hba data structure.
16184  * @fcf_index: FCF table entry offset.
16185  *
16186  * This routine is invoked to read an FCF record indicated by @fcf_index to
16187  * determine whether it's eligible for FLOGI roundrobin failover list.
16188  *
16189  * Return 0 if the mailbox command is submitted successfully, none 0
16190  * otherwise.
16191  **/
16192 int
16193 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16194 {
16195         int rc = 0, error;
16196         LPFC_MBOXQ_t *mboxq;
16197
16198         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16199         if (!mboxq) {
16200                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16201                                 "2758 Failed to allocate mbox for "
16202                                 "READ_FCF cmd\n");
16203                                 error = -ENOMEM;
16204                                 goto fail_fcf_read;
16205         }
16206         /* Construct the read FCF record mailbox command */
16207         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16208         if (rc) {
16209                 error = -EINVAL;
16210                 goto fail_fcf_read;
16211         }
16212         /* Issue the mailbox command asynchronously */
16213         mboxq->vport = phba->pport;
16214         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16215         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16216         if (rc == MBX_NOT_FINISHED)
16217                 error = -EIO;
16218         else
16219                 error = 0;
16220
16221 fail_fcf_read:
16222         if (error && mboxq)
16223                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16224         return error;
16225 }
16226
16227 /**
16228  * lpfc_check_next_fcf_pri_level
16229  * phba pointer to the lpfc_hba struct for this port.
16230  * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16231  * routine when the rr_bmask is empty. The FCF indecies are put into the
16232  * rr_bmask based on their priority level. Starting from the highest priority
16233  * to the lowest. The most likely FCF candidate will be in the highest
16234  * priority group. When this routine is called it searches the fcf_pri list for
16235  * next lowest priority group and repopulates the rr_bmask with only those
16236  * fcf_indexes.
16237  * returns:
16238  * 1=success 0=failure
16239  **/
16240 static int
16241 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16242 {
16243         uint16_t next_fcf_pri;
16244         uint16_t last_index;
16245         struct lpfc_fcf_pri *fcf_pri;
16246         int rc;
16247         int ret = 0;
16248
16249         last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16250                         LPFC_SLI4_FCF_TBL_INDX_MAX);
16251         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16252                         "3060 Last IDX %d\n", last_index);
16253
16254         /* Verify the priority list has 2 or more entries */
16255         spin_lock_irq(&phba->hbalock);
16256         if (list_empty(&phba->fcf.fcf_pri_list) ||
16257             list_is_singular(&phba->fcf.fcf_pri_list)) {
16258                 spin_unlock_irq(&phba->hbalock);
16259                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16260                         "3061 Last IDX %d\n", last_index);
16261                 return 0; /* Empty rr list */
16262         }
16263         spin_unlock_irq(&phba->hbalock);
16264
16265         next_fcf_pri = 0;
16266         /*
16267          * Clear the rr_bmask and set all of the bits that are at this
16268          * priority.
16269          */
16270         memset(phba->fcf.fcf_rr_bmask, 0,
16271                         sizeof(*phba->fcf.fcf_rr_bmask));
16272         spin_lock_irq(&phba->hbalock);
16273         list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16274                 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16275                         continue;
16276                 /*
16277                  * the 1st priority that has not FLOGI failed
16278                  * will be the highest.
16279                  */
16280                 if (!next_fcf_pri)
16281                         next_fcf_pri = fcf_pri->fcf_rec.priority;
16282                 spin_unlock_irq(&phba->hbalock);
16283                 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16284                         rc = lpfc_sli4_fcf_rr_index_set(phba,
16285                                                 fcf_pri->fcf_rec.fcf_index);
16286                         if (rc)
16287                                 return 0;
16288                 }
16289                 spin_lock_irq(&phba->hbalock);
16290         }
16291         /*
16292          * if next_fcf_pri was not set above and the list is not empty then
16293          * we have failed flogis on all of them. So reset flogi failed
16294          * and start at the beginning.
16295          */
16296         if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16297                 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16298                         fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16299                         /*
16300                          * the 1st priority that has not FLOGI failed
16301                          * will be the highest.
16302                          */
16303                         if (!next_fcf_pri)
16304                                 next_fcf_pri = fcf_pri->fcf_rec.priority;
16305                         spin_unlock_irq(&phba->hbalock);
16306                         if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16307                                 rc = lpfc_sli4_fcf_rr_index_set(phba,
16308                                                 fcf_pri->fcf_rec.fcf_index);
16309                                 if (rc)
16310                                         return 0;
16311                         }
16312                         spin_lock_irq(&phba->hbalock);
16313                 }
16314         } else
16315                 ret = 1;
16316         spin_unlock_irq(&phba->hbalock);
16317
16318         return ret;
16319 }
16320 /**
16321  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16322  * @phba: pointer to lpfc hba data structure.
16323  *
16324  * This routine is to get the next eligible FCF record index in a round
16325  * robin fashion. If the next eligible FCF record index equals to the
16326  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
16327  * shall be returned, otherwise, the next eligible FCF record's index
16328  * shall be returned.
16329  **/
16330 uint16_t
16331 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16332 {
16333         uint16_t next_fcf_index;
16334
16335 initial_priority:
16336         /* Search start from next bit of currently registered FCF index */
16337         next_fcf_index = phba->fcf.current_rec.fcf_indx;
16338
16339 next_priority:
16340         /* Determine the next fcf index to check */
16341         next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
16342         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16343                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
16344                                        next_fcf_index);
16345
16346         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
16347         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16348                 /*
16349                  * If we have wrapped then we need to clear the bits that
16350                  * have been tested so that we can detect when we should
16351                  * change the priority level.
16352                  */
16353                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16354                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
16355         }
16356
16357
16358         /* Check roundrobin failover list empty condition */
16359         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16360                 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16361                 /*
16362                  * If next fcf index is not found check if there are lower
16363                  * Priority level fcf's in the fcf_priority list.
16364                  * Set up the rr_bmask with all of the avaiable fcf bits
16365                  * at that level and continue the selection process.
16366                  */
16367                 if (lpfc_check_next_fcf_pri_level(phba))
16368                         goto initial_priority;
16369                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16370                                 "2844 No roundrobin failover FCF available\n");
16371
16372                 return LPFC_FCOE_FCF_NEXT_NONE;
16373         }
16374
16375         if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16376                 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
16377                 LPFC_FCF_FLOGI_FAILED) {
16378                 if (list_is_singular(&phba->fcf.fcf_pri_list))
16379                         return LPFC_FCOE_FCF_NEXT_NONE;
16380
16381                 goto next_priority;
16382         }
16383
16384         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16385                         "2845 Get next roundrobin failover FCF (x%x)\n",
16386                         next_fcf_index);
16387
16388         return next_fcf_index;
16389 }
16390
16391 /**
16392  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16393  * @phba: pointer to lpfc hba data structure.
16394  *
16395  * This routine sets the FCF record index in to the eligible bmask for
16396  * roundrobin failover search. It checks to make sure that the index
16397  * does not go beyond the range of the driver allocated bmask dimension
16398  * before setting the bit.
16399  *
16400  * Returns 0 if the index bit successfully set, otherwise, it returns
16401  * -EINVAL.
16402  **/
16403 int
16404 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16405 {
16406         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16407                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16408                                 "2610 FCF (x%x) reached driver's book "
16409                                 "keeping dimension:x%x\n",
16410                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16411                 return -EINVAL;
16412         }
16413         /* Set the eligible FCF record index bmask */
16414         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16415
16416         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16417                         "2790 Set FCF (x%x) to roundrobin FCF failover "
16418                         "bmask\n", fcf_index);
16419
16420         return 0;
16421 }
16422
16423 /**
16424  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
16425  * @phba: pointer to lpfc hba data structure.
16426  *
16427  * This routine clears the FCF record index from the eligible bmask for
16428  * roundrobin failover search. It checks to make sure that the index
16429  * does not go beyond the range of the driver allocated bmask dimension
16430  * before clearing the bit.
16431  **/
16432 void
16433 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16434 {
16435         struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
16436         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16437                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16438                                 "2762 FCF (x%x) reached driver's book "
16439                                 "keeping dimension:x%x\n",
16440                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16441                 return;
16442         }
16443         /* Clear the eligible FCF record index bmask */
16444         spin_lock_irq(&phba->hbalock);
16445         list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16446                                  list) {
16447                 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16448                         list_del_init(&fcf_pri->list);
16449                         break;
16450                 }
16451         }
16452         spin_unlock_irq(&phba->hbalock);
16453         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16454
16455         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16456                         "2791 Clear FCF (x%x) from roundrobin failover "
16457                         "bmask\n", fcf_index);
16458 }
16459
16460 /**
16461  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16462  * @phba: pointer to lpfc hba data structure.
16463  *
16464  * This routine is the completion routine for the rediscover FCF table mailbox
16465  * command. If the mailbox command returned failure, it will try to stop the
16466  * FCF rediscover wait timer.
16467  **/
16468 static void
16469 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16470 {
16471         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16472         uint32_t shdr_status, shdr_add_status;
16473
16474         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16475
16476         shdr_status = bf_get(lpfc_mbox_hdr_status,
16477                              &redisc_fcf->header.cfg_shdr.response);
16478         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16479                              &redisc_fcf->header.cfg_shdr.response);
16480         if (shdr_status || shdr_add_status) {
16481                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16482                                 "2746 Requesting for FCF rediscovery failed "
16483                                 "status x%x add_status x%x\n",
16484                                 shdr_status, shdr_add_status);
16485                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
16486                         spin_lock_irq(&phba->hbalock);
16487                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
16488                         spin_unlock_irq(&phba->hbalock);
16489                         /*
16490                          * CVL event triggered FCF rediscover request failed,
16491                          * last resort to re-try current registered FCF entry.
16492                          */
16493                         lpfc_retry_pport_discovery(phba);
16494                 } else {
16495                         spin_lock_irq(&phba->hbalock);
16496                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
16497                         spin_unlock_irq(&phba->hbalock);
16498                         /*
16499                          * DEAD FCF event triggered FCF rediscover request
16500                          * failed, last resort to fail over as a link down
16501                          * to FCF registration.
16502                          */
16503                         lpfc_sli4_fcf_dead_failthrough(phba);
16504                 }
16505         } else {
16506                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16507                                 "2775 Start FCF rediscover quiescent timer\n");
16508                 /*
16509                  * Start FCF rediscovery wait timer for pending FCF
16510                  * before rescan FCF record table.
16511                  */
16512                 lpfc_fcf_redisc_wait_start_timer(phba);
16513         }
16514
16515         mempool_free(mbox, phba->mbox_mem_pool);
16516 }
16517
16518 /**
16519  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
16520  * @phba: pointer to lpfc hba data structure.
16521  *
16522  * This routine is invoked to request for rediscovery of the entire FCF table
16523  * by the port.
16524  **/
16525 int
16526 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16527 {
16528         LPFC_MBOXQ_t *mbox;
16529         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16530         int rc, length;
16531
16532         /* Cancel retry delay timers to all vports before FCF rediscover */
16533         lpfc_cancel_all_vport_retry_delay_timer(phba);
16534
16535         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16536         if (!mbox) {
16537                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16538                                 "2745 Failed to allocate mbox for "
16539                                 "requesting FCF rediscover.\n");
16540                 return -ENOMEM;
16541         }
16542
16543         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16544                   sizeof(struct lpfc_sli4_cfg_mhdr));
16545         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16546                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16547                          length, LPFC_SLI4_MBX_EMBED);
16548
16549         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16550         /* Set count to 0 for invalidating the entire FCF database */
16551         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16552
16553         /* Issue the mailbox command asynchronously */
16554         mbox->vport = phba->pport;
16555         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16556         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16557
16558         if (rc == MBX_NOT_FINISHED) {
16559                 mempool_free(mbox, phba->mbox_mem_pool);
16560                 return -EIO;
16561         }
16562         return 0;
16563 }
16564
16565 /**
16566  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16567  * @phba: pointer to lpfc hba data structure.
16568  *
16569  * This function is the failover routine as a last resort to the FCF DEAD
16570  * event when driver failed to perform fast FCF failover.
16571  **/
16572 void
16573 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16574 {
16575         uint32_t link_state;
16576
16577         /*
16578          * Last resort as FCF DEAD event failover will treat this as
16579          * a link down, but save the link state because we don't want
16580          * it to be changed to Link Down unless it is already down.
16581          */
16582         link_state = phba->link_state;
16583         lpfc_linkdown(phba);
16584         phba->link_state = link_state;
16585
16586         /* Unregister FCF if no devices connected to it */
16587         lpfc_unregister_unused_fcf(phba);
16588 }
16589
16590 /**
16591  * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
16592  * @phba: pointer to lpfc hba data structure.
16593  * @rgn23_data: pointer to configure region 23 data.
16594  *
16595  * This function gets SLI3 port configure region 23 data through memory dump
16596  * mailbox command. When it successfully retrieves data, the size of the data
16597  * will be returned, otherwise, 0 will be returned.
16598  **/
16599 static uint32_t
16600 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16601 {
16602         LPFC_MBOXQ_t *pmb = NULL;
16603         MAILBOX_t *mb;
16604         uint32_t offset = 0;
16605         int rc;
16606
16607         if (!rgn23_data)
16608                 return 0;
16609
16610         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16611         if (!pmb) {
16612                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16613                                 "2600 failed to allocate mailbox memory\n");
16614                 return 0;
16615         }
16616         mb = &pmb->u.mb;
16617
16618         do {
16619                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16620                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16621
16622                 if (rc != MBX_SUCCESS) {
16623                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16624                                         "2601 failed to read config "
16625                                         "region 23, rc 0x%x Status 0x%x\n",
16626                                         rc, mb->mbxStatus);
16627                         mb->un.varDmp.word_cnt = 0;
16628                 }
16629                 /*
16630                  * dump mem may return a zero when finished or we got a
16631                  * mailbox error, either way we are done.
16632                  */
16633                 if (mb->un.varDmp.word_cnt == 0)
16634                         break;
16635                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16636                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16637
16638                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
16639                                        rgn23_data + offset,
16640                                        mb->un.varDmp.word_cnt);
16641                 offset += mb->un.varDmp.word_cnt;
16642         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16643
16644         mempool_free(pmb, phba->mbox_mem_pool);
16645         return offset;
16646 }
16647
16648 /**
16649  * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16650  * @phba: pointer to lpfc hba data structure.
16651  * @rgn23_data: pointer to configure region 23 data.
16652  *
16653  * This function gets SLI4 port configure region 23 data through memory dump
16654  * mailbox command. When it successfully retrieves data, the size of the data
16655  * will be returned, otherwise, 0 will be returned.
16656  **/
16657 static uint32_t
16658 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16659 {
16660         LPFC_MBOXQ_t *mboxq = NULL;
16661         struct lpfc_dmabuf *mp = NULL;
16662         struct lpfc_mqe *mqe;
16663         uint32_t data_length = 0;
16664         int rc;
16665
16666         if (!rgn23_data)
16667                 return 0;
16668
16669         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16670         if (!mboxq) {
16671                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16672                                 "3105 failed to allocate mailbox memory\n");
16673                 return 0;
16674         }
16675
16676         if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16677                 goto out;
16678         mqe = &mboxq->u.mqe;
16679         mp = (struct lpfc_dmabuf *) mboxq->context1;
16680         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16681         if (rc)
16682                 goto out;
16683         data_length = mqe->un.mb_words[5];
16684         if (data_length == 0)
16685                 goto out;
16686         if (data_length > DMP_RGN23_SIZE) {
16687                 data_length = 0;
16688                 goto out;
16689         }
16690         lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16691 out:
16692         mempool_free(mboxq, phba->mbox_mem_pool);
16693         if (mp) {
16694                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16695                 kfree(mp);
16696         }
16697         return data_length;
16698 }
16699
16700 /**
16701  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16702  * @phba: pointer to lpfc hba data structure.
16703  *
16704  * This function read region 23 and parse TLV for port status to
16705  * decide if the user disaled the port. If the TLV indicates the
16706  * port is disabled, the hba_flag is set accordingly.
16707  **/
16708 void
16709 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16710 {
16711         uint8_t *rgn23_data = NULL;
16712         uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16713         uint32_t offset = 0;
16714
16715         /* Get adapter Region 23 data */
16716         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16717         if (!rgn23_data)
16718                 goto out;
16719
16720         if (phba->sli_rev < LPFC_SLI_REV4)
16721                 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16722         else {
16723                 if_type = bf_get(lpfc_sli_intf_if_type,
16724                                  &phba->sli4_hba.sli_intf);
16725                 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16726                         goto out;
16727                 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16728         }
16729
16730         if (!data_size)
16731                 goto out;
16732
16733         /* Check the region signature first */
16734         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16735                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16736                         "2619 Config region 23 has bad signature\n");
16737                         goto out;
16738         }
16739         offset += 4;
16740
16741         /* Check the data structure version */
16742         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16743                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16744                         "2620 Config region 23 has bad version\n");
16745                 goto out;
16746         }
16747         offset += 4;
16748
16749         /* Parse TLV entries in the region */
16750         while (offset < data_size) {
16751                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16752                         break;
16753                 /*
16754                  * If the TLV is not driver specific TLV or driver id is
16755                  * not linux driver id, skip the record.
16756                  */
16757                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16758                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16759                     (rgn23_data[offset + 3] != 0)) {
16760                         offset += rgn23_data[offset + 1] * 4 + 4;
16761                         continue;
16762                 }
16763
16764                 /* Driver found a driver specific TLV in the config region */
16765                 sub_tlv_len = rgn23_data[offset + 1] * 4;
16766                 offset += 4;
16767                 tlv_offset = 0;
16768
16769                 /*
16770                  * Search for configured port state sub-TLV.
16771                  */
16772                 while ((offset < data_size) &&
16773                         (tlv_offset < sub_tlv_len)) {
16774                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16775                                 offset += 4;
16776                                 tlv_offset += 4;
16777                                 break;
16778                         }
16779                         if (rgn23_data[offset] != PORT_STE_TYPE) {
16780                                 offset += rgn23_data[offset + 1] * 4 + 4;
16781                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16782                                 continue;
16783                         }
16784
16785                         /* This HBA contains PORT_STE configured */
16786                         if (!rgn23_data[offset + 2])
16787                                 phba->hba_flag |= LINK_DISABLED;
16788
16789                         goto out;
16790                 }
16791         }
16792
16793 out:
16794         kfree(rgn23_data);
16795         return;
16796 }
16797
16798 /**
16799  * lpfc_wr_object - write an object to the firmware
16800  * @phba: HBA structure that indicates port to create a queue on.
16801  * @dmabuf_list: list of dmabufs to write to the port.
16802  * @size: the total byte value of the objects to write to the port.
16803  * @offset: the current offset to be used to start the transfer.
16804  *
16805  * This routine will create a wr_object mailbox command to send to the port.
16806  * the mailbox command will be constructed using the dma buffers described in
16807  * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16808  * BDEs that the imbedded mailbox can support. The @offset variable will be
16809  * used to indicate the starting offset of the transfer and will also return
16810  * the offset after the write object mailbox has completed. @size is used to
16811  * determine the end of the object and whether the eof bit should be set.
16812  *
16813  * Return 0 is successful and offset will contain the the new offset to use
16814  * for the next write.
16815  * Return negative value for error cases.
16816  **/
16817 int
16818 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16819                uint32_t size, uint32_t *offset)
16820 {
16821         struct lpfc_mbx_wr_object *wr_object;
16822         LPFC_MBOXQ_t *mbox;
16823         int rc = 0, i = 0;
16824         uint32_t shdr_status, shdr_add_status;
16825         uint32_t mbox_tmo;
16826         union lpfc_sli4_cfg_shdr *shdr;
16827         struct lpfc_dmabuf *dmabuf;
16828         uint32_t written = 0;
16829
16830         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16831         if (!mbox)
16832                 return -ENOMEM;
16833
16834         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16835                         LPFC_MBOX_OPCODE_WRITE_OBJECT,
16836                         sizeof(struct lpfc_mbx_wr_object) -
16837                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16838
16839         wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16840         wr_object->u.request.write_offset = *offset;
16841         sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16842         wr_object->u.request.object_name[0] =
16843                 cpu_to_le32(wr_object->u.request.object_name[0]);
16844         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16845         list_for_each_entry(dmabuf, dmabuf_list, list) {
16846                 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16847                         break;
16848                 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16849                 wr_object->u.request.bde[i].addrHigh =
16850                         putPaddrHigh(dmabuf->phys);
16851                 if (written + SLI4_PAGE_SIZE >= size) {
16852                         wr_object->u.request.bde[i].tus.f.bdeSize =
16853                                 (size - written);
16854                         written += (size - written);
16855                         bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16856                 } else {
16857                         wr_object->u.request.bde[i].tus.f.bdeSize =
16858                                 SLI4_PAGE_SIZE;
16859                         written += SLI4_PAGE_SIZE;
16860                 }
16861                 i++;
16862         }
16863         wr_object->u.request.bde_count = i;
16864         bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16865         if (!phba->sli4_hba.intr_enable)
16866                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16867         else {
16868                 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16869                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16870         }
16871         /* The IOCTL status is embedded in the mailbox subheader. */
16872         shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16873         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16874         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16875         if (rc != MBX_TIMEOUT)
16876                 mempool_free(mbox, phba->mbox_mem_pool);
16877         if (shdr_status || shdr_add_status || rc) {
16878                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16879                                 "3025 Write Object mailbox failed with "
16880                                 "status x%x add_status x%x, mbx status x%x\n",
16881                                 shdr_status, shdr_add_status, rc);
16882                 rc = -ENXIO;
16883         } else
16884                 *offset += wr_object->u.response.actual_write_length;
16885         return rc;
16886 }
16887
16888 /**
16889  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16890  * @vport: pointer to vport data structure.
16891  *
16892  * This function iterate through the mailboxq and clean up all REG_LOGIN
16893  * and REG_VPI mailbox commands associated with the vport. This function
16894  * is called when driver want to restart discovery of the vport due to
16895  * a Clear Virtual Link event.
16896  **/
16897 void
16898 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16899 {
16900         struct lpfc_hba *phba = vport->phba;
16901         LPFC_MBOXQ_t *mb, *nextmb;
16902         struct lpfc_dmabuf *mp;
16903         struct lpfc_nodelist *ndlp;
16904         struct lpfc_nodelist *act_mbx_ndlp = NULL;
16905         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
16906         LIST_HEAD(mbox_cmd_list);
16907         uint8_t restart_loop;
16908
16909         /* Clean up internally queued mailbox commands with the vport */
16910         spin_lock_irq(&phba->hbalock);
16911         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16912                 if (mb->vport != vport)
16913                         continue;
16914
16915                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16916                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
16917                         continue;
16918
16919                 list_del(&mb->list);
16920                 list_add_tail(&mb->list, &mbox_cmd_list);
16921         }
16922         /* Clean up active mailbox command with the vport */
16923         mb = phba->sli.mbox_active;
16924         if (mb && (mb->vport == vport)) {
16925                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16926                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
16927                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16928                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16929                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16930                         /* Put reference count for delayed processing */
16931                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16932                         /* Unregister the RPI when mailbox complete */
16933                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16934                 }
16935         }
16936         /* Cleanup any mailbox completions which are not yet processed */
16937         do {
16938                 restart_loop = 0;
16939                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16940                         /*
16941                          * If this mailox is already processed or it is
16942                          * for another vport ignore it.
16943                          */
16944                         if ((mb->vport != vport) ||
16945                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16946                                 continue;
16947
16948                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16949                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16950                                 continue;
16951
16952                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16953                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16954                                 ndlp = (struct lpfc_nodelist *)mb->context2;
16955                                 /* Unregister the RPI when mailbox complete */
16956                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16957                                 restart_loop = 1;
16958                                 spin_unlock_irq(&phba->hbalock);
16959                                 spin_lock(shost->host_lock);
16960                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16961                                 spin_unlock(shost->host_lock);
16962                                 spin_lock_irq(&phba->hbalock);
16963                                 break;
16964                         }
16965                 }
16966         } while (restart_loop);
16967
16968         spin_unlock_irq(&phba->hbalock);
16969
16970         /* Release the cleaned-up mailbox commands */
16971         while (!list_empty(&mbox_cmd_list)) {
16972                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
16973                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16974                         mp = (struct lpfc_dmabuf *) (mb->context1);
16975                         if (mp) {
16976                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16977                                 kfree(mp);
16978                         }
16979                         ndlp = (struct lpfc_nodelist *) mb->context2;
16980                         mb->context2 = NULL;
16981                         if (ndlp) {
16982                                 spin_lock(shost->host_lock);
16983                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16984                                 spin_unlock(shost->host_lock);
16985                                 lpfc_nlp_put(ndlp);
16986                         }
16987                 }
16988                 mempool_free(mb, phba->mbox_mem_pool);
16989         }
16990
16991         /* Release the ndlp with the cleaned-up active mailbox command */
16992         if (act_mbx_ndlp) {
16993                 spin_lock(shost->host_lock);
16994                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16995                 spin_unlock(shost->host_lock);
16996                 lpfc_nlp_put(act_mbx_ndlp);
16997         }
16998 }
16999
17000 /**
17001  * lpfc_drain_txq - Drain the txq
17002  * @phba: Pointer to HBA context object.
17003  *
17004  * This function attempt to submit IOCBs on the txq
17005  * to the adapter.  For SLI4 adapters, the txq contains
17006  * ELS IOCBs that have been deferred because the there
17007  * are no SGLs.  This congestion can occur with large
17008  * vport counts during node discovery.
17009  **/
17010
17011 uint32_t
17012 lpfc_drain_txq(struct lpfc_hba *phba)
17013 {
17014         LIST_HEAD(completions);
17015         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
17016         struct lpfc_iocbq *piocbq = NULL;
17017         unsigned long iflags = 0;
17018         char *fail_msg = NULL;
17019         struct lpfc_sglq *sglq;
17020         union lpfc_wqe wqe;
17021         uint32_t txq_cnt = 0;
17022
17023         spin_lock_irqsave(&pring->ring_lock, iflags);
17024         list_for_each_entry(piocbq, &pring->txq, list) {
17025                 txq_cnt++;
17026         }
17027
17028         if (txq_cnt > pring->txq_max)
17029                 pring->txq_max = txq_cnt;
17030
17031         spin_unlock_irqrestore(&pring->ring_lock, iflags);
17032
17033         while (!list_empty(&pring->txq)) {
17034                 spin_lock_irqsave(&pring->ring_lock, iflags);
17035
17036                 piocbq = lpfc_sli_ringtx_get(phba, pring);
17037                 if (!piocbq) {
17038                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
17039                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17040                                 "2823 txq empty and txq_cnt is %d\n ",
17041                                 txq_cnt);
17042                         break;
17043                 }
17044                 sglq = __lpfc_sli_get_sglq(phba, piocbq);
17045                 if (!sglq) {
17046                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
17047                         spin_unlock_irqrestore(&pring->ring_lock, iflags);
17048                         break;
17049                 }
17050                 txq_cnt--;
17051
17052                 /* The xri and iocb resources secured,
17053                  * attempt to issue request
17054                  */
17055                 piocbq->sli4_lxritag = sglq->sli4_lxritag;
17056                 piocbq->sli4_xritag = sglq->sli4_xritag;
17057                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17058                         fail_msg = "to convert bpl to sgl";
17059                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17060                         fail_msg = "to convert iocb to wqe";
17061                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17062                         fail_msg = " - Wq is full";
17063                 else
17064                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17065
17066                 if (fail_msg) {
17067                         /* Failed means we can't issue and need to cancel */
17068                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17069                                         "2822 IOCB failed %s iotag 0x%x "
17070                                         "xri 0x%x\n",
17071                                         fail_msg,
17072                                         piocbq->iotag, piocbq->sli4_xritag);
17073                         list_add_tail(&piocbq->list, &completions);
17074                 }
17075                 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17076         }
17077
17078         /* Cancel all the IOCBs that cannot be issued */
17079         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17080                                 IOERR_SLI_ABORTED);
17081
17082         return txq_cnt;
17083 }