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