fd5835e1c0395b93a387353b93fdddbf753c1f32
[linux-2.6.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-2011 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 IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71         return &iocbq->iocb;
72 }
73
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90         struct lpfc_register doorbell;
91         uint32_t host_index;
92
93         /* If the host has not yet processed the next entry then we are done */
94         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95                 return -ENOMEM;
96         /* set consumption flag every once in a while */
97         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
99         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
100                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
101         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
102
103         /* Update the host index before invoking device */
104         host_index = q->host_index;
105         q->host_index = ((q->host_index + 1) % q->entry_count);
106
107         /* Ring Doorbell */
108         doorbell.word0 = 0;
109         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
110         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
111         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
112         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
113         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
114
115         return 0;
116 }
117
118 /**
119  * lpfc_sli4_wq_release - Updates internal hba index for WQ
120  * @q: The Work Queue to operate on.
121  * @index: The index to advance the hba index to.
122  *
123  * This routine will update the HBA index of a queue to reflect consumption of
124  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
125  * an entry the host calls this function to update the queue's internal
126  * pointers. This routine returns the number of entries that were consumed by
127  * the HBA.
128  **/
129 static uint32_t
130 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
131 {
132         uint32_t released = 0;
133
134         if (q->hba_index == index)
135                 return 0;
136         do {
137                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
138                 released++;
139         } while (q->hba_index != index);
140         return released;
141 }
142
143 /**
144  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
145  * @q: The Mailbox Queue to operate on.
146  * @wqe: The Mailbox Queue Entry to put on the Work queue.
147  *
148  * This routine will copy the contents of @mqe to the next available entry on
149  * the @q. This function will then ring the Work Queue Doorbell to signal the
150  * HBA to start processing the Work Queue Entry. This function returns 0 if
151  * successful. If no entries are available on @q then this function will return
152  * -ENOMEM.
153  * The caller is expected to hold the hbalock when calling this routine.
154  **/
155 static uint32_t
156 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
157 {
158         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
159         struct lpfc_register doorbell;
160         uint32_t host_index;
161
162         /* If the host has not yet processed the next entry then we are done */
163         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
164                 return -ENOMEM;
165         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
166         /* Save off the mailbox pointer for completion */
167         q->phba->mbox = (MAILBOX_t *)temp_mqe;
168
169         /* Update the host index before invoking device */
170         host_index = q->host_index;
171         q->host_index = ((q->host_index + 1) % q->entry_count);
172
173         /* Ring Doorbell */
174         doorbell.word0 = 0;
175         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
176         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
177         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
178         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
179         return 0;
180 }
181
182 /**
183  * lpfc_sli4_mq_release - Updates internal hba index for MQ
184  * @q: The Mailbox Queue to operate on.
185  *
186  * This routine will update the HBA index of a queue to reflect consumption of
187  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
188  * an entry the host calls this function to update the queue's internal
189  * pointers. This routine returns the number of entries that were consumed by
190  * the HBA.
191  **/
192 static uint32_t
193 lpfc_sli4_mq_release(struct lpfc_queue *q)
194 {
195         /* Clear the mailbox pointer for completion */
196         q->phba->mbox = NULL;
197         q->hba_index = ((q->hba_index + 1) % q->entry_count);
198         return 1;
199 }
200
201 /**
202  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
203  * @q: The Event Queue to get the first valid EQE from
204  *
205  * This routine will get the first valid Event Queue Entry from @q, update
206  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
207  * the Queue (no more work to do), or the Queue is full of EQEs that have been
208  * processed, but not popped back to the HBA then this routine will return NULL.
209  **/
210 static struct lpfc_eqe *
211 lpfc_sli4_eq_get(struct lpfc_queue *q)
212 {
213         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
214
215         /* If the next EQE is not valid then we are done */
216         if (!bf_get_le32(lpfc_eqe_valid, eqe))
217                 return NULL;
218         /* If the host has not yet processed the next entry then we are done */
219         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
220                 return NULL;
221
222         q->hba_index = ((q->hba_index + 1) % q->entry_count);
223         return eqe;
224 }
225
226 /**
227  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
228  * @q: The Event Queue that the host has completed processing for.
229  * @arm: Indicates whether the host wants to arms this CQ.
230  *
231  * This routine will mark all Event Queue Entries on @q, from the last
232  * known completed entry to the last entry that was processed, as completed
233  * by clearing the valid bit for each completion queue entry. Then it will
234  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
235  * The internal host index in the @q will be updated by this routine to indicate
236  * that the host has finished processing the entries. The @arm parameter
237  * indicates that the queue should be rearmed when ringing the doorbell.
238  *
239  * This function will return the number of EQEs that were popped.
240  **/
241 uint32_t
242 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
243 {
244         uint32_t released = 0;
245         struct lpfc_eqe *temp_eqe;
246         struct lpfc_register doorbell;
247
248         /* while there are valid entries */
249         while (q->hba_index != q->host_index) {
250                 temp_eqe = q->qe[q->host_index].eqe;
251                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
252                 released++;
253                 q->host_index = ((q->host_index + 1) % q->entry_count);
254         }
255         if (unlikely(released == 0 && !arm))
256                 return 0;
257
258         /* ring doorbell for number popped */
259         doorbell.word0 = 0;
260         if (arm) {
261                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
262                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
263         }
264         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
265         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
266         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
267         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
268         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
269         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
270                 readl(q->phba->sli4_hba.EQCQDBregaddr);
271         return released;
272 }
273
274 /**
275  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
276  * @q: The Completion Queue to get the first valid CQE from
277  *
278  * This routine will get the first valid Completion Queue Entry from @q, update
279  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
280  * the Queue (no more work to do), or the Queue is full of CQEs that have been
281  * processed, but not popped back to the HBA then this routine will return NULL.
282  **/
283 static struct lpfc_cqe *
284 lpfc_sli4_cq_get(struct lpfc_queue *q)
285 {
286         struct lpfc_cqe *cqe;
287
288         /* If the next CQE is not valid then we are done */
289         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
290                 return NULL;
291         /* If the host has not yet processed the next entry then we are done */
292         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
293                 return NULL;
294
295         cqe = q->qe[q->hba_index].cqe;
296         q->hba_index = ((q->hba_index + 1) % q->entry_count);
297         return cqe;
298 }
299
300 /**
301  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
302  * @q: The Completion Queue that the host has completed processing for.
303  * @arm: Indicates whether the host wants to arms this CQ.
304  *
305  * This routine will mark all Completion queue entries on @q, from the last
306  * known completed entry to the last entry that was processed, as completed
307  * by clearing the valid bit for each completion queue entry. Then it will
308  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
309  * The internal host index in the @q will be updated by this routine to indicate
310  * that the host has finished processing the entries. The @arm parameter
311  * indicates that the queue should be rearmed when ringing the doorbell.
312  *
313  * This function will return the number of CQEs that were released.
314  **/
315 uint32_t
316 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
317 {
318         uint32_t released = 0;
319         struct lpfc_cqe *temp_qe;
320         struct lpfc_register doorbell;
321
322         /* while there are valid entries */
323         while (q->hba_index != q->host_index) {
324                 temp_qe = q->qe[q->host_index].cqe;
325                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
326                 released++;
327                 q->host_index = ((q->host_index + 1) % q->entry_count);
328         }
329         if (unlikely(released == 0 && !arm))
330                 return 0;
331
332         /* ring doorbell for number popped */
333         doorbell.word0 = 0;
334         if (arm)
335                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
336         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
337         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
338         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
339         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
340         return released;
341 }
342
343 /**
344  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
345  * @q: The Header Receive Queue to operate on.
346  * @wqe: The Receive Queue Entry to put on the Receive queue.
347  *
348  * This routine will copy the contents of @wqe to the next available entry on
349  * the @q. This function will then ring the Receive Queue Doorbell to signal the
350  * HBA to start processing the Receive Queue Entry. This function returns the
351  * index that the rqe was copied to if successful. If no entries are available
352  * on @q then this function will return -ENOMEM.
353  * The caller is expected to hold the hbalock when calling this routine.
354  **/
355 static int
356 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
357                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
358 {
359         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
360         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
361         struct lpfc_register doorbell;
362         int put_index = hq->host_index;
363
364         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
365                 return -EINVAL;
366         if (hq->host_index != dq->host_index)
367                 return -EINVAL;
368         /* If the host has not yet processed the next entry then we are done */
369         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
370                 return -EBUSY;
371         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
372         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
373
374         /* Update the host index to point to the next slot */
375         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
376         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
377
378         /* Ring The Header Receive Queue Doorbell */
379         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
380                 doorbell.word0 = 0;
381                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
382                        LPFC_RQ_POST_BATCH);
383                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
384                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
385         }
386         return put_index;
387 }
388
389 /**
390  * lpfc_sli4_rq_release - Updates internal hba index for RQ
391  * @q: The Header Receive Queue to operate on.
392  *
393  * This routine will update the HBA index of a queue to reflect consumption of
394  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
395  * consumed an entry the host calls this function to update the queue's
396  * internal pointers. This routine returns the number of entries that were
397  * consumed by the HBA.
398  **/
399 static uint32_t
400 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
401 {
402         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
403                 return 0;
404         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
405         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
406         return 1;
407 }
408
409 /**
410  * lpfc_cmd_iocb - Get next command iocb entry in the ring
411  * @phba: Pointer to HBA context object.
412  * @pring: Pointer to driver SLI ring object.
413  *
414  * This function returns pointer to next command iocb entry
415  * in the command ring. The caller must hold hbalock to prevent
416  * other threads consume the next command iocb.
417  * SLI-2/SLI-3 provide different sized iocbs.
418  **/
419 static inline IOCB_t *
420 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
421 {
422         return (IOCB_t *) (((char *) pring->cmdringaddr) +
423                            pring->cmdidx * phba->iocb_cmd_size);
424 }
425
426 /**
427  * lpfc_resp_iocb - Get next response iocb entry in the ring
428  * @phba: Pointer to HBA context object.
429  * @pring: Pointer to driver SLI ring object.
430  *
431  * This function returns pointer to next response iocb entry
432  * in the response ring. The caller must hold hbalock to make sure
433  * that no other thread consume the next response iocb.
434  * SLI-2/SLI-3 provide different sized iocbs.
435  **/
436 static inline IOCB_t *
437 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
438 {
439         return (IOCB_t *) (((char *) pring->rspringaddr) +
440                            pring->rspidx * phba->iocb_rsp_size);
441 }
442
443 /**
444  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
445  * @phba: Pointer to HBA context object.
446  *
447  * This function is called with hbalock held. This function
448  * allocates a new driver iocb object from the iocb pool. If the
449  * allocation is successful, it returns pointer to the newly
450  * allocated iocb object else it returns NULL.
451  **/
452 static struct lpfc_iocbq *
453 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
454 {
455         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
456         struct lpfc_iocbq * iocbq = NULL;
457
458         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
459
460         if (iocbq)
461                 phba->iocb_cnt++;
462         if (phba->iocb_cnt > phba->iocb_max)
463                 phba->iocb_max = phba->iocb_cnt;
464         return iocbq;
465 }
466
467 /**
468  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
469  * @phba: Pointer to HBA context object.
470  * @xritag: XRI value.
471  *
472  * This function clears the sglq pointer from the array of acive
473  * sglq's. The xritag that is passed in is used to index into the
474  * array. Before the xritag can be used it needs to be adjusted
475  * by subtracting the xribase.
476  *
477  * Returns sglq ponter = success, NULL = Failure.
478  **/
479 static struct lpfc_sglq *
480 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
481 {
482         uint16_t adj_xri;
483         struct lpfc_sglq *sglq;
484         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
485         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
486                 return NULL;
487         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
488         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
489         return sglq;
490 }
491
492 /**
493  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
494  * @phba: Pointer to HBA context object.
495  * @xritag: XRI value.
496  *
497  * This function returns the sglq pointer from the array of acive
498  * sglq's. The xritag that is passed in is used to index into the
499  * array. Before the xritag can be used it needs to be adjusted
500  * by subtracting the xribase.
501  *
502  * Returns sglq ponter = success, NULL = Failure.
503  **/
504 struct lpfc_sglq *
505 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
506 {
507         uint16_t adj_xri;
508         struct lpfc_sglq *sglq;
509         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
510         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
511                 return NULL;
512         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
513         return sglq;
514 }
515
516 /**
517  * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
518  * @phba: Pointer to HBA context object.
519  * @ndlp: nodelist pointer for this target.
520  * @xritag: xri used in this exchange.
521  * @rxid: Remote Exchange ID.
522  * @send_rrq: Flag used to determine if we should send rrq els cmd.
523  *
524  * This function is called with hbalock held.
525  * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
526  * rrq struct and adds it to the active_rrq_list.
527  *
528  * returns  0 for rrq slot for this xri
529  *         < 0  Were not able to get rrq mem or invalid parameter.
530  **/
531 static int
532 __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
533                 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
534 {
535         uint16_t adj_xri;
536         struct lpfc_node_rrq *rrq;
537         int empty;
538         uint32_t did = 0;
539
540
541         if (!ndlp)
542                 return -EINVAL;
543
544         if (!phba->cfg_enable_rrq)
545                 return -EINVAL;
546
547         if (phba->pport->load_flag & FC_UNLOADING) {
548                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
549                 goto out;
550         }
551         did = ndlp->nlp_DID;
552
553         /*
554          * set the active bit even if there is no mem available.
555          */
556         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
557
558         if (NLP_CHK_FREE_REQ(ndlp))
559                 goto out;
560
561         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
562                 goto out;
563
564         if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
565                 goto out;
566
567         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
568         if (rrq) {
569                 rrq->send_rrq = send_rrq;
570                 rrq->xritag = xritag;
571                 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
572                 rrq->ndlp = ndlp;
573                 rrq->nlp_DID = ndlp->nlp_DID;
574                 rrq->vport = ndlp->vport;
575                 rrq->rxid = rxid;
576                 empty = list_empty(&phba->active_rrq_list);
577                 rrq->send_rrq = send_rrq;
578                 list_add_tail(&rrq->list, &phba->active_rrq_list);
579                 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
580                         phba->hba_flag |= HBA_RRQ_ACTIVE;
581                         if (empty)
582                                 lpfc_worker_wake_up(phba);
583                 }
584                 return 0;
585         }
586 out:
587         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
588                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
589                         " DID:0x%x Send:%d\n",
590                         xritag, rxid, did, send_rrq);
591         return -EINVAL;
592 }
593
594 /**
595  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
596  * @phba: Pointer to HBA context object.
597  * @xritag: xri used in this exchange.
598  * @rrq: The RRQ to be cleared.
599  *
600  **/
601 void
602 lpfc_clr_rrq_active(struct lpfc_hba *phba,
603                     uint16_t xritag,
604                     struct lpfc_node_rrq *rrq)
605 {
606         uint16_t adj_xri;
607         struct lpfc_nodelist *ndlp = NULL;
608
609         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
610                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
611
612         /* The target DID could have been swapped (cable swap)
613          * we should use the ndlp from the findnode if it is
614          * available.
615          */
616         if ((!ndlp) && rrq->ndlp)
617                 ndlp = rrq->ndlp;
618
619         if (!ndlp)
620                 goto out;
621
622         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
623         if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
624                 rrq->send_rrq = 0;
625                 rrq->xritag = 0;
626                 rrq->rrq_stop_time = 0;
627         }
628 out:
629         mempool_free(rrq, phba->rrq_pool);
630 }
631
632 /**
633  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
634  * @phba: Pointer to HBA context object.
635  *
636  * This function is called with hbalock held. This function
637  * Checks if stop_time (ratov from setting rrq active) has
638  * been reached, if it has and the send_rrq flag is set then
639  * it will call lpfc_send_rrq. If the send_rrq flag is not set
640  * then it will just call the routine to clear the rrq and
641  * free the rrq resource.
642  * The timer is set to the next rrq that is going to expire before
643  * leaving the routine.
644  *
645  **/
646 void
647 lpfc_handle_rrq_active(struct lpfc_hba *phba)
648 {
649         struct lpfc_node_rrq *rrq;
650         struct lpfc_node_rrq *nextrrq;
651         unsigned long next_time;
652         unsigned long iflags;
653         LIST_HEAD(send_rrq);
654
655         spin_lock_irqsave(&phba->hbalock, iflags);
656         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
657         next_time = jiffies + HZ * (phba->fc_ratov + 1);
658         list_for_each_entry_safe(rrq, nextrrq,
659                                  &phba->active_rrq_list, list) {
660                 if (time_after(jiffies, rrq->rrq_stop_time))
661                         list_move(&rrq->list, &send_rrq);
662                 else if (time_before(rrq->rrq_stop_time, next_time))
663                         next_time = rrq->rrq_stop_time;
664         }
665         spin_unlock_irqrestore(&phba->hbalock, iflags);
666         if (!list_empty(&phba->active_rrq_list))
667                 mod_timer(&phba->rrq_tmr, next_time);
668         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
669                 list_del(&rrq->list);
670                 if (!rrq->send_rrq)
671                         /* this call will free the rrq */
672                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
673                 else if (lpfc_send_rrq(phba, rrq)) {
674                         /* if we send the rrq then the completion handler
675                         *  will clear the bit in the xribitmap.
676                         */
677                         lpfc_clr_rrq_active(phba, rrq->xritag,
678                                             rrq);
679                 }
680         }
681 }
682
683 /**
684  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
685  * @vport: Pointer to vport context object.
686  * @xri: The xri used in the exchange.
687  * @did: The targets DID for this exchange.
688  *
689  * returns NULL = rrq not found in the phba->active_rrq_list.
690  *         rrq = rrq for this xri and target.
691  **/
692 struct lpfc_node_rrq *
693 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
694 {
695         struct lpfc_hba *phba = vport->phba;
696         struct lpfc_node_rrq *rrq;
697         struct lpfc_node_rrq *nextrrq;
698         unsigned long iflags;
699
700         if (phba->sli_rev != LPFC_SLI_REV4)
701                 return NULL;
702         spin_lock_irqsave(&phba->hbalock, iflags);
703         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
704                 if (rrq->vport == vport && rrq->xritag == xri &&
705                                 rrq->nlp_DID == did){
706                         list_del(&rrq->list);
707                         spin_unlock_irqrestore(&phba->hbalock, iflags);
708                         return rrq;
709                 }
710         }
711         spin_unlock_irqrestore(&phba->hbalock, iflags);
712         return NULL;
713 }
714
715 /**
716  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
717  * @vport: Pointer to vport context object.
718  * @ndlp: Pointer to the lpfc_node_list structure.
719  * If ndlp is NULL Remove all active RRQs for this vport from the
720  * phba->active_rrq_list and clear the rrq.
721  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
722  **/
723 void
724 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
725
726 {
727         struct lpfc_hba *phba = vport->phba;
728         struct lpfc_node_rrq *rrq;
729         struct lpfc_node_rrq *nextrrq;
730         unsigned long iflags;
731         LIST_HEAD(rrq_list);
732
733         if (phba->sli_rev != LPFC_SLI_REV4)
734                 return;
735         if (!ndlp) {
736                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
737                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
738         }
739         spin_lock_irqsave(&phba->hbalock, iflags);
740         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
741                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
742                         list_move(&rrq->list, &rrq_list);
743         spin_unlock_irqrestore(&phba->hbalock, iflags);
744
745         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
746                 list_del(&rrq->list);
747                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
748         }
749 }
750
751 /**
752  * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
753  * @phba: Pointer to HBA context object.
754  *
755  * Remove all rrqs from the phba->active_rrq_list and free them by
756  * calling __lpfc_clr_active_rrq
757  *
758  **/
759 void
760 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
761 {
762         struct lpfc_node_rrq *rrq;
763         struct lpfc_node_rrq *nextrrq;
764         unsigned long next_time;
765         unsigned long iflags;
766         LIST_HEAD(rrq_list);
767
768         if (phba->sli_rev != LPFC_SLI_REV4)
769                 return;
770         spin_lock_irqsave(&phba->hbalock, iflags);
771         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
772         next_time = jiffies + HZ * (phba->fc_ratov * 2);
773         list_splice_init(&phba->active_rrq_list, &rrq_list);
774         spin_unlock_irqrestore(&phba->hbalock, iflags);
775
776         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
777                 list_del(&rrq->list);
778                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
779         }
780         if (!list_empty(&phba->active_rrq_list))
781                 mod_timer(&phba->rrq_tmr, next_time);
782 }
783
784
785 /**
786  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
787  * @phba: Pointer to HBA context object.
788  * @ndlp: Targets nodelist pointer for this exchange.
789  * @xritag the xri in the bitmap to test.
790  *
791  * This function is called with hbalock held. This function
792  * returns 0 = rrq not active for this xri
793  *         1 = rrq is valid for this xri.
794  **/
795 int
796 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
797                         uint16_t  xritag)
798 {
799         uint16_t adj_xri;
800
801         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
802         if (!ndlp)
803                 return 0;
804         if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
805                         return 1;
806         else
807                 return 0;
808 }
809
810 /**
811  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
812  * @phba: Pointer to HBA context object.
813  * @ndlp: nodelist pointer for this target.
814  * @xritag: xri used in this exchange.
815  * @rxid: Remote Exchange ID.
816  * @send_rrq: Flag used to determine if we should send rrq els cmd.
817  *
818  * This function takes the hbalock.
819  * The active bit is always set in the active rrq xri_bitmap even
820  * if there is no slot avaiable for the other rrq information.
821  *
822  * returns 0 rrq actived for this xri
823  *         < 0 No memory or invalid ndlp.
824  **/
825 int
826 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
827                         uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
828 {
829         int ret;
830         unsigned long iflags;
831
832         spin_lock_irqsave(&phba->hbalock, iflags);
833         ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
834         spin_unlock_irqrestore(&phba->hbalock, iflags);
835         return ret;
836 }
837
838 /**
839  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
840  * @phba: Pointer to HBA context object.
841  * @piocb: Pointer to the iocbq.
842  *
843  * This function is called with hbalock held. This function
844  * Gets a new driver sglq object from the sglq list. If the
845  * list is not empty then it is successful, it returns pointer to the newly
846  * allocated sglq object else it returns NULL.
847  **/
848 static struct lpfc_sglq *
849 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
850 {
851         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
852         struct lpfc_sglq *sglq = NULL;
853         struct lpfc_sglq *start_sglq = NULL;
854         uint16_t adj_xri;
855         struct lpfc_scsi_buf *lpfc_cmd;
856         struct lpfc_nodelist *ndlp;
857         int found = 0;
858
859         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
860                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
861                 ndlp = lpfc_cmd->rdata->pnode;
862         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
863                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
864                 ndlp = piocbq->context_un.ndlp;
865         else
866                 ndlp = piocbq->context1;
867
868         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
869         start_sglq = sglq;
870         while (!found) {
871                 if (!sglq)
872                         return NULL;
873                 adj_xri = sglq->sli4_xritag -
874                                 phba->sli4_hba.max_cfg_param.xri_base;
875                 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
876                         /* This xri has an rrq outstanding for this DID.
877                          * put it back in the list and get another xri.
878                          */
879                         list_add_tail(&sglq->list, lpfc_sgl_list);
880                         sglq = NULL;
881                         list_remove_head(lpfc_sgl_list, sglq,
882                                                 struct lpfc_sglq, list);
883                         if (sglq == start_sglq) {
884                                 sglq = NULL;
885                                 break;
886                         } else
887                                 continue;
888                 }
889                 sglq->ndlp = ndlp;
890                 found = 1;
891                 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
892                 sglq->state = SGL_ALLOCATED;
893         }
894         return sglq;
895 }
896
897 /**
898  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
899  * @phba: Pointer to HBA context object.
900  *
901  * This function is called with no lock held. This function
902  * allocates a new driver iocb object from the iocb pool. If the
903  * allocation is successful, it returns pointer to the newly
904  * allocated iocb object else it returns NULL.
905  **/
906 struct lpfc_iocbq *
907 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
908 {
909         struct lpfc_iocbq * iocbq = NULL;
910         unsigned long iflags;
911
912         spin_lock_irqsave(&phba->hbalock, iflags);
913         iocbq = __lpfc_sli_get_iocbq(phba);
914         spin_unlock_irqrestore(&phba->hbalock, iflags);
915         return iocbq;
916 }
917
918 /**
919  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
920  * @phba: Pointer to HBA context object.
921  * @iocbq: Pointer to driver iocb object.
922  *
923  * This function is called with hbalock held to release driver
924  * iocb object to the iocb pool. The iotag in the iocb object
925  * does not change for each use of the iocb object. This function
926  * clears all other fields of the iocb object when it is freed.
927  * The sqlq structure that holds the xritag and phys and virtual
928  * mappings for the scatter gather list is retrieved from the
929  * active array of sglq. The get of the sglq pointer also clears
930  * the entry in the array. If the status of the IO indiactes that
931  * this IO was aborted then the sglq entry it put on the
932  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
933  * IO has good status or fails for any other reason then the sglq
934  * entry is added to the free list (lpfc_sgl_list).
935  **/
936 static void
937 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
938 {
939         struct lpfc_sglq *sglq;
940         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
941         unsigned long iflag = 0;
942         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
943
944         if (iocbq->sli4_xritag == NO_XRI)
945                 sglq = NULL;
946         else
947                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
948         if (sglq)  {
949                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
950                         (sglq->state != SGL_XRI_ABORTED)) {
951                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
952                                         iflag);
953                         list_add(&sglq->list,
954                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
955                         spin_unlock_irqrestore(
956                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
957                 } else {
958                         sglq->state = SGL_FREED;
959                         sglq->ndlp = NULL;
960                         list_add_tail(&sglq->list,
961                                 &phba->sli4_hba.lpfc_sgl_list);
962
963                         /* Check if TXQ queue needs to be serviced */
964                         if (pring->txq_cnt)
965                                 lpfc_worker_wake_up(phba);
966                 }
967         }
968
969
970         /*
971          * Clean all volatile data fields, preserve iotag and node struct.
972          */
973         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
974         iocbq->sli4_xritag = NO_XRI;
975         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
976 }
977
978
979 /**
980  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
981  * @phba: Pointer to HBA context object.
982  * @iocbq: Pointer to driver iocb object.
983  *
984  * This function is called with hbalock held to release driver
985  * iocb object to the iocb pool. The iotag in the iocb object
986  * does not change for each use of the iocb object. This function
987  * clears all other fields of the iocb object when it is freed.
988  **/
989 static void
990 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
991 {
992         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
993
994         /*
995          * Clean all volatile data fields, preserve iotag and node struct.
996          */
997         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
998         iocbq->sli4_xritag = NO_XRI;
999         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1000 }
1001
1002 /**
1003  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1004  * @phba: Pointer to HBA context object.
1005  * @iocbq: Pointer to driver iocb object.
1006  *
1007  * This function is called with hbalock held to release driver
1008  * iocb object to the iocb pool. The iotag in the iocb object
1009  * does not change for each use of the iocb object. This function
1010  * clears all other fields of the iocb object when it is freed.
1011  **/
1012 static void
1013 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1014 {
1015         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1016         phba->iocb_cnt--;
1017 }
1018
1019 /**
1020  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1021  * @phba: Pointer to HBA context object.
1022  * @iocbq: Pointer to driver iocb object.
1023  *
1024  * This function is called with no lock held to release the iocb to
1025  * iocb pool.
1026  **/
1027 void
1028 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1029 {
1030         unsigned long iflags;
1031
1032         /*
1033          * Clean all volatile data fields, preserve iotag and node struct.
1034          */
1035         spin_lock_irqsave(&phba->hbalock, iflags);
1036         __lpfc_sli_release_iocbq(phba, iocbq);
1037         spin_unlock_irqrestore(&phba->hbalock, iflags);
1038 }
1039
1040 /**
1041  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1042  * @phba: Pointer to HBA context object.
1043  * @iocblist: List of IOCBs.
1044  * @ulpstatus: ULP status in IOCB command field.
1045  * @ulpWord4: ULP word-4 in IOCB command field.
1046  *
1047  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1048  * on the list by invoking the complete callback function associated with the
1049  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1050  * fields.
1051  **/
1052 void
1053 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1054                       uint32_t ulpstatus, uint32_t ulpWord4)
1055 {
1056         struct lpfc_iocbq *piocb;
1057
1058         while (!list_empty(iocblist)) {
1059                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1060
1061                 if (!piocb->iocb_cmpl)
1062                         lpfc_sli_release_iocbq(phba, piocb);
1063                 else {
1064                         piocb->iocb.ulpStatus = ulpstatus;
1065                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1066                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1067                 }
1068         }
1069         return;
1070 }
1071
1072 /**
1073  * lpfc_sli_iocb_cmd_type - Get the iocb type
1074  * @iocb_cmnd: iocb command code.
1075  *
1076  * This function is called by ring event handler function to get the iocb type.
1077  * This function translates the iocb command to an iocb command type used to
1078  * decide the final disposition of each completed IOCB.
1079  * The function returns
1080  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1081  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1082  * LPFC_ABORT_IOCB   if it is an abort iocb
1083  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1084  *
1085  * The caller is not required to hold any lock.
1086  **/
1087 static lpfc_iocb_type
1088 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1089 {
1090         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1091
1092         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1093                 return 0;
1094
1095         switch (iocb_cmnd) {
1096         case CMD_XMIT_SEQUENCE_CR:
1097         case CMD_XMIT_SEQUENCE_CX:
1098         case CMD_XMIT_BCAST_CN:
1099         case CMD_XMIT_BCAST_CX:
1100         case CMD_ELS_REQUEST_CR:
1101         case CMD_ELS_REQUEST_CX:
1102         case CMD_CREATE_XRI_CR:
1103         case CMD_CREATE_XRI_CX:
1104         case CMD_GET_RPI_CN:
1105         case CMD_XMIT_ELS_RSP_CX:
1106         case CMD_GET_RPI_CR:
1107         case CMD_FCP_IWRITE_CR:
1108         case CMD_FCP_IWRITE_CX:
1109         case CMD_FCP_IREAD_CR:
1110         case CMD_FCP_IREAD_CX:
1111         case CMD_FCP_ICMND_CR:
1112         case CMD_FCP_ICMND_CX:
1113         case CMD_FCP_TSEND_CX:
1114         case CMD_FCP_TRSP_CX:
1115         case CMD_FCP_TRECEIVE_CX:
1116         case CMD_FCP_AUTO_TRSP_CX:
1117         case CMD_ADAPTER_MSG:
1118         case CMD_ADAPTER_DUMP:
1119         case CMD_XMIT_SEQUENCE64_CR:
1120         case CMD_XMIT_SEQUENCE64_CX:
1121         case CMD_XMIT_BCAST64_CN:
1122         case CMD_XMIT_BCAST64_CX:
1123         case CMD_ELS_REQUEST64_CR:
1124         case CMD_ELS_REQUEST64_CX:
1125         case CMD_FCP_IWRITE64_CR:
1126         case CMD_FCP_IWRITE64_CX:
1127         case CMD_FCP_IREAD64_CR:
1128         case CMD_FCP_IREAD64_CX:
1129         case CMD_FCP_ICMND64_CR:
1130         case CMD_FCP_ICMND64_CX:
1131         case CMD_FCP_TSEND64_CX:
1132         case CMD_FCP_TRSP64_CX:
1133         case CMD_FCP_TRECEIVE64_CX:
1134         case CMD_GEN_REQUEST64_CR:
1135         case CMD_GEN_REQUEST64_CX:
1136         case CMD_XMIT_ELS_RSP64_CX:
1137         case DSSCMD_IWRITE64_CR:
1138         case DSSCMD_IWRITE64_CX:
1139         case DSSCMD_IREAD64_CR:
1140         case DSSCMD_IREAD64_CX:
1141                 type = LPFC_SOL_IOCB;
1142                 break;
1143         case CMD_ABORT_XRI_CN:
1144         case CMD_ABORT_XRI_CX:
1145         case CMD_CLOSE_XRI_CN:
1146         case CMD_CLOSE_XRI_CX:
1147         case CMD_XRI_ABORTED_CX:
1148         case CMD_ABORT_MXRI64_CN:
1149         case CMD_XMIT_BLS_RSP64_CX:
1150                 type = LPFC_ABORT_IOCB;
1151                 break;
1152         case CMD_RCV_SEQUENCE_CX:
1153         case CMD_RCV_ELS_REQ_CX:
1154         case CMD_RCV_SEQUENCE64_CX:
1155         case CMD_RCV_ELS_REQ64_CX:
1156         case CMD_ASYNC_STATUS:
1157         case CMD_IOCB_RCV_SEQ64_CX:
1158         case CMD_IOCB_RCV_ELS64_CX:
1159         case CMD_IOCB_RCV_CONT64_CX:
1160         case CMD_IOCB_RET_XRI64_CX:
1161                 type = LPFC_UNSOL_IOCB;
1162                 break;
1163         case CMD_IOCB_XMIT_MSEQ64_CR:
1164         case CMD_IOCB_XMIT_MSEQ64_CX:
1165         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1166         case CMD_IOCB_RCV_ELS_LIST64_CX:
1167         case CMD_IOCB_CLOSE_EXTENDED_CN:
1168         case CMD_IOCB_ABORT_EXTENDED_CN:
1169         case CMD_IOCB_RET_HBQE64_CN:
1170         case CMD_IOCB_FCP_IBIDIR64_CR:
1171         case CMD_IOCB_FCP_IBIDIR64_CX:
1172         case CMD_IOCB_FCP_ITASKMGT64_CX:
1173         case CMD_IOCB_LOGENTRY_CN:
1174         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1175                 printk("%s - Unhandled SLI-3 Command x%x\n",
1176                                 __func__, iocb_cmnd);
1177                 type = LPFC_UNKNOWN_IOCB;
1178                 break;
1179         default:
1180                 type = LPFC_UNKNOWN_IOCB;
1181                 break;
1182         }
1183
1184         return type;
1185 }
1186
1187 /**
1188  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1189  * @phba: Pointer to HBA context object.
1190  *
1191  * This function is called from SLI initialization code
1192  * to configure every ring of the HBA's SLI interface. The
1193  * caller is not required to hold any lock. This function issues
1194  * a config_ring mailbox command for each ring.
1195  * This function returns zero if successful else returns a negative
1196  * error code.
1197  **/
1198 static int
1199 lpfc_sli_ring_map(struct lpfc_hba *phba)
1200 {
1201         struct lpfc_sli *psli = &phba->sli;
1202         LPFC_MBOXQ_t *pmb;
1203         MAILBOX_t *pmbox;
1204         int i, rc, ret = 0;
1205
1206         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1207         if (!pmb)
1208                 return -ENOMEM;
1209         pmbox = &pmb->u.mb;
1210         phba->link_state = LPFC_INIT_MBX_CMDS;
1211         for (i = 0; i < psli->num_rings; i++) {
1212                 lpfc_config_ring(phba, i, pmb);
1213                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1214                 if (rc != MBX_SUCCESS) {
1215                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1216                                         "0446 Adapter failed to init (%d), "
1217                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1218                                         "ring %d\n",
1219                                         rc, pmbox->mbxCommand,
1220                                         pmbox->mbxStatus, i);
1221                         phba->link_state = LPFC_HBA_ERROR;
1222                         ret = -ENXIO;
1223                         break;
1224                 }
1225         }
1226         mempool_free(pmb, phba->mbox_mem_pool);
1227         return ret;
1228 }
1229
1230 /**
1231  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1232  * @phba: Pointer to HBA context object.
1233  * @pring: Pointer to driver SLI ring object.
1234  * @piocb: Pointer to the driver iocb object.
1235  *
1236  * This function is called with hbalock held. The function adds the
1237  * new iocb to txcmplq of the given ring. This function always returns
1238  * 0. If this function is called for ELS ring, this function checks if
1239  * there is a vport associated with the ELS command. This function also
1240  * starts els_tmofunc timer if this is an ELS command.
1241  **/
1242 static int
1243 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1244                         struct lpfc_iocbq *piocb)
1245 {
1246         list_add_tail(&piocb->list, &pring->txcmplq);
1247         piocb->iocb_flag |= LPFC_IO_ON_Q;
1248         pring->txcmplq_cnt++;
1249         if (pring->txcmplq_cnt > pring->txcmplq_max)
1250                 pring->txcmplq_max = pring->txcmplq_cnt;
1251
1252         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1253            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1254            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1255                 if (!piocb->vport)
1256                         BUG();
1257                 else
1258                         mod_timer(&piocb->vport->els_tmofunc,
1259                                   jiffies + HZ * (phba->fc_ratov << 1));
1260         }
1261
1262
1263         return 0;
1264 }
1265
1266 /**
1267  * lpfc_sli_ringtx_get - Get first element of the txq
1268  * @phba: Pointer to HBA context object.
1269  * @pring: Pointer to driver SLI ring object.
1270  *
1271  * This function is called with hbalock held to get next
1272  * iocb in txq of the given ring. If there is any iocb in
1273  * the txq, the function returns first iocb in the list after
1274  * removing the iocb from the list, else it returns NULL.
1275  **/
1276 struct lpfc_iocbq *
1277 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1278 {
1279         struct lpfc_iocbq *cmd_iocb;
1280
1281         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1282         if (cmd_iocb != NULL)
1283                 pring->txq_cnt--;
1284         return cmd_iocb;
1285 }
1286
1287 /**
1288  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1289  * @phba: Pointer to HBA context object.
1290  * @pring: Pointer to driver SLI ring object.
1291  *
1292  * This function is called with hbalock held and the caller must post the
1293  * iocb without releasing the lock. If the caller releases the lock,
1294  * iocb slot returned by the function is not guaranteed to be available.
1295  * The function returns pointer to the next available iocb slot if there
1296  * is available slot in the ring, else it returns NULL.
1297  * If the get index of the ring is ahead of the put index, the function
1298  * will post an error attention event to the worker thread to take the
1299  * HBA to offline state.
1300  **/
1301 static IOCB_t *
1302 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1303 {
1304         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1305         uint32_t  max_cmd_idx = pring->numCiocb;
1306         if ((pring->next_cmdidx == pring->cmdidx) &&
1307            (++pring->next_cmdidx >= max_cmd_idx))
1308                 pring->next_cmdidx = 0;
1309
1310         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1311
1312                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1313
1314                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1315                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1316                                         "0315 Ring %d issue: portCmdGet %d "
1317                                         "is bigger than cmd ring %d\n",
1318                                         pring->ringno,
1319                                         pring->local_getidx, max_cmd_idx);
1320
1321                         phba->link_state = LPFC_HBA_ERROR;
1322                         /*
1323                          * All error attention handlers are posted to
1324                          * worker thread
1325                          */
1326                         phba->work_ha |= HA_ERATT;
1327                         phba->work_hs = HS_FFER3;
1328
1329                         lpfc_worker_wake_up(phba);
1330
1331                         return NULL;
1332                 }
1333
1334                 if (pring->local_getidx == pring->next_cmdidx)
1335                         return NULL;
1336         }
1337
1338         return lpfc_cmd_iocb(phba, pring);
1339 }
1340
1341 /**
1342  * lpfc_sli_next_iotag - Get an iotag for the iocb
1343  * @phba: Pointer to HBA context object.
1344  * @iocbq: Pointer to driver iocb object.
1345  *
1346  * This function gets an iotag for the iocb. If there is no unused iotag and
1347  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1348  * array and assigns a new iotag.
1349  * The function returns the allocated iotag if successful, else returns zero.
1350  * Zero is not a valid iotag.
1351  * The caller is not required to hold any lock.
1352  **/
1353 uint16_t
1354 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1355 {
1356         struct lpfc_iocbq **new_arr;
1357         struct lpfc_iocbq **old_arr;
1358         size_t new_len;
1359         struct lpfc_sli *psli = &phba->sli;
1360         uint16_t iotag;
1361
1362         spin_lock_irq(&phba->hbalock);
1363         iotag = psli->last_iotag;
1364         if(++iotag < psli->iocbq_lookup_len) {
1365                 psli->last_iotag = iotag;
1366                 psli->iocbq_lookup[iotag] = iocbq;
1367                 spin_unlock_irq(&phba->hbalock);
1368                 iocbq->iotag = iotag;
1369                 return iotag;
1370         } else if (psli->iocbq_lookup_len < (0xffff
1371                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1372                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1373                 spin_unlock_irq(&phba->hbalock);
1374                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1375                                   GFP_KERNEL);
1376                 if (new_arr) {
1377                         spin_lock_irq(&phba->hbalock);
1378                         old_arr = psli->iocbq_lookup;
1379                         if (new_len <= psli->iocbq_lookup_len) {
1380                                 /* highly unprobable case */
1381                                 kfree(new_arr);
1382                                 iotag = psli->last_iotag;
1383                                 if(++iotag < psli->iocbq_lookup_len) {
1384                                         psli->last_iotag = iotag;
1385                                         psli->iocbq_lookup[iotag] = iocbq;
1386                                         spin_unlock_irq(&phba->hbalock);
1387                                         iocbq->iotag = iotag;
1388                                         return iotag;
1389                                 }
1390                                 spin_unlock_irq(&phba->hbalock);
1391                                 return 0;
1392                         }
1393                         if (psli->iocbq_lookup)
1394                                 memcpy(new_arr, old_arr,
1395                                        ((psli->last_iotag  + 1) *
1396                                         sizeof (struct lpfc_iocbq *)));
1397                         psli->iocbq_lookup = new_arr;
1398                         psli->iocbq_lookup_len = new_len;
1399                         psli->last_iotag = iotag;
1400                         psli->iocbq_lookup[iotag] = iocbq;
1401                         spin_unlock_irq(&phba->hbalock);
1402                         iocbq->iotag = iotag;
1403                         kfree(old_arr);
1404                         return iotag;
1405                 }
1406         } else
1407                 spin_unlock_irq(&phba->hbalock);
1408
1409         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1410                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1411                         psli->last_iotag);
1412
1413         return 0;
1414 }
1415
1416 /**
1417  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1418  * @phba: Pointer to HBA context object.
1419  * @pring: Pointer to driver SLI ring object.
1420  * @iocb: Pointer to iocb slot in the ring.
1421  * @nextiocb: Pointer to driver iocb object which need to be
1422  *            posted to firmware.
1423  *
1424  * This function is called with hbalock held to post a new iocb to
1425  * the firmware. This function copies the new iocb to ring iocb slot and
1426  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1427  * a completion call back for this iocb else the function will free the
1428  * iocb object.
1429  **/
1430 static void
1431 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1432                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1433 {
1434         /*
1435          * Set up an iotag
1436          */
1437         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1438
1439
1440         if (pring->ringno == LPFC_ELS_RING) {
1441                 lpfc_debugfs_slow_ring_trc(phba,
1442                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1443                         *(((uint32_t *) &nextiocb->iocb) + 4),
1444                         *(((uint32_t *) &nextiocb->iocb) + 6),
1445                         *(((uint32_t *) &nextiocb->iocb) + 7));
1446         }
1447
1448         /*
1449          * Issue iocb command to adapter
1450          */
1451         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1452         wmb();
1453         pring->stats.iocb_cmd++;
1454
1455         /*
1456          * If there is no completion routine to call, we can release the
1457          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1458          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1459          */
1460         if (nextiocb->iocb_cmpl)
1461                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1462         else
1463                 __lpfc_sli_release_iocbq(phba, nextiocb);
1464
1465         /*
1466          * Let the HBA know what IOCB slot will be the next one the
1467          * driver will put a command into.
1468          */
1469         pring->cmdidx = pring->next_cmdidx;
1470         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1471 }
1472
1473 /**
1474  * lpfc_sli_update_full_ring - Update the chip attention register
1475  * @phba: Pointer to HBA context object.
1476  * @pring: Pointer to driver SLI ring object.
1477  *
1478  * The caller is not required to hold any lock for calling this function.
1479  * This function updates the chip attention bits for the ring to inform firmware
1480  * that there are pending work to be done for this ring and requests an
1481  * interrupt when there is space available in the ring. This function is
1482  * called when the driver is unable to post more iocbs to the ring due
1483  * to unavailability of space in the ring.
1484  **/
1485 static void
1486 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1487 {
1488         int ringno = pring->ringno;
1489
1490         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1491
1492         wmb();
1493
1494         /*
1495          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1496          * The HBA will tell us when an IOCB entry is available.
1497          */
1498         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1499         readl(phba->CAregaddr); /* flush */
1500
1501         pring->stats.iocb_cmd_full++;
1502 }
1503
1504 /**
1505  * lpfc_sli_update_ring - Update chip attention register
1506  * @phba: Pointer to HBA context object.
1507  * @pring: Pointer to driver SLI ring object.
1508  *
1509  * This function updates the chip attention register bit for the
1510  * given ring to inform HBA that there is more work to be done
1511  * in this ring. The caller is not required to hold any lock.
1512  **/
1513 static void
1514 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1515 {
1516         int ringno = pring->ringno;
1517
1518         /*
1519          * Tell the HBA that there is work to do in this ring.
1520          */
1521         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1522                 wmb();
1523                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1524                 readl(phba->CAregaddr); /* flush */
1525         }
1526 }
1527
1528 /**
1529  * lpfc_sli_resume_iocb - Process iocbs in the txq
1530  * @phba: Pointer to HBA context object.
1531  * @pring: Pointer to driver SLI ring object.
1532  *
1533  * This function is called with hbalock held to post pending iocbs
1534  * in the txq to the firmware. This function is called when driver
1535  * detects space available in the ring.
1536  **/
1537 static void
1538 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1539 {
1540         IOCB_t *iocb;
1541         struct lpfc_iocbq *nextiocb;
1542
1543         /*
1544          * Check to see if:
1545          *  (a) there is anything on the txq to send
1546          *  (b) link is up
1547          *  (c) link attention events can be processed (fcp ring only)
1548          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1549          */
1550         if (pring->txq_cnt &&
1551             lpfc_is_link_up(phba) &&
1552             (pring->ringno != phba->sli.fcp_ring ||
1553              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1554
1555                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1556                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1557                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1558
1559                 if (iocb)
1560                         lpfc_sli_update_ring(phba, pring);
1561                 else
1562                         lpfc_sli_update_full_ring(phba, pring);
1563         }
1564
1565         return;
1566 }
1567
1568 /**
1569  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1570  * @phba: Pointer to HBA context object.
1571  * @hbqno: HBQ number.
1572  *
1573  * This function is called with hbalock held to get the next
1574  * available slot for the given HBQ. If there is free slot
1575  * available for the HBQ it will return pointer to the next available
1576  * HBQ entry else it will return NULL.
1577  **/
1578 static struct lpfc_hbq_entry *
1579 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1580 {
1581         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1582
1583         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1584             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1585                 hbqp->next_hbqPutIdx = 0;
1586
1587         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1588                 uint32_t raw_index = phba->hbq_get[hbqno];
1589                 uint32_t getidx = le32_to_cpu(raw_index);
1590
1591                 hbqp->local_hbqGetIdx = getidx;
1592
1593                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1594                         lpfc_printf_log(phba, KERN_ERR,
1595                                         LOG_SLI | LOG_VPORT,
1596                                         "1802 HBQ %d: local_hbqGetIdx "
1597                                         "%u is > than hbqp->entry_count %u\n",
1598                                         hbqno, hbqp->local_hbqGetIdx,
1599                                         hbqp->entry_count);
1600
1601                         phba->link_state = LPFC_HBA_ERROR;
1602                         return NULL;
1603                 }
1604
1605                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1606                         return NULL;
1607         }
1608
1609         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1610                         hbqp->hbqPutIdx;
1611 }
1612
1613 /**
1614  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1615  * @phba: Pointer to HBA context object.
1616  *
1617  * This function is called with no lock held to free all the
1618  * hbq buffers while uninitializing the SLI interface. It also
1619  * frees the HBQ buffers returned by the firmware but not yet
1620  * processed by the upper layers.
1621  **/
1622 void
1623 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1624 {
1625         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1626         struct hbq_dmabuf *hbq_buf;
1627         unsigned long flags;
1628         int i, hbq_count;
1629         uint32_t hbqno;
1630
1631         hbq_count = lpfc_sli_hbq_count();
1632         /* Return all memory used by all HBQs */
1633         spin_lock_irqsave(&phba->hbalock, flags);
1634         for (i = 0; i < hbq_count; ++i) {
1635                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1636                                 &phba->hbqs[i].hbq_buffer_list, list) {
1637                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1638                         list_del(&hbq_buf->dbuf.list);
1639                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1640                 }
1641                 phba->hbqs[i].buffer_count = 0;
1642         }
1643         /* Return all HBQ buffer that are in-fly */
1644         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1645                                  list) {
1646                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1647                 list_del(&hbq_buf->dbuf.list);
1648                 if (hbq_buf->tag == -1) {
1649                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1650                                 (phba, hbq_buf);
1651                 } else {
1652                         hbqno = hbq_buf->tag >> 16;
1653                         if (hbqno >= LPFC_MAX_HBQS)
1654                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1655                                         (phba, hbq_buf);
1656                         else
1657                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1658                                         hbq_buf);
1659                 }
1660         }
1661
1662         /* Mark the HBQs not in use */
1663         phba->hbq_in_use = 0;
1664         spin_unlock_irqrestore(&phba->hbalock, flags);
1665 }
1666
1667 /**
1668  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1669  * @phba: Pointer to HBA context object.
1670  * @hbqno: HBQ number.
1671  * @hbq_buf: Pointer to HBQ buffer.
1672  *
1673  * This function is called with the hbalock held to post a
1674  * hbq buffer to the firmware. If the function finds an empty
1675  * slot in the HBQ, it will post the buffer. The function will return
1676  * pointer to the hbq entry if it successfully post the buffer
1677  * else it will return NULL.
1678  **/
1679 static int
1680 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1681                          struct hbq_dmabuf *hbq_buf)
1682 {
1683         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1684 }
1685
1686 /**
1687  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1688  * @phba: Pointer to HBA context object.
1689  * @hbqno: HBQ number.
1690  * @hbq_buf: Pointer to HBQ buffer.
1691  *
1692  * This function is called with the hbalock held to post a hbq buffer to the
1693  * firmware. If the function finds an empty slot in the HBQ, it will post the
1694  * buffer and place it on the hbq_buffer_list. The function will return zero if
1695  * it successfully post the buffer else it will return an error.
1696  **/
1697 static int
1698 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1699                             struct hbq_dmabuf *hbq_buf)
1700 {
1701         struct lpfc_hbq_entry *hbqe;
1702         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1703
1704         /* Get next HBQ entry slot to use */
1705         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1706         if (hbqe) {
1707                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1708
1709                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1710                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1711                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1712                 hbqe->bde.tus.f.bdeFlags = 0;
1713                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1714                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1715                                 /* Sync SLIM */
1716                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1717                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1718                                 /* flush */
1719                 readl(phba->hbq_put + hbqno);
1720                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1721                 return 0;
1722         } else
1723                 return -ENOMEM;
1724 }
1725
1726 /**
1727  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1728  * @phba: Pointer to HBA context object.
1729  * @hbqno: HBQ number.
1730  * @hbq_buf: Pointer to HBQ buffer.
1731  *
1732  * This function is called with the hbalock held to post an RQE to the SLI4
1733  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1734  * the hbq_buffer_list and return zero, otherwise it will return an error.
1735  **/
1736 static int
1737 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1738                             struct hbq_dmabuf *hbq_buf)
1739 {
1740         int rc;
1741         struct lpfc_rqe hrqe;
1742         struct lpfc_rqe drqe;
1743
1744         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1745         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1746         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1747         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1748         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1749                               &hrqe, &drqe);
1750         if (rc < 0)
1751                 return rc;
1752         hbq_buf->tag = rc;
1753         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1754         return 0;
1755 }
1756
1757 /* HBQ for ELS and CT traffic. */
1758 static struct lpfc_hbq_init lpfc_els_hbq = {
1759         .rn = 1,
1760         .entry_count = 256,
1761         .mask_count = 0,
1762         .profile = 0,
1763         .ring_mask = (1 << LPFC_ELS_RING),
1764         .buffer_count = 0,
1765         .init_count = 40,
1766         .add_count = 40,
1767 };
1768
1769 /* HBQ for the extra ring if needed */
1770 static struct lpfc_hbq_init lpfc_extra_hbq = {
1771         .rn = 1,
1772         .entry_count = 200,
1773         .mask_count = 0,
1774         .profile = 0,
1775         .ring_mask = (1 << LPFC_EXTRA_RING),
1776         .buffer_count = 0,
1777         .init_count = 0,
1778         .add_count = 5,
1779 };
1780
1781 /* Array of HBQs */
1782 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1783         &lpfc_els_hbq,
1784         &lpfc_extra_hbq,
1785 };
1786
1787 /**
1788  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1789  * @phba: Pointer to HBA context object.
1790  * @hbqno: HBQ number.
1791  * @count: Number of HBQ buffers to be posted.
1792  *
1793  * This function is called with no lock held to post more hbq buffers to the
1794  * given HBQ. The function returns the number of HBQ buffers successfully
1795  * posted.
1796  **/
1797 static int
1798 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1799 {
1800         uint32_t i, posted = 0;
1801         unsigned long flags;
1802         struct hbq_dmabuf *hbq_buffer;
1803         LIST_HEAD(hbq_buf_list);
1804         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1805                 return 0;
1806
1807         if ((phba->hbqs[hbqno].buffer_count + count) >
1808             lpfc_hbq_defs[hbqno]->entry_count)
1809                 count = lpfc_hbq_defs[hbqno]->entry_count -
1810                                         phba->hbqs[hbqno].buffer_count;
1811         if (!count)
1812                 return 0;
1813         /* Allocate HBQ entries */
1814         for (i = 0; i < count; i++) {
1815                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1816                 if (!hbq_buffer)
1817                         break;
1818                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1819         }
1820         /* Check whether HBQ is still in use */
1821         spin_lock_irqsave(&phba->hbalock, flags);
1822         if (!phba->hbq_in_use)
1823                 goto err;
1824         while (!list_empty(&hbq_buf_list)) {
1825                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1826                                  dbuf.list);
1827                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1828                                       (hbqno << 16));
1829                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1830                         phba->hbqs[hbqno].buffer_count++;
1831                         posted++;
1832                 } else
1833                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1834         }
1835         spin_unlock_irqrestore(&phba->hbalock, flags);
1836         return posted;
1837 err:
1838         spin_unlock_irqrestore(&phba->hbalock, flags);
1839         while (!list_empty(&hbq_buf_list)) {
1840                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1841                                  dbuf.list);
1842                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1843         }
1844         return 0;
1845 }
1846
1847 /**
1848  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1849  * @phba: Pointer to HBA context object.
1850  * @qno: HBQ number.
1851  *
1852  * This function posts more buffers to the HBQ. This function
1853  * is called with no lock held. The function returns the number of HBQ entries
1854  * successfully allocated.
1855  **/
1856 int
1857 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1858 {
1859         if (phba->sli_rev == LPFC_SLI_REV4)
1860                 return 0;
1861         else
1862                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1863                                          lpfc_hbq_defs[qno]->add_count);
1864 }
1865
1866 /**
1867  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1868  * @phba: Pointer to HBA context object.
1869  * @qno:  HBQ queue number.
1870  *
1871  * This function is called from SLI initialization code path with
1872  * no lock held to post initial HBQ buffers to firmware. The
1873  * function returns the number of HBQ entries successfully allocated.
1874  **/
1875 static int
1876 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1877 {
1878         if (phba->sli_rev == LPFC_SLI_REV4)
1879                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1880                                          lpfc_hbq_defs[qno]->entry_count);
1881         else
1882                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1883                                          lpfc_hbq_defs[qno]->init_count);
1884 }
1885
1886 /**
1887  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1888  * @phba: Pointer to HBA context object.
1889  * @hbqno: HBQ number.
1890  *
1891  * This function removes the first hbq buffer on an hbq list and returns a
1892  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1893  **/
1894 static struct hbq_dmabuf *
1895 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1896 {
1897         struct lpfc_dmabuf *d_buf;
1898
1899         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1900         if (!d_buf)
1901                 return NULL;
1902         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1903 }
1904
1905 /**
1906  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1907  * @phba: Pointer to HBA context object.
1908  * @tag: Tag of the hbq buffer.
1909  *
1910  * This function is called with hbalock held. This function searches
1911  * for the hbq buffer associated with the given tag in the hbq buffer
1912  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1913  * it returns NULL.
1914  **/
1915 static struct hbq_dmabuf *
1916 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1917 {
1918         struct lpfc_dmabuf *d_buf;
1919         struct hbq_dmabuf *hbq_buf;
1920         uint32_t hbqno;
1921
1922         hbqno = tag >> 16;
1923         if (hbqno >= LPFC_MAX_HBQS)
1924                 return NULL;
1925
1926         spin_lock_irq(&phba->hbalock);
1927         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1928                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1929                 if (hbq_buf->tag == tag) {
1930                         spin_unlock_irq(&phba->hbalock);
1931                         return hbq_buf;
1932                 }
1933         }
1934         spin_unlock_irq(&phba->hbalock);
1935         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1936                         "1803 Bad hbq tag. Data: x%x x%x\n",
1937                         tag, phba->hbqs[tag >> 16].buffer_count);
1938         return NULL;
1939 }
1940
1941 /**
1942  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1943  * @phba: Pointer to HBA context object.
1944  * @hbq_buffer: Pointer to HBQ buffer.
1945  *
1946  * This function is called with hbalock. This function gives back
1947  * the hbq buffer to firmware. If the HBQ does not have space to
1948  * post the buffer, it will free the buffer.
1949  **/
1950 void
1951 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1952 {
1953         uint32_t hbqno;
1954
1955         if (hbq_buffer) {
1956                 hbqno = hbq_buffer->tag >> 16;
1957                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1958                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1959         }
1960 }
1961
1962 /**
1963  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1964  * @mbxCommand: mailbox command code.
1965  *
1966  * This function is called by the mailbox event handler function to verify
1967  * that the completed mailbox command is a legitimate mailbox command. If the
1968  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1969  * and the mailbox event handler will take the HBA offline.
1970  **/
1971 static int
1972 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1973 {
1974         uint8_t ret;
1975
1976         switch (mbxCommand) {
1977         case MBX_LOAD_SM:
1978         case MBX_READ_NV:
1979         case MBX_WRITE_NV:
1980         case MBX_WRITE_VPARMS:
1981         case MBX_RUN_BIU_DIAG:
1982         case MBX_INIT_LINK:
1983         case MBX_DOWN_LINK:
1984         case MBX_CONFIG_LINK:
1985         case MBX_CONFIG_RING:
1986         case MBX_RESET_RING:
1987         case MBX_READ_CONFIG:
1988         case MBX_READ_RCONFIG:
1989         case MBX_READ_SPARM:
1990         case MBX_READ_STATUS:
1991         case MBX_READ_RPI:
1992         case MBX_READ_XRI:
1993         case MBX_READ_REV:
1994         case MBX_READ_LNK_STAT:
1995         case MBX_REG_LOGIN:
1996         case MBX_UNREG_LOGIN:
1997         case MBX_CLEAR_LA:
1998         case MBX_DUMP_MEMORY:
1999         case MBX_DUMP_CONTEXT:
2000         case MBX_RUN_DIAGS:
2001         case MBX_RESTART:
2002         case MBX_UPDATE_CFG:
2003         case MBX_DOWN_LOAD:
2004         case MBX_DEL_LD_ENTRY:
2005         case MBX_RUN_PROGRAM:
2006         case MBX_SET_MASK:
2007         case MBX_SET_VARIABLE:
2008         case MBX_UNREG_D_ID:
2009         case MBX_KILL_BOARD:
2010         case MBX_CONFIG_FARP:
2011         case MBX_BEACON:
2012         case MBX_LOAD_AREA:
2013         case MBX_RUN_BIU_DIAG64:
2014         case MBX_CONFIG_PORT:
2015         case MBX_READ_SPARM64:
2016         case MBX_READ_RPI64:
2017         case MBX_REG_LOGIN64:
2018         case MBX_READ_TOPOLOGY:
2019         case MBX_WRITE_WWN:
2020         case MBX_SET_DEBUG:
2021         case MBX_LOAD_EXP_ROM:
2022         case MBX_ASYNCEVT_ENABLE:
2023         case MBX_REG_VPI:
2024         case MBX_UNREG_VPI:
2025         case MBX_HEARTBEAT:
2026         case MBX_PORT_CAPABILITIES:
2027         case MBX_PORT_IOV_CONTROL:
2028         case MBX_SLI4_CONFIG:
2029         case MBX_SLI4_REQ_FTRS:
2030         case MBX_REG_FCFI:
2031         case MBX_UNREG_FCFI:
2032         case MBX_REG_VFI:
2033         case MBX_UNREG_VFI:
2034         case MBX_INIT_VPI:
2035         case MBX_INIT_VFI:
2036         case MBX_RESUME_RPI:
2037         case MBX_READ_EVENT_LOG_STATUS:
2038         case MBX_READ_EVENT_LOG:
2039         case MBX_SECURITY_MGMT:
2040         case MBX_AUTH_PORT:
2041                 ret = mbxCommand;
2042                 break;
2043         default:
2044                 ret = MBX_SHUTDOWN;
2045                 break;
2046         }
2047         return ret;
2048 }
2049
2050 /**
2051  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2052  * @phba: Pointer to HBA context object.
2053  * @pmboxq: Pointer to mailbox command.
2054  *
2055  * This is completion handler function for mailbox commands issued from
2056  * lpfc_sli_issue_mbox_wait function. This function is called by the
2057  * mailbox event handler function with no lock held. This function
2058  * will wake up thread waiting on the wait queue pointed by context1
2059  * of the mailbox.
2060  **/
2061 void
2062 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2063 {
2064         wait_queue_head_t *pdone_q;
2065         unsigned long drvr_flag;
2066
2067         /*
2068          * If pdone_q is empty, the driver thread gave up waiting and
2069          * continued running.
2070          */
2071         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2072         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2073         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2074         if (pdone_q)
2075                 wake_up_interruptible(pdone_q);
2076         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2077         return;
2078 }
2079
2080
2081 /**
2082  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2083  * @phba: Pointer to HBA context object.
2084  * @pmb: Pointer to mailbox object.
2085  *
2086  * This function is the default mailbox completion handler. It
2087  * frees the memory resources associated with the completed mailbox
2088  * command. If the completed command is a REG_LOGIN mailbox command,
2089  * this function will issue a UREG_LOGIN to re-claim the RPI.
2090  **/
2091 void
2092 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2093 {
2094         struct lpfc_vport  *vport = pmb->vport;
2095         struct lpfc_dmabuf *mp;
2096         struct lpfc_nodelist *ndlp;
2097         struct Scsi_Host *shost;
2098         uint16_t rpi, vpi;
2099         int rc;
2100
2101         mp = (struct lpfc_dmabuf *) (pmb->context1);
2102
2103         if (mp) {
2104                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2105                 kfree(mp);
2106         }
2107
2108         /*
2109          * If a REG_LOGIN succeeded  after node is destroyed or node
2110          * is in re-discovery driver need to cleanup the RPI.
2111          */
2112         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2113             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2114             !pmb->u.mb.mbxStatus) {
2115                 rpi = pmb->u.mb.un.varWords[0];
2116                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2117                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2118                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2119                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2120                 if (rc != MBX_NOT_FINISHED)
2121                         return;
2122         }
2123
2124         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2125                 !(phba->pport->load_flag & FC_UNLOADING) &&
2126                 !pmb->u.mb.mbxStatus) {
2127                 shost = lpfc_shost_from_vport(vport);
2128                 spin_lock_irq(shost->host_lock);
2129                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2130                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2131                 spin_unlock_irq(shost->host_lock);
2132         }
2133
2134         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2135                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2136                 lpfc_nlp_put(ndlp);
2137                 pmb->context2 = NULL;
2138         }
2139
2140         /* Check security permission status on INIT_LINK mailbox command */
2141         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2142             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2143                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2144                                 "2860 SLI authentication is required "
2145                                 "for INIT_LINK but has not done yet\n");
2146
2147         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2148                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2149         else
2150                 mempool_free(pmb, phba->mbox_mem_pool);
2151 }
2152
2153 /**
2154  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2155  * @phba: Pointer to HBA context object.
2156  *
2157  * This function is called with no lock held. This function processes all
2158  * the completed mailbox commands and gives it to upper layers. The interrupt
2159  * service routine processes mailbox completion interrupt and adds completed
2160  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2161  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2162  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2163  * function returns the mailbox commands to the upper layer by calling the
2164  * completion handler function of each mailbox.
2165  **/
2166 int
2167 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2168 {
2169         MAILBOX_t *pmbox;
2170         LPFC_MBOXQ_t *pmb;
2171         int rc;
2172         LIST_HEAD(cmplq);
2173
2174         phba->sli.slistat.mbox_event++;
2175
2176         /* Get all completed mailboxe buffers into the cmplq */
2177         spin_lock_irq(&phba->hbalock);
2178         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2179         spin_unlock_irq(&phba->hbalock);
2180
2181         /* Get a Mailbox buffer to setup mailbox commands for callback */
2182         do {
2183                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2184                 if (pmb == NULL)
2185                         break;
2186
2187                 pmbox = &pmb->u.mb;
2188
2189                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2190                         if (pmb->vport) {
2191                                 lpfc_debugfs_disc_trc(pmb->vport,
2192                                         LPFC_DISC_TRC_MBOX_VPORT,
2193                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2194                                         (uint32_t)pmbox->mbxCommand,
2195                                         pmbox->un.varWords[0],
2196                                         pmbox->un.varWords[1]);
2197                         }
2198                         else {
2199                                 lpfc_debugfs_disc_trc(phba->pport,
2200                                         LPFC_DISC_TRC_MBOX,
2201                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2202                                         (uint32_t)pmbox->mbxCommand,
2203                                         pmbox->un.varWords[0],
2204                                         pmbox->un.varWords[1]);
2205                         }
2206                 }
2207
2208                 /*
2209                  * It is a fatal error if unknown mbox command completion.
2210                  */
2211                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2212                     MBX_SHUTDOWN) {
2213                         /* Unknown mailbox command compl */
2214                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2215                                         "(%d):0323 Unknown Mailbox command "
2216                                         "x%x (x%x) Cmpl\n",
2217                                         pmb->vport ? pmb->vport->vpi : 0,
2218                                         pmbox->mbxCommand,
2219                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
2220                         phba->link_state = LPFC_HBA_ERROR;
2221                         phba->work_hs = HS_FFER3;
2222                         lpfc_handle_eratt(phba);
2223                         continue;
2224                 }
2225
2226                 if (pmbox->mbxStatus) {
2227                         phba->sli.slistat.mbox_stat_err++;
2228                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2229                                 /* Mbox cmd cmpl error - RETRYing */
2230                                 lpfc_printf_log(phba, KERN_INFO,
2231                                                 LOG_MBOX | LOG_SLI,
2232                                                 "(%d):0305 Mbox cmd cmpl "
2233                                                 "error - RETRYing Data: x%x "
2234                                                 "(x%x) x%x x%x x%x\n",
2235                                                 pmb->vport ? pmb->vport->vpi :0,
2236                                                 pmbox->mbxCommand,
2237                                                 lpfc_sli4_mbox_opcode_get(phba,
2238                                                                           pmb),
2239                                                 pmbox->mbxStatus,
2240                                                 pmbox->un.varWords[0],
2241                                                 pmb->vport->port_state);
2242                                 pmbox->mbxStatus = 0;
2243                                 pmbox->mbxOwner = OWN_HOST;
2244                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2245                                 if (rc != MBX_NOT_FINISHED)
2246                                         continue;
2247                         }
2248                 }
2249
2250                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2251                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2252                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
2253                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2254                                 pmb->vport ? pmb->vport->vpi : 0,
2255                                 pmbox->mbxCommand,
2256                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
2257                                 pmb->mbox_cmpl,
2258                                 *((uint32_t *) pmbox),
2259                                 pmbox->un.varWords[0],
2260                                 pmbox->un.varWords[1],
2261                                 pmbox->un.varWords[2],
2262                                 pmbox->un.varWords[3],
2263                                 pmbox->un.varWords[4],
2264                                 pmbox->un.varWords[5],
2265                                 pmbox->un.varWords[6],
2266                                 pmbox->un.varWords[7]);
2267
2268                 if (pmb->mbox_cmpl)
2269                         pmb->mbox_cmpl(phba,pmb);
2270         } while (1);
2271         return 0;
2272 }
2273
2274 /**
2275  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2276  * @phba: Pointer to HBA context object.
2277  * @pring: Pointer to driver SLI ring object.
2278  * @tag: buffer tag.
2279  *
2280  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2281  * is set in the tag the buffer is posted for a particular exchange,
2282  * the function will return the buffer without replacing the buffer.
2283  * If the buffer is for unsolicited ELS or CT traffic, this function
2284  * returns the buffer and also posts another buffer to the firmware.
2285  **/
2286 static struct lpfc_dmabuf *
2287 lpfc_sli_get_buff(struct lpfc_hba *phba,
2288                   struct lpfc_sli_ring *pring,
2289                   uint32_t tag)
2290 {
2291         struct hbq_dmabuf *hbq_entry;
2292
2293         if (tag & QUE_BUFTAG_BIT)
2294                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2295         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2296         if (!hbq_entry)
2297                 return NULL;
2298         return &hbq_entry->dbuf;
2299 }
2300
2301 /**
2302  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2303  * @phba: Pointer to HBA context object.
2304  * @pring: Pointer to driver SLI ring object.
2305  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2306  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2307  * @fch_type: the type for the first frame of the sequence.
2308  *
2309  * This function is called with no lock held. This function uses the r_ctl and
2310  * type of the received sequence to find the correct callback function to call
2311  * to process the sequence.
2312  **/
2313 static int
2314 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2315                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2316                          uint32_t fch_type)
2317 {
2318         int i;
2319
2320         /* unSolicited Responses */
2321         if (pring->prt[0].profile) {
2322                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2323                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2324                                                                         saveq);
2325                 return 1;
2326         }
2327         /* We must search, based on rctl / type
2328            for the right routine */
2329         for (i = 0; i < pring->num_mask; i++) {
2330                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2331                     (pring->prt[i].type == fch_type)) {
2332                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2333                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2334                                                 (phba, pring, saveq);
2335                         return 1;
2336                 }
2337         }
2338         return 0;
2339 }
2340
2341 /**
2342  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2343  * @phba: Pointer to HBA context object.
2344  * @pring: Pointer to driver SLI ring object.
2345  * @saveq: Pointer to the unsolicited iocb.
2346  *
2347  * This function is called with no lock held by the ring event handler
2348  * when there is an unsolicited iocb posted to the response ring by the
2349  * firmware. This function gets the buffer associated with the iocbs
2350  * and calls the event handler for the ring. This function handles both
2351  * qring buffers and hbq buffers.
2352  * When the function returns 1 the caller can free the iocb object otherwise
2353  * upper layer functions will free the iocb objects.
2354  **/
2355 static int
2356 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2357                             struct lpfc_iocbq *saveq)
2358 {
2359         IOCB_t           * irsp;
2360         WORD5            * w5p;
2361         uint32_t           Rctl, Type;
2362         uint32_t           match;
2363         struct lpfc_iocbq *iocbq;
2364         struct lpfc_dmabuf *dmzbuf;
2365
2366         match = 0;
2367         irsp = &(saveq->iocb);
2368
2369         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2370                 if (pring->lpfc_sli_rcv_async_status)
2371                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2372                 else
2373                         lpfc_printf_log(phba,
2374                                         KERN_WARNING,
2375                                         LOG_SLI,
2376                                         "0316 Ring %d handler: unexpected "
2377                                         "ASYNC_STATUS iocb received evt_code "
2378                                         "0x%x\n",
2379                                         pring->ringno,
2380                                         irsp->un.asyncstat.evt_code);
2381                 return 1;
2382         }
2383
2384         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2385                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2386                 if (irsp->ulpBdeCount > 0) {
2387                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2388                                         irsp->un.ulpWord[3]);
2389                         lpfc_in_buf_free(phba, dmzbuf);
2390                 }
2391
2392                 if (irsp->ulpBdeCount > 1) {
2393                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2394                                         irsp->unsli3.sli3Words[3]);
2395                         lpfc_in_buf_free(phba, dmzbuf);
2396                 }
2397
2398                 if (irsp->ulpBdeCount > 2) {
2399                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2400                                 irsp->unsli3.sli3Words[7]);
2401                         lpfc_in_buf_free(phba, dmzbuf);
2402                 }
2403
2404                 return 1;
2405         }
2406
2407         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2408                 if (irsp->ulpBdeCount != 0) {
2409                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2410                                                 irsp->un.ulpWord[3]);
2411                         if (!saveq->context2)
2412                                 lpfc_printf_log(phba,
2413                                         KERN_ERR,
2414                                         LOG_SLI,
2415                                         "0341 Ring %d Cannot find buffer for "
2416                                         "an unsolicited iocb. tag 0x%x\n",
2417                                         pring->ringno,
2418                                         irsp->un.ulpWord[3]);
2419                 }
2420                 if (irsp->ulpBdeCount == 2) {
2421                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2422                                                 irsp->unsli3.sli3Words[7]);
2423                         if (!saveq->context3)
2424                                 lpfc_printf_log(phba,
2425                                         KERN_ERR,
2426                                         LOG_SLI,
2427                                         "0342 Ring %d Cannot find buffer for an"
2428                                         " unsolicited iocb. tag 0x%x\n",
2429                                         pring->ringno,
2430                                         irsp->unsli3.sli3Words[7]);
2431                 }
2432                 list_for_each_entry(iocbq, &saveq->list, list) {
2433                         irsp = &(iocbq->iocb);
2434                         if (irsp->ulpBdeCount != 0) {
2435                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2436                                                         irsp->un.ulpWord[3]);
2437                                 if (!iocbq->context2)
2438                                         lpfc_printf_log(phba,
2439                                                 KERN_ERR,
2440                                                 LOG_SLI,
2441                                                 "0343 Ring %d Cannot find "
2442                                                 "buffer for an unsolicited iocb"
2443                                                 ". tag 0x%x\n", pring->ringno,
2444                                                 irsp->un.ulpWord[3]);
2445                         }
2446                         if (irsp->ulpBdeCount == 2) {
2447                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2448                                                 irsp->unsli3.sli3Words[7]);
2449                                 if (!iocbq->context3)
2450                                         lpfc_printf_log(phba,
2451                                                 KERN_ERR,
2452                                                 LOG_SLI,
2453                                                 "0344 Ring %d Cannot find "
2454                                                 "buffer for an unsolicited "
2455                                                 "iocb. tag 0x%x\n",
2456                                                 pring->ringno,
2457                                                 irsp->unsli3.sli3Words[7]);
2458                         }
2459                 }
2460         }
2461         if (irsp->ulpBdeCount != 0 &&
2462             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2463              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2464                 int found = 0;
2465
2466                 /* search continue save q for same XRI */
2467                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2468                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2469                                 list_add_tail(&saveq->list, &iocbq->list);
2470                                 found = 1;
2471                                 break;
2472                         }
2473                 }
2474                 if (!found)
2475                         list_add_tail(&saveq->clist,
2476                                       &pring->iocb_continue_saveq);
2477                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2478                         list_del_init(&iocbq->clist);
2479                         saveq = iocbq;
2480                         irsp = &(saveq->iocb);
2481                 } else
2482                         return 0;
2483         }
2484         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2485             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2486             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2487                 Rctl = FC_RCTL_ELS_REQ;
2488                 Type = FC_TYPE_ELS;
2489         } else {
2490                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2491                 Rctl = w5p->hcsw.Rctl;
2492                 Type = w5p->hcsw.Type;
2493
2494                 /* Firmware Workaround */
2495                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2496                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2497                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2498                         Rctl = FC_RCTL_ELS_REQ;
2499                         Type = FC_TYPE_ELS;
2500                         w5p->hcsw.Rctl = Rctl;
2501                         w5p->hcsw.Type = Type;
2502                 }
2503         }
2504
2505         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2506                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2507                                 "0313 Ring %d handler: unexpected Rctl x%x "
2508                                 "Type x%x received\n",
2509                                 pring->ringno, Rctl, Type);
2510
2511         return 1;
2512 }
2513
2514 /**
2515  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2516  * @phba: Pointer to HBA context object.
2517  * @pring: Pointer to driver SLI ring object.
2518  * @prspiocb: Pointer to response iocb object.
2519  *
2520  * This function looks up the iocb_lookup table to get the command iocb
2521  * corresponding to the given response iocb using the iotag of the
2522  * response iocb. This function is called with the hbalock held.
2523  * This function returns the command iocb object if it finds the command
2524  * iocb else returns NULL.
2525  **/
2526 static struct lpfc_iocbq *
2527 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2528                       struct lpfc_sli_ring *pring,
2529                       struct lpfc_iocbq *prspiocb)
2530 {
2531         struct lpfc_iocbq *cmd_iocb = NULL;
2532         uint16_t iotag;
2533
2534         iotag = prspiocb->iocb.ulpIoTag;
2535
2536         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2537                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2538                 list_del_init(&cmd_iocb->list);
2539                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2540                         pring->txcmplq_cnt--;
2541                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2542                 }
2543                 return cmd_iocb;
2544         }
2545
2546         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2547                         "0317 iotag x%x is out off "
2548                         "range: max iotag x%x wd0 x%x\n",
2549                         iotag, phba->sli.last_iotag,
2550                         *(((uint32_t *) &prspiocb->iocb) + 7));
2551         return NULL;
2552 }
2553
2554 /**
2555  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2556  * @phba: Pointer to HBA context object.
2557  * @pring: Pointer to driver SLI ring object.
2558  * @iotag: IOCB tag.
2559  *
2560  * This function looks up the iocb_lookup table to get the command iocb
2561  * corresponding to the given iotag. This function is called with the
2562  * hbalock held.
2563  * This function returns the command iocb object if it finds the command
2564  * iocb else returns NULL.
2565  **/
2566 static struct lpfc_iocbq *
2567 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2568                              struct lpfc_sli_ring *pring, uint16_t iotag)
2569 {
2570         struct lpfc_iocbq *cmd_iocb;
2571
2572         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2573                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2574                 list_del_init(&cmd_iocb->list);
2575                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2576                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2577                         pring->txcmplq_cnt--;
2578                 }
2579                 return cmd_iocb;
2580         }
2581
2582         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2583                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2584                         iotag, phba->sli.last_iotag);
2585         return NULL;
2586 }
2587
2588 /**
2589  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2590  * @phba: Pointer to HBA context object.
2591  * @pring: Pointer to driver SLI ring object.
2592  * @saveq: Pointer to the response iocb to be processed.
2593  *
2594  * This function is called by the ring event handler for non-fcp
2595  * rings when there is a new response iocb in the response ring.
2596  * The caller is not required to hold any locks. This function
2597  * gets the command iocb associated with the response iocb and
2598  * calls the completion handler for the command iocb. If there
2599  * is no completion handler, the function will free the resources
2600  * associated with command iocb. If the response iocb is for
2601  * an already aborted command iocb, the status of the completion
2602  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2603  * This function always returns 1.
2604  **/
2605 static int
2606 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2607                           struct lpfc_iocbq *saveq)
2608 {
2609         struct lpfc_iocbq *cmdiocbp;
2610         int rc = 1;
2611         unsigned long iflag;
2612
2613         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2614         spin_lock_irqsave(&phba->hbalock, iflag);
2615         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2616         spin_unlock_irqrestore(&phba->hbalock, iflag);
2617
2618         if (cmdiocbp) {
2619                 if (cmdiocbp->iocb_cmpl) {
2620                         /*
2621                          * If an ELS command failed send an event to mgmt
2622                          * application.
2623                          */
2624                         if (saveq->iocb.ulpStatus &&
2625                              (pring->ringno == LPFC_ELS_RING) &&
2626                              (cmdiocbp->iocb.ulpCommand ==
2627                                 CMD_ELS_REQUEST64_CR))
2628                                 lpfc_send_els_failure_event(phba,
2629                                         cmdiocbp, saveq);
2630
2631                         /*
2632                          * Post all ELS completions to the worker thread.
2633                          * All other are passed to the completion callback.
2634                          */
2635                         if (pring->ringno == LPFC_ELS_RING) {
2636                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2637                                     (cmdiocbp->iocb_flag &
2638                                                         LPFC_DRIVER_ABORTED)) {
2639                                         spin_lock_irqsave(&phba->hbalock,
2640                                                           iflag);
2641                                         cmdiocbp->iocb_flag &=
2642                                                 ~LPFC_DRIVER_ABORTED;
2643                                         spin_unlock_irqrestore(&phba->hbalock,
2644                                                                iflag);
2645                                         saveq->iocb.ulpStatus =
2646                                                 IOSTAT_LOCAL_REJECT;
2647                                         saveq->iocb.un.ulpWord[4] =
2648                                                 IOERR_SLI_ABORTED;
2649
2650                                         /* Firmware could still be in progress
2651                                          * of DMAing payload, so don't free data
2652                                          * buffer till after a hbeat.
2653                                          */
2654                                         spin_lock_irqsave(&phba->hbalock,
2655                                                           iflag);
2656                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2657                                         spin_unlock_irqrestore(&phba->hbalock,
2658                                                                iflag);
2659                                 }
2660                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2661                                         if (saveq->iocb_flag &
2662                                             LPFC_EXCHANGE_BUSY) {
2663                                                 /* Set cmdiocb flag for the
2664                                                  * exchange busy so sgl (xri)
2665                                                  * will not be released until
2666                                                  * the abort xri is received
2667                                                  * from hba.
2668                                                  */
2669                                                 spin_lock_irqsave(
2670                                                         &phba->hbalock, iflag);
2671                                                 cmdiocbp->iocb_flag |=
2672                                                         LPFC_EXCHANGE_BUSY;
2673                                                 spin_unlock_irqrestore(
2674                                                         &phba->hbalock, iflag);
2675                                         }
2676                                         if (cmdiocbp->iocb_flag &
2677                                             LPFC_DRIVER_ABORTED) {
2678                                                 /*
2679                                                  * Clear LPFC_DRIVER_ABORTED
2680                                                  * bit in case it was driver
2681                                                  * initiated abort.
2682                                                  */
2683                                                 spin_lock_irqsave(
2684                                                         &phba->hbalock, iflag);
2685                                                 cmdiocbp->iocb_flag &=
2686                                                         ~LPFC_DRIVER_ABORTED;
2687                                                 spin_unlock_irqrestore(
2688                                                         &phba->hbalock, iflag);
2689                                                 cmdiocbp->iocb.ulpStatus =
2690                                                         IOSTAT_LOCAL_REJECT;
2691                                                 cmdiocbp->iocb.un.ulpWord[4] =
2692                                                         IOERR_ABORT_REQUESTED;
2693                                                 /*
2694                                                  * For SLI4, irsiocb contains
2695                                                  * NO_XRI in sli_xritag, it
2696                                                  * shall not affect releasing
2697                                                  * sgl (xri) process.
2698                                                  */
2699                                                 saveq->iocb.ulpStatus =
2700                                                         IOSTAT_LOCAL_REJECT;
2701                                                 saveq->iocb.un.ulpWord[4] =
2702                                                         IOERR_SLI_ABORTED;
2703                                                 spin_lock_irqsave(
2704                                                         &phba->hbalock, iflag);
2705                                                 saveq->iocb_flag |=
2706                                                         LPFC_DELAY_MEM_FREE;
2707                                                 spin_unlock_irqrestore(
2708                                                         &phba->hbalock, iflag);
2709                                         }
2710                                 }
2711                         }
2712                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2713                 } else
2714                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2715         } else {
2716                 /*
2717                  * Unknown initiating command based on the response iotag.
2718                  * This could be the case on the ELS ring because of
2719                  * lpfc_els_abort().
2720                  */
2721                 if (pring->ringno != LPFC_ELS_RING) {
2722                         /*
2723                          * Ring <ringno> handler: unexpected completion IoTag
2724                          * <IoTag>
2725                          */
2726                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2727                                          "0322 Ring %d handler: "
2728                                          "unexpected completion IoTag x%x "
2729                                          "Data: x%x x%x x%x x%x\n",
2730                                          pring->ringno,
2731                                          saveq->iocb.ulpIoTag,
2732                                          saveq->iocb.ulpStatus,
2733                                          saveq->iocb.un.ulpWord[4],
2734                                          saveq->iocb.ulpCommand,
2735                                          saveq->iocb.ulpContext);
2736                 }
2737         }
2738
2739         return rc;
2740 }
2741
2742 /**
2743  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2744  * @phba: Pointer to HBA context object.
2745  * @pring: Pointer to driver SLI ring object.
2746  *
2747  * This function is called from the iocb ring event handlers when
2748  * put pointer is ahead of the get pointer for a ring. This function signal
2749  * an error attention condition to the worker thread and the worker
2750  * thread will transition the HBA to offline state.
2751  **/
2752 static void
2753 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2754 {
2755         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2756         /*
2757          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2758          * rsp ring <portRspMax>
2759          */
2760         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2761                         "0312 Ring %d handler: portRspPut %d "
2762                         "is bigger than rsp ring %d\n",
2763                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2764                         pring->numRiocb);
2765
2766         phba->link_state = LPFC_HBA_ERROR;
2767
2768         /*
2769          * All error attention handlers are posted to
2770          * worker thread
2771          */
2772         phba->work_ha |= HA_ERATT;
2773         phba->work_hs = HS_FFER3;
2774
2775         lpfc_worker_wake_up(phba);
2776
2777         return;
2778 }
2779
2780 /**
2781  * lpfc_poll_eratt - Error attention polling timer timeout handler
2782  * @ptr: Pointer to address of HBA context object.
2783  *
2784  * This function is invoked by the Error Attention polling timer when the
2785  * timer times out. It will check the SLI Error Attention register for
2786  * possible attention events. If so, it will post an Error Attention event
2787  * and wake up worker thread to process it. Otherwise, it will set up the
2788  * Error Attention polling timer for the next poll.
2789  **/
2790 void lpfc_poll_eratt(unsigned long ptr)
2791 {
2792         struct lpfc_hba *phba;
2793         uint32_t eratt = 0;
2794
2795         phba = (struct lpfc_hba *)ptr;
2796
2797         /* Check chip HA register for error event */
2798         eratt = lpfc_sli_check_eratt(phba);
2799
2800         if (eratt)
2801                 /* Tell the worker thread there is work to do */
2802                 lpfc_worker_wake_up(phba);
2803         else
2804                 /* Restart the timer for next eratt poll */
2805                 mod_timer(&phba->eratt_poll, jiffies +
2806                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2807         return;
2808 }
2809
2810
2811 /**
2812  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2813  * @phba: Pointer to HBA context object.
2814  * @pring: Pointer to driver SLI ring object.
2815  * @mask: Host attention register mask for this ring.
2816  *
2817  * This function is called from the interrupt context when there is a ring
2818  * event for the fcp ring. The caller does not hold any lock.
2819  * The function processes each response iocb in the response ring until it
2820  * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2821  * LE bit set. The function will call the completion handler of the command iocb
2822  * if the response iocb indicates a completion for a command iocb or it is
2823  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2824  * function if this is an unsolicited iocb.
2825  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2826  * to check it explicitly.
2827  */
2828 int
2829 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2830                                 struct lpfc_sli_ring *pring, uint32_t mask)
2831 {
2832         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2833         IOCB_t *irsp = NULL;
2834         IOCB_t *entry = NULL;
2835         struct lpfc_iocbq *cmdiocbq = NULL;
2836         struct lpfc_iocbq rspiocbq;
2837         uint32_t status;
2838         uint32_t portRspPut, portRspMax;
2839         int rc = 1;
2840         lpfc_iocb_type type;
2841         unsigned long iflag;
2842         uint32_t rsp_cmpl = 0;
2843
2844         spin_lock_irqsave(&phba->hbalock, iflag);
2845         pring->stats.iocb_event++;
2846
2847         /*
2848          * The next available response entry should never exceed the maximum
2849          * entries.  If it does, treat it as an adapter hardware error.
2850          */
2851         portRspMax = pring->numRiocb;
2852         portRspPut = le32_to_cpu(pgp->rspPutInx);
2853         if (unlikely(portRspPut >= portRspMax)) {
2854                 lpfc_sli_rsp_pointers_error(phba, pring);
2855                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2856                 return 1;
2857         }
2858         if (phba->fcp_ring_in_use) {
2859                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2860                 return 1;
2861         } else
2862                 phba->fcp_ring_in_use = 1;
2863
2864         rmb();
2865         while (pring->rspidx != portRspPut) {
2866                 /*
2867                  * Fetch an entry off the ring and copy it into a local data
2868                  * structure.  The copy involves a byte-swap since the
2869                  * network byte order and pci byte orders are different.
2870                  */
2871                 entry = lpfc_resp_iocb(phba, pring);
2872                 phba->last_completion_time = jiffies;
2873
2874                 if (++pring->rspidx >= portRspMax)
2875                         pring->rspidx = 0;
2876
2877                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2878                                       (uint32_t *) &rspiocbq.iocb,
2879                                       phba->iocb_rsp_size);
2880                 INIT_LIST_HEAD(&(rspiocbq.list));
2881                 irsp = &rspiocbq.iocb;
2882
2883                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2884                 pring->stats.iocb_rsp++;
2885                 rsp_cmpl++;
2886
2887                 if (unlikely(irsp->ulpStatus)) {
2888                         /*
2889                          * If resource errors reported from HBA, reduce
2890                          * queuedepths of the SCSI device.
2891                          */
2892                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2893                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2894                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2895                                 phba->lpfc_rampdown_queue_depth(phba);
2896                                 spin_lock_irqsave(&phba->hbalock, iflag);
2897                         }
2898
2899                         /* Rsp ring <ringno> error: IOCB */
2900                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2901                                         "0336 Rsp Ring %d error: IOCB Data: "
2902                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2903                                         pring->ringno,
2904                                         irsp->un.ulpWord[0],
2905                                         irsp->un.ulpWord[1],
2906                                         irsp->un.ulpWord[2],
2907                                         irsp->un.ulpWord[3],
2908                                         irsp->un.ulpWord[4],
2909                                         irsp->un.ulpWord[5],
2910                                         *(uint32_t *)&irsp->un1,
2911                                         *((uint32_t *)&irsp->un1 + 1));
2912                 }
2913
2914                 switch (type) {
2915                 case LPFC_ABORT_IOCB:
2916                 case LPFC_SOL_IOCB:
2917                         /*
2918                          * Idle exchange closed via ABTS from port.  No iocb
2919                          * resources need to be recovered.
2920                          */
2921                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2922                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2923                                                 "0333 IOCB cmd 0x%x"
2924                                                 " processed. Skipping"
2925                                                 " completion\n",
2926                                                 irsp->ulpCommand);
2927                                 break;
2928                         }
2929
2930                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2931                                                          &rspiocbq);
2932                         if (unlikely(!cmdiocbq))
2933                                 break;
2934                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2935                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2936                         if (cmdiocbq->iocb_cmpl) {
2937                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2938                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2939                                                       &rspiocbq);
2940                                 spin_lock_irqsave(&phba->hbalock, iflag);
2941                         }
2942                         break;
2943                 case LPFC_UNSOL_IOCB:
2944                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2945                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2946                         spin_lock_irqsave(&phba->hbalock, iflag);
2947                         break;
2948                 default:
2949                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2950                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2951                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2952                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2953                                        MAX_MSG_DATA);
2954                                 dev_warn(&((phba->pcidev)->dev),
2955                                          "lpfc%d: %s\n",
2956                                          phba->brd_no, adaptermsg);
2957                         } else {
2958                                 /* Unknown IOCB command */
2959                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2960                                                 "0334 Unknown IOCB command "
2961                                                 "Data: x%x, x%x x%x x%x x%x\n",
2962                                                 type, irsp->ulpCommand,
2963                                                 irsp->ulpStatus,
2964                                                 irsp->ulpIoTag,
2965                                                 irsp->ulpContext);
2966                         }
2967                         break;
2968                 }
2969
2970                 /*
2971                  * The response IOCB has been processed.  Update the ring
2972                  * pointer in SLIM.  If the port response put pointer has not
2973                  * been updated, sync the pgp->rspPutInx and fetch the new port
2974                  * response put pointer.
2975                  */
2976                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2977
2978                 if (pring->rspidx == portRspPut)
2979                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2980         }
2981
2982         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2983                 pring->stats.iocb_rsp_full++;
2984                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2985                 writel(status, phba->CAregaddr);
2986                 readl(phba->CAregaddr);
2987         }
2988         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2989                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2990                 pring->stats.iocb_cmd_empty++;
2991
2992                 /* Force update of the local copy of cmdGetInx */
2993                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2994                 lpfc_sli_resume_iocb(phba, pring);
2995
2996                 if ((pring->lpfc_sli_cmd_available))
2997                         (pring->lpfc_sli_cmd_available) (phba, pring);
2998
2999         }
3000
3001         phba->fcp_ring_in_use = 0;
3002         spin_unlock_irqrestore(&phba->hbalock, iflag);
3003         return rc;
3004 }
3005
3006 /**
3007  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3008  * @phba: Pointer to HBA context object.
3009  * @pring: Pointer to driver SLI ring object.
3010  * @rspiocbp: Pointer to driver response IOCB object.
3011  *
3012  * This function is called from the worker thread when there is a slow-path
3013  * response IOCB to process. This function chains all the response iocbs until
3014  * seeing the iocb with the LE bit set. The function will call
3015  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3016  * completion of a command iocb. The function will call the
3017  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3018  * The function frees the resources or calls the completion handler if this
3019  * iocb is an abort completion. The function returns NULL when the response
3020  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3021  * this function shall chain the iocb on to the iocb_continueq and return the
3022  * response iocb passed in.
3023  **/
3024 static struct lpfc_iocbq *
3025 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3026                         struct lpfc_iocbq *rspiocbp)
3027 {
3028         struct lpfc_iocbq *saveq;
3029         struct lpfc_iocbq *cmdiocbp;
3030         struct lpfc_iocbq *next_iocb;
3031         IOCB_t *irsp = NULL;
3032         uint32_t free_saveq;
3033         uint8_t iocb_cmd_type;
3034         lpfc_iocb_type type;
3035         unsigned long iflag;
3036         int rc;
3037
3038         spin_lock_irqsave(&phba->hbalock, iflag);
3039         /* First add the response iocb to the countinueq list */
3040         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3041         pring->iocb_continueq_cnt++;
3042
3043         /* Now, determine whether the list is completed for processing */
3044         irsp = &rspiocbp->iocb;
3045         if (irsp->ulpLe) {
3046                 /*
3047                  * By default, the driver expects to free all resources
3048                  * associated with this iocb completion.
3049                  */
3050                 free_saveq = 1;
3051                 saveq = list_get_first(&pring->iocb_continueq,
3052                                        struct lpfc_iocbq, list);
3053                 irsp = &(saveq->iocb);
3054                 list_del_init(&pring->iocb_continueq);
3055                 pring->iocb_continueq_cnt = 0;
3056
3057                 pring->stats.iocb_rsp++;
3058
3059                 /*
3060                  * If resource errors reported from HBA, reduce
3061                  * queuedepths of the SCSI device.
3062                  */
3063                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3064                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3065                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3066                         phba->lpfc_rampdown_queue_depth(phba);
3067                         spin_lock_irqsave(&phba->hbalock, iflag);
3068                 }
3069
3070                 if (irsp->ulpStatus) {
3071                         /* Rsp ring <ringno> error: IOCB */
3072                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3073                                         "0328 Rsp Ring %d error: "
3074                                         "IOCB Data: "
3075                                         "x%x x%x x%x x%x "
3076                                         "x%x x%x x%x x%x "
3077                                         "x%x x%x x%x x%x "
3078                                         "x%x x%x x%x x%x\n",
3079                                         pring->ringno,
3080                                         irsp->un.ulpWord[0],
3081                                         irsp->un.ulpWord[1],
3082                                         irsp->un.ulpWord[2],
3083                                         irsp->un.ulpWord[3],
3084                                         irsp->un.ulpWord[4],
3085                                         irsp->un.ulpWord[5],
3086                                         *(((uint32_t *) irsp) + 6),
3087                                         *(((uint32_t *) irsp) + 7),
3088                                         *(((uint32_t *) irsp) + 8),
3089                                         *(((uint32_t *) irsp) + 9),
3090                                         *(((uint32_t *) irsp) + 10),
3091                                         *(((uint32_t *) irsp) + 11),
3092                                         *(((uint32_t *) irsp) + 12),
3093                                         *(((uint32_t *) irsp) + 13),
3094                                         *(((uint32_t *) irsp) + 14),
3095                                         *(((uint32_t *) irsp) + 15));
3096                 }
3097
3098                 /*
3099                  * Fetch the IOCB command type and call the correct completion
3100                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3101                  * get freed back to the lpfc_iocb_list by the discovery
3102                  * kernel thread.
3103                  */
3104                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3105                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3106                 switch (type) {
3107                 case LPFC_SOL_IOCB:
3108                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3109                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3110                         spin_lock_irqsave(&phba->hbalock, iflag);
3111                         break;
3112
3113                 case LPFC_UNSOL_IOCB:
3114                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3115                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3116                         spin_lock_irqsave(&phba->hbalock, iflag);
3117                         if (!rc)
3118                                 free_saveq = 0;
3119                         break;
3120
3121                 case LPFC_ABORT_IOCB:
3122                         cmdiocbp = NULL;
3123                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3124                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3125                                                                  saveq);
3126                         if (cmdiocbp) {
3127                                 /* Call the specified completion routine */
3128                                 if (cmdiocbp->iocb_cmpl) {
3129                                         spin_unlock_irqrestore(&phba->hbalock,
3130                                                                iflag);
3131                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3132                                                               saveq);
3133                                         spin_lock_irqsave(&phba->hbalock,
3134                                                           iflag);
3135                                 } else
3136                                         __lpfc_sli_release_iocbq(phba,
3137                                                                  cmdiocbp);
3138                         }
3139                         break;
3140
3141                 case LPFC_UNKNOWN_IOCB:
3142                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3143                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3144                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3145                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3146                                        MAX_MSG_DATA);
3147                                 dev_warn(&((phba->pcidev)->dev),
3148                                          "lpfc%d: %s\n",
3149                                          phba->brd_no, adaptermsg);
3150                         } else {
3151                                 /* Unknown IOCB command */
3152                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3153                                                 "0335 Unknown IOCB "
3154                                                 "command Data: x%x "
3155                                                 "x%x x%x x%x\n",
3156                                                 irsp->ulpCommand,
3157                                                 irsp->ulpStatus,
3158                                                 irsp->ulpIoTag,
3159                                                 irsp->ulpContext);
3160                         }
3161                         break;
3162                 }
3163
3164                 if (free_saveq) {
3165                         list_for_each_entry_safe(rspiocbp, next_iocb,
3166                                                  &saveq->list, list) {
3167                                 list_del(&rspiocbp->list);
3168                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3169                         }
3170                         __lpfc_sli_release_iocbq(phba, saveq);
3171                 }
3172                 rspiocbp = NULL;
3173         }
3174         spin_unlock_irqrestore(&phba->hbalock, iflag);
3175         return rspiocbp;
3176 }
3177
3178 /**
3179  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3180  * @phba: Pointer to HBA context object.
3181  * @pring: Pointer to driver SLI ring object.
3182  * @mask: Host attention register mask for this ring.
3183  *
3184  * This routine wraps the actual slow_ring event process routine from the
3185  * API jump table function pointer from the lpfc_hba struct.
3186  **/
3187 void
3188 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3189                                 struct lpfc_sli_ring *pring, uint32_t mask)
3190 {
3191         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3192 }
3193
3194 /**
3195  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3196  * @phba: Pointer to HBA context object.
3197  * @pring: Pointer to driver SLI ring object.
3198  * @mask: Host attention register mask for this ring.
3199  *
3200  * This function is called from the worker thread when there is a ring event
3201  * for non-fcp rings. The caller does not hold any lock. The function will
3202  * remove each response iocb in the response ring and calls the handle
3203  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3204  **/
3205 static void
3206 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3207                                    struct lpfc_sli_ring *pring, uint32_t mask)
3208 {
3209         struct lpfc_pgp *pgp;
3210         IOCB_t *entry;
3211         IOCB_t *irsp = NULL;
3212         struct lpfc_iocbq *rspiocbp = NULL;
3213         uint32_t portRspPut, portRspMax;
3214         unsigned long iflag;
3215         uint32_t status;
3216
3217         pgp = &phba->port_gp[pring->ringno];
3218         spin_lock_irqsave(&phba->hbalock, iflag);
3219         pring->stats.iocb_event++;
3220
3221         /*
3222          * The next available response entry should never exceed the maximum
3223          * entries.  If it does, treat it as an adapter hardware error.
3224          */
3225         portRspMax = pring->numRiocb;
3226         portRspPut = le32_to_cpu(pgp->rspPutInx);
3227         if (portRspPut >= portRspMax) {
3228                 /*
3229                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3230                  * rsp ring <portRspMax>
3231                  */
3232                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3233                                 "0303 Ring %d handler: portRspPut %d "
3234                                 "is bigger than rsp ring %d\n",
3235                                 pring->ringno, portRspPut, portRspMax);
3236
3237                 phba->link_state = LPFC_HBA_ERROR;
3238                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3239
3240                 phba->work_hs = HS_FFER3;
3241                 lpfc_handle_eratt(phba);
3242
3243                 return;
3244         }
3245
3246         rmb();
3247         while (pring->rspidx != portRspPut) {
3248                 /*
3249                  * Build a completion list and call the appropriate handler.
3250                  * The process is to get the next available response iocb, get
3251                  * a free iocb from the list, copy the response data into the
3252                  * free iocb, insert to the continuation list, and update the
3253                  * next response index to slim.  This process makes response
3254                  * iocb's in the ring available to DMA as fast as possible but
3255                  * pays a penalty for a copy operation.  Since the iocb is
3256                  * only 32 bytes, this penalty is considered small relative to
3257                  * the PCI reads for register values and a slim write.  When
3258                  * the ulpLe field is set, the entire Command has been
3259                  * received.
3260                  */
3261                 entry = lpfc_resp_iocb(phba, pring);
3262
3263                 phba->last_completion_time = jiffies;
3264                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3265                 if (rspiocbp == NULL) {
3266                         printk(KERN_ERR "%s: out of buffers! Failing "
3267                                "completion.\n", __func__);
3268                         break;
3269                 }
3270
3271                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3272                                       phba->iocb_rsp_size);
3273                 irsp = &rspiocbp->iocb;
3274
3275                 if (++pring->rspidx >= portRspMax)
3276                         pring->rspidx = 0;
3277
3278                 if (pring->ringno == LPFC_ELS_RING) {
3279                         lpfc_debugfs_slow_ring_trc(phba,
3280                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3281                                 *(((uint32_t *) irsp) + 4),
3282                                 *(((uint32_t *) irsp) + 6),
3283                                 *(((uint32_t *) irsp) + 7));
3284                 }
3285
3286                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3287
3288                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3289                 /* Handle the response IOCB */
3290                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3291                 spin_lock_irqsave(&phba->hbalock, iflag);
3292
3293                 /*
3294                  * If the port response put pointer has not been updated, sync
3295                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3296                  * response put pointer.
3297                  */
3298                 if (pring->rspidx == portRspPut) {
3299                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3300                 }
3301         } /* while (pring->rspidx != portRspPut) */
3302
3303         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3304                 /* At least one response entry has been freed */
3305                 pring->stats.iocb_rsp_full++;
3306                 /* SET RxRE_RSP in Chip Att register */
3307                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3308                 writel(status, phba->CAregaddr);
3309                 readl(phba->CAregaddr); /* flush */
3310         }
3311         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3312                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3313                 pring->stats.iocb_cmd_empty++;
3314
3315                 /* Force update of the local copy of cmdGetInx */
3316                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3317                 lpfc_sli_resume_iocb(phba, pring);
3318
3319                 if ((pring->lpfc_sli_cmd_available))
3320                         (pring->lpfc_sli_cmd_available) (phba, pring);
3321
3322         }
3323
3324         spin_unlock_irqrestore(&phba->hbalock, iflag);
3325         return;
3326 }
3327
3328 /**
3329  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3330  * @phba: Pointer to HBA context object.
3331  * @pring: Pointer to driver SLI ring object.
3332  * @mask: Host attention register mask for this ring.
3333  *
3334  * This function is called from the worker thread when there is a pending
3335  * ELS response iocb on the driver internal slow-path response iocb worker
3336  * queue. The caller does not hold any lock. The function will remove each
3337  * response iocb from the response worker queue and calls the handle
3338  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3339  **/
3340 static void
3341 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3342                                    struct lpfc_sli_ring *pring, uint32_t mask)
3343 {
3344         struct lpfc_iocbq *irspiocbq;
3345         struct hbq_dmabuf *dmabuf;
3346         struct lpfc_cq_event *cq_event;
3347         unsigned long iflag;
3348
3349         spin_lock_irqsave(&phba->hbalock, iflag);
3350         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3351         spin_unlock_irqrestore(&phba->hbalock, iflag);
3352         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3353                 /* Get the response iocb from the head of work queue */
3354                 spin_lock_irqsave(&phba->hbalock, iflag);
3355                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3356                                  cq_event, struct lpfc_cq_event, list);
3357                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3358
3359                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3360                 case CQE_CODE_COMPL_WQE:
3361                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3362                                                  cq_event);
3363                         /* Translate ELS WCQE to response IOCBQ */
3364                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3365                                                                    irspiocbq);
3366                         if (irspiocbq)
3367                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3368                                                            irspiocbq);
3369                         break;
3370                 case CQE_CODE_RECEIVE:
3371                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3372                                               cq_event);
3373                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3374                         break;
3375                 default:
3376                         break;
3377                 }
3378         }
3379 }
3380
3381 /**
3382  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3383  * @phba: Pointer to HBA context object.
3384  * @pring: Pointer to driver SLI ring object.
3385  *
3386  * This function aborts all iocbs in the given ring and frees all the iocb
3387  * objects in txq. This function issues an abort iocb for all the iocb commands
3388  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3389  * the return of this function. The caller is not required to hold any locks.
3390  **/
3391 void
3392 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3393 {
3394         LIST_HEAD(completions);
3395         struct lpfc_iocbq *iocb, *next_iocb;
3396
3397         if (pring->ringno == LPFC_ELS_RING) {
3398                 lpfc_fabric_abort_hba(phba);
3399         }
3400
3401         /* Error everything on txq and txcmplq
3402          * First do the txq.
3403          */
3404         spin_lock_irq(&phba->hbalock);
3405         list_splice_init(&pring->txq, &completions);
3406         pring->txq_cnt = 0;
3407
3408         /* Next issue ABTS for everything on the txcmplq */
3409         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3410                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3411
3412         spin_unlock_irq(&phba->hbalock);
3413
3414         /* Cancel all the IOCBs from the completions list */
3415         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3416                               IOERR_SLI_ABORTED);
3417 }
3418
3419 /**
3420  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3421  * @phba: Pointer to HBA context object.
3422  *
3423  * This function flushes all iocbs in the fcp ring and frees all the iocb
3424  * objects in txq and txcmplq. This function will not issue abort iocbs
3425  * for all the iocb commands in txcmplq, they will just be returned with
3426  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3427  * slot has been permanently disabled.
3428  **/
3429 void
3430 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3431 {
3432         LIST_HEAD(txq);
3433         LIST_HEAD(txcmplq);
3434         struct lpfc_sli *psli = &phba->sli;
3435         struct lpfc_sli_ring  *pring;
3436
3437         /* Currently, only one fcp ring */
3438         pring = &psli->ring[psli->fcp_ring];
3439
3440         spin_lock_irq(&phba->hbalock);
3441         /* Retrieve everything on txq */
3442         list_splice_init(&pring->txq, &txq);
3443         pring->txq_cnt = 0;
3444
3445         /* Retrieve everything on the txcmplq */
3446         list_splice_init(&pring->txcmplq, &txcmplq);
3447         pring->txcmplq_cnt = 0;
3448         spin_unlock_irq(&phba->hbalock);
3449
3450         /* Flush the txq */
3451         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3452                               IOERR_SLI_DOWN);
3453
3454         /* Flush the txcmpq */
3455         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3456                               IOERR_SLI_DOWN);
3457 }
3458
3459 /**
3460  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3461  * @phba: Pointer to HBA context object.
3462  * @mask: Bit mask to be checked.
3463  *
3464  * This function reads the host status register and compares
3465  * with the provided bit mask to check if HBA completed
3466  * the restart. This function will wait in a loop for the
3467  * HBA to complete restart. If the HBA does not restart within
3468  * 15 iterations, the function will reset the HBA again. The
3469  * function returns 1 when HBA fail to restart otherwise returns
3470  * zero.
3471  **/
3472 static int
3473 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3474 {
3475         uint32_t status;
3476         int i = 0;
3477         int retval = 0;
3478
3479         /* Read the HBA Host Status Register */
3480         if (lpfc_readl(phba->HSregaddr, &status))
3481                 return 1;
3482
3483         /*
3484          * Check status register every 100ms for 5 retries, then every
3485          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3486          * every 2.5 sec for 4.
3487          * Break our of the loop if errors occurred during init.
3488          */
3489         while (((status & mask) != mask) &&
3490                !(status & HS_FFERM) &&
3491                i++ < 20) {
3492
3493                 if (i <= 5)
3494                         msleep(10);
3495                 else if (i <= 10)
3496                         msleep(500);
3497                 else
3498                         msleep(2500);
3499
3500                 if (i == 15) {
3501                                 /* Do post */
3502                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3503                         lpfc_sli_brdrestart(phba);
3504                 }
3505                 /* Read the HBA Host Status Register */
3506                 if (lpfc_readl(phba->HSregaddr, &status)) {
3507                         retval = 1;
3508                         break;
3509                 }
3510         }
3511
3512         /* Check to see if any errors occurred during init */
3513         if ((status & HS_FFERM) || (i >= 20)) {
3514                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3515                                 "2751 Adapter failed to restart, "
3516                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3517                                 status,
3518                                 readl(phba->MBslimaddr + 0xa8),
3519                                 readl(phba->MBslimaddr + 0xac));
3520                 phba->link_state = LPFC_HBA_ERROR;
3521                 retval = 1;
3522         }
3523
3524         return retval;
3525 }
3526
3527 /**
3528  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3529  * @phba: Pointer to HBA context object.
3530  * @mask: Bit mask to be checked.
3531  *
3532  * This function checks the host status register to check if HBA is
3533  * ready. This function will wait in a loop for the HBA to be ready
3534  * If the HBA is not ready , the function will will reset the HBA PCI
3535  * function again. The function returns 1 when HBA fail to be ready
3536  * otherwise returns zero.
3537  **/
3538 static int
3539 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3540 {
3541         uint32_t status;
3542         int retval = 0;
3543
3544         /* Read the HBA Host Status Register */
3545         status = lpfc_sli4_post_status_check(phba);
3546
3547         if (status) {
3548                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3549                 lpfc_sli_brdrestart(phba);
3550                 status = lpfc_sli4_post_status_check(phba);
3551         }
3552
3553         /* Check to see if any errors occurred during init */
3554         if (status) {
3555                 phba->link_state = LPFC_HBA_ERROR;
3556                 retval = 1;
3557         } else
3558                 phba->sli4_hba.intr_enable = 0;
3559
3560         return retval;
3561 }
3562
3563 /**
3564  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3565  * @phba: Pointer to HBA context object.
3566  * @mask: Bit mask to be checked.
3567  *
3568  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3569  * from the API jump table function pointer from the lpfc_hba struct.
3570  **/
3571 int
3572 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3573 {
3574         return phba->lpfc_sli_brdready(phba, mask);
3575 }
3576
3577 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3578
3579 /**
3580  * lpfc_reset_barrier - Make HBA ready for HBA reset
3581  * @phba: Pointer to HBA context object.
3582  *
3583  * This function is called before resetting an HBA. This
3584  * function requests HBA to quiesce DMAs before a reset.
3585  **/
3586 void lpfc_reset_barrier(struct lpfc_hba *phba)
3587 {
3588         uint32_t __iomem *resp_buf;
3589         uint32_t __iomem *mbox_buf;
3590         volatile uint32_t mbox;
3591         uint32_t hc_copy, ha_copy, resp_data;
3592         int  i;
3593         uint8_t hdrtype;
3594
3595         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3596         if (hdrtype != 0x80 ||
3597             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3598              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3599                 return;
3600
3601         /*
3602          * Tell the other part of the chip to suspend temporarily all
3603          * its DMA activity.
3604          */
3605         resp_buf = phba->MBslimaddr;
3606
3607         /* Disable the error attention */
3608         if (lpfc_readl(phba->HCregaddr, &hc_copy))
3609                 return;
3610         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3611         readl(phba->HCregaddr); /* flush */
3612         phba->link_flag |= LS_IGNORE_ERATT;
3613
3614         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3615                 return;
3616         if (ha_copy & HA_ERATT) {
3617                 /* Clear Chip error bit */
3618                 writel(HA_ERATT, phba->HAregaddr);
3619                 phba->pport->stopped = 1;
3620         }
3621
3622         mbox = 0;
3623         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3624         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3625
3626         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3627         mbox_buf = phba->MBslimaddr;
3628         writel(mbox, mbox_buf);
3629
3630         for (i = 0; i < 50; i++) {
3631                 if (lpfc_readl((resp_buf + 1), &resp_data))
3632                         return;
3633                 if (resp_data != ~(BARRIER_TEST_PATTERN))
3634                         mdelay(1);
3635                 else
3636                         break;
3637         }
3638         resp_data = 0;
3639         if (lpfc_readl((resp_buf + 1), &resp_data))
3640                 return;
3641         if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
3642                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3643                     phba->pport->stopped)
3644                         goto restore_hc;
3645                 else
3646                         goto clear_errat;
3647         }
3648
3649         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3650         resp_data = 0;
3651         for (i = 0; i < 500; i++) {
3652                 if (lpfc_readl(resp_buf, &resp_data))
3653                         return;
3654                 if (resp_data != mbox)
3655                         mdelay(1);
3656                 else
3657                         break;
3658         }
3659
3660 clear_errat:
3661
3662         while (++i < 500) {
3663                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3664                         return;
3665                 if (!(ha_copy & HA_ERATT))
3666                         mdelay(1);
3667                 else
3668                         break;
3669         }
3670
3671         if (readl(phba->HAregaddr) & HA_ERATT) {
3672                 writel(HA_ERATT, phba->HAregaddr);
3673                 phba->pport->stopped = 1;
3674         }
3675
3676 restore_hc:
3677         phba->link_flag &= ~LS_IGNORE_ERATT;
3678         writel(hc_copy, phba->HCregaddr);
3679         readl(phba->HCregaddr); /* flush */
3680 }
3681
3682 /**
3683  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3684  * @phba: Pointer to HBA context object.
3685  *
3686  * This function issues a kill_board mailbox command and waits for
3687  * the error attention interrupt. This function is called for stopping
3688  * the firmware processing. The caller is not required to hold any
3689  * locks. This function calls lpfc_hba_down_post function to free
3690  * any pending commands after the kill. The function will return 1 when it
3691  * fails to kill the board else will return 0.
3692  **/
3693 int
3694 lpfc_sli_brdkill(struct lpfc_hba *phba)
3695 {
3696         struct lpfc_sli *psli;
3697         LPFC_MBOXQ_t *pmb;
3698         uint32_t status;
3699         uint32_t ha_copy;
3700         int retval;
3701         int i = 0;
3702
3703         psli = &phba->sli;
3704
3705         /* Kill HBA */
3706         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3707                         "0329 Kill HBA Data: x%x x%x\n",
3708                         phba->pport->port_state, psli->sli_flag);
3709
3710         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3711         if (!pmb)
3712                 return 1;
3713
3714         /* Disable the error attention */
3715         spin_lock_irq(&phba->hbalock);
3716         if (lpfc_readl(phba->HCregaddr, &status)) {
3717                 spin_unlock_irq(&phba->hbalock);
3718                 mempool_free(pmb, phba->mbox_mem_pool);
3719                 return 1;
3720         }
3721         status &= ~HC_ERINT_ENA;
3722         writel(status, phba->HCregaddr);
3723         readl(phba->HCregaddr); /* flush */
3724         phba->link_flag |= LS_IGNORE_ERATT;
3725         spin_unlock_irq(&phba->hbalock);
3726
3727         lpfc_kill_board(phba, pmb);
3728         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3729         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3730
3731         if (retval != MBX_SUCCESS) {
3732                 if (retval != MBX_BUSY)
3733                         mempool_free(pmb, phba->mbox_mem_pool);
3734                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3735                                 "2752 KILL_BOARD command failed retval %d\n",
3736                                 retval);
3737                 spin_lock_irq(&phba->hbalock);
3738                 phba->link_flag &= ~LS_IGNORE_ERATT;
3739                 spin_unlock_irq(&phba->hbalock);
3740                 return 1;
3741         }
3742
3743         spin_lock_irq(&phba->hbalock);
3744         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3745         spin_unlock_irq(&phba->hbalock);
3746
3747         mempool_free(pmb, phba->mbox_mem_pool);
3748
3749         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3750          * attention every 100ms for 3 seconds. If we don't get ERATT after
3751          * 3 seconds we still set HBA_ERROR state because the status of the
3752          * board is now undefined.
3753          */
3754         if (lpfc_readl(phba->HAregaddr, &ha_copy))
3755                 return 1;
3756         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3757                 mdelay(100);
3758                 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3759                         return 1;
3760         }
3761
3762         del_timer_sync(&psli->mbox_tmo);
3763         if (ha_copy & HA_ERATT) {
3764                 writel(HA_ERATT, phba->HAregaddr);
3765                 phba->pport->stopped = 1;
3766         }
3767         spin_lock_irq(&phba->hbalock);
3768         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3769         psli->mbox_active = NULL;
3770         phba->link_flag &= ~LS_IGNORE_ERATT;
3771         spin_unlock_irq(&phba->hbalock);
3772
3773         lpfc_hba_down_post(phba);
3774         phba->link_state = LPFC_HBA_ERROR;
3775
3776         return ha_copy & HA_ERATT ? 0 : 1;
3777 }
3778
3779 /**
3780  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3781  * @phba: Pointer to HBA context object.
3782  *
3783  * This function resets the HBA by writing HC_INITFF to the control
3784  * register. After the HBA resets, this function resets all the iocb ring
3785  * indices. This function disables PCI layer parity checking during
3786  * the reset.
3787  * This function returns 0 always.
3788  * The caller is not required to hold any locks.
3789  **/
3790 int
3791 lpfc_sli_brdreset(struct lpfc_hba *phba)
3792 {
3793         struct lpfc_sli *psli;
3794         struct lpfc_sli_ring *pring;
3795         uint16_t cfg_value;
3796         int i;
3797
3798         psli = &phba->sli;
3799
3800         /* Reset HBA */
3801         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3802                         "0325 Reset HBA Data: x%x x%x\n",
3803                         phba->pport->port_state, psli->sli_flag);
3804
3805         /* perform board reset */
3806         phba->fc_eventTag = 0;
3807         phba->link_events = 0;
3808         phba->pport->fc_myDID = 0;
3809         phba->pport->fc_prevDID = 0;
3810
3811         /* Turn off parity checking and serr during the physical reset */
3812         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3813         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3814                               (cfg_value &
3815                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3816
3817         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3818
3819         /* Now toggle INITFF bit in the Host Control Register */
3820         writel(HC_INITFF, phba->HCregaddr);
3821         mdelay(1);
3822         readl(phba->HCregaddr); /* flush */
3823         writel(0, phba->HCregaddr);
3824         readl(phba->HCregaddr); /* flush */
3825
3826         /* Restore PCI cmd register */
3827         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3828
3829         /* Initialize relevant SLI info */
3830         for (i = 0; i < psli->num_rings; i++) {
3831                 pring = &psli->ring[i];
3832                 pring->flag = 0;
3833                 pring->rspidx = 0;
3834                 pring->next_cmdidx  = 0;
3835                 pring->local_getidx = 0;
3836                 pring->cmdidx = 0;
3837                 pring->missbufcnt = 0;
3838         }
3839
3840         phba->link_state = LPFC_WARM_START;
3841         return 0;
3842 }
3843
3844 /**
3845  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3846  * @phba: Pointer to HBA context object.
3847  *
3848  * This function resets a SLI4 HBA. This function disables PCI layer parity
3849  * checking during resets the device. The caller is not required to hold
3850  * any locks.
3851  *
3852  * This function returns 0 always.
3853  **/
3854 int
3855 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3856 {
3857         struct lpfc_sli *psli = &phba->sli;
3858         uint16_t cfg_value;
3859         uint8_t qindx;
3860
3861         /* Reset HBA */
3862         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3863                         "0295 Reset HBA Data: x%x x%x\n",
3864                         phba->pport->port_state, psli->sli_flag);
3865
3866         /* perform board reset */
3867         phba->fc_eventTag = 0;
3868         phba->link_events = 0;
3869         phba->pport->fc_myDID = 0;
3870         phba->pport->fc_prevDID = 0;
3871
3872         spin_lock_irq(&phba->hbalock);
3873         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3874         phba->fcf.fcf_flag = 0;
3875         /* Clean up the child queue list for the CQs */
3876         list_del_init(&phba->sli4_hba.mbx_wq->list);
3877         list_del_init(&phba->sli4_hba.els_wq->list);
3878         list_del_init(&phba->sli4_hba.hdr_rq->list);
3879         list_del_init(&phba->sli4_hba.dat_rq->list);
3880         list_del_init(&phba->sli4_hba.mbx_cq->list);
3881         list_del_init(&phba->sli4_hba.els_cq->list);
3882         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3883                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3884         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3885                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3886         spin_unlock_irq(&phba->hbalock);
3887
3888         /* Now physically reset the device */
3889         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3890                         "0389 Performing PCI function reset!\n");
3891
3892         /* Turn off parity checking and serr during the physical reset */
3893         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3894         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3895                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3896
3897         /* Perform FCoE PCI function reset */
3898         lpfc_pci_function_reset(phba);
3899
3900         /* Restore PCI cmd register */
3901         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3902
3903         return 0;
3904 }
3905
3906 /**
3907  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3908  * @phba: Pointer to HBA context object.
3909  *
3910  * This function is called in the SLI initialization code path to
3911  * restart the HBA. The caller is not required to hold any lock.
3912  * This function writes MBX_RESTART mailbox command to the SLIM and
3913  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3914  * function to free any pending commands. The function enables
3915  * POST only during the first initialization. The function returns zero.
3916  * The function does not guarantee completion of MBX_RESTART mailbox
3917  * command before the return of this function.
3918  **/
3919 static int
3920 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3921 {
3922         MAILBOX_t *mb;
3923         struct lpfc_sli *psli;
3924         volatile uint32_t word0;
3925         void __iomem *to_slim;
3926         uint32_t hba_aer_enabled;
3927
3928         spin_lock_irq(&phba->hbalock);
3929
3930         /* Take PCIe device Advanced Error Reporting (AER) state */
3931         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3932
3933         psli = &phba->sli;
3934
3935         /* Restart HBA */
3936         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3937                         "0337 Restart HBA Data: x%x x%x\n",
3938                         phba->pport->port_state, psli->sli_flag);
3939
3940         word0 = 0;
3941         mb = (MAILBOX_t *) &word0;
3942         mb->mbxCommand = MBX_RESTART;
3943         mb->mbxHc = 1;
3944
3945         lpfc_reset_barrier(phba);
3946
3947         to_slim = phba->MBslimaddr;
3948         writel(*(uint32_t *) mb, to_slim);
3949         readl(to_slim); /* flush */
3950
3951         /* Only skip post after fc_ffinit is completed */
3952         if (phba->pport->port_state)
3953                 word0 = 1;      /* This is really setting up word1 */
3954         else
3955                 word0 = 0;      /* This is really setting up word1 */
3956         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3957         writel(*(uint32_t *) mb, to_slim);
3958         readl(to_slim); /* flush */
3959
3960         lpfc_sli_brdreset(phba);
3961         phba->pport->stopped = 0;
3962         phba->link_state = LPFC_INIT_START;
3963         phba->hba_flag = 0;
3964         spin_unlock_irq(&phba->hbalock);
3965
3966         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3967         psli->stats_start = get_seconds();
3968
3969         /* Give the INITFF and Post time to settle. */
3970         mdelay(100);
3971
3972         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3973         if (hba_aer_enabled)
3974                 pci_disable_pcie_error_reporting(phba->pcidev);
3975
3976         lpfc_hba_down_post(phba);
3977
3978         return 0;
3979 }
3980
3981 /**
3982  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3983  * @phba: Pointer to HBA context object.
3984  *
3985  * This function is called in the SLI initialization code path to restart
3986  * a SLI4 HBA. The caller is not required to hold any lock.
3987  * At the end of the function, it calls lpfc_hba_down_post function to
3988  * free any pending commands.
3989  **/
3990 static int
3991 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3992 {
3993         struct lpfc_sli *psli = &phba->sli;
3994         uint32_t hba_aer_enabled;
3995
3996         /* Restart HBA */
3997         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3998                         "0296 Restart HBA Data: x%x x%x\n",
3999                         phba->pport->port_state, psli->sli_flag);
4000
4001         /* Take PCIe device Advanced Error Reporting (AER) state */
4002         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4003
4004         lpfc_sli4_brdreset(phba);
4005
4006         spin_lock_irq(&phba->hbalock);
4007         phba->pport->stopped = 0;
4008         phba->link_state = LPFC_INIT_START;
4009         phba->hba_flag = 0;
4010         spin_unlock_irq(&phba->hbalock);
4011
4012         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4013         psli->stats_start = get_seconds();
4014
4015         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4016         if (hba_aer_enabled)
4017                 pci_disable_pcie_error_reporting(phba->pcidev);
4018
4019         lpfc_hba_down_post(phba);
4020
4021         return 0;
4022 }
4023
4024 /**
4025  * lpfc_sli_brdrestart - Wrapper func for restarting hba
4026  * @phba: Pointer to HBA context object.
4027  *
4028  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4029  * API jump table function pointer from the lpfc_hba struct.
4030 **/
4031 int
4032 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4033 {
4034         return phba->lpfc_sli_brdrestart(phba);
4035 }
4036
4037 /**
4038  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4039  * @phba: Pointer to HBA context object.
4040  *
4041  * This function is called after a HBA restart to wait for successful
4042  * restart of the HBA. Successful restart of the HBA is indicated by
4043  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4044  * iteration, the function will restart the HBA again. The function returns
4045  * zero if HBA successfully restarted else returns negative error code.
4046  **/
4047 static int
4048 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4049 {
4050         uint32_t status, i = 0;
4051
4052         /* Read the HBA Host Status Register */
4053         if (lpfc_readl(phba->HSregaddr, &status))
4054                 return -EIO;
4055
4056         /* Check status register to see what current state is */
4057         i = 0;
4058         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4059
4060                 /* Check every 10ms for 10 retries, then every 100ms for 90
4061                  * retries, then every 1 sec for 50 retires for a total of
4062                  * ~60 seconds before reset the board again and check every
4063                  * 1 sec for 50 retries. The up to 60 seconds before the
4064                  * board ready is required by the Falcon FIPS zeroization
4065                  * complete, and any reset the board in between shall cause
4066                  * restart of zeroization, further delay the board ready.
4067                  */
4068                 if (i++ >= 200) {
4069                         /* Adapter failed to init, timeout, status reg
4070                            <status> */
4071                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4072                                         "0436 Adapter failed to init, "
4073                                         "timeout, status reg x%x, "
4074                                         "FW Data: A8 x%x AC x%x\n", status,
4075                                         readl(phba->MBslimaddr + 0xa8),
4076                                         readl(phba->MBslimaddr + 0xac));
4077                         phba->link_state = LPFC_HBA_ERROR;
4078                         return -ETIMEDOUT;
4079                 }
4080
4081                 /* Check to see if any errors occurred during init */
4082                 if (status & HS_FFERM) {
4083                         /* ERROR: During chipset initialization */
4084                         /* Adapter failed to init, chipset, status reg
4085                            <status> */
4086                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4087                                         "0437 Adapter failed to init, "
4088                                         "chipset, status reg x%x, "
4089                                         "FW Data: A8 x%x AC x%x\n", status,
4090                                         readl(phba->MBslimaddr + 0xa8),
4091                                         readl(phba->MBslimaddr + 0xac));
4092                         phba->link_state = LPFC_HBA_ERROR;
4093                         return -EIO;
4094                 }
4095
4096                 if (i <= 10)
4097                         msleep(10);
4098                 else if (i <= 100)
4099                         msleep(100);
4100                 else
4101                         msleep(1000);
4102
4103                 if (i == 150) {
4104                         /* Do post */
4105                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4106                         lpfc_sli_brdrestart(phba);
4107                 }
4108                 /* Read the HBA Host Status Register */
4109                 if (lpfc_readl(phba->HSregaddr, &status))
4110                         return -EIO;
4111         }
4112
4113         /* Check to see if any errors occurred during init */
4114         if (status & HS_FFERM) {
4115                 /* ERROR: During chipset initialization */
4116                 /* Adapter failed to init, chipset, status reg <status> */
4117                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4118                                 "0438 Adapter failed to init, chipset, "
4119                                 "status reg x%x, "
4120                                 "FW Data: A8 x%x AC x%x\n", status,
4121                                 readl(phba->MBslimaddr + 0xa8),
4122                                 readl(phba->MBslimaddr + 0xac));
4123                 phba->link_state = LPFC_HBA_ERROR;
4124                 return -EIO;
4125         }
4126
4127         /* Clear all interrupt enable conditions */
4128         writel(0, phba->HCregaddr);
4129         readl(phba->HCregaddr); /* flush */
4130
4131         /* setup host attn register */
4132         writel(0xffffffff, phba->HAregaddr);
4133         readl(phba->HAregaddr); /* flush */
4134         return 0;
4135 }
4136
4137 /**
4138  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4139  *
4140  * This function calculates and returns the number of HBQs required to be
4141  * configured.
4142  **/
4143 int
4144 lpfc_sli_hbq_count(void)
4145 {
4146         return ARRAY_SIZE(lpfc_hbq_defs);
4147 }
4148
4149 /**
4150  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4151  *
4152  * This function adds the number of hbq entries in every HBQ to get
4153  * the total number of hbq entries required for the HBA and returns
4154  * the total count.
4155  **/
4156 static int
4157 lpfc_sli_hbq_entry_count(void)
4158 {
4159         int  hbq_count = lpfc_sli_hbq_count();
4160         int  count = 0;
4161         int  i;
4162
4163         for (i = 0; i < hbq_count; ++i)
4164                 count += lpfc_hbq_defs[i]->entry_count;
4165         return count;
4166 }
4167
4168 /**
4169  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4170  *
4171  * This function calculates amount of memory required for all hbq entries
4172  * to be configured and returns the total memory required.
4173  **/
4174 int
4175 lpfc_sli_hbq_size(void)
4176 {
4177         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4178 }
4179
4180 /**
4181  * lpfc_sli_hbq_setup - configure and initialize HBQs
4182  * @phba: Pointer to HBA context object.
4183  *
4184  * This function is called during the SLI initialization to configure
4185  * all the HBQs and post buffers to the HBQ. The caller is not
4186  * required to hold any locks. This function will return zero if successful
4187  * else it will return negative error code.
4188  **/
4189 static int
4190 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4191 {
4192         int  hbq_count = lpfc_sli_hbq_count();
4193         LPFC_MBOXQ_t *pmb;
4194         MAILBOX_t *pmbox;
4195         uint32_t hbqno;
4196         uint32_t hbq_entry_index;
4197
4198                                 /* Get a Mailbox buffer to setup mailbox
4199                                  * commands for HBA initialization
4200                                  */
4201         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4202
4203         if (!pmb)
4204                 return -ENOMEM;
4205
4206         pmbox = &pmb->u.mb;
4207
4208         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4209         phba->link_state = LPFC_INIT_MBX_CMDS;
4210         phba->hbq_in_use = 1;
4211
4212         hbq_entry_index = 0;
4213         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4214                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4215                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4216                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4217                 phba->hbqs[hbqno].entry_count =
4218                         lpfc_hbq_defs[hbqno]->entry_count;
4219                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4220                         hbq_entry_index, pmb);
4221                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4222
4223                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4224                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4225                            mbxStatus <status>, ring <num> */
4226
4227                         lpfc_printf_log(phba, KERN_ERR,
4228                                         LOG_SLI | LOG_VPORT,
4229                                         "1805 Adapter failed to init. "
4230                                         "Data: x%x x%x x%x\n",
4231                                         pmbox->mbxCommand,
4232                                         pmbox->mbxStatus, hbqno);
4233
4234                         phba->link_state = LPFC_HBA_ERROR;
4235                         mempool_free(pmb, phba->mbox_mem_pool);
4236                         return -ENXIO;
4237                 }
4238         }
4239         phba->hbq_count = hbq_count;
4240
4241         mempool_free(pmb, phba->mbox_mem_pool);
4242
4243         /* Initially populate or replenish the HBQs */
4244         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4245                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4246         return 0;
4247 }
4248
4249 /**
4250  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4251  * @phba: Pointer to HBA context object.
4252  *
4253  * This function is called during the SLI initialization to configure
4254  * all the HBQs and post buffers to the HBQ. The caller is not
4255  * required to hold any locks. This function will return zero if successful
4256  * else it will return negative error code.
4257  **/
4258 static int
4259 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4260 {
4261         phba->hbq_in_use = 1;
4262         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4263         phba->hbq_count = 1;
4264         /* Initially populate or replenish the HBQs */
4265         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4266         return 0;
4267 }
4268
4269 /**
4270  * lpfc_sli_config_port - Issue config port mailbox command
4271  * @phba: Pointer to HBA context object.
4272  * @sli_mode: sli mode - 2/3
4273  *
4274  * This function is called by the sli intialization code path
4275  * to issue config_port mailbox command. This function restarts the
4276  * HBA firmware and issues a config_port mailbox command to configure
4277  * the SLI interface in the sli mode specified by sli_mode
4278  * variable. The caller is not required to hold any locks.
4279  * The function returns 0 if successful, else returns negative error
4280  * code.
4281  **/
4282 int
4283 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4284 {
4285         LPFC_MBOXQ_t *pmb;
4286         uint32_t resetcount = 0, rc = 0, done = 0;
4287
4288         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4289         if (!pmb) {
4290                 phba->link_state = LPFC_HBA_ERROR;
4291                 return -ENOMEM;
4292         }
4293
4294         phba->sli_rev = sli_mode;
4295         while (resetcount < 2 && !done) {
4296                 spin_lock_irq(&phba->hbalock);
4297                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4298                 spin_unlock_irq(&phba->hbalock);
4299                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4300                 lpfc_sli_brdrestart(phba);
4301                 rc = lpfc_sli_chipset_init(phba);
4302                 if (rc)
4303                         break;
4304
4305                 spin_lock_irq(&phba->hbalock);
4306                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4307                 spin_unlock_irq(&phba->hbalock);
4308                 resetcount++;
4309
4310                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4311                  * value of 0 means the call was successful.  Any other
4312                  * nonzero value is a failure, but if ERESTART is returned,
4313                  * the driver may reset the HBA and try again.
4314                  */
4315                 rc = lpfc_config_port_prep(phba);
4316                 if (rc == -ERESTART) {
4317                         phba->link_state = LPFC_LINK_UNKNOWN;
4318                         continue;
4319                 } else if (rc)
4320                         break;
4321                 phba->link_state = LPFC_INIT_MBX_CMDS;
4322                 lpfc_config_port(phba, pmb);
4323                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4324                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4325                                         LPFC_SLI3_HBQ_ENABLED |
4326                                         LPFC_SLI3_CRP_ENABLED |
4327                                         LPFC_SLI3_BG_ENABLED |
4328                                         LPFC_SLI3_DSS_ENABLED);
4329                 if (rc != MBX_SUCCESS) {
4330                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4331                                 "0442 Adapter failed to init, mbxCmd x%x "
4332                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4333                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4334                         spin_lock_irq(&phba->hbalock);
4335                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4336                         spin_unlock_irq(&phba->hbalock);
4337                         rc = -ENXIO;
4338                 } else {
4339                         /* Allow asynchronous mailbox command to go through */
4340                         spin_lock_irq(&phba->hbalock);
4341                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4342                         spin_unlock_irq(&phba->hbalock);
4343                         done = 1;
4344                 }
4345         }
4346         if (!done) {
4347                 rc = -EINVAL;
4348                 goto do_prep_failed;
4349         }
4350         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4351                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4352                         rc = -ENXIO;
4353                         goto do_prep_failed;
4354                 }
4355                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4356                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4357                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4358                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4359                                 phba->max_vpi : phba->max_vports;
4360
4361                 } else
4362                         phba->max_vpi = 0;
4363                 phba->fips_level = 0;
4364                 phba->fips_spec_rev = 0;
4365                 if (pmb->u.mb.un.varCfgPort.gdss) {
4366                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4367                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4368                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4369                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4370                                         "2850 Security Crypto Active. FIPS x%d "
4371                                         "(Spec Rev: x%d)",
4372                                         phba->fips_level, phba->fips_spec_rev);
4373                 }
4374                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4375                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4376                                         "2856 Config Port Security Crypto "
4377                                         "Error: x%x ",
4378                                         pmb->u.mb.un.varCfgPort.sec_err);
4379                 }
4380                 if (pmb->u.mb.un.varCfgPort.gerbm)
4381                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4382                 if (pmb->u.mb.un.varCfgPort.gcrp)
4383                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4384
4385                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4386                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4387
4388                 if (phba->cfg_enable_bg) {
4389                         if (pmb->u.mb.un.varCfgPort.gbg)
4390                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4391                         else
4392                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4393                                                 "0443 Adapter did not grant "
4394                                                 "BlockGuard\n");
4395                 }
4396         } else {
4397                 phba->hbq_get = NULL;
4398                 phba->port_gp = phba->mbox->us.s2.port;
4399                 phba->max_vpi = 0;
4400         }
4401 do_prep_failed:
4402         mempool_free(pmb, phba->mbox_mem_pool);
4403         return rc;
4404 }
4405
4406
4407 /**
4408  * lpfc_sli_hba_setup - SLI intialization function
4409  * @phba: Pointer to HBA context object.
4410  *
4411  * This function is the main SLI intialization function. This function
4412  * is called by the HBA intialization code, HBA reset code and HBA
4413  * error attention handler code. Caller is not required to hold any
4414  * locks. This function issues config_port mailbox command to configure
4415  * the SLI, setup iocb rings and HBQ rings. In the end the function
4416  * calls the config_port_post function to issue init_link mailbox
4417  * command and to start the discovery. The function will return zero
4418  * if successful, else it will return negative error code.
4419  **/
4420 int
4421 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4422 {
4423         uint32_t rc;
4424         int  mode = 3;
4425
4426         switch (lpfc_sli_mode) {
4427         case 2:
4428                 if (phba->cfg_enable_npiv) {
4429                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4430                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4431                                 "parameter (%d) to auto (0).\n",
4432                                 lpfc_sli_mode);
4433                         break;
4434                 }
4435                 mode = 2;
4436                 break;
4437         case 0:
4438         case 3:
4439                 break;
4440         default:
4441                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4442                                 "1819 Unrecognized lpfc_sli_mode "
4443                                 "parameter: %d.\n", lpfc_sli_mode);
4444
4445                 break;
4446         }
4447
4448         rc = lpfc_sli_config_port(phba, mode);
4449
4450         if (rc && lpfc_sli_mode == 3)
4451                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4452                                 "1820 Unable to select SLI-3.  "
4453                                 "Not supported by adapter.\n");
4454         if (rc && mode != 2)
4455                 rc = lpfc_sli_config_port(phba, 2);
4456         if (rc)
4457                 goto lpfc_sli_hba_setup_error;
4458
4459         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4460         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4461                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4462                 if (!rc) {
4463                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4464                                         "2709 This device supports "
4465                                         "Advanced Error Reporting (AER)\n");
4466                         spin_lock_irq(&phba->hbalock);
4467                         phba->hba_flag |= HBA_AER_ENABLED;
4468                         spin_unlock_irq(&phba->hbalock);
4469                 } else {
4470                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4471                                         "2708 This device does not support "
4472                                         "Advanced Error Reporting (AER)\n");
4473                         phba->cfg_aer_support = 0;
4474                 }
4475         }
4476
4477         if (phba->sli_rev == 3) {
4478                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4479                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4480         } else {
4481                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4482                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4483                 phba->sli3_options = 0;
4484         }
4485
4486         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4487                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4488                         phba->sli_rev, phba->max_vpi);
4489         rc = lpfc_sli_ring_map(phba);
4490
4491         if (rc)
4492                 goto lpfc_sli_hba_setup_error;
4493
4494         /* Init HBQs */
4495         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4496                 rc = lpfc_sli_hbq_setup(phba);
4497                 if (rc)
4498                         goto lpfc_sli_hba_setup_error;
4499         }
4500         spin_lock_irq(&phba->hbalock);
4501         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4502         spin_unlock_irq(&phba->hbalock);
4503
4504         rc = lpfc_config_port_post(phba);
4505         if (rc)
4506                 goto lpfc_sli_hba_setup_error;
4507
4508         return rc;
4509
4510 lpfc_sli_hba_setup_error:
4511         phba->link_state = LPFC_HBA_ERROR;
4512         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4513                         "0445 Firmware initialization failed\n");
4514         return rc;
4515 }
4516
4517 /**
4518  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4519  * @phba: Pointer to HBA context object.
4520  * @mboxq: mailbox pointer.
4521  * This function issue a dump mailbox command to read config region
4522  * 23 and parse the records in the region and populate driver
4523  * data structure.
4524  **/
4525 static int
4526 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4527                 LPFC_MBOXQ_t *mboxq)
4528 {
4529         struct lpfc_dmabuf *mp;
4530         struct lpfc_mqe *mqe;
4531         uint32_t data_length;
4532         int rc;
4533
4534         /* Program the default value of vlan_id and fc_map */
4535         phba->valid_vlan = 0;
4536         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4537         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4538         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4539
4540         mqe = &mboxq->u.mqe;
4541         if (lpfc_dump_fcoe_param(phba, mboxq))
4542                 return -ENOMEM;
4543
4544         mp = (struct lpfc_dmabuf *) mboxq->context1;
4545         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4546
4547         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4548                         "(%d):2571 Mailbox cmd x%x Status x%x "
4549                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4550                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4551                         "CQ: x%x x%x x%x x%x\n",
4552                         mboxq->vport ? mboxq->vport->vpi : 0,
4553                         bf_get(lpfc_mqe_command, mqe),
4554                         bf_get(lpfc_mqe_status, mqe),
4555                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4556                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4557                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4558                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4559                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4560                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4561                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4562                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4563                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4564                         mboxq->mcqe.word0,
4565                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4566                         mboxq->mcqe.trailer);
4567
4568         if (rc) {
4569                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4570                 kfree(mp);
4571                 return -EIO;
4572         }
4573         data_length = mqe->un.mb_words[5];
4574         if (data_length > DMP_RGN23_SIZE) {
4575                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4576                 kfree(mp);
4577                 return -EIO;
4578         }
4579
4580         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4581         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4582         kfree(mp);
4583         return 0;
4584 }
4585
4586 /**
4587  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4588  * @phba: pointer to lpfc hba data structure.
4589  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4590  * @vpd: pointer to the memory to hold resulting port vpd data.
4591  * @vpd_size: On input, the number of bytes allocated to @vpd.
4592  *            On output, the number of data bytes in @vpd.
4593  *
4594  * This routine executes a READ_REV SLI4 mailbox command.  In
4595  * addition, this routine gets the port vpd data.
4596  *
4597  * Return codes
4598  *      0 - successful
4599  *      -ENOMEM - could not allocated memory.
4600  **/
4601 static int
4602 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4603                     uint8_t *vpd, uint32_t *vpd_size)
4604 {
4605         int rc = 0;
4606         uint32_t dma_size;
4607         struct lpfc_dmabuf *dmabuf;
4608         struct lpfc_mqe *mqe;
4609
4610         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4611         if (!dmabuf)
4612                 return -ENOMEM;
4613
4614         /*
4615          * Get a DMA buffer for the vpd data resulting from the READ_REV
4616          * mailbox command.
4617          */
4618         dma_size = *vpd_size;
4619         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4620                                           dma_size,
4621                                           &dmabuf->phys,
4622                                           GFP_KERNEL);
4623         if (!dmabuf->virt) {
4624                 kfree(dmabuf);
4625                 return -ENOMEM;
4626         }
4627         memset(dmabuf->virt, 0, dma_size);
4628
4629         /*
4630          * The SLI4 implementation of READ_REV conflicts at word1,
4631          * bits 31:16 and SLI4 adds vpd functionality not present
4632          * in SLI3.  This code corrects the conflicts.
4633          */
4634         lpfc_read_rev(phba, mboxq);
4635         mqe = &mboxq->u.mqe;
4636         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4637         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4638         mqe->un.read_rev.word1 &= 0x0000FFFF;
4639         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4640         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4641
4642         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4643         if (rc) {
4644                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4645                                   dmabuf->virt, dmabuf->phys);
4646                 kfree(dmabuf);
4647                 return -EIO;
4648         }
4649
4650         /*
4651          * The available vpd length cannot be bigger than the
4652          * DMA buffer passed to the port.  Catch the less than
4653          * case and update the caller's size.
4654          */
4655         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4656                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4657
4658         memcpy(vpd, dmabuf->virt, *vpd_size);
4659
4660         dma_free_coherent(&phba->pcidev->dev, dma_size,
4661                           dmabuf->virt, dmabuf->phys);
4662         kfree(dmabuf);
4663         return 0;
4664 }
4665
4666 /**
4667  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4668  * @phba: pointer to lpfc hba data structure.
4669  *
4670  * This routine is called to explicitly arm the SLI4 device's completion and
4671  * event queues
4672  **/
4673 static void
4674 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4675 {
4676         uint8_t fcp_eqidx;
4677
4678         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4679         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4680         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4681                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4682                                      LPFC_QUEUE_REARM);
4683         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4684         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4685                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4686                                      LPFC_QUEUE_REARM);
4687 }
4688
4689 /**
4690  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4691  * @phba: Pointer to HBA context object.
4692  *
4693  * This function is the main SLI4 device intialization PCI function. This
4694  * function is called by the HBA intialization code, HBA reset code and
4695  * HBA error attention handler code. Caller is not required to hold any
4696  * locks.
4697  **/
4698 int
4699 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4700 {
4701         int rc;
4702         LPFC_MBOXQ_t *mboxq;
4703         struct lpfc_mqe *mqe;
4704         uint8_t *vpd;
4705         uint32_t vpd_size;
4706         uint32_t ftr_rsp = 0;
4707         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4708         struct lpfc_vport *vport = phba->pport;
4709         struct lpfc_dmabuf *mp;
4710
4711         /*
4712          * TODO:  Why does this routine execute these task in a different
4713          * order from probe?
4714          */
4715         /* Perform a PCI function reset to start from clean */
4716         rc = lpfc_pci_function_reset(phba);
4717         if (unlikely(rc))
4718                 return -ENODEV;
4719
4720         /* Check the HBA Host Status Register for readyness */
4721         rc = lpfc_sli4_post_status_check(phba);
4722         if (unlikely(rc))
4723                 return -ENODEV;
4724         else {
4725                 spin_lock_irq(&phba->hbalock);
4726                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4727                 spin_unlock_irq(&phba->hbalock);
4728         }
4729
4730         /*
4731          * Allocate a single mailbox container for initializing the
4732          * port.
4733          */
4734         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4735         if (!mboxq)
4736                 return -ENOMEM;
4737
4738         /*
4739          * Continue initialization with default values even if driver failed
4740          * to read FCoE param config regions
4741          */
4742         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4743                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4744                         "2570 Failed to read FCoE parameters\n");
4745
4746         /* Issue READ_REV to collect vpd and FW information. */
4747         vpd_size = SLI4_PAGE_SIZE;
4748         vpd = kzalloc(vpd_size, GFP_KERNEL);
4749         if (!vpd) {
4750                 rc = -ENOMEM;
4751                 goto out_free_mbox;
4752         }
4753
4754         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4755         if (unlikely(rc)) {
4756                 kfree(vpd);
4757                 goto out_free_mbox;
4758         }
4759         mqe = &mboxq->u.mqe;
4760         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4761         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4762                 phba->hba_flag |= HBA_FCOE_MODE;
4763         else
4764                 phba->hba_flag &= ~HBA_FCOE_MODE;
4765
4766         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4767                 LPFC_DCBX_CEE_MODE)
4768                 phba->hba_flag |= HBA_FIP_SUPPORT;
4769         else
4770                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4771
4772         if (phba->sli_rev != LPFC_SLI_REV4) {
4773                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4774                         "0376 READ_REV Error. SLI Level %d "
4775                         "FCoE enabled %d\n",
4776                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
4777                 rc = -EIO;
4778                 kfree(vpd);
4779                 goto out_free_mbox;
4780         }
4781         /*
4782          * Evaluate the read rev and vpd data. Populate the driver
4783          * state with the results. If this routine fails, the failure
4784          * is not fatal as the driver will use generic values.
4785          */
4786         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4787         if (unlikely(!rc)) {
4788                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4789                                 "0377 Error %d parsing vpd. "
4790                                 "Using defaults.\n", rc);
4791                 rc = 0;
4792         }
4793         kfree(vpd);
4794
4795         /* Save information as VPD data */
4796         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4797         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4798         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4799         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4800                                          &mqe->un.read_rev);
4801         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4802                                        &mqe->un.read_rev);
4803         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4804                                             &mqe->un.read_rev);
4805         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4806                                            &mqe->un.read_rev);
4807         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4808         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4809         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4810         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4811         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4812         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4813         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4814                         "(%d):0380 READ_REV Status x%x "
4815                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4816                         mboxq->vport ? mboxq->vport->vpi : 0,
4817                         bf_get(lpfc_mqe_status, mqe),
4818                         phba->vpd.rev.opFwName,
4819                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4820                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4821
4822         /*
4823          * Discover the port's supported feature set and match it against the
4824          * hosts requests.
4825          */
4826         lpfc_request_features(phba, mboxq);
4827         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4828         if (unlikely(rc)) {
4829                 rc = -EIO;
4830                 goto out_free_mbox;
4831         }
4832
4833         /*
4834          * The port must support FCP initiator mode as this is the
4835          * only mode running in the host.
4836          */
4837         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4838                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4839                                 "0378 No support for fcpi mode.\n");
4840                 ftr_rsp++;
4841         }
4842         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
4843                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
4844         else
4845                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
4846         /*
4847          * If the port cannot support the host's requested features
4848          * then turn off the global config parameters to disable the
4849          * feature in the driver.  This is not a fatal error.
4850          */
4851         if ((phba->cfg_enable_bg) &&
4852             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4853                 ftr_rsp++;
4854
4855         if (phba->max_vpi && phba->cfg_enable_npiv &&
4856             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4857                 ftr_rsp++;
4858
4859         if (ftr_rsp) {
4860                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4861                                 "0379 Feature Mismatch Data: x%08x %08x "
4862                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4863                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4864                                 phba->cfg_enable_npiv, phba->max_vpi);
4865                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4866                         phba->cfg_enable_bg = 0;
4867                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4868                         phba->cfg_enable_npiv = 0;
4869         }
4870
4871         /* These SLI3 features are assumed in SLI4 */
4872         spin_lock_irq(&phba->hbalock);
4873         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4874         spin_unlock_irq(&phba->hbalock);
4875
4876         /* Read the port's service parameters. */
4877         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4878         if (rc) {
4879                 phba->link_state = LPFC_HBA_ERROR;
4880                 rc = -ENOMEM;
4881                 goto out_free_mbox;
4882         }
4883
4884         mboxq->vport = vport;
4885         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4886         mp = (struct lpfc_dmabuf *) mboxq->context1;
4887         if (rc == MBX_SUCCESS) {
4888                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4889                 rc = 0;
4890         }
4891
4892         /*
4893          * This memory was allocated by the lpfc_read_sparam routine. Release
4894          * it to the mbuf pool.
4895          */
4896         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4897         kfree(mp);
4898         mboxq->context1 = NULL;
4899         if (unlikely(rc)) {
4900                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4901                                 "0382 READ_SPARAM command failed "
4902                                 "status %d, mbxStatus x%x\n",
4903                                 rc, bf_get(lpfc_mqe_status, mqe));
4904                 phba->link_state = LPFC_HBA_ERROR;
4905                 rc = -EIO;
4906                 goto out_free_mbox;
4907         }
4908
4909         if (phba->cfg_soft_wwnn)
4910                 u64_to_wwn(phba->cfg_soft_wwnn,
4911                            vport->fc_sparam.nodeName.u.wwn);
4912         if (phba->cfg_soft_wwpn)
4913                 u64_to_wwn(phba->cfg_soft_wwpn,
4914                            vport->fc_sparam.portName.u.wwn);
4915         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4916                sizeof(struct lpfc_name));
4917         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4918                sizeof(struct lpfc_name));
4919
4920         /* Update the fc_host data structures with new wwn. */
4921         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4922         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4923
4924         /* Register SGL pool to the device using non-embedded mailbox command */
4925         rc = lpfc_sli4_post_sgl_list(phba);
4926         if (unlikely(rc)) {
4927                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4928                                 "0582 Error %d during sgl post operation\n",
4929                                         rc);
4930                 rc = -ENODEV;
4931                 goto out_free_mbox;
4932         }
4933
4934         /* Register SCSI SGL pool to the device */
4935         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4936         if (unlikely(rc)) {
4937                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4938                                 "0383 Error %d during scsi sgl post "
4939                                 "operation\n", rc);
4940                 /* Some Scsi buffers were moved to the abort scsi list */
4941                 /* A pci function reset will repost them */
4942                 rc = -ENODEV;
4943                 goto out_free_mbox;
4944         }
4945
4946         /* Post the rpi header region to the device. */
4947         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4948         if (unlikely(rc)) {
4949                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4950                                 "0393 Error %d during rpi post operation\n",
4951                                 rc);
4952                 rc = -ENODEV;
4953                 goto out_free_mbox;
4954         }
4955
4956         /* Set up all the queues to the device */
4957         rc = lpfc_sli4_queue_setup(phba);
4958         if (unlikely(rc)) {
4959                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4960                                 "0381 Error %d during queue setup.\n ", rc);
4961                 goto out_stop_timers;
4962         }
4963
4964         /* Arm the CQs and then EQs on device */
4965         lpfc_sli4_arm_cqeq_intr(phba);
4966
4967         /* Indicate device interrupt mode */
4968         phba->sli4_hba.intr_enable = 1;
4969
4970         /* Allow asynchronous mailbox command to go through */
4971         spin_lock_irq(&phba->hbalock);
4972         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4973         spin_unlock_irq(&phba->hbalock);
4974
4975         /* Post receive buffers to the device */
4976         lpfc_sli4_rb_setup(phba);
4977
4978         /* Reset HBA FCF states after HBA reset */
4979         phba->fcf.fcf_flag = 0;
4980         phba->fcf.current_rec.flag = 0;
4981
4982         /* Start the ELS watchdog timer */
4983         mod_timer(&vport->els_tmofunc,
4984                   jiffies + HZ * (phba->fc_ratov * 2));
4985
4986         /* Start heart beat timer */
4987         mod_timer(&phba->hb_tmofunc,
4988                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4989         phba->hb_outstanding = 0;
4990         phba->last_completion_time = jiffies;
4991
4992         /* Start error attention (ERATT) polling timer */
4993         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4994
4995         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4996         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4997                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4998                 if (!rc) {
4999                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5000                                         "2829 This device supports "
5001                                         "Advanced Error Reporting (AER)\n");
5002                         spin_lock_irq(&phba->hbalock);
5003                         phba->hba_flag |= HBA_AER_ENABLED;
5004                         spin_unlock_irq(&phba->hbalock);
5005                 } else {
5006                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5007                                         "2830 This device does not support "
5008                                         "Advanced Error Reporting (AER)\n");
5009                         phba->cfg_aer_support = 0;
5010                 }
5011         }
5012
5013         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
5014                 /*
5015                  * The FC Port needs to register FCFI (index 0)
5016                  */
5017                 lpfc_reg_fcfi(phba, mboxq);
5018                 mboxq->vport = phba->pport;
5019                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5020                 if (rc != MBX_SUCCESS)
5021                         goto out_unset_queue;
5022                 rc = 0;
5023                 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
5024                                         &mboxq->u.mqe.un.reg_fcfi);
5025         }
5026         /*
5027          * The port is ready, set the host's link state to LINK_DOWN
5028          * in preparation for link interrupts.
5029          */
5030         spin_lock_irq(&phba->hbalock);
5031         phba->link_state = LPFC_LINK_DOWN;
5032         spin_unlock_irq(&phba->hbalock);
5033         if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5034                 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5035 out_unset_queue:
5036         /* Unset all the queues set up in this routine when error out */
5037         if (rc)
5038                 lpfc_sli4_queue_unset(phba);
5039 out_stop_timers:
5040         if (rc)
5041                 lpfc_stop_hba_timers(phba);
5042 out_free_mbox:
5043         mempool_free(mboxq, phba->mbox_mem_pool);
5044         return rc;
5045 }
5046
5047 /**
5048  * lpfc_mbox_timeout - Timeout call back function for mbox timer
5049  * @ptr: context object - pointer to hba structure.
5050  *
5051  * This is the callback function for mailbox timer. The mailbox
5052  * timer is armed when a new mailbox command is issued and the timer
5053  * is deleted when the mailbox complete. The function is called by
5054  * the kernel timer code when a mailbox does not complete within
5055  * expected time. This function wakes up the worker thread to
5056  * process the mailbox timeout and returns. All the processing is
5057  * done by the worker thread function lpfc_mbox_timeout_handler.
5058  **/
5059 void
5060 lpfc_mbox_timeout(unsigned long ptr)
5061 {
5062         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
5063         unsigned long iflag;
5064         uint32_t tmo_posted;
5065
5066         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5067         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
5068         if (!tmo_posted)
5069                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5070         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5071
5072         if (!tmo_posted)
5073                 lpfc_worker_wake_up(phba);
5074         return;
5075 }
5076
5077
5078 /**
5079  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
5080  * @phba: Pointer to HBA context object.
5081  *
5082  * This function is called from worker thread when a mailbox command times out.
5083  * The caller is not required to hold any locks. This function will reset the
5084  * HBA and recover all the pending commands.
5085  **/
5086 void
5087 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5088 {
5089         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
5090         MAILBOX_t *mb = &pmbox->u.mb;
5091         struct lpfc_sli *psli = &phba->sli;
5092         struct lpfc_sli_ring *pring;
5093
5094         /* Check the pmbox pointer first.  There is a race condition
5095          * between the mbox timeout handler getting executed in the
5096          * worklist and the mailbox actually completing. When this
5097          * race condition occurs, the mbox_active will be NULL.
5098          */
5099         spin_lock_irq(&phba->hbalock);
5100         if (pmbox == NULL) {
5101                 lpfc_printf_log(phba, KERN_WARNING,
5102                                 LOG_MBOX | LOG_SLI,
5103                                 "0353 Active Mailbox cleared - mailbox timeout "
5104                                 "exiting\n");
5105                 spin_unlock_irq(&phba->hbalock);
5106                 return;
5107         }
5108
5109         /* Mbox cmd <mbxCommand> timeout */
5110         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5111                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
5112                         mb->mbxCommand,
5113                         phba->pport->port_state,
5114                         phba->sli.sli_flag,
5115                         phba->sli.mbox_active);
5116         spin_unlock_irq(&phba->hbalock);
5117
5118         /* Setting state unknown so lpfc_sli_abort_iocb_ring
5119          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5120          * it to fail all outstanding SCSI IO.
5121          */
5122         spin_lock_irq(&phba->pport->work_port_lock);
5123         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5124         spin_unlock_irq(&phba->pport->work_port_lock);
5125         spin_lock_irq(&phba->hbalock);
5126         phba->link_state = LPFC_LINK_UNKNOWN;
5127         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5128         spin_unlock_irq(&phba->hbalock);
5129
5130         pring = &psli->ring[psli->fcp_ring];
5131         lpfc_sli_abort_iocb_ring(phba, pring);
5132
5133         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5134                         "0345 Resetting board due to mailbox timeout\n");
5135
5136         /* Reset the HBA device */
5137         lpfc_reset_hba(phba);
5138 }
5139
5140 /**
5141  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
5142  * @phba: Pointer to HBA context object.
5143  * @pmbox: Pointer to mailbox object.
5144  * @flag: Flag indicating how the mailbox need to be processed.
5145  *
5146  * This function is called by discovery code and HBA management code
5147  * to submit a mailbox command to firmware with SLI-3 interface spec. This
5148  * function gets the hbalock to protect the data structures.
5149  * The mailbox command can be submitted in polling mode, in which case
5150  * this function will wait in a polling loop for the completion of the
5151  * mailbox.
5152  * If the mailbox is submitted in no_wait mode (not polling) the
5153  * function will submit the command and returns immediately without waiting
5154  * for the mailbox completion. The no_wait is supported only when HBA
5155  * is in SLI2/SLI3 mode - interrupts are enabled.
5156  * The SLI interface allows only one mailbox pending at a time. If the
5157  * mailbox is issued in polling mode and there is already a mailbox
5158  * pending, then the function will return an error. If the mailbox is issued
5159  * in NO_WAIT mode and there is a mailbox pending already, the function
5160  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5161  * The sli layer owns the mailbox object until the completion of mailbox
5162  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5163  * return codes the caller owns the mailbox command after the return of
5164  * the function.
5165  **/
5166 static int
5167 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5168                        uint32_t flag)
5169 {
5170         MAILBOX_t *mb;
5171         struct lpfc_sli *psli = &phba->sli;
5172         uint32_t status, evtctr;
5173         uint32_t ha_copy, hc_copy;
5174         int i;
5175         unsigned long timeout;
5176         unsigned long drvr_flag = 0;
5177         uint32_t word0, ldata;
5178         void __iomem *to_slim;
5179         int processing_queue = 0;
5180
5181         spin_lock_irqsave(&phba->hbalock, drvr_flag);
5182         if (!pmbox) {
5183                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5184                 /* processing mbox queue from intr_handler */
5185                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5186                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5187                         return MBX_SUCCESS;
5188                 }
5189                 processing_queue = 1;
5190                 pmbox = lpfc_mbox_get(phba);
5191                 if (!pmbox) {
5192                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5193                         return MBX_SUCCESS;
5194                 }
5195         }
5196
5197         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
5198                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
5199                 if(!pmbox->vport) {
5200                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5201                         lpfc_printf_log(phba, KERN_ERR,
5202                                         LOG_MBOX | LOG_VPORT,
5203                                         "1806 Mbox x%x failed. No vport\n",
5204                                         pmbox->u.mb.mbxCommand);
5205                         dump_stack();
5206                         goto out_not_finished;
5207                 }
5208         }
5209
5210         /* If the PCI channel is in offline state, do not post mbox. */
5211         if (unlikely(pci_channel_offline(phba->pcidev))) {
5212                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5213                 goto out_not_finished;
5214         }
5215
5216         /* If HBA has a deferred error attention, fail the iocb. */
5217         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5218                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5219                 goto out_not_finished;
5220         }
5221
5222         psli = &phba->sli;
5223
5224         mb = &pmbox->u.mb;
5225         status = MBX_SUCCESS;
5226
5227         if (phba->link_state == LPFC_HBA_ERROR) {
5228                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5229
5230                 /* Mbox command <mbxCommand> cannot issue */
5231                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5232                                 "(%d):0311 Mailbox command x%x cannot "
5233                                 "issue Data: x%x x%x\n",
5234                                 pmbox->vport ? pmbox->vport->vpi : 0,
5235                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5236                 goto out_not_finished;
5237         }
5238
5239         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
5240                 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
5241                         !(hc_copy & HC_MBINT_ENA)) {
5242                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5243                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5244                                 "(%d):2528 Mailbox command x%x cannot "
5245                                 "issue Data: x%x x%x\n",
5246                                 pmbox->vport ? pmbox->vport->vpi : 0,
5247                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5248                         goto out_not_finished;
5249                 }
5250         }
5251
5252         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5253                 /* Polling for a mbox command when another one is already active
5254                  * is not allowed in SLI. Also, the driver must have established
5255                  * SLI2 mode to queue and process multiple mbox commands.
5256                  */
5257
5258                 if (flag & MBX_POLL) {
5259                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5260
5261                         /* Mbox command <mbxCommand> cannot issue */
5262                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5263                                         "(%d):2529 Mailbox command x%x "
5264                                         "cannot issue Data: x%x x%x\n",
5265                                         pmbox->vport ? pmbox->vport->vpi : 0,
5266                                         pmbox->u.mb.mbxCommand,
5267                                         psli->sli_flag, flag);
5268                         goto out_not_finished;
5269                 }
5270
5271                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
5272                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5273                         /* Mbox command <mbxCommand> cannot issue */
5274                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5275                                         "(%d):2530 Mailbox command x%x "
5276                                         "cannot issue Data: x%x x%x\n",
5277                                         pmbox->vport ? pmbox->vport->vpi : 0,
5278                                         pmbox->u.mb.mbxCommand,
5279                                         psli->sli_flag, flag);
5280                         goto out_not_finished;
5281                 }
5282
5283                 /* Another mailbox command is still being processed, queue this
5284                  * command to be processed later.
5285                  */
5286                 lpfc_mbox_put(phba, pmbox);
5287
5288                 /* Mbox cmd issue - BUSY */
5289                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5290                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
5291                                 "x%x x%x x%x x%x\n",
5292                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5293                                 mb->mbxCommand, phba->pport->port_state,
5294                                 psli->sli_flag, flag);
5295
5296                 psli->slistat.mbox_busy++;
5297                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5298
5299                 if (pmbox->vport) {
5300                         lpfc_debugfs_disc_trc(pmbox->vport,
5301                                 LPFC_DISC_TRC_MBOX_VPORT,
5302                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
5303                                 (uint32_t)mb->mbxCommand,
5304                                 mb->un.varWords[0], mb->un.varWords[1]);
5305                 }
5306                 else {
5307                         lpfc_debugfs_disc_trc(phba->pport,
5308                                 LPFC_DISC_TRC_MBOX,
5309                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
5310                                 (uint32_t)mb->mbxCommand,
5311                                 mb->un.varWords[0], mb->un.varWords[1]);
5312                 }
5313
5314                 return MBX_BUSY;
5315         }
5316
5317         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5318
5319         /* If we are not polling, we MUST be in SLI2 mode */
5320         if (flag != MBX_POLL) {
5321                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
5322                     (mb->mbxCommand != MBX_KILL_BOARD)) {
5323                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5324                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5325                         /* Mbox command <mbxCommand> cannot issue */
5326                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5327                                         "(%d):2531 Mailbox command x%x "
5328                                         "cannot issue Data: x%x x%x\n",
5329                                         pmbox->vport ? pmbox->vport->vpi : 0,
5330                                         pmbox->u.mb.mbxCommand,
5331                                         psli->sli_flag, flag);
5332                         goto out_not_finished;
5333                 }
5334                 /* timeout active mbox command */
5335                 mod_timer(&psli->mbox_tmo, (jiffies +
5336                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
5337         }
5338
5339         /* Mailbox cmd <cmd> issue */
5340         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5341                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
5342                         "x%x\n",
5343                         pmbox->vport ? pmbox->vport->vpi : 0,
5344                         mb->mbxCommand, phba->pport->port_state,
5345                         psli->sli_flag, flag);
5346
5347         if (mb->mbxCommand != MBX_HEARTBEAT) {
5348                 if (pmbox->vport) {
5349                         lpfc_debugfs_disc_trc(pmbox->vport,
5350                                 LPFC_DISC_TRC_MBOX_VPORT,
5351                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5352                                 (uint32_t)mb->mbxCommand,
5353                                 mb->un.varWords[0], mb->un.varWords[1]);
5354                 }
5355                 else {
5356                         lpfc_debugfs_disc_trc(phba->pport,
5357                                 LPFC_DISC_TRC_MBOX,
5358                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
5359                                 (uint32_t)mb->mbxCommand,
5360                                 mb->un.varWords[0], mb->un.varWords[1]);
5361                 }
5362         }
5363
5364         psli->slistat.mbox_cmd++;
5365         evtctr = psli->slistat.mbox_event;
5366
5367         /* next set own bit for the adapter and copy over command word */
5368         mb->mbxOwner = OWN_CHIP;
5369
5370         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5371                 /* Populate mbox extension offset word. */
5372                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5373                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5374                                 = (uint8_t *)phba->mbox_ext
5375                                   - (uint8_t *)phba->mbox;
5376                 }
5377
5378                 /* Copy the mailbox extension data */
5379                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5380                         lpfc_sli_pcimem_bcopy(pmbox->context2,
5381                                 (uint8_t *)phba->mbox_ext,
5382                                 pmbox->in_ext_byte_len);
5383                 }
5384                 /* Copy command data to host SLIM area */
5385                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5386         } else {
5387                 /* Populate mbox extension offset word. */
5388                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5389                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5390                                 = MAILBOX_HBA_EXT_OFFSET;
5391
5392                 /* Copy the mailbox extension data */
5393                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5394                         lpfc_memcpy_to_slim(phba->MBslimaddr +
5395                                 MAILBOX_HBA_EXT_OFFSET,
5396                                 pmbox->context2, pmbox->in_ext_byte_len);
5397
5398                 }
5399                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5400                         /* copy command data into host mbox for cmpl */
5401                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5402                 }
5403
5404                 /* First copy mbox command data to HBA SLIM, skip past first
5405                    word */
5406                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5407                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5408                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
5409
5410                 /* Next copy over first word, with mbxOwner set */
5411                 ldata = *((uint32_t *)mb);
5412                 to_slim = phba->MBslimaddr;
5413                 writel(ldata, to_slim);
5414                 readl(to_slim); /* flush */
5415
5416                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5417                         /* switch over to host mailbox */
5418                         psli->sli_flag |= LPFC_SLI_ACTIVE;
5419                 }
5420         }
5421
5422         wmb();
5423
5424         switch (flag) {
5425         case MBX_NOWAIT:
5426                 /* Set up reference to mailbox command */
5427                 psli->mbox_active = pmbox;
5428                 /* Interrupt board to do it */
5429                 writel(CA_MBATT, phba->CAregaddr);
5430                 readl(phba->CAregaddr); /* flush */
5431                 /* Don't wait for it to finish, just return */
5432                 break;
5433
5434         case MBX_POLL:
5435                 /* Set up null reference to mailbox command */
5436                 psli->mbox_active = NULL;
5437                 /* Interrupt board to do it */
5438                 writel(CA_MBATT, phba->CAregaddr);
5439                 readl(phba->CAregaddr); /* flush */
5440
5441                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5442                         /* First read mbox status word */
5443                         word0 = *((uint32_t *)phba->mbox);
5444                         word0 = le32_to_cpu(word0);
5445                 } else {
5446                         /* First read mbox status word */
5447                         if (lpfc_readl(phba->MBslimaddr, &word0)) {
5448                                 spin_unlock_irqrestore(&phba->hbalock,
5449                                                        drvr_flag);
5450                                 goto out_not_finished;
5451                         }
5452                 }
5453
5454                 /* Read the HBA Host Attention Register */
5455                 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
5456                         spin_unlock_irqrestore(&phba->hbalock,
5457                                                        drvr_flag);
5458                         goto out_not_finished;
5459                 }
5460                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5461                                                              mb->mbxCommand) *
5462                                            1000) + jiffies;
5463                 i = 0;
5464                 /* Wait for command to complete */
5465                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5466                        (!(ha_copy & HA_MBATT) &&
5467                         (phba->link_state > LPFC_WARM_START))) {
5468                         if (time_after(jiffies, timeout)) {
5469                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5470                                 spin_unlock_irqrestore(&phba->hbalock,
5471                                                        drvr_flag);
5472                                 goto out_not_finished;
5473                         }
5474
5475                         /* Check if we took a mbox interrupt while we were
5476                            polling */
5477                         if (((word0 & OWN_CHIP) != OWN_CHIP)
5478                             && (evtctr != psli->slistat.mbox_event))
5479                                 break;
5480
5481                         if (i++ > 10) {
5482                                 spin_unlock_irqrestore(&phba->hbalock,
5483                                                        drvr_flag);
5484                                 msleep(1);
5485                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5486                         }
5487
5488                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5489                                 /* First copy command data */
5490                                 word0 = *((uint32_t *)phba->mbox);
5491                                 word0 = le32_to_cpu(word0);
5492                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5493                                         MAILBOX_t *slimmb;
5494                                         uint32_t slimword0;
5495                                         /* Check real SLIM for any errors */
5496                                         slimword0 = readl(phba->MBslimaddr);
5497                                         slimmb = (MAILBOX_t *) & slimword0;
5498                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5499                                             && slimmb->mbxStatus) {
5500                                                 psli->sli_flag &=
5501                                                     ~LPFC_SLI_ACTIVE;
5502                                                 word0 = slimword0;
5503                                         }
5504                                 }
5505                         } else {
5506                                 /* First copy command data */
5507                                 word0 = readl(phba->MBslimaddr);
5508                         }
5509                         /* Read the HBA Host Attention Register */
5510                         if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
5511                                 spin_unlock_irqrestore(&phba->hbalock,
5512                                                        drvr_flag);
5513                                 goto out_not_finished;
5514                         }
5515                 }
5516
5517                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5518                         /* copy results back to user */
5519                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5520                         /* Copy the mailbox extension data */
5521                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5522                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5523                                                       pmbox->context2,
5524                                                       pmbox->out_ext_byte_len);
5525                         }
5526                 } else {
5527                         /* First copy command data */
5528                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5529                                                         MAILBOX_CMD_SIZE);
5530                         /* Copy the mailbox extension data */
5531                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5532                                 lpfc_memcpy_from_slim(pmbox->context2,
5533                                         phba->MBslimaddr +
5534                                         MAILBOX_HBA_EXT_OFFSET,
5535                                         pmbox->out_ext_byte_len);
5536                         }
5537                 }
5538
5539                 writel(HA_MBATT, phba->HAregaddr);
5540                 readl(phba->HAregaddr); /* flush */
5541
5542                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5543                 status = mb->mbxStatus;
5544         }
5545
5546         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5547         return status;
5548
5549 out_not_finished:
5550         if (processing_queue) {
5551                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5552                 lpfc_mbox_cmpl_put(phba, pmbox);
5553         }
5554         return MBX_NOT_FINISHED;
5555 }
5556
5557 /**
5558  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5559  * @phba: Pointer to HBA context object.
5560  *
5561  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5562  * the driver internal pending mailbox queue. It will then try to wait out the
5563  * possible outstanding mailbox command before return.
5564  *
5565  * Returns:
5566  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5567  *      the outstanding mailbox command timed out.
5568  **/
5569 static int
5570 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5571 {
5572         struct lpfc_sli *psli = &phba->sli;
5573         uint8_t actcmd = MBX_HEARTBEAT;
5574         int rc = 0;
5575         unsigned long timeout;
5576
5577         /* Mark the asynchronous mailbox command posting as blocked */
5578         spin_lock_irq(&phba->hbalock);
5579         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5580         if (phba->sli.mbox_active)
5581                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5582         spin_unlock_irq(&phba->hbalock);
5583         /* Determine how long we might wait for the active mailbox
5584          * command to be gracefully completed by firmware.
5585          */
5586         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5587                                    jiffies;
5588         /* Wait for the outstnading mailbox command to complete */
5589         while (phba->sli.mbox_active) {
5590                 /* Check active mailbox complete status every 2ms */
5591                 msleep(2);
5592                 if (time_after(jiffies, timeout)) {
5593                         /* Timeout, marked the outstanding cmd not complete */
5594                         rc = 1;
5595