[SCSI] lpfc 8.3.10: Fix user interface issues
[safe/jmp/linux-2.6] / 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
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport_fc.h>
32 #include <scsi/fc/fc_fs.h>
33 #include <linux/aer.h>
34
35 #include "lpfc_hw4.h"
36 #include "lpfc_hw.h"
37 #include "lpfc_sli.h"
38 #include "lpfc_sli4.h"
39 #include "lpfc_nl.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_compat.h"
46 #include "lpfc_debugfs.h"
47 #include "lpfc_vport.h"
48
49 /* There are only four IOCB completion types. */
50 typedef enum _lpfc_iocb_type {
51         LPFC_UNKNOWN_IOCB,
52         LPFC_UNSOL_IOCB,
53         LPFC_SOL_IOCB,
54         LPFC_ABORT_IOCB
55 } lpfc_iocb_type;
56
57
58 /* Provide function prototypes local to this module. */
59 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
60                                   uint32_t);
61 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
62                               uint8_t *, uint32_t *);
63 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
64                                                          struct lpfc_iocbq *);
65 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
66                                       struct hbq_dmabuf *);
67 static IOCB_t *
68 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
69 {
70         return &iocbq->iocb;
71 }
72
73 /**
74  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
75  * @q: The Work Queue to operate on.
76  * @wqe: The work Queue Entry to put on the Work queue.
77  *
78  * This routine will copy the contents of @wqe to the next available entry on
79  * the @q. This function will then ring the Work Queue Doorbell to signal the
80  * HBA to start processing the Work Queue Entry. This function returns 0 if
81  * successful. If no entries are available on @q then this function will return
82  * -ENOMEM.
83  * The caller is expected to hold the hbalock when calling this routine.
84  **/
85 static uint32_t
86 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
87 {
88         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
89         struct lpfc_register doorbell;
90         uint32_t host_index;
91
92         /* If the host has not yet processed the next entry then we are done */
93         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
94                 return -ENOMEM;
95         /* set consumption flag every once in a while */
96         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
97                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
98
99         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
100
101         /* Update the host index before invoking device */
102         host_index = q->host_index;
103         q->host_index = ((q->host_index + 1) % q->entry_count);
104
105         /* Ring Doorbell */
106         doorbell.word0 = 0;
107         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
108         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
109         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
110         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
111         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
112
113         return 0;
114 }
115
116 /**
117  * lpfc_sli4_wq_release - Updates internal hba index for WQ
118  * @q: The Work Queue to operate on.
119  * @index: The index to advance the hba index to.
120  *
121  * This routine will update the HBA index of a queue to reflect consumption of
122  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
123  * an entry the host calls this function to update the queue's internal
124  * pointers. This routine returns the number of entries that were consumed by
125  * the HBA.
126  **/
127 static uint32_t
128 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
129 {
130         uint32_t released = 0;
131
132         if (q->hba_index == index)
133                 return 0;
134         do {
135                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
136                 released++;
137         } while (q->hba_index != index);
138         return released;
139 }
140
141 /**
142  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
143  * @q: The Mailbox Queue to operate on.
144  * @wqe: The Mailbox Queue Entry to put on the Work queue.
145  *
146  * This routine will copy the contents of @mqe to the next available entry on
147  * the @q. This function will then ring the Work Queue Doorbell to signal the
148  * HBA to start processing the Work Queue Entry. This function returns 0 if
149  * successful. If no entries are available on @q then this function will return
150  * -ENOMEM.
151  * The caller is expected to hold the hbalock when calling this routine.
152  **/
153 static uint32_t
154 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
155 {
156         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
157         struct lpfc_register doorbell;
158         uint32_t host_index;
159
160         /* If the host has not yet processed the next entry then we are done */
161         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
162                 return -ENOMEM;
163         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
164         /* Save off the mailbox pointer for completion */
165         q->phba->mbox = (MAILBOX_t *)temp_mqe;
166
167         /* Update the host index before invoking device */
168         host_index = q->host_index;
169         q->host_index = ((q->host_index + 1) % q->entry_count);
170
171         /* Ring Doorbell */
172         doorbell.word0 = 0;
173         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
174         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
175         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
176         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
177         return 0;
178 }
179
180 /**
181  * lpfc_sli4_mq_release - Updates internal hba index for MQ
182  * @q: The Mailbox Queue to operate on.
183  *
184  * This routine will update the HBA index of a queue to reflect consumption of
185  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
186  * an entry the host calls this function to update the queue's internal
187  * pointers. This routine returns the number of entries that were consumed by
188  * the HBA.
189  **/
190 static uint32_t
191 lpfc_sli4_mq_release(struct lpfc_queue *q)
192 {
193         /* Clear the mailbox pointer for completion */
194         q->phba->mbox = NULL;
195         q->hba_index = ((q->hba_index + 1) % q->entry_count);
196         return 1;
197 }
198
199 /**
200  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
201  * @q: The Event Queue to get the first valid EQE from
202  *
203  * This routine will get the first valid Event Queue Entry from @q, update
204  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
205  * the Queue (no more work to do), or the Queue is full of EQEs that have been
206  * processed, but not popped back to the HBA then this routine will return NULL.
207  **/
208 static struct lpfc_eqe *
209 lpfc_sli4_eq_get(struct lpfc_queue *q)
210 {
211         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
212
213         /* If the next EQE is not valid then we are done */
214         if (!bf_get(lpfc_eqe_valid, eqe))
215                 return NULL;
216         /* If the host has not yet processed the next entry then we are done */
217         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
218                 return NULL;
219
220         q->hba_index = ((q->hba_index + 1) % q->entry_count);
221         return eqe;
222 }
223
224 /**
225  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
226  * @q: The Event Queue that the host has completed processing for.
227  * @arm: Indicates whether the host wants to arms this CQ.
228  *
229  * This routine will mark all Event Queue Entries on @q, from the last
230  * known completed entry to the last entry that was processed, as completed
231  * by clearing the valid bit for each completion queue entry. Then it will
232  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
233  * The internal host index in the @q will be updated by this routine to indicate
234  * that the host has finished processing the entries. The @arm parameter
235  * indicates that the queue should be rearmed when ringing the doorbell.
236  *
237  * This function will return the number of EQEs that were popped.
238  **/
239 uint32_t
240 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
241 {
242         uint32_t released = 0;
243         struct lpfc_eqe *temp_eqe;
244         struct lpfc_register doorbell;
245
246         /* while there are valid entries */
247         while (q->hba_index != q->host_index) {
248                 temp_eqe = q->qe[q->host_index].eqe;
249                 bf_set(lpfc_eqe_valid, temp_eqe, 0);
250                 released++;
251                 q->host_index = ((q->host_index + 1) % q->entry_count);
252         }
253         if (unlikely(released == 0 && !arm))
254                 return 0;
255
256         /* ring doorbell for number popped */
257         doorbell.word0 = 0;
258         if (arm) {
259                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
260                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
261         }
262         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
263         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
264         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
265         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
266         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
267         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
268                 readl(q->phba->sli4_hba.EQCQDBregaddr);
269         return released;
270 }
271
272 /**
273  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
274  * @q: The Completion Queue to get the first valid CQE from
275  *
276  * This routine will get the first valid Completion Queue Entry from @q, update
277  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
278  * the Queue (no more work to do), or the Queue is full of CQEs that have been
279  * processed, but not popped back to the HBA then this routine will return NULL.
280  **/
281 static struct lpfc_cqe *
282 lpfc_sli4_cq_get(struct lpfc_queue *q)
283 {
284         struct lpfc_cqe *cqe;
285
286         /* If the next CQE is not valid then we are done */
287         if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
288                 return NULL;
289         /* If the host has not yet processed the next entry then we are done */
290         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
291                 return NULL;
292
293         cqe = q->qe[q->hba_index].cqe;
294         q->hba_index = ((q->hba_index + 1) % q->entry_count);
295         return cqe;
296 }
297
298 /**
299  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
300  * @q: The Completion Queue that the host has completed processing for.
301  * @arm: Indicates whether the host wants to arms this CQ.
302  *
303  * This routine will mark all Completion queue entries on @q, from the last
304  * known completed entry to the last entry that was processed, as completed
305  * by clearing the valid bit for each completion queue entry. Then it will
306  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
307  * The internal host index in the @q will be updated by this routine to indicate
308  * that the host has finished processing the entries. The @arm parameter
309  * indicates that the queue should be rearmed when ringing the doorbell.
310  *
311  * This function will return the number of CQEs that were released.
312  **/
313 uint32_t
314 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
315 {
316         uint32_t released = 0;
317         struct lpfc_cqe *temp_qe;
318         struct lpfc_register doorbell;
319
320         /* while there are valid entries */
321         while (q->hba_index != q->host_index) {
322                 temp_qe = q->qe[q->host_index].cqe;
323                 bf_set(lpfc_cqe_valid, temp_qe, 0);
324                 released++;
325                 q->host_index = ((q->host_index + 1) % q->entry_count);
326         }
327         if (unlikely(released == 0 && !arm))
328                 return 0;
329
330         /* ring doorbell for number popped */
331         doorbell.word0 = 0;
332         if (arm)
333                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
334         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
335         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
336         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
337         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
338         return released;
339 }
340
341 /**
342  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
343  * @q: The Header Receive Queue to operate on.
344  * @wqe: The Receive Queue Entry to put on the Receive queue.
345  *
346  * This routine will copy the contents of @wqe to the next available entry on
347  * the @q. This function will then ring the Receive Queue Doorbell to signal the
348  * HBA to start processing the Receive Queue Entry. This function returns the
349  * index that the rqe was copied to if successful. If no entries are available
350  * on @q then this function will return -ENOMEM.
351  * The caller is expected to hold the hbalock when calling this routine.
352  **/
353 static int
354 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
355                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
356 {
357         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
358         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
359         struct lpfc_register doorbell;
360         int put_index = hq->host_index;
361
362         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
363                 return -EINVAL;
364         if (hq->host_index != dq->host_index)
365                 return -EINVAL;
366         /* If the host has not yet processed the next entry then we are done */
367         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
368                 return -EBUSY;
369         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
370         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
371
372         /* Update the host index to point to the next slot */
373         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
374         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
375
376         /* Ring The Header Receive Queue Doorbell */
377         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
378                 doorbell.word0 = 0;
379                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
380                        LPFC_RQ_POST_BATCH);
381                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
382                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
383         }
384         return put_index;
385 }
386
387 /**
388  * lpfc_sli4_rq_release - Updates internal hba index for RQ
389  * @q: The Header Receive Queue to operate on.
390  *
391  * This routine will update the HBA index of a queue to reflect consumption of
392  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
393  * consumed an entry the host calls this function to update the queue's
394  * internal pointers. This routine returns the number of entries that were
395  * consumed by the HBA.
396  **/
397 static uint32_t
398 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
399 {
400         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
401                 return 0;
402         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
403         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
404         return 1;
405 }
406
407 /**
408  * lpfc_cmd_iocb - Get next command iocb entry in the ring
409  * @phba: Pointer to HBA context object.
410  * @pring: Pointer to driver SLI ring object.
411  *
412  * This function returns pointer to next command iocb entry
413  * in the command ring. The caller must hold hbalock to prevent
414  * other threads consume the next command iocb.
415  * SLI-2/SLI-3 provide different sized iocbs.
416  **/
417 static inline IOCB_t *
418 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
419 {
420         return (IOCB_t *) (((char *) pring->cmdringaddr) +
421                            pring->cmdidx * phba->iocb_cmd_size);
422 }
423
424 /**
425  * lpfc_resp_iocb - Get next response iocb entry in the ring
426  * @phba: Pointer to HBA context object.
427  * @pring: Pointer to driver SLI ring object.
428  *
429  * This function returns pointer to next response iocb entry
430  * in the response ring. The caller must hold hbalock to make sure
431  * that no other thread consume the next response iocb.
432  * SLI-2/SLI-3 provide different sized iocbs.
433  **/
434 static inline IOCB_t *
435 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
436 {
437         return (IOCB_t *) (((char *) pring->rspringaddr) +
438                            pring->rspidx * phba->iocb_rsp_size);
439 }
440
441 /**
442  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
443  * @phba: Pointer to HBA context object.
444  *
445  * This function is called with hbalock held. This function
446  * allocates a new driver iocb object from the iocb pool. If the
447  * allocation is successful, it returns pointer to the newly
448  * allocated iocb object else it returns NULL.
449  **/
450 static struct lpfc_iocbq *
451 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
452 {
453         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
454         struct lpfc_iocbq * iocbq = NULL;
455
456         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
457         return iocbq;
458 }
459
460 /**
461  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
462  * @phba: Pointer to HBA context object.
463  * @xritag: XRI value.
464  *
465  * This function clears the sglq pointer from the array of acive
466  * sglq's. The xritag that is passed in is used to index into the
467  * array. Before the xritag can be used it needs to be adjusted
468  * by subtracting the xribase.
469  *
470  * Returns sglq ponter = success, NULL = Failure.
471  **/
472 static struct lpfc_sglq *
473 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
474 {
475         uint16_t adj_xri;
476         struct lpfc_sglq *sglq;
477         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
478         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
479                 return NULL;
480         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
481         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
482         return sglq;
483 }
484
485 /**
486  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
487  * @phba: Pointer to HBA context object.
488  * @xritag: XRI value.
489  *
490  * This function returns the sglq pointer from the array of acive
491  * sglq's. The xritag that is passed in is used to index into the
492  * array. Before the xritag can be used it needs to be adjusted
493  * by subtracting the xribase.
494  *
495  * Returns sglq ponter = success, NULL = Failure.
496  **/
497 static struct lpfc_sglq *
498 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
499 {
500         uint16_t adj_xri;
501         struct lpfc_sglq *sglq;
502         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
503         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
504                 return NULL;
505         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
506         return sglq;
507 }
508
509 /**
510  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
511  * @phba: Pointer to HBA context object.
512  *
513  * This function is called with hbalock held. This function
514  * Gets a new driver sglq object from the sglq list. If the
515  * list is not empty then it is successful, it returns pointer to the newly
516  * allocated sglq object else it returns NULL.
517  **/
518 static struct lpfc_sglq *
519 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
520 {
521         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
522         struct lpfc_sglq *sglq = NULL;
523         uint16_t adj_xri;
524         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
525         if (!sglq)
526                 return NULL;
527         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
528         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
529         return sglq;
530 }
531
532 /**
533  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
534  * @phba: Pointer to HBA context object.
535  *
536  * This function is called with no lock held. This function
537  * allocates a new driver iocb object from the iocb pool. If the
538  * allocation is successful, it returns pointer to the newly
539  * allocated iocb object else it returns NULL.
540  **/
541 struct lpfc_iocbq *
542 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
543 {
544         struct lpfc_iocbq * iocbq = NULL;
545         unsigned long iflags;
546
547         spin_lock_irqsave(&phba->hbalock, iflags);
548         iocbq = __lpfc_sli_get_iocbq(phba);
549         spin_unlock_irqrestore(&phba->hbalock, iflags);
550         return iocbq;
551 }
552
553 /**
554  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
555  * @phba: Pointer to HBA context object.
556  * @iocbq: Pointer to driver iocb object.
557  *
558  * This function is called with hbalock held to release driver
559  * iocb object to the iocb pool. The iotag in the iocb object
560  * does not change for each use of the iocb object. This function
561  * clears all other fields of the iocb object when it is freed.
562  * The sqlq structure that holds the xritag and phys and virtual
563  * mappings for the scatter gather list is retrieved from the
564  * active array of sglq. The get of the sglq pointer also clears
565  * the entry in the array. If the status of the IO indiactes that
566  * this IO was aborted then the sglq entry it put on the
567  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
568  * IO has good status or fails for any other reason then the sglq
569  * entry is added to the free list (lpfc_sgl_list).
570  **/
571 static void
572 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
573 {
574         struct lpfc_sglq *sglq;
575         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
576         unsigned long iflag;
577
578         if (iocbq->sli4_xritag == NO_XRI)
579                 sglq = NULL;
580         else
581                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
582         if (sglq)  {
583                 if (iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) {
584                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
585                                         iflag);
586                         list_add(&sglq->list,
587                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
588                         spin_unlock_irqrestore(
589                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
590                 } else
591                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
592         }
593
594
595         /*
596          * Clean all volatile data fields, preserve iotag and node struct.
597          */
598         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
599         iocbq->sli4_xritag = NO_XRI;
600         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
601 }
602
603 /**
604  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
605  * @phba: Pointer to HBA context object.
606  * @iocbq: Pointer to driver iocb object.
607  *
608  * This function is called with hbalock held to release driver
609  * iocb object to the iocb pool. The iotag in the iocb object
610  * does not change for each use of the iocb object. This function
611  * clears all other fields of the iocb object when it is freed.
612  **/
613 static void
614 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
615 {
616         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
617
618         /*
619          * Clean all volatile data fields, preserve iotag and node struct.
620          */
621         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
622         iocbq->sli4_xritag = NO_XRI;
623         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
624 }
625
626 /**
627  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
628  * @phba: Pointer to HBA context object.
629  * @iocbq: Pointer to driver iocb object.
630  *
631  * This function is called with hbalock held to release driver
632  * iocb object to the iocb pool. The iotag in the iocb object
633  * does not change for each use of the iocb object. This function
634  * clears all other fields of the iocb object when it is freed.
635  **/
636 static void
637 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
638 {
639         phba->__lpfc_sli_release_iocbq(phba, iocbq);
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 no lock held to release the iocb to
648  * iocb pool.
649  **/
650 void
651 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
652 {
653         unsigned long iflags;
654
655         /*
656          * Clean all volatile data fields, preserve iotag and node struct.
657          */
658         spin_lock_irqsave(&phba->hbalock, iflags);
659         __lpfc_sli_release_iocbq(phba, iocbq);
660         spin_unlock_irqrestore(&phba->hbalock, iflags);
661 }
662
663 /**
664  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
665  * @phba: Pointer to HBA context object.
666  * @iocblist: List of IOCBs.
667  * @ulpstatus: ULP status in IOCB command field.
668  * @ulpWord4: ULP word-4 in IOCB command field.
669  *
670  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
671  * on the list by invoking the complete callback function associated with the
672  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
673  * fields.
674  **/
675 void
676 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
677                       uint32_t ulpstatus, uint32_t ulpWord4)
678 {
679         struct lpfc_iocbq *piocb;
680
681         while (!list_empty(iocblist)) {
682                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
683
684                 if (!piocb->iocb_cmpl)
685                         lpfc_sli_release_iocbq(phba, piocb);
686                 else {
687                         piocb->iocb.ulpStatus = ulpstatus;
688                         piocb->iocb.un.ulpWord[4] = ulpWord4;
689                         (piocb->iocb_cmpl) (phba, piocb, piocb);
690                 }
691         }
692         return;
693 }
694
695 /**
696  * lpfc_sli_iocb_cmd_type - Get the iocb type
697  * @iocb_cmnd: iocb command code.
698  *
699  * This function is called by ring event handler function to get the iocb type.
700  * This function translates the iocb command to an iocb command type used to
701  * decide the final disposition of each completed IOCB.
702  * The function returns
703  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
704  * LPFC_SOL_IOCB     if it is a solicited iocb completion
705  * LPFC_ABORT_IOCB   if it is an abort iocb
706  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
707  *
708  * The caller is not required to hold any lock.
709  **/
710 static lpfc_iocb_type
711 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
712 {
713         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
714
715         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
716                 return 0;
717
718         switch (iocb_cmnd) {
719         case CMD_XMIT_SEQUENCE_CR:
720         case CMD_XMIT_SEQUENCE_CX:
721         case CMD_XMIT_BCAST_CN:
722         case CMD_XMIT_BCAST_CX:
723         case CMD_ELS_REQUEST_CR:
724         case CMD_ELS_REQUEST_CX:
725         case CMD_CREATE_XRI_CR:
726         case CMD_CREATE_XRI_CX:
727         case CMD_GET_RPI_CN:
728         case CMD_XMIT_ELS_RSP_CX:
729         case CMD_GET_RPI_CR:
730         case CMD_FCP_IWRITE_CR:
731         case CMD_FCP_IWRITE_CX:
732         case CMD_FCP_IREAD_CR:
733         case CMD_FCP_IREAD_CX:
734         case CMD_FCP_ICMND_CR:
735         case CMD_FCP_ICMND_CX:
736         case CMD_FCP_TSEND_CX:
737         case CMD_FCP_TRSP_CX:
738         case CMD_FCP_TRECEIVE_CX:
739         case CMD_FCP_AUTO_TRSP_CX:
740         case CMD_ADAPTER_MSG:
741         case CMD_ADAPTER_DUMP:
742         case CMD_XMIT_SEQUENCE64_CR:
743         case CMD_XMIT_SEQUENCE64_CX:
744         case CMD_XMIT_BCAST64_CN:
745         case CMD_XMIT_BCAST64_CX:
746         case CMD_ELS_REQUEST64_CR:
747         case CMD_ELS_REQUEST64_CX:
748         case CMD_FCP_IWRITE64_CR:
749         case CMD_FCP_IWRITE64_CX:
750         case CMD_FCP_IREAD64_CR:
751         case CMD_FCP_IREAD64_CX:
752         case CMD_FCP_ICMND64_CR:
753         case CMD_FCP_ICMND64_CX:
754         case CMD_FCP_TSEND64_CX:
755         case CMD_FCP_TRSP64_CX:
756         case CMD_FCP_TRECEIVE64_CX:
757         case CMD_GEN_REQUEST64_CR:
758         case CMD_GEN_REQUEST64_CX:
759         case CMD_XMIT_ELS_RSP64_CX:
760         case DSSCMD_IWRITE64_CR:
761         case DSSCMD_IWRITE64_CX:
762         case DSSCMD_IREAD64_CR:
763         case DSSCMD_IREAD64_CX:
764                 type = LPFC_SOL_IOCB;
765                 break;
766         case CMD_ABORT_XRI_CN:
767         case CMD_ABORT_XRI_CX:
768         case CMD_CLOSE_XRI_CN:
769         case CMD_CLOSE_XRI_CX:
770         case CMD_XRI_ABORTED_CX:
771         case CMD_ABORT_MXRI64_CN:
772         case CMD_XMIT_BLS_RSP64_CX:
773                 type = LPFC_ABORT_IOCB;
774                 break;
775         case CMD_RCV_SEQUENCE_CX:
776         case CMD_RCV_ELS_REQ_CX:
777         case CMD_RCV_SEQUENCE64_CX:
778         case CMD_RCV_ELS_REQ64_CX:
779         case CMD_ASYNC_STATUS:
780         case CMD_IOCB_RCV_SEQ64_CX:
781         case CMD_IOCB_RCV_ELS64_CX:
782         case CMD_IOCB_RCV_CONT64_CX:
783         case CMD_IOCB_RET_XRI64_CX:
784                 type = LPFC_UNSOL_IOCB;
785                 break;
786         case CMD_IOCB_XMIT_MSEQ64_CR:
787         case CMD_IOCB_XMIT_MSEQ64_CX:
788         case CMD_IOCB_RCV_SEQ_LIST64_CX:
789         case CMD_IOCB_RCV_ELS_LIST64_CX:
790         case CMD_IOCB_CLOSE_EXTENDED_CN:
791         case CMD_IOCB_ABORT_EXTENDED_CN:
792         case CMD_IOCB_RET_HBQE64_CN:
793         case CMD_IOCB_FCP_IBIDIR64_CR:
794         case CMD_IOCB_FCP_IBIDIR64_CX:
795         case CMD_IOCB_FCP_ITASKMGT64_CX:
796         case CMD_IOCB_LOGENTRY_CN:
797         case CMD_IOCB_LOGENTRY_ASYNC_CN:
798                 printk("%s - Unhandled SLI-3 Command x%x\n",
799                                 __func__, iocb_cmnd);
800                 type = LPFC_UNKNOWN_IOCB;
801                 break;
802         default:
803                 type = LPFC_UNKNOWN_IOCB;
804                 break;
805         }
806
807         return type;
808 }
809
810 /**
811  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
812  * @phba: Pointer to HBA context object.
813  *
814  * This function is called from SLI initialization code
815  * to configure every ring of the HBA's SLI interface. The
816  * caller is not required to hold any lock. This function issues
817  * a config_ring mailbox command for each ring.
818  * This function returns zero if successful else returns a negative
819  * error code.
820  **/
821 static int
822 lpfc_sli_ring_map(struct lpfc_hba *phba)
823 {
824         struct lpfc_sli *psli = &phba->sli;
825         LPFC_MBOXQ_t *pmb;
826         MAILBOX_t *pmbox;
827         int i, rc, ret = 0;
828
829         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
830         if (!pmb)
831                 return -ENOMEM;
832         pmbox = &pmb->u.mb;
833         phba->link_state = LPFC_INIT_MBX_CMDS;
834         for (i = 0; i < psli->num_rings; i++) {
835                 lpfc_config_ring(phba, i, pmb);
836                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
837                 if (rc != MBX_SUCCESS) {
838                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
839                                         "0446 Adapter failed to init (%d), "
840                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
841                                         "ring %d\n",
842                                         rc, pmbox->mbxCommand,
843                                         pmbox->mbxStatus, i);
844                         phba->link_state = LPFC_HBA_ERROR;
845                         ret = -ENXIO;
846                         break;
847                 }
848         }
849         mempool_free(pmb, phba->mbox_mem_pool);
850         return ret;
851 }
852
853 /**
854  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
855  * @phba: Pointer to HBA context object.
856  * @pring: Pointer to driver SLI ring object.
857  * @piocb: Pointer to the driver iocb object.
858  *
859  * This function is called with hbalock held. The function adds the
860  * new iocb to txcmplq of the given ring. This function always returns
861  * 0. If this function is called for ELS ring, this function checks if
862  * there is a vport associated with the ELS command. This function also
863  * starts els_tmofunc timer if this is an ELS command.
864  **/
865 static int
866 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
867                         struct lpfc_iocbq *piocb)
868 {
869         list_add_tail(&piocb->list, &pring->txcmplq);
870         pring->txcmplq_cnt++;
871         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
872            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
873            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
874                 if (!piocb->vport)
875                         BUG();
876                 else
877                         mod_timer(&piocb->vport->els_tmofunc,
878                                   jiffies + HZ * (phba->fc_ratov << 1));
879         }
880
881
882         return 0;
883 }
884
885 /**
886  * lpfc_sli_ringtx_get - Get first element of the txq
887  * @phba: Pointer to HBA context object.
888  * @pring: Pointer to driver SLI ring object.
889  *
890  * This function is called with hbalock held to get next
891  * iocb in txq of the given ring. If there is any iocb in
892  * the txq, the function returns first iocb in the list after
893  * removing the iocb from the list, else it returns NULL.
894  **/
895 static struct lpfc_iocbq *
896 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
897 {
898         struct lpfc_iocbq *cmd_iocb;
899
900         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
901         if (cmd_iocb != NULL)
902                 pring->txq_cnt--;
903         return cmd_iocb;
904 }
905
906 /**
907  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
908  * @phba: Pointer to HBA context object.
909  * @pring: Pointer to driver SLI ring object.
910  *
911  * This function is called with hbalock held and the caller must post the
912  * iocb without releasing the lock. If the caller releases the lock,
913  * iocb slot returned by the function is not guaranteed to be available.
914  * The function returns pointer to the next available iocb slot if there
915  * is available slot in the ring, else it returns NULL.
916  * If the get index of the ring is ahead of the put index, the function
917  * will post an error attention event to the worker thread to take the
918  * HBA to offline state.
919  **/
920 static IOCB_t *
921 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
922 {
923         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
924         uint32_t  max_cmd_idx = pring->numCiocb;
925         if ((pring->next_cmdidx == pring->cmdidx) &&
926            (++pring->next_cmdidx >= max_cmd_idx))
927                 pring->next_cmdidx = 0;
928
929         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
930
931                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
932
933                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
934                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
935                                         "0315 Ring %d issue: portCmdGet %d "
936                                         "is bigger than cmd ring %d\n",
937                                         pring->ringno,
938                                         pring->local_getidx, max_cmd_idx);
939
940                         phba->link_state = LPFC_HBA_ERROR;
941                         /*
942                          * All error attention handlers are posted to
943                          * worker thread
944                          */
945                         phba->work_ha |= HA_ERATT;
946                         phba->work_hs = HS_FFER3;
947
948                         lpfc_worker_wake_up(phba);
949
950                         return NULL;
951                 }
952
953                 if (pring->local_getidx == pring->next_cmdidx)
954                         return NULL;
955         }
956
957         return lpfc_cmd_iocb(phba, pring);
958 }
959
960 /**
961  * lpfc_sli_next_iotag - Get an iotag for the iocb
962  * @phba: Pointer to HBA context object.
963  * @iocbq: Pointer to driver iocb object.
964  *
965  * This function gets an iotag for the iocb. If there is no unused iotag and
966  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
967  * array and assigns a new iotag.
968  * The function returns the allocated iotag if successful, else returns zero.
969  * Zero is not a valid iotag.
970  * The caller is not required to hold any lock.
971  **/
972 uint16_t
973 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
974 {
975         struct lpfc_iocbq **new_arr;
976         struct lpfc_iocbq **old_arr;
977         size_t new_len;
978         struct lpfc_sli *psli = &phba->sli;
979         uint16_t iotag;
980
981         spin_lock_irq(&phba->hbalock);
982         iotag = psli->last_iotag;
983         if(++iotag < psli->iocbq_lookup_len) {
984                 psli->last_iotag = iotag;
985                 psli->iocbq_lookup[iotag] = iocbq;
986                 spin_unlock_irq(&phba->hbalock);
987                 iocbq->iotag = iotag;
988                 return iotag;
989         } else if (psli->iocbq_lookup_len < (0xffff
990                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
991                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
992                 spin_unlock_irq(&phba->hbalock);
993                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
994                                   GFP_KERNEL);
995                 if (new_arr) {
996                         spin_lock_irq(&phba->hbalock);
997                         old_arr = psli->iocbq_lookup;
998                         if (new_len <= psli->iocbq_lookup_len) {
999                                 /* highly unprobable case */
1000                                 kfree(new_arr);
1001                                 iotag = psli->last_iotag;
1002                                 if(++iotag < psli->iocbq_lookup_len) {
1003                                         psli->last_iotag = iotag;
1004                                         psli->iocbq_lookup[iotag] = iocbq;
1005                                         spin_unlock_irq(&phba->hbalock);
1006                                         iocbq->iotag = iotag;
1007                                         return iotag;
1008                                 }
1009                                 spin_unlock_irq(&phba->hbalock);
1010                                 return 0;
1011                         }
1012                         if (psli->iocbq_lookup)
1013                                 memcpy(new_arr, old_arr,
1014                                        ((psli->last_iotag  + 1) *
1015                                         sizeof (struct lpfc_iocbq *)));
1016                         psli->iocbq_lookup = new_arr;
1017                         psli->iocbq_lookup_len = new_len;
1018                         psli->last_iotag = iotag;
1019                         psli->iocbq_lookup[iotag] = iocbq;
1020                         spin_unlock_irq(&phba->hbalock);
1021                         iocbq->iotag = iotag;
1022                         kfree(old_arr);
1023                         return iotag;
1024                 }
1025         } else
1026                 spin_unlock_irq(&phba->hbalock);
1027
1028         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1029                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1030                         psli->last_iotag);
1031
1032         return 0;
1033 }
1034
1035 /**
1036  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1037  * @phba: Pointer to HBA context object.
1038  * @pring: Pointer to driver SLI ring object.
1039  * @iocb: Pointer to iocb slot in the ring.
1040  * @nextiocb: Pointer to driver iocb object which need to be
1041  *            posted to firmware.
1042  *
1043  * This function is called with hbalock held to post a new iocb to
1044  * the firmware. This function copies the new iocb to ring iocb slot and
1045  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1046  * a completion call back for this iocb else the function will free the
1047  * iocb object.
1048  **/
1049 static void
1050 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1051                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1052 {
1053         /*
1054          * Set up an iotag
1055          */
1056         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1057
1058
1059         if (pring->ringno == LPFC_ELS_RING) {
1060                 lpfc_debugfs_slow_ring_trc(phba,
1061                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1062                         *(((uint32_t *) &nextiocb->iocb) + 4),
1063                         *(((uint32_t *) &nextiocb->iocb) + 6),
1064                         *(((uint32_t *) &nextiocb->iocb) + 7));
1065         }
1066
1067         /*
1068          * Issue iocb command to adapter
1069          */
1070         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1071         wmb();
1072         pring->stats.iocb_cmd++;
1073
1074         /*
1075          * If there is no completion routine to call, we can release the
1076          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1077          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1078          */
1079         if (nextiocb->iocb_cmpl)
1080                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1081         else
1082                 __lpfc_sli_release_iocbq(phba, nextiocb);
1083
1084         /*
1085          * Let the HBA know what IOCB slot will be the next one the
1086          * driver will put a command into.
1087          */
1088         pring->cmdidx = pring->next_cmdidx;
1089         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1090 }
1091
1092 /**
1093  * lpfc_sli_update_full_ring - Update the chip attention register
1094  * @phba: Pointer to HBA context object.
1095  * @pring: Pointer to driver SLI ring object.
1096  *
1097  * The caller is not required to hold any lock for calling this function.
1098  * This function updates the chip attention bits for the ring to inform firmware
1099  * that there are pending work to be done for this ring and requests an
1100  * interrupt when there is space available in the ring. This function is
1101  * called when the driver is unable to post more iocbs to the ring due
1102  * to unavailability of space in the ring.
1103  **/
1104 static void
1105 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1106 {
1107         int ringno = pring->ringno;
1108
1109         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1110
1111         wmb();
1112
1113         /*
1114          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1115          * The HBA will tell us when an IOCB entry is available.
1116          */
1117         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1118         readl(phba->CAregaddr); /* flush */
1119
1120         pring->stats.iocb_cmd_full++;
1121 }
1122
1123 /**
1124  * lpfc_sli_update_ring - Update chip attention register
1125  * @phba: Pointer to HBA context object.
1126  * @pring: Pointer to driver SLI ring object.
1127  *
1128  * This function updates the chip attention register bit for the
1129  * given ring to inform HBA that there is more work to be done
1130  * in this ring. The caller is not required to hold any lock.
1131  **/
1132 static void
1133 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1134 {
1135         int ringno = pring->ringno;
1136
1137         /*
1138          * Tell the HBA that there is work to do in this ring.
1139          */
1140         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1141                 wmb();
1142                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1143                 readl(phba->CAregaddr); /* flush */
1144         }
1145 }
1146
1147 /**
1148  * lpfc_sli_resume_iocb - Process iocbs in the txq
1149  * @phba: Pointer to HBA context object.
1150  * @pring: Pointer to driver SLI ring object.
1151  *
1152  * This function is called with hbalock held to post pending iocbs
1153  * in the txq to the firmware. This function is called when driver
1154  * detects space available in the ring.
1155  **/
1156 static void
1157 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1158 {
1159         IOCB_t *iocb;
1160         struct lpfc_iocbq *nextiocb;
1161
1162         /*
1163          * Check to see if:
1164          *  (a) there is anything on the txq to send
1165          *  (b) link is up
1166          *  (c) link attention events can be processed (fcp ring only)
1167          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1168          */
1169         if (pring->txq_cnt &&
1170             lpfc_is_link_up(phba) &&
1171             (pring->ringno != phba->sli.fcp_ring ||
1172              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1173
1174                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1175                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1176                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1177
1178                 if (iocb)
1179                         lpfc_sli_update_ring(phba, pring);
1180                 else
1181                         lpfc_sli_update_full_ring(phba, pring);
1182         }
1183
1184         return;
1185 }
1186
1187 /**
1188  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1189  * @phba: Pointer to HBA context object.
1190  * @hbqno: HBQ number.
1191  *
1192  * This function is called with hbalock held to get the next
1193  * available slot for the given HBQ. If there is free slot
1194  * available for the HBQ it will return pointer to the next available
1195  * HBQ entry else it will return NULL.
1196  **/
1197 static struct lpfc_hbq_entry *
1198 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1199 {
1200         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1201
1202         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1203             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1204                 hbqp->next_hbqPutIdx = 0;
1205
1206         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1207                 uint32_t raw_index = phba->hbq_get[hbqno];
1208                 uint32_t getidx = le32_to_cpu(raw_index);
1209
1210                 hbqp->local_hbqGetIdx = getidx;
1211
1212                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1213                         lpfc_printf_log(phba, KERN_ERR,
1214                                         LOG_SLI | LOG_VPORT,
1215                                         "1802 HBQ %d: local_hbqGetIdx "
1216                                         "%u is > than hbqp->entry_count %u\n",
1217                                         hbqno, hbqp->local_hbqGetIdx,
1218                                         hbqp->entry_count);
1219
1220                         phba->link_state = LPFC_HBA_ERROR;
1221                         return NULL;
1222                 }
1223
1224                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1225                         return NULL;
1226         }
1227
1228         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1229                         hbqp->hbqPutIdx;
1230 }
1231
1232 /**
1233  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1234  * @phba: Pointer to HBA context object.
1235  *
1236  * This function is called with no lock held to free all the
1237  * hbq buffers while uninitializing the SLI interface. It also
1238  * frees the HBQ buffers returned by the firmware but not yet
1239  * processed by the upper layers.
1240  **/
1241 void
1242 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1243 {
1244         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1245         struct hbq_dmabuf *hbq_buf;
1246         unsigned long flags;
1247         int i, hbq_count;
1248         uint32_t hbqno;
1249
1250         hbq_count = lpfc_sli_hbq_count();
1251         /* Return all memory used by all HBQs */
1252         spin_lock_irqsave(&phba->hbalock, flags);
1253         for (i = 0; i < hbq_count; ++i) {
1254                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1255                                 &phba->hbqs[i].hbq_buffer_list, list) {
1256                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1257                         list_del(&hbq_buf->dbuf.list);
1258                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1259                 }
1260                 phba->hbqs[i].buffer_count = 0;
1261         }
1262         /* Return all HBQ buffer that are in-fly */
1263         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1264                                  list) {
1265                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1266                 list_del(&hbq_buf->dbuf.list);
1267                 if (hbq_buf->tag == -1) {
1268                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1269                                 (phba, hbq_buf);
1270                 } else {
1271                         hbqno = hbq_buf->tag >> 16;
1272                         if (hbqno >= LPFC_MAX_HBQS)
1273                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274                                         (phba, hbq_buf);
1275                         else
1276                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1277                                         hbq_buf);
1278                 }
1279         }
1280
1281         /* Mark the HBQs not in use */
1282         phba->hbq_in_use = 0;
1283         spin_unlock_irqrestore(&phba->hbalock, flags);
1284 }
1285
1286 /**
1287  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1288  * @phba: Pointer to HBA context object.
1289  * @hbqno: HBQ number.
1290  * @hbq_buf: Pointer to HBQ buffer.
1291  *
1292  * This function is called with the hbalock held to post a
1293  * hbq buffer to the firmware. If the function finds an empty
1294  * slot in the HBQ, it will post the buffer. The function will return
1295  * pointer to the hbq entry if it successfully post the buffer
1296  * else it will return NULL.
1297  **/
1298 static int
1299 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1300                          struct hbq_dmabuf *hbq_buf)
1301 {
1302         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1303 }
1304
1305 /**
1306  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1307  * @phba: Pointer to HBA context object.
1308  * @hbqno: HBQ number.
1309  * @hbq_buf: Pointer to HBQ buffer.
1310  *
1311  * This function is called with the hbalock held to post a hbq buffer to the
1312  * firmware. If the function finds an empty slot in the HBQ, it will post the
1313  * buffer and place it on the hbq_buffer_list. The function will return zero if
1314  * it successfully post the buffer else it will return an error.
1315  **/
1316 static int
1317 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1318                             struct hbq_dmabuf *hbq_buf)
1319 {
1320         struct lpfc_hbq_entry *hbqe;
1321         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1322
1323         /* Get next HBQ entry slot to use */
1324         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1325         if (hbqe) {
1326                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1327
1328                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1329                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1330                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1331                 hbqe->bde.tus.f.bdeFlags = 0;
1332                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1333                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1334                                 /* Sync SLIM */
1335                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1336                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1337                                 /* flush */
1338                 readl(phba->hbq_put + hbqno);
1339                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1340                 return 0;
1341         } else
1342                 return -ENOMEM;
1343 }
1344
1345 /**
1346  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1347  * @phba: Pointer to HBA context object.
1348  * @hbqno: HBQ number.
1349  * @hbq_buf: Pointer to HBQ buffer.
1350  *
1351  * This function is called with the hbalock held to post an RQE to the SLI4
1352  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1353  * the hbq_buffer_list and return zero, otherwise it will return an error.
1354  **/
1355 static int
1356 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1357                             struct hbq_dmabuf *hbq_buf)
1358 {
1359         int rc;
1360         struct lpfc_rqe hrqe;
1361         struct lpfc_rqe drqe;
1362
1363         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1364         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1365         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1366         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1367         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1368                               &hrqe, &drqe);
1369         if (rc < 0)
1370                 return rc;
1371         hbq_buf->tag = rc;
1372         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1373         return 0;
1374 }
1375
1376 /* HBQ for ELS and CT traffic. */
1377 static struct lpfc_hbq_init lpfc_els_hbq = {
1378         .rn = 1,
1379         .entry_count = 256,
1380         .mask_count = 0,
1381         .profile = 0,
1382         .ring_mask = (1 << LPFC_ELS_RING),
1383         .buffer_count = 0,
1384         .init_count = 40,
1385         .add_count = 40,
1386 };
1387
1388 /* HBQ for the extra ring if needed */
1389 static struct lpfc_hbq_init lpfc_extra_hbq = {
1390         .rn = 1,
1391         .entry_count = 200,
1392         .mask_count = 0,
1393         .profile = 0,
1394         .ring_mask = (1 << LPFC_EXTRA_RING),
1395         .buffer_count = 0,
1396         .init_count = 0,
1397         .add_count = 5,
1398 };
1399
1400 /* Array of HBQs */
1401 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1402         &lpfc_els_hbq,
1403         &lpfc_extra_hbq,
1404 };
1405
1406 /**
1407  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1408  * @phba: Pointer to HBA context object.
1409  * @hbqno: HBQ number.
1410  * @count: Number of HBQ buffers to be posted.
1411  *
1412  * This function is called with no lock held to post more hbq buffers to the
1413  * given HBQ. The function returns the number of HBQ buffers successfully
1414  * posted.
1415  **/
1416 static int
1417 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1418 {
1419         uint32_t i, posted = 0;
1420         unsigned long flags;
1421         struct hbq_dmabuf *hbq_buffer;
1422         LIST_HEAD(hbq_buf_list);
1423         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1424                 return 0;
1425
1426         if ((phba->hbqs[hbqno].buffer_count + count) >
1427             lpfc_hbq_defs[hbqno]->entry_count)
1428                 count = lpfc_hbq_defs[hbqno]->entry_count -
1429                                         phba->hbqs[hbqno].buffer_count;
1430         if (!count)
1431                 return 0;
1432         /* Allocate HBQ entries */
1433         for (i = 0; i < count; i++) {
1434                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1435                 if (!hbq_buffer)
1436                         break;
1437                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1438         }
1439         /* Check whether HBQ is still in use */
1440         spin_lock_irqsave(&phba->hbalock, flags);
1441         if (!phba->hbq_in_use)
1442                 goto err;
1443         while (!list_empty(&hbq_buf_list)) {
1444                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1445                                  dbuf.list);
1446                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1447                                       (hbqno << 16));
1448                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1449                         phba->hbqs[hbqno].buffer_count++;
1450                         posted++;
1451                 } else
1452                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1453         }
1454         spin_unlock_irqrestore(&phba->hbalock, flags);
1455         return posted;
1456 err:
1457         spin_unlock_irqrestore(&phba->hbalock, flags);
1458         while (!list_empty(&hbq_buf_list)) {
1459                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1460                                  dbuf.list);
1461                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1462         }
1463         return 0;
1464 }
1465
1466 /**
1467  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1468  * @phba: Pointer to HBA context object.
1469  * @qno: HBQ number.
1470  *
1471  * This function posts more buffers to the HBQ. This function
1472  * is called with no lock held. The function returns the number of HBQ entries
1473  * successfully allocated.
1474  **/
1475 int
1476 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1477 {
1478         if (phba->sli_rev == LPFC_SLI_REV4)
1479                 return 0;
1480         else
1481                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1482                                          lpfc_hbq_defs[qno]->add_count);
1483 }
1484
1485 /**
1486  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1487  * @phba: Pointer to HBA context object.
1488  * @qno:  HBQ queue number.
1489  *
1490  * This function is called from SLI initialization code path with
1491  * no lock held to post initial HBQ buffers to firmware. The
1492  * function returns the number of HBQ entries successfully allocated.
1493  **/
1494 static int
1495 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1496 {
1497         if (phba->sli_rev == LPFC_SLI_REV4)
1498                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1499                                          lpfc_hbq_defs[qno]->entry_count);
1500         else
1501                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1502                                          lpfc_hbq_defs[qno]->init_count);
1503 }
1504
1505 /**
1506  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1507  * @phba: Pointer to HBA context object.
1508  * @hbqno: HBQ number.
1509  *
1510  * This function removes the first hbq buffer on an hbq list and returns a
1511  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1512  **/
1513 static struct hbq_dmabuf *
1514 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1515 {
1516         struct lpfc_dmabuf *d_buf;
1517
1518         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1519         if (!d_buf)
1520                 return NULL;
1521         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1522 }
1523
1524 /**
1525  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1526  * @phba: Pointer to HBA context object.
1527  * @tag: Tag of the hbq buffer.
1528  *
1529  * This function is called with hbalock held. This function searches
1530  * for the hbq buffer associated with the given tag in the hbq buffer
1531  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1532  * it returns NULL.
1533  **/
1534 static struct hbq_dmabuf *
1535 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1536 {
1537         struct lpfc_dmabuf *d_buf;
1538         struct hbq_dmabuf *hbq_buf;
1539         uint32_t hbqno;
1540
1541         hbqno = tag >> 16;
1542         if (hbqno >= LPFC_MAX_HBQS)
1543                 return NULL;
1544
1545         spin_lock_irq(&phba->hbalock);
1546         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1547                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1548                 if (hbq_buf->tag == tag) {
1549                         spin_unlock_irq(&phba->hbalock);
1550                         return hbq_buf;
1551                 }
1552         }
1553         spin_unlock_irq(&phba->hbalock);
1554         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1555                         "1803 Bad hbq tag. Data: x%x x%x\n",
1556                         tag, phba->hbqs[tag >> 16].buffer_count);
1557         return NULL;
1558 }
1559
1560 /**
1561  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1562  * @phba: Pointer to HBA context object.
1563  * @hbq_buffer: Pointer to HBQ buffer.
1564  *
1565  * This function is called with hbalock. This function gives back
1566  * the hbq buffer to firmware. If the HBQ does not have space to
1567  * post the buffer, it will free the buffer.
1568  **/
1569 void
1570 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1571 {
1572         uint32_t hbqno;
1573
1574         if (hbq_buffer) {
1575                 hbqno = hbq_buffer->tag >> 16;
1576                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1577                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1578         }
1579 }
1580
1581 /**
1582  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1583  * @mbxCommand: mailbox command code.
1584  *
1585  * This function is called by the mailbox event handler function to verify
1586  * that the completed mailbox command is a legitimate mailbox command. If the
1587  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1588  * and the mailbox event handler will take the HBA offline.
1589  **/
1590 static int
1591 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1592 {
1593         uint8_t ret;
1594
1595         switch (mbxCommand) {
1596         case MBX_LOAD_SM:
1597         case MBX_READ_NV:
1598         case MBX_WRITE_NV:
1599         case MBX_WRITE_VPARMS:
1600         case MBX_RUN_BIU_DIAG:
1601         case MBX_INIT_LINK:
1602         case MBX_DOWN_LINK:
1603         case MBX_CONFIG_LINK:
1604         case MBX_CONFIG_RING:
1605         case MBX_RESET_RING:
1606         case MBX_READ_CONFIG:
1607         case MBX_READ_RCONFIG:
1608         case MBX_READ_SPARM:
1609         case MBX_READ_STATUS:
1610         case MBX_READ_RPI:
1611         case MBX_READ_XRI:
1612         case MBX_READ_REV:
1613         case MBX_READ_LNK_STAT:
1614         case MBX_REG_LOGIN:
1615         case MBX_UNREG_LOGIN:
1616         case MBX_READ_LA:
1617         case MBX_CLEAR_LA:
1618         case MBX_DUMP_MEMORY:
1619         case MBX_DUMP_CONTEXT:
1620         case MBX_RUN_DIAGS:
1621         case MBX_RESTART:
1622         case MBX_UPDATE_CFG:
1623         case MBX_DOWN_LOAD:
1624         case MBX_DEL_LD_ENTRY:
1625         case MBX_RUN_PROGRAM:
1626         case MBX_SET_MASK:
1627         case MBX_SET_VARIABLE:
1628         case MBX_UNREG_D_ID:
1629         case MBX_KILL_BOARD:
1630         case MBX_CONFIG_FARP:
1631         case MBX_BEACON:
1632         case MBX_LOAD_AREA:
1633         case MBX_RUN_BIU_DIAG64:
1634         case MBX_CONFIG_PORT:
1635         case MBX_READ_SPARM64:
1636         case MBX_READ_RPI64:
1637         case MBX_REG_LOGIN64:
1638         case MBX_READ_LA64:
1639         case MBX_WRITE_WWN:
1640         case MBX_SET_DEBUG:
1641         case MBX_LOAD_EXP_ROM:
1642         case MBX_ASYNCEVT_ENABLE:
1643         case MBX_REG_VPI:
1644         case MBX_UNREG_VPI:
1645         case MBX_HEARTBEAT:
1646         case MBX_PORT_CAPABILITIES:
1647         case MBX_PORT_IOV_CONTROL:
1648         case MBX_SLI4_CONFIG:
1649         case MBX_SLI4_REQ_FTRS:
1650         case MBX_REG_FCFI:
1651         case MBX_UNREG_FCFI:
1652         case MBX_REG_VFI:
1653         case MBX_UNREG_VFI:
1654         case MBX_INIT_VPI:
1655         case MBX_INIT_VFI:
1656         case MBX_RESUME_RPI:
1657                 ret = mbxCommand;
1658                 break;
1659         default:
1660                 ret = MBX_SHUTDOWN;
1661                 break;
1662         }
1663         return ret;
1664 }
1665
1666 /**
1667  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1668  * @phba: Pointer to HBA context object.
1669  * @pmboxq: Pointer to mailbox command.
1670  *
1671  * This is completion handler function for mailbox commands issued from
1672  * lpfc_sli_issue_mbox_wait function. This function is called by the
1673  * mailbox event handler function with no lock held. This function
1674  * will wake up thread waiting on the wait queue pointed by context1
1675  * of the mailbox.
1676  **/
1677 void
1678 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1679 {
1680         wait_queue_head_t *pdone_q;
1681         unsigned long drvr_flag;
1682
1683         /*
1684          * If pdone_q is empty, the driver thread gave up waiting and
1685          * continued running.
1686          */
1687         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1688         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1689         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1690         if (pdone_q)
1691                 wake_up_interruptible(pdone_q);
1692         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1693         return;
1694 }
1695
1696
1697 /**
1698  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1699  * @phba: Pointer to HBA context object.
1700  * @pmb: Pointer to mailbox object.
1701  *
1702  * This function is the default mailbox completion handler. It
1703  * frees the memory resources associated with the completed mailbox
1704  * command. If the completed command is a REG_LOGIN mailbox command,
1705  * this function will issue a UREG_LOGIN to re-claim the RPI.
1706  **/
1707 void
1708 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1709 {
1710         struct lpfc_dmabuf *mp;
1711         uint16_t rpi, vpi;
1712         int rc;
1713         struct lpfc_vport  *vport = pmb->vport;
1714
1715         mp = (struct lpfc_dmabuf *) (pmb->context1);
1716
1717         if (mp) {
1718                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1719                 kfree(mp);
1720         }
1721
1722         if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1723             (phba->sli_rev == LPFC_SLI_REV4))
1724                 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1725
1726         /*
1727          * If a REG_LOGIN succeeded  after node is destroyed or node
1728          * is in re-discovery driver need to cleanup the RPI.
1729          */
1730         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1731             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1732             !pmb->u.mb.mbxStatus) {
1733                 rpi = pmb->u.mb.un.varWords[0];
1734                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1735                 lpfc_unreg_login(phba, vpi, rpi, pmb);
1736                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1737                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1738                 if (rc != MBX_NOT_FINISHED)
1739                         return;
1740         }
1741
1742         /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1743         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1744                 !(phba->pport->load_flag & FC_UNLOADING) &&
1745                 !pmb->u.mb.mbxStatus) {
1746                 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1747                 pmb->vport = vport;
1748                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1749                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1750                 if (rc != MBX_NOT_FINISHED)
1751                         return;
1752         }
1753
1754         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1755                 lpfc_sli4_mbox_cmd_free(phba, pmb);
1756         else
1757                 mempool_free(pmb, phba->mbox_mem_pool);
1758 }
1759
1760 /**
1761  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1762  * @phba: Pointer to HBA context object.
1763  *
1764  * This function is called with no lock held. This function processes all
1765  * the completed mailbox commands and gives it to upper layers. The interrupt
1766  * service routine processes mailbox completion interrupt and adds completed
1767  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1768  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1769  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1770  * function returns the mailbox commands to the upper layer by calling the
1771  * completion handler function of each mailbox.
1772  **/
1773 int
1774 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1775 {
1776         MAILBOX_t *pmbox;
1777         LPFC_MBOXQ_t *pmb;
1778         int rc;
1779         LIST_HEAD(cmplq);
1780
1781         phba->sli.slistat.mbox_event++;
1782
1783         /* Get all completed mailboxe buffers into the cmplq */
1784         spin_lock_irq(&phba->hbalock);
1785         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1786         spin_unlock_irq(&phba->hbalock);
1787
1788         /* Get a Mailbox buffer to setup mailbox commands for callback */
1789         do {
1790                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1791                 if (pmb == NULL)
1792                         break;
1793
1794                 pmbox = &pmb->u.mb;
1795
1796                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1797                         if (pmb->vport) {
1798                                 lpfc_debugfs_disc_trc(pmb->vport,
1799                                         LPFC_DISC_TRC_MBOX_VPORT,
1800                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1801                                         (uint32_t)pmbox->mbxCommand,
1802                                         pmbox->un.varWords[0],
1803                                         pmbox->un.varWords[1]);
1804                         }
1805                         else {
1806                                 lpfc_debugfs_disc_trc(phba->pport,
1807                                         LPFC_DISC_TRC_MBOX,
1808                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1809                                         (uint32_t)pmbox->mbxCommand,
1810                                         pmbox->un.varWords[0],
1811                                         pmbox->un.varWords[1]);
1812                         }
1813                 }
1814
1815                 /*
1816                  * It is a fatal error if unknown mbox command completion.
1817                  */
1818                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1819                     MBX_SHUTDOWN) {
1820                         /* Unknown mailbox command compl */
1821                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1822                                         "(%d):0323 Unknown Mailbox command "
1823                                         "x%x (x%x) Cmpl\n",
1824                                         pmb->vport ? pmb->vport->vpi : 0,
1825                                         pmbox->mbxCommand,
1826                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
1827                         phba->link_state = LPFC_HBA_ERROR;
1828                         phba->work_hs = HS_FFER3;
1829                         lpfc_handle_eratt(phba);
1830                         continue;
1831                 }
1832
1833                 if (pmbox->mbxStatus) {
1834                         phba->sli.slistat.mbox_stat_err++;
1835                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1836                                 /* Mbox cmd cmpl error - RETRYing */
1837                                 lpfc_printf_log(phba, KERN_INFO,
1838                                                 LOG_MBOX | LOG_SLI,
1839                                                 "(%d):0305 Mbox cmd cmpl "
1840                                                 "error - RETRYing Data: x%x "
1841                                                 "(x%x) x%x x%x x%x\n",
1842                                                 pmb->vport ? pmb->vport->vpi :0,
1843                                                 pmbox->mbxCommand,
1844                                                 lpfc_sli4_mbox_opcode_get(phba,
1845                                                                           pmb),
1846                                                 pmbox->mbxStatus,
1847                                                 pmbox->un.varWords[0],
1848                                                 pmb->vport->port_state);
1849                                 pmbox->mbxStatus = 0;
1850                                 pmbox->mbxOwner = OWN_HOST;
1851                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1852                                 if (rc != MBX_NOT_FINISHED)
1853                                         continue;
1854                         }
1855                 }
1856
1857                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1858                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1859                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
1860                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1861                                 pmb->vport ? pmb->vport->vpi : 0,
1862                                 pmbox->mbxCommand,
1863                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
1864                                 pmb->mbox_cmpl,
1865                                 *((uint32_t *) pmbox),
1866                                 pmbox->un.varWords[0],
1867                                 pmbox->un.varWords[1],
1868                                 pmbox->un.varWords[2],
1869                                 pmbox->un.varWords[3],
1870                                 pmbox->un.varWords[4],
1871                                 pmbox->un.varWords[5],
1872                                 pmbox->un.varWords[6],
1873                                 pmbox->un.varWords[7]);
1874
1875                 if (pmb->mbox_cmpl)
1876                         pmb->mbox_cmpl(phba,pmb);
1877         } while (1);
1878         return 0;
1879 }
1880
1881 /**
1882  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1883  * @phba: Pointer to HBA context object.
1884  * @pring: Pointer to driver SLI ring object.
1885  * @tag: buffer tag.
1886  *
1887  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1888  * is set in the tag the buffer is posted for a particular exchange,
1889  * the function will return the buffer without replacing the buffer.
1890  * If the buffer is for unsolicited ELS or CT traffic, this function
1891  * returns the buffer and also posts another buffer to the firmware.
1892  **/
1893 static struct lpfc_dmabuf *
1894 lpfc_sli_get_buff(struct lpfc_hba *phba,
1895                   struct lpfc_sli_ring *pring,
1896                   uint32_t tag)
1897 {
1898         struct hbq_dmabuf *hbq_entry;
1899
1900         if (tag & QUE_BUFTAG_BIT)
1901                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1902         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1903         if (!hbq_entry)
1904                 return NULL;
1905         return &hbq_entry->dbuf;
1906 }
1907
1908 /**
1909  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1910  * @phba: Pointer to HBA context object.
1911  * @pring: Pointer to driver SLI ring object.
1912  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1913  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1914  * @fch_type: the type for the first frame of the sequence.
1915  *
1916  * This function is called with no lock held. This function uses the r_ctl and
1917  * type of the received sequence to find the correct callback function to call
1918  * to process the sequence.
1919  **/
1920 static int
1921 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1922                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1923                          uint32_t fch_type)
1924 {
1925         int i;
1926
1927         /* unSolicited Responses */
1928         if (pring->prt[0].profile) {
1929                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1930                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1931                                                                         saveq);
1932                 return 1;
1933         }
1934         /* We must search, based on rctl / type
1935            for the right routine */
1936         for (i = 0; i < pring->num_mask; i++) {
1937                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1938                     (pring->prt[i].type == fch_type)) {
1939                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1940                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1941                                                 (phba, pring, saveq);
1942                         return 1;
1943                 }
1944         }
1945         return 0;
1946 }
1947
1948 /**
1949  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1950  * @phba: Pointer to HBA context object.
1951  * @pring: Pointer to driver SLI ring object.
1952  * @saveq: Pointer to the unsolicited iocb.
1953  *
1954  * This function is called with no lock held by the ring event handler
1955  * when there is an unsolicited iocb posted to the response ring by the
1956  * firmware. This function gets the buffer associated with the iocbs
1957  * and calls the event handler for the ring. This function handles both
1958  * qring buffers and hbq buffers.
1959  * When the function returns 1 the caller can free the iocb object otherwise
1960  * upper layer functions will free the iocb objects.
1961  **/
1962 static int
1963 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1964                             struct lpfc_iocbq *saveq)
1965 {
1966         IOCB_t           * irsp;
1967         WORD5            * w5p;
1968         uint32_t           Rctl, Type;
1969         uint32_t           match;
1970         struct lpfc_iocbq *iocbq;
1971         struct lpfc_dmabuf *dmzbuf;
1972
1973         match = 0;
1974         irsp = &(saveq->iocb);
1975
1976         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1977                 if (pring->lpfc_sli_rcv_async_status)
1978                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1979                 else
1980                         lpfc_printf_log(phba,
1981                                         KERN_WARNING,
1982                                         LOG_SLI,
1983                                         "0316 Ring %d handler: unexpected "
1984                                         "ASYNC_STATUS iocb received evt_code "
1985                                         "0x%x\n",
1986                                         pring->ringno,
1987                                         irsp->un.asyncstat.evt_code);
1988                 return 1;
1989         }
1990
1991         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1992                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1993                 if (irsp->ulpBdeCount > 0) {
1994                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1995                                         irsp->un.ulpWord[3]);
1996                         lpfc_in_buf_free(phba, dmzbuf);
1997                 }
1998
1999                 if (irsp->ulpBdeCount > 1) {
2000                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2001                                         irsp->unsli3.sli3Words[3]);
2002                         lpfc_in_buf_free(phba, dmzbuf);
2003                 }
2004
2005                 if (irsp->ulpBdeCount > 2) {
2006                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2007                                 irsp->unsli3.sli3Words[7]);
2008                         lpfc_in_buf_free(phba, dmzbuf);
2009                 }
2010
2011                 return 1;
2012         }
2013
2014         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2015                 if (irsp->ulpBdeCount != 0) {
2016                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2017                                                 irsp->un.ulpWord[3]);
2018                         if (!saveq->context2)
2019                                 lpfc_printf_log(phba,
2020                                         KERN_ERR,
2021                                         LOG_SLI,
2022                                         "0341 Ring %d Cannot find buffer for "
2023                                         "an unsolicited iocb. tag 0x%x\n",
2024                                         pring->ringno,
2025                                         irsp->un.ulpWord[3]);
2026                 }
2027                 if (irsp->ulpBdeCount == 2) {
2028                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2029                                                 irsp->unsli3.sli3Words[7]);
2030                         if (!saveq->context3)
2031                                 lpfc_printf_log(phba,
2032                                         KERN_ERR,
2033                                         LOG_SLI,
2034                                         "0342 Ring %d Cannot find buffer for an"
2035                                         " unsolicited iocb. tag 0x%x\n",
2036                                         pring->ringno,
2037                                         irsp->unsli3.sli3Words[7]);
2038                 }
2039                 list_for_each_entry(iocbq, &saveq->list, list) {
2040                         irsp = &(iocbq->iocb);
2041                         if (irsp->ulpBdeCount != 0) {
2042                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2043                                                         irsp->un.ulpWord[3]);
2044                                 if (!iocbq->context2)
2045                                         lpfc_printf_log(phba,
2046                                                 KERN_ERR,
2047                                                 LOG_SLI,
2048                                                 "0343 Ring %d Cannot find "
2049                                                 "buffer for an unsolicited iocb"
2050                                                 ". tag 0x%x\n", pring->ringno,
2051                                                 irsp->un.ulpWord[3]);
2052                         }
2053                         if (irsp->ulpBdeCount == 2) {
2054                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2055                                                 irsp->unsli3.sli3Words[7]);
2056                                 if (!iocbq->context3)
2057                                         lpfc_printf_log(phba,
2058                                                 KERN_ERR,
2059                                                 LOG_SLI,
2060                                                 "0344 Ring %d Cannot find "
2061                                                 "buffer for an unsolicited "
2062                                                 "iocb. tag 0x%x\n",
2063                                                 pring->ringno,
2064                                                 irsp->unsli3.sli3Words[7]);
2065                         }
2066                 }
2067         }
2068         if (irsp->ulpBdeCount != 0 &&
2069             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2070              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2071                 int found = 0;
2072
2073                 /* search continue save q for same XRI */
2074                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2075                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2076                                 list_add_tail(&saveq->list, &iocbq->list);
2077                                 found = 1;
2078                                 break;
2079                         }
2080                 }
2081                 if (!found)
2082                         list_add_tail(&saveq->clist,
2083                                       &pring->iocb_continue_saveq);
2084                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2085                         list_del_init(&iocbq->clist);
2086                         saveq = iocbq;
2087                         irsp = &(saveq->iocb);
2088                 } else
2089                         return 0;
2090         }
2091         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2092             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2093             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2094                 Rctl = FC_RCTL_ELS_REQ;
2095                 Type = FC_TYPE_ELS;
2096         } else {
2097                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2098                 Rctl = w5p->hcsw.Rctl;
2099                 Type = w5p->hcsw.Type;
2100
2101                 /* Firmware Workaround */
2102                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2103                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2104                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2105                         Rctl = FC_RCTL_ELS_REQ;
2106                         Type = FC_TYPE_ELS;
2107                         w5p->hcsw.Rctl = Rctl;
2108                         w5p->hcsw.Type = Type;
2109                 }
2110         }
2111
2112         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2113                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2114                                 "0313 Ring %d handler: unexpected Rctl x%x "
2115                                 "Type x%x received\n",
2116                                 pring->ringno, Rctl, Type);
2117
2118         return 1;
2119 }
2120
2121 /**
2122  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2123  * @phba: Pointer to HBA context object.
2124  * @pring: Pointer to driver SLI ring object.
2125  * @prspiocb: Pointer to response iocb object.
2126  *
2127  * This function looks up the iocb_lookup table to get the command iocb
2128  * corresponding to the given response iocb using the iotag of the
2129  * response iocb. This function is called with the hbalock held.
2130  * This function returns the command iocb object if it finds the command
2131  * iocb else returns NULL.
2132  **/
2133 static struct lpfc_iocbq *
2134 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2135                       struct lpfc_sli_ring *pring,
2136                       struct lpfc_iocbq *prspiocb)
2137 {
2138         struct lpfc_iocbq *cmd_iocb = NULL;
2139         uint16_t iotag;
2140
2141         iotag = prspiocb->iocb.ulpIoTag;
2142
2143         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2144                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2145                 list_del_init(&cmd_iocb->list);
2146                 pring->txcmplq_cnt--;
2147                 return cmd_iocb;
2148         }
2149
2150         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2151                         "0317 iotag x%x is out off "
2152                         "range: max iotag x%x wd0 x%x\n",
2153                         iotag, phba->sli.last_iotag,
2154                         *(((uint32_t *) &prspiocb->iocb) + 7));
2155         return NULL;
2156 }
2157
2158 /**
2159  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2160  * @phba: Pointer to HBA context object.
2161  * @pring: Pointer to driver SLI ring object.
2162  * @iotag: IOCB tag.
2163  *
2164  * This function looks up the iocb_lookup table to get the command iocb
2165  * corresponding to the given iotag. This function is called with the
2166  * hbalock held.
2167  * This function returns the command iocb object if it finds the command
2168  * iocb else returns NULL.
2169  **/
2170 static struct lpfc_iocbq *
2171 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2172                              struct lpfc_sli_ring *pring, uint16_t iotag)
2173 {
2174         struct lpfc_iocbq *cmd_iocb;
2175
2176         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2177                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2178                 list_del_init(&cmd_iocb->list);
2179                 pring->txcmplq_cnt--;
2180                 return cmd_iocb;
2181         }
2182
2183         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2184                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2185                         iotag, phba->sli.last_iotag);
2186         return NULL;
2187 }
2188
2189 /**
2190  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2191  * @phba: Pointer to HBA context object.
2192  * @pring: Pointer to driver SLI ring object.
2193  * @saveq: Pointer to the response iocb to be processed.
2194  *
2195  * This function is called by the ring event handler for non-fcp
2196  * rings when there is a new response iocb in the response ring.
2197  * The caller is not required to hold any locks. This function
2198  * gets the command iocb associated with the response iocb and
2199  * calls the completion handler for the command iocb. If there
2200  * is no completion handler, the function will free the resources
2201  * associated with command iocb. If the response iocb is for
2202  * an already aborted command iocb, the status of the completion
2203  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2204  * This function always returns 1.
2205  **/
2206 static int
2207 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2208                           struct lpfc_iocbq *saveq)
2209 {
2210         struct lpfc_iocbq *cmdiocbp;
2211         int rc = 1;
2212         unsigned long iflag;
2213
2214         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2215         spin_lock_irqsave(&phba->hbalock, iflag);
2216         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2217         spin_unlock_irqrestore(&phba->hbalock, iflag);
2218
2219         if (cmdiocbp) {
2220                 if (cmdiocbp->iocb_cmpl) {
2221                         /*
2222                          * If an ELS command failed send an event to mgmt
2223                          * application.
2224                          */
2225                         if (saveq->iocb.ulpStatus &&
2226                              (pring->ringno == LPFC_ELS_RING) &&
2227                              (cmdiocbp->iocb.ulpCommand ==
2228                                 CMD_ELS_REQUEST64_CR))
2229                                 lpfc_send_els_failure_event(phba,
2230                                         cmdiocbp, saveq);
2231
2232                         /*
2233                          * Post all ELS completions to the worker thread.
2234                          * All other are passed to the completion callback.
2235                          */
2236                         if (pring->ringno == LPFC_ELS_RING) {
2237                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2238                                     (cmdiocbp->iocb_flag &
2239                                                         LPFC_DRIVER_ABORTED)) {
2240                                         spin_lock_irqsave(&phba->hbalock,
2241                                                           iflag);
2242                                         cmdiocbp->iocb_flag &=
2243                                                 ~LPFC_DRIVER_ABORTED;
2244                                         spin_unlock_irqrestore(&phba->hbalock,
2245                                                                iflag);
2246                                         saveq->iocb.ulpStatus =
2247                                                 IOSTAT_LOCAL_REJECT;
2248                                         saveq->iocb.un.ulpWord[4] =
2249                                                 IOERR_SLI_ABORTED;
2250
2251                                         /* Firmware could still be in progress
2252                                          * of DMAing payload, so don't free data
2253                                          * buffer till after a hbeat.
2254                                          */
2255                                         spin_lock_irqsave(&phba->hbalock,
2256                                                           iflag);
2257                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2258                                         spin_unlock_irqrestore(&phba->hbalock,
2259                                                                iflag);
2260                                 }
2261                                 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2262                                     (saveq->iocb_flag & LPFC_EXCHANGE_BUSY)) {
2263                                         /* Set cmdiocb flag for the exchange
2264                                          * busy so sgl (xri) will not be
2265                                          * released until the abort xri is
2266                                          * received from hba, clear the
2267                                          * LPFC_DRIVER_ABORTED bit in case
2268                                          * it was driver initiated abort.
2269                                          */
2270                                         spin_lock_irqsave(&phba->hbalock,
2271                                                           iflag);
2272                                         cmdiocbp->iocb_flag &=
2273                                                 ~LPFC_DRIVER_ABORTED;
2274                                         cmdiocbp->iocb_flag |=
2275                                                 LPFC_EXCHANGE_BUSY;
2276                                         spin_unlock_irqrestore(&phba->hbalock,
2277                                                                iflag);
2278                                         cmdiocbp->iocb.ulpStatus =
2279                                                 IOSTAT_LOCAL_REJECT;
2280                                         cmdiocbp->iocb.un.ulpWord[4] =
2281                                                 IOERR_ABORT_REQUESTED;
2282                                         /*
2283                                          * For SLI4, irsiocb contains NO_XRI
2284                                          * in sli_xritag, it shall not affect
2285                                          * releasing sgl (xri) process.
2286                                          */
2287                                         saveq->iocb.ulpStatus =
2288                                                 IOSTAT_LOCAL_REJECT;
2289                                         saveq->iocb.un.ulpWord[4] =
2290                                                 IOERR_SLI_ABORTED;
2291                                         spin_lock_irqsave(&phba->hbalock,
2292                                                           iflag);
2293                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2294                                         spin_unlock_irqrestore(&phba->hbalock,
2295                                                                iflag);
2296                                 }
2297                         }
2298                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2299                 } else
2300                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2301         } else {
2302                 /*
2303                  * Unknown initiating command based on the response iotag.
2304                  * This could be the case on the ELS ring because of
2305                  * lpfc_els_abort().
2306                  */
2307                 if (pring->ringno != LPFC_ELS_RING) {
2308                         /*
2309                          * Ring <ringno> handler: unexpected completion IoTag
2310                          * <IoTag>
2311                          */
2312                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2313                                          "0322 Ring %d handler: "
2314                                          "unexpected completion IoTag x%x "
2315                                          "Data: x%x x%x x%x x%x\n",
2316                                          pring->ringno,
2317                                          saveq->iocb.ulpIoTag,
2318                                          saveq->iocb.ulpStatus,
2319                                          saveq->iocb.un.ulpWord[4],
2320                                          saveq->iocb.ulpCommand,
2321                                          saveq->iocb.ulpContext);
2322                 }
2323         }
2324
2325         return rc;
2326 }
2327
2328 /**
2329  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2330  * @phba: Pointer to HBA context object.
2331  * @pring: Pointer to driver SLI ring object.
2332  *
2333  * This function is called from the iocb ring event handlers when
2334  * put pointer is ahead of the get pointer for a ring. This function signal
2335  * an error attention condition to the worker thread and the worker
2336  * thread will transition the HBA to offline state.
2337  **/
2338 static void
2339 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2340 {
2341         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2342         /*
2343          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2344          * rsp ring <portRspMax>
2345          */
2346         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2347                         "0312 Ring %d handler: portRspPut %d "
2348                         "is bigger than rsp ring %d\n",
2349                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2350                         pring->numRiocb);
2351
2352         phba->link_state = LPFC_HBA_ERROR;
2353
2354         /*
2355          * All error attention handlers are posted to
2356          * worker thread
2357          */
2358         phba->work_ha |= HA_ERATT;
2359         phba->work_hs = HS_FFER3;
2360
2361         lpfc_worker_wake_up(phba);
2362
2363         return;
2364 }
2365
2366 /**
2367  * lpfc_poll_eratt - Error attention polling timer timeout handler
2368  * @ptr: Pointer to address of HBA context object.
2369  *
2370  * This function is invoked by the Error Attention polling timer when the
2371  * timer times out. It will check the SLI Error Attention register for
2372  * possible attention events. If so, it will post an Error Attention event
2373  * and wake up worker thread to process it. Otherwise, it will set up the
2374  * Error Attention polling timer for the next poll.
2375  **/
2376 void lpfc_poll_eratt(unsigned long ptr)
2377 {
2378         struct lpfc_hba *phba;
2379         uint32_t eratt = 0;
2380
2381         phba = (struct lpfc_hba *)ptr;
2382
2383         /* Check chip HA register for error event */
2384         eratt = lpfc_sli_check_eratt(phba);
2385
2386         if (eratt)
2387                 /* Tell the worker thread there is work to do */
2388                 lpfc_worker_wake_up(phba);
2389         else
2390                 /* Restart the timer for next eratt poll */
2391                 mod_timer(&phba->eratt_poll, jiffies +
2392                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2393         return;
2394 }
2395
2396
2397 /**
2398  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2399  * @phba: Pointer to HBA context object.
2400  * @pring: Pointer to driver SLI ring object.
2401  * @mask: Host attention register mask for this ring.
2402  *
2403  * This function is called from the interrupt context when there is a ring
2404  * event for the fcp ring. The caller does not hold any lock.
2405  * The function processes each response iocb in the response ring until it
2406  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2407  * LE bit set. The function will call the completion handler of the command iocb
2408  * if the response iocb indicates a completion for a command iocb or it is
2409  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2410  * function if this is an unsolicited iocb.
2411  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2412  * to check it explicitly.
2413  */
2414 int
2415 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2416                                 struct lpfc_sli_ring *pring, uint32_t mask)
2417 {
2418         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2419         IOCB_t *irsp = NULL;
2420         IOCB_t *entry = NULL;
2421         struct lpfc_iocbq *cmdiocbq = NULL;
2422         struct lpfc_iocbq rspiocbq;
2423         uint32_t status;
2424         uint32_t portRspPut, portRspMax;
2425         int rc = 1;
2426         lpfc_iocb_type type;
2427         unsigned long iflag;
2428         uint32_t rsp_cmpl = 0;
2429
2430         spin_lock_irqsave(&phba->hbalock, iflag);
2431         pring->stats.iocb_event++;
2432
2433         /*
2434          * The next available response entry should never exceed the maximum
2435          * entries.  If it does, treat it as an adapter hardware error.
2436          */
2437         portRspMax = pring->numRiocb;
2438         portRspPut = le32_to_cpu(pgp->rspPutInx);
2439         if (unlikely(portRspPut >= portRspMax)) {
2440                 lpfc_sli_rsp_pointers_error(phba, pring);
2441                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2442                 return 1;
2443         }
2444         if (phba->fcp_ring_in_use) {
2445                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2446                 return 1;
2447         } else
2448                 phba->fcp_ring_in_use = 1;
2449
2450         rmb();
2451         while (pring->rspidx != portRspPut) {
2452                 /*
2453                  * Fetch an entry off the ring and copy it into a local data
2454                  * structure.  The copy involves a byte-swap since the
2455                  * network byte order and pci byte orders are different.
2456                  */
2457                 entry = lpfc_resp_iocb(phba, pring);
2458                 phba->last_completion_time = jiffies;
2459
2460                 if (++pring->rspidx >= portRspMax)
2461                         pring->rspidx = 0;
2462
2463                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2464                                       (uint32_t *) &rspiocbq.iocb,
2465                                       phba->iocb_rsp_size);
2466                 INIT_LIST_HEAD(&(rspiocbq.list));
2467                 irsp = &rspiocbq.iocb;
2468
2469                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2470                 pring->stats.iocb_rsp++;
2471                 rsp_cmpl++;
2472
2473                 if (unlikely(irsp->ulpStatus)) {
2474                         /*
2475                          * If resource errors reported from HBA, reduce
2476                          * queuedepths of the SCSI device.
2477                          */
2478                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2479                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2480                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2481                                 phba->lpfc_rampdown_queue_depth(phba);
2482                                 spin_lock_irqsave(&phba->hbalock, iflag);
2483                         }
2484
2485                         /* Rsp ring <ringno> error: IOCB */
2486                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2487                                         "0336 Rsp Ring %d error: IOCB Data: "
2488                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2489                                         pring->ringno,
2490                                         irsp->un.ulpWord[0],
2491                                         irsp->un.ulpWord[1],
2492                                         irsp->un.ulpWord[2],
2493                                         irsp->un.ulpWord[3],
2494                                         irsp->un.ulpWord[4],
2495                                         irsp->un.ulpWord[5],
2496                                         *(uint32_t *)&irsp->un1,
2497                                         *((uint32_t *)&irsp->un1 + 1));
2498                 }
2499
2500                 switch (type) {
2501                 case LPFC_ABORT_IOCB:
2502                 case LPFC_SOL_IOCB:
2503                         /*
2504                          * Idle exchange closed via ABTS from port.  No iocb
2505                          * resources need to be recovered.
2506                          */
2507                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2508                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2509                                                 "0333 IOCB cmd 0x%x"
2510                                                 " processed. Skipping"
2511                                                 " completion\n",
2512                                                 irsp->ulpCommand);
2513                                 break;
2514                         }
2515
2516                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2517                                                          &rspiocbq);
2518                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2519                                         spin_unlock_irqrestore(&phba->hbalock,
2520                                                                iflag);
2521                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2522                                                               &rspiocbq);
2523                                         spin_lock_irqsave(&phba->hbalock,
2524                                                           iflag);
2525                                 }
2526                         break;
2527                 case LPFC_UNSOL_IOCB:
2528                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2529                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2530                         spin_lock_irqsave(&phba->hbalock, iflag);
2531                         break;
2532                 default:
2533                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2534                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2535                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2536                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2537                                        MAX_MSG_DATA);
2538                                 dev_warn(&((phba->pcidev)->dev),
2539                                          "lpfc%d: %s\n",
2540                                          phba->brd_no, adaptermsg);
2541                         } else {
2542                                 /* Unknown IOCB command */
2543                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2544                                                 "0334 Unknown IOCB command "
2545                                                 "Data: x%x, x%x x%x x%x x%x\n",
2546                                                 type, irsp->ulpCommand,
2547                                                 irsp->ulpStatus,
2548                                                 irsp->ulpIoTag,
2549                                                 irsp->ulpContext);
2550                         }
2551                         break;
2552                 }
2553
2554                 /*
2555                  * The response IOCB has been processed.  Update the ring
2556                  * pointer in SLIM.  If the port response put pointer has not
2557                  * been updated, sync the pgp->rspPutInx and fetch the new port
2558                  * response put pointer.
2559                  */
2560                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2561
2562                 if (pring->rspidx == portRspPut)
2563                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2564         }
2565
2566         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2567                 pring->stats.iocb_rsp_full++;
2568                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2569                 writel(status, phba->CAregaddr);
2570                 readl(phba->CAregaddr);
2571         }
2572         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2573                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2574                 pring->stats.iocb_cmd_empty++;
2575
2576                 /* Force update of the local copy of cmdGetInx */
2577                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2578                 lpfc_sli_resume_iocb(phba, pring);
2579
2580                 if ((pring->lpfc_sli_cmd_available))
2581                         (pring->lpfc_sli_cmd_available) (phba, pring);
2582
2583         }
2584
2585         phba->fcp_ring_in_use = 0;
2586         spin_unlock_irqrestore(&phba->hbalock, iflag);
2587         return rc;
2588 }
2589
2590 /**
2591  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2592  * @phba: Pointer to HBA context object.
2593  * @pring: Pointer to driver SLI ring object.
2594  * @rspiocbp: Pointer to driver response IOCB object.
2595  *
2596  * This function is called from the worker thread when there is a slow-path
2597  * response IOCB to process. This function chains all the response iocbs until
2598  * seeing the iocb with the LE bit set. The function will call
2599  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2600  * completion of a command iocb. The function will call the
2601  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2602  * The function frees the resources or calls the completion handler if this
2603  * iocb is an abort completion. The function returns NULL when the response
2604  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2605  * this function shall chain the iocb on to the iocb_continueq and return the
2606  * response iocb passed in.
2607  **/
2608 static struct lpfc_iocbq *
2609 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2610                         struct lpfc_iocbq *rspiocbp)
2611 {
2612         struct lpfc_iocbq *saveq;
2613         struct lpfc_iocbq *cmdiocbp;
2614         struct lpfc_iocbq *next_iocb;
2615         IOCB_t *irsp = NULL;
2616         uint32_t free_saveq;
2617         uint8_t iocb_cmd_type;
2618         lpfc_iocb_type type;
2619         unsigned long iflag;
2620         int rc;
2621
2622         spin_lock_irqsave(&phba->hbalock, iflag);
2623         /* First add the response iocb to the countinueq list */
2624         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2625         pring->iocb_continueq_cnt++;
2626
2627         /* Now, determine whetehr the list is completed for processing */
2628         irsp = &rspiocbp->iocb;
2629         if (irsp->ulpLe) {
2630                 /*
2631                  * By default, the driver expects to free all resources
2632                  * associated with this iocb completion.
2633                  */
2634                 free_saveq = 1;
2635                 saveq = list_get_first(&pring->iocb_continueq,
2636                                        struct lpfc_iocbq, list);
2637                 irsp = &(saveq->iocb);
2638                 list_del_init(&pring->iocb_continueq);
2639                 pring->iocb_continueq_cnt = 0;
2640
2641                 pring->stats.iocb_rsp++;
2642
2643                 /*
2644                  * If resource errors reported from HBA, reduce
2645                  * queuedepths of the SCSI device.
2646                  */
2647                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2648                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2649                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2650                         phba->lpfc_rampdown_queue_depth(phba);
2651                         spin_lock_irqsave(&phba->hbalock, iflag);
2652                 }
2653
2654                 if (irsp->ulpStatus) {
2655                         /* Rsp ring <ringno> error: IOCB */
2656                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2657                                         "0328 Rsp Ring %d error: "
2658                                         "IOCB Data: "
2659                                         "x%x x%x x%x x%x "
2660                                         "x%x x%x x%x x%x "
2661                                         "x%x x%x x%x x%x "
2662                                         "x%x x%x x%x x%x\n",
2663                                         pring->ringno,
2664                                         irsp->un.ulpWord[0],
2665                                         irsp->un.ulpWord[1],
2666                                         irsp->un.ulpWord[2],
2667                                         irsp->un.ulpWord[3],
2668                                         irsp->un.ulpWord[4],
2669                                         irsp->un.ulpWord[5],
2670                                         *(((uint32_t *) irsp) + 6),
2671                                         *(((uint32_t *) irsp) + 7),
2672                                         *(((uint32_t *) irsp) + 8),
2673                                         *(((uint32_t *) irsp) + 9),
2674                                         *(((uint32_t *) irsp) + 10),
2675                                         *(((uint32_t *) irsp) + 11),
2676                                         *(((uint32_t *) irsp) + 12),
2677                                         *(((uint32_t *) irsp) + 13),
2678                                         *(((uint32_t *) irsp) + 14),
2679                                         *(((uint32_t *) irsp) + 15));
2680                 }
2681
2682                 /*
2683                  * Fetch the IOCB command type and call the correct completion
2684                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2685                  * get freed back to the lpfc_iocb_list by the discovery
2686                  * kernel thread.
2687                  */
2688                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2689                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2690                 switch (type) {
2691                 case LPFC_SOL_IOCB:
2692                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2693                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2694                         spin_lock_irqsave(&phba->hbalock, iflag);
2695                         break;
2696
2697                 case LPFC_UNSOL_IOCB:
2698                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2699                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2700                         spin_lock_irqsave(&phba->hbalock, iflag);
2701                         if (!rc)
2702                                 free_saveq = 0;
2703                         break;
2704
2705                 case LPFC_ABORT_IOCB:
2706                         cmdiocbp = NULL;
2707                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2708                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2709                                                                  saveq);
2710                         if (cmdiocbp) {
2711                                 /* Call the specified completion routine */
2712                                 if (cmdiocbp->iocb_cmpl) {
2713                                         spin_unlock_irqrestore(&phba->hbalock,
2714                                                                iflag);
2715                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2716                                                               saveq);
2717                                         spin_lock_irqsave(&phba->hbalock,
2718                                                           iflag);
2719                                 } else
2720                                         __lpfc_sli_release_iocbq(phba,
2721                                                                  cmdiocbp);
2722                         }
2723                         break;
2724
2725                 case LPFC_UNKNOWN_IOCB:
2726                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2727                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2728                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2729                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2730                                        MAX_MSG_DATA);
2731                                 dev_warn(&((phba->pcidev)->dev),
2732                                          "lpfc%d: %s\n",
2733                                          phba->brd_no, adaptermsg);
2734                         } else {
2735                                 /* Unknown IOCB command */
2736                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2737                                                 "0335 Unknown IOCB "
2738                                                 "command Data: x%x "
2739                                                 "x%x x%x x%x\n",
2740                                                 irsp->ulpCommand,
2741                                                 irsp->ulpStatus,
2742                                                 irsp->ulpIoTag,
2743                                                 irsp->ulpContext);
2744                         }
2745                         break;
2746                 }
2747
2748                 if (free_saveq) {
2749                         list_for_each_entry_safe(rspiocbp, next_iocb,
2750                                                  &saveq->list, list) {
2751                                 list_del(&rspiocbp->list);
2752                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2753                         }
2754                         __lpfc_sli_release_iocbq(phba, saveq);
2755                 }
2756                 rspiocbp = NULL;
2757         }
2758         spin_unlock_irqrestore(&phba->hbalock, iflag);
2759         return rspiocbp;
2760 }
2761
2762 /**
2763  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2764  * @phba: Pointer to HBA context object.
2765  * @pring: Pointer to driver SLI ring object.
2766  * @mask: Host attention register mask for this ring.
2767  *
2768  * This routine wraps the actual slow_ring event process routine from the
2769  * API jump table function pointer from the lpfc_hba struct.
2770  **/
2771 void
2772 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2773                                 struct lpfc_sli_ring *pring, uint32_t mask)
2774 {
2775         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2776 }
2777
2778 /**
2779  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2780  * @phba: Pointer to HBA context object.
2781  * @pring: Pointer to driver SLI ring object.
2782  * @mask: Host attention register mask for this ring.
2783  *
2784  * This function is called from the worker thread when there is a ring event
2785  * for non-fcp rings. The caller does not hold any lock. The function will
2786  * remove each response iocb in the response ring and calls the handle
2787  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2788  **/
2789 static void
2790 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2791                                    struct lpfc_sli_ring *pring, uint32_t mask)
2792 {
2793         struct lpfc_pgp *pgp;
2794         IOCB_t *entry;
2795         IOCB_t *irsp = NULL;
2796         struct lpfc_iocbq *rspiocbp = NULL;
2797         uint32_t portRspPut, portRspMax;
2798         unsigned long iflag;
2799         uint32_t status;
2800
2801         pgp = &phba->port_gp[pring->ringno];
2802         spin_lock_irqsave(&phba->hbalock, iflag);
2803         pring->stats.iocb_event++;
2804
2805         /*
2806          * The next available response entry should never exceed the maximum
2807          * entries.  If it does, treat it as an adapter hardware error.
2808          */
2809         portRspMax = pring->numRiocb;
2810         portRspPut = le32_to_cpu(pgp->rspPutInx);
2811         if (portRspPut >= portRspMax) {
2812                 /*
2813                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2814                  * rsp ring <portRspMax>
2815                  */
2816                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2817                                 "0303 Ring %d handler: portRspPut %d "
2818                                 "is bigger than rsp ring %d\n",
2819                                 pring->ringno, portRspPut, portRspMax);
2820
2821                 phba->link_state = LPFC_HBA_ERROR;
2822                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2823
2824                 phba->work_hs = HS_FFER3;
2825                 lpfc_handle_eratt(phba);
2826
2827                 return;
2828         }
2829
2830         rmb();
2831         while (pring->rspidx != portRspPut) {
2832                 /*
2833                  * Build a completion list and call the appropriate handler.
2834                  * The process is to get the next available response iocb, get
2835                  * a free iocb from the list, copy the response data into the
2836                  * free iocb, insert to the continuation list, and update the
2837                  * next response index to slim.  This process makes response
2838                  * iocb's in the ring available to DMA as fast as possible but
2839                  * pays a penalty for a copy operation.  Since the iocb is
2840                  * only 32 bytes, this penalty is considered small relative to
2841                  * the PCI reads for register values and a slim write.  When
2842                  * the ulpLe field is set, the entire Command has been
2843                  * received.
2844                  */
2845                 entry = lpfc_resp_iocb(phba, pring);
2846
2847                 phba->last_completion_time = jiffies;
2848                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2849                 if (rspiocbp == NULL) {
2850                         printk(KERN_ERR "%s: out of buffers! Failing "
2851                                "completion.\n", __func__);
2852                         break;
2853                 }
2854
2855                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2856                                       phba->iocb_rsp_size);
2857                 irsp = &rspiocbp->iocb;
2858
2859                 if (++pring->rspidx >= portRspMax)
2860                         pring->rspidx = 0;
2861
2862                 if (pring->ringno == LPFC_ELS_RING) {
2863                         lpfc_debugfs_slow_ring_trc(phba,
2864                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2865                                 *(((uint32_t *) irsp) + 4),
2866                                 *(((uint32_t *) irsp) + 6),
2867                                 *(((uint32_t *) irsp) + 7));
2868                 }
2869
2870                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2871
2872                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2873                 /* Handle the response IOCB */
2874                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2875                 spin_lock_irqsave(&phba->hbalock, iflag);
2876
2877                 /*
2878                  * If the port response put pointer has not been updated, sync
2879                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2880                  * response put pointer.
2881                  */
2882                 if (pring->rspidx == portRspPut) {
2883                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2884                 }
2885         } /* while (pring->rspidx != portRspPut) */
2886
2887         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2888                 /* At least one response entry has been freed */
2889                 pring->stats.iocb_rsp_full++;
2890                 /* SET RxRE_RSP in Chip Att register */
2891                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2892                 writel(status, phba->CAregaddr);
2893                 readl(phba->CAregaddr); /* flush */
2894         }
2895         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2896                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2897                 pring->stats.iocb_cmd_empty++;
2898
2899                 /* Force update of the local copy of cmdGetInx */
2900                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2901                 lpfc_sli_resume_iocb(phba, pring);
2902
2903                 if ((pring->lpfc_sli_cmd_available))
2904                         (pring->lpfc_sli_cmd_available) (phba, pring);
2905
2906         }
2907
2908         spin_unlock_irqrestore(&phba->hbalock, iflag);
2909         return;
2910 }
2911
2912 /**
2913  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2914  * @phba: Pointer to HBA context object.
2915  * @pring: Pointer to driver SLI ring object.
2916  * @mask: Host attention register mask for this ring.
2917  *
2918  * This function is called from the worker thread when there is a pending
2919  * ELS response iocb on the driver internal slow-path response iocb worker
2920  * queue. The caller does not hold any lock. The function will remove each
2921  * response iocb from the response worker queue and calls the handle
2922  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2923  **/
2924 static void
2925 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2926                                    struct lpfc_sli_ring *pring, uint32_t mask)
2927 {
2928         struct lpfc_iocbq *irspiocbq;
2929         struct hbq_dmabuf *dmabuf;
2930         struct lpfc_cq_event *cq_event;
2931         unsigned long iflag;
2932
2933         spin_lock_irqsave(&phba->hbalock, iflag);
2934         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2935         spin_unlock_irqrestore(&phba->hbalock, iflag);
2936         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
2937                 /* Get the response iocb from the head of work queue */
2938                 spin_lock_irqsave(&phba->hbalock, iflag);
2939                 list_remove_head(&phba->sli4_hba.sp_queue_event,
2940                                  cq_event, struct lpfc_cq_event, list);
2941                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2942
2943                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2944                 case CQE_CODE_COMPL_WQE:
2945                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2946                                                  cq_event);
2947                         /* Translate ELS WCQE to response IOCBQ */
2948                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2949                                                                    irspiocbq);
2950                         if (irspiocbq)
2951                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
2952                                                            irspiocbq);
2953                         break;
2954                 case CQE_CODE_RECEIVE:
2955                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
2956                                               cq_event);
2957                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
2958                         break;
2959                 default:
2960                         break;
2961                 }
2962         }
2963 }
2964
2965 /**
2966  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2967  * @phba: Pointer to HBA context object.
2968  * @pring: Pointer to driver SLI ring object.
2969  *
2970  * This function aborts all iocbs in the given ring and frees all the iocb
2971  * objects in txq. This function issues an abort iocb for all the iocb commands
2972  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2973  * the return of this function. The caller is not required to hold any locks.
2974  **/
2975 void
2976 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2977 {
2978         LIST_HEAD(completions);
2979         struct lpfc_iocbq *iocb, *next_iocb;
2980
2981         if (pring->ringno == LPFC_ELS_RING) {
2982                 lpfc_fabric_abort_hba(phba);
2983         }
2984
2985         /* Error everything on txq and txcmplq
2986          * First do the txq.
2987          */
2988         spin_lock_irq(&phba->hbalock);
2989         list_splice_init(&pring->txq, &completions);
2990         pring->txq_cnt = 0;
2991
2992         /* Next issue ABTS for everything on the txcmplq */
2993         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2994                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2995
2996         spin_unlock_irq(&phba->hbalock);
2997
2998         /* Cancel all the IOCBs from the completions list */
2999         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3000                               IOERR_SLI_ABORTED);
3001 }
3002
3003 /**
3004  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3005  * @phba: Pointer to HBA context object.
3006  *
3007  * This function flushes all iocbs in the fcp ring and frees all the iocb
3008  * objects in txq and txcmplq. This function will not issue abort iocbs
3009  * for all the iocb commands in txcmplq, they will just be returned with
3010  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3011  * slot has been permanently disabled.
3012  **/
3013 void
3014 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3015 {
3016         LIST_HEAD(txq);
3017         LIST_HEAD(txcmplq);
3018         struct lpfc_sli *psli = &phba->sli;
3019         struct lpfc_sli_ring  *pring;
3020
3021         /* Currently, only one fcp ring */
3022         pring = &psli->ring[psli->fcp_ring];
3023
3024         spin_lock_irq(&phba->hbalock);
3025         /* Retrieve everything on txq */
3026         list_splice_init(&pring->txq, &txq);
3027         pring->txq_cnt = 0;
3028
3029         /* Retrieve everything on the txcmplq */
3030         list_splice_init(&pring->txcmplq, &txcmplq);
3031         pring->txcmplq_cnt = 0;
3032         spin_unlock_irq(&phba->hbalock);
3033
3034         /* Flush the txq */
3035         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3036                               IOERR_SLI_DOWN);
3037
3038         /* Flush the txcmpq */
3039         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3040                               IOERR_SLI_DOWN);
3041 }
3042
3043 /**
3044  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3045  * @phba: Pointer to HBA context object.
3046  * @mask: Bit mask to be checked.
3047  *
3048  * This function reads the host status register and compares
3049  * with the provided bit mask to check if HBA completed
3050  * the restart. This function will wait in a loop for the
3051  * HBA to complete restart. If the HBA does not restart within
3052  * 15 iterations, the function will reset the HBA again. The
3053  * function returns 1 when HBA fail to restart otherwise returns
3054  * zero.
3055  **/
3056 static int
3057 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3058 {
3059         uint32_t status;
3060         int i = 0;
3061         int retval = 0;
3062
3063         /* Read the HBA Host Status Register */
3064         status = readl(phba->HSregaddr);
3065
3066         /*
3067          * Check status register every 100ms for 5 retries, then every
3068          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3069          * every 2.5 sec for 4.
3070          * Break our of the loop if errors occurred during init.
3071          */
3072         while (((status & mask) != mask) &&
3073                !(status & HS_FFERM) &&
3074                i++ < 20) {
3075
3076                 if (i <= 5)
3077                         msleep(10);
3078                 else if (i <= 10)
3079                         msleep(500);
3080                 else
3081                         msleep(2500);
3082
3083                 if (i == 15) {
3084                                 /* Do post */
3085                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3086                         lpfc_sli_brdrestart(phba);
3087                 }
3088                 /* Read the HBA Host Status Register */
3089                 status = readl(phba->HSregaddr);
3090         }
3091
3092         /* Check to see if any errors occurred during init */
3093         if ((status & HS_FFERM) || (i >= 20)) {
3094                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3095                                 "2751 Adapter failed to restart, "
3096                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3097                                 status,
3098                                 readl(phba->MBslimaddr + 0xa8),
3099                                 readl(phba->MBslimaddr + 0xac));
3100                 phba->link_state = LPFC_HBA_ERROR;
3101                 retval = 1;
3102         }
3103
3104         return retval;
3105 }
3106
3107 /**
3108  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3109  * @phba: Pointer to HBA context object.
3110  * @mask: Bit mask to be checked.
3111  *
3112  * This function checks the host status register to check if HBA is
3113  * ready. This function will wait in a loop for the HBA to be ready
3114  * If the HBA is not ready , the function will will reset the HBA PCI
3115  * function again. The function returns 1 when HBA fail to be ready
3116  * otherwise returns zero.
3117  **/
3118 static int
3119 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3120 {
3121         uint32_t status;
3122         int retval = 0;
3123
3124         /* Read the HBA Host Status Register */
3125         status = lpfc_sli4_post_status_check(phba);
3126
3127         if (status) {
3128                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3129                 lpfc_sli_brdrestart(phba);
3130                 status = lpfc_sli4_post_status_check(phba);
3131         }
3132
3133         /* Check to see if any errors occurred during init */
3134         if (status) {
3135                 phba->link_state = LPFC_HBA_ERROR;
3136                 retval = 1;
3137         } else
3138                 phba->sli4_hba.intr_enable = 0;
3139
3140         return retval;
3141 }
3142
3143 /**
3144  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3145  * @phba: Pointer to HBA context object.
3146  * @mask: Bit mask to be checked.
3147  *
3148  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3149  * from the API jump table function pointer from the lpfc_hba struct.
3150  **/
3151 int
3152 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3153 {
3154         return phba->lpfc_sli_brdready(phba, mask);
3155 }
3156
3157 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3158
3159 /**
3160  * lpfc_reset_barrier - Make HBA ready for HBA reset
3161  * @phba: Pointer to HBA context object.
3162  *
3163  * This function is called before resetting an HBA. This
3164  * function requests HBA to quiesce DMAs before a reset.
3165  **/
3166 void lpfc_reset_barrier(struct lpfc_hba *phba)
3167 {
3168         uint32_t __iomem *resp_buf;
3169         uint32_t __iomem *mbox_buf;
3170         volatile uint32_t mbox;
3171         uint32_t hc_copy;
3172         int  i;
3173         uint8_t hdrtype;
3174
3175         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3176         if (hdrtype != 0x80 ||
3177             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3178              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3179                 return;
3180
3181         /*
3182          * Tell the other part of the chip to suspend temporarily all
3183          * its DMA activity.
3184          */
3185         resp_buf = phba->MBslimaddr;
3186
3187         /* Disable the error attention */
3188         hc_copy = readl(phba->HCregaddr);
3189         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3190         readl(phba->HCregaddr); /* flush */
3191         phba->link_flag |= LS_IGNORE_ERATT;
3192
3193         if (readl(phba->HAregaddr) & HA_ERATT) {
3194                 /* Clear Chip error bit */
3195                 writel(HA_ERATT, phba->HAregaddr);
3196                 phba->pport->stopped = 1;
3197         }
3198
3199         mbox = 0;
3200         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3201         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3202
3203         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3204         mbox_buf = phba->MBslimaddr;
3205         writel(mbox, mbox_buf);
3206
3207         for (i = 0;
3208              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3209                 mdelay(1);
3210
3211         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3212                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3213                     phba->pport->stopped)
3214                         goto restore_hc;
3215                 else
3216                         goto clear_errat;
3217         }
3218
3219         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3220         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3221                 mdelay(1);
3222
3223 clear_errat:
3224
3225         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3226                 mdelay(1);
3227
3228         if (readl(phba->HAregaddr) & HA_ERATT) {
3229                 writel(HA_ERATT, phba->HAregaddr);
3230                 phba->pport->stopped = 1;
3231         }
3232
3233 restore_hc:
3234         phba->link_flag &= ~LS_IGNORE_ERATT;
3235         writel(hc_copy, phba->HCregaddr);
3236         readl(phba->HCregaddr); /* flush */
3237 }
3238
3239 /**
3240  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3241  * @phba: Pointer to HBA context object.
3242  *
3243  * This function issues a kill_board mailbox command and waits for
3244  * the error attention interrupt. This function is called for stopping
3245  * the firmware processing. The caller is not required to hold any
3246  * locks. This function calls lpfc_hba_down_post function to free
3247  * any pending commands after the kill. The function will return 1 when it
3248  * fails to kill the board else will return 0.
3249  **/
3250 int
3251 lpfc_sli_brdkill(struct lpfc_hba *phba)
3252 {
3253         struct lpfc_sli *psli;
3254         LPFC_MBOXQ_t *pmb;
3255         uint32_t status;
3256         uint32_t ha_copy;
3257         int retval;
3258         int i = 0;
3259
3260         psli = &phba->sli;
3261
3262         /* Kill HBA */
3263         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3264                         "0329 Kill HBA Data: x%x x%x\n",
3265                         phba->pport->port_state, psli->sli_flag);
3266
3267         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3268         if (!pmb)
3269                 return 1;
3270
3271         /* Disable the error attention */
3272         spin_lock_irq(&phba->hbalock);
3273         status = readl(phba->HCregaddr);
3274         status &= ~HC_ERINT_ENA;
3275         writel(status, phba->HCregaddr);
3276         readl(phba->HCregaddr); /* flush */
3277         phba->link_flag |= LS_IGNORE_ERATT;
3278         spin_unlock_irq(&phba->hbalock);
3279
3280         lpfc_kill_board(phba, pmb);
3281         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3282         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3283
3284         if (retval != MBX_SUCCESS) {
3285                 if (retval != MBX_BUSY)
3286                         mempool_free(pmb, phba->mbox_mem_pool);
3287                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3288                                 "2752 KILL_BOARD command failed retval %d\n",
3289                                 retval);
3290                 spin_lock_irq(&phba->hbalock);
3291                 phba->link_flag &= ~LS_IGNORE_ERATT;
3292                 spin_unlock_irq(&phba->hbalock);
3293                 return 1;
3294         }
3295
3296         spin_lock_irq(&phba->hbalock);
3297         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3298         spin_unlock_irq(&phba->hbalock);
3299
3300         mempool_free(pmb, phba->mbox_mem_pool);
3301
3302         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3303          * attention every 100ms for 3 seconds. If we don't get ERATT after
3304          * 3 seconds we still set HBA_ERROR state because the status of the
3305          * board is now undefined.
3306          */
3307         ha_copy = readl(phba->HAregaddr);
3308
3309         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3310                 mdelay(100);
3311                 ha_copy = readl(phba->HAregaddr);
3312         }
3313
3314         del_timer_sync(&psli->mbox_tmo);
3315         if (ha_copy & HA_ERATT) {
3316                 writel(HA_ERATT, phba->HAregaddr);
3317                 phba->pport->stopped = 1;
3318         }
3319         spin_lock_irq(&phba->hbalock);
3320         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3321         psli->mbox_active = NULL;
3322         phba->link_flag &= ~LS_IGNORE_ERATT;
3323         spin_unlock_irq(&phba->hbalock);
3324
3325         lpfc_hba_down_post(phba);
3326         phba->link_state = LPFC_HBA_ERROR;
3327
3328         return ha_copy & HA_ERATT ? 0 : 1;
3329 }
3330
3331 /**
3332  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3333  * @phba: Pointer to HBA context object.
3334  *
3335  * This function resets the HBA by writing HC_INITFF to the control
3336  * register. After the HBA resets, this function resets all the iocb ring
3337  * indices. This function disables PCI layer parity checking during
3338  * the reset.
3339  * This function returns 0 always.
3340  * The caller is not required to hold any locks.
3341  **/
3342 int
3343 lpfc_sli_brdreset(struct lpfc_hba *phba)
3344 {
3345         struct lpfc_sli *psli;
3346         struct lpfc_sli_ring *pring;
3347         uint16_t cfg_value;
3348         int i;
3349
3350         psli = &phba->sli;
3351
3352         /* Reset HBA */
3353         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3354                         "0325 Reset HBA Data: x%x x%x\n",
3355                         phba->pport->port_state, psli->sli_flag);
3356
3357         /* perform board reset */
3358         phba->fc_eventTag = 0;
3359         phba->link_events = 0;
3360         phba->pport->fc_myDID = 0;
3361         phba->pport->fc_prevDID = 0;
3362
3363         /* Turn off parity checking and serr during the physical reset */
3364         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3365         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3366                               (cfg_value &
3367                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3368
3369         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3370
3371         /* Now toggle INITFF bit in the Host Control Register */
3372         writel(HC_INITFF, phba->HCregaddr);
3373         mdelay(1);
3374         readl(phba->HCregaddr); /* flush */
3375         writel(0, phba->HCregaddr);
3376         readl(phba->HCregaddr); /* flush */
3377
3378         /* Restore PCI cmd register */
3379         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3380
3381         /* Initialize relevant SLI info */
3382         for (i = 0; i < psli->num_rings; i++) {
3383                 pring = &psli->ring[i];
3384                 pring->flag = 0;
3385                 pring->rspidx = 0;
3386                 pring->next_cmdidx  = 0;
3387                 pring->local_getidx = 0;
3388                 pring->cmdidx = 0;
3389                 pring->missbufcnt = 0;
3390         }
3391
3392         phba->link_state = LPFC_WARM_START;
3393         return 0;
3394 }
3395
3396 /**
3397  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3398  * @phba: Pointer to HBA context object.
3399  *
3400  * This function resets a SLI4 HBA. This function disables PCI layer parity
3401  * checking during resets the device. The caller is not required to hold
3402  * any locks.
3403  *
3404  * This function returns 0 always.
3405  **/
3406 int
3407 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3408 {
3409         struct lpfc_sli *psli = &phba->sli;
3410         uint16_t cfg_value;
3411         uint8_t qindx;
3412
3413         /* Reset HBA */
3414         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3415                         "0295 Reset HBA Data: x%x x%x\n",
3416                         phba->pport->port_state, psli->sli_flag);
3417
3418         /* perform board reset */
3419         phba->fc_eventTag = 0;
3420         phba->link_events = 0;
3421         phba->pport->fc_myDID = 0;
3422         phba->pport->fc_prevDID = 0;
3423
3424         /* Turn off parity checking and serr during the physical reset */
3425         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3426         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3427                               (cfg_value &
3428                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3429
3430         spin_lock_irq(&phba->hbalock);
3431         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3432         phba->fcf.fcf_flag = 0;
3433         /* Clean up the child queue list for the CQs */
3434         list_del_init(&phba->sli4_hba.mbx_wq->list);
3435         list_del_init(&phba->sli4_hba.els_wq->list);
3436         list_del_init(&phba->sli4_hba.hdr_rq->list);
3437         list_del_init(&phba->sli4_hba.dat_rq->list);
3438         list_del_init(&phba->sli4_hba.mbx_cq->list);
3439         list_del_init(&phba->sli4_hba.els_cq->list);
3440         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3441                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3442         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3443                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3444         spin_unlock_irq(&phba->hbalock);
3445
3446         /* Now physically reset the device */
3447         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3448                         "0389 Performing PCI function reset!\n");
3449         /* Perform FCoE PCI function reset */
3450         lpfc_pci_function_reset(phba);
3451
3452         return 0;
3453 }
3454
3455 /**
3456  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3457  * @phba: Pointer to HBA context object.
3458  *
3459  * This function is called in the SLI initialization code path to
3460  * restart the HBA. The caller is not required to hold any lock.
3461  * This function writes MBX_RESTART mailbox command to the SLIM and
3462  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3463  * function to free any pending commands. The function enables
3464  * POST only during the first initialization. The function returns zero.
3465  * The function does not guarantee completion of MBX_RESTART mailbox
3466  * command before the return of this function.
3467  **/
3468 static int
3469 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3470 {
3471         MAILBOX_t *mb;
3472         struct lpfc_sli *psli;
3473         volatile uint32_t word0;
3474         void __iomem *to_slim;
3475         uint32_t hba_aer_enabled;
3476
3477         spin_lock_irq(&phba->hbalock);
3478
3479         /* Take PCIe device Advanced Error Reporting (AER) state */
3480         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3481
3482         psli = &phba->sli;
3483
3484         /* Restart HBA */
3485         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3486                         "0337 Restart HBA Data: x%x x%x\n",
3487                         phba->pport->port_state, psli->sli_flag);
3488
3489         word0 = 0;
3490         mb = (MAILBOX_t *) &word0;
3491         mb->mbxCommand = MBX_RESTART;
3492         mb->mbxHc = 1;
3493
3494         lpfc_reset_barrier(phba);
3495
3496         to_slim = phba->MBslimaddr;
3497         writel(*(uint32_t *) mb, to_slim);
3498         readl(to_slim); /* flush */
3499
3500         /* Only skip post after fc_ffinit is completed */
3501         if (phba->pport->port_state)
3502                 word0 = 1;      /* This is really setting up word1 */
3503         else
3504                 word0 = 0;      /* This is really setting up word1 */
3505         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3506         writel(*(uint32_t *) mb, to_slim);
3507         readl(to_slim); /* flush */
3508
3509         lpfc_sli_brdreset(phba);
3510         phba->pport->stopped = 0;
3511         phba->link_state = LPFC_INIT_START;
3512         phba->hba_flag = 0;
3513         spin_unlock_irq(&phba->hbalock);
3514
3515         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3516         psli->stats_start = get_seconds();
3517
3518         /* Give the INITFF and Post time to settle. */
3519         mdelay(100);
3520
3521         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3522         if (hba_aer_enabled)
3523                 pci_disable_pcie_error_reporting(phba->pcidev);
3524
3525         lpfc_hba_down_post(phba);
3526
3527         return 0;
3528 }
3529
3530 /**
3531  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3532  * @phba: Pointer to HBA context object.
3533  *
3534  * This function is called in the SLI initialization code path to restart
3535  * a SLI4 HBA. The caller is not required to hold any lock.
3536  * At the end of the function, it calls lpfc_hba_down_post function to
3537  * free any pending commands.
3538  **/
3539 static int
3540 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3541 {
3542         struct lpfc_sli *psli = &phba->sli;
3543
3544
3545         /* Restart HBA */
3546         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3547                         "0296 Restart HBA Data: x%x x%x\n",
3548                         phba->pport->port_state, psli->sli_flag);
3549
3550         lpfc_sli4_brdreset(phba);
3551
3552         spin_lock_irq(&phba->hbalock);
3553         phba->pport->stopped = 0;
3554         phba->link_state = LPFC_INIT_START;
3555         phba->hba_flag = 0;
3556         spin_unlock_irq(&phba->hbalock);
3557
3558         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3559         psli->stats_start = get_seconds();
3560
3561         lpfc_hba_down_post(phba);
3562
3563         return 0;
3564 }
3565
3566 /**
3567  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3568  * @phba: Pointer to HBA context object.
3569  *
3570  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3571  * API jump table function pointer from the lpfc_hba struct.
3572 **/
3573 int
3574 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3575 {
3576         return phba->lpfc_sli_brdrestart(phba);
3577 }
3578
3579 /**
3580  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3581  * @phba: Pointer to HBA context object.
3582  *
3583  * This function is called after a HBA restart to wait for successful
3584  * restart of the HBA. Successful restart of the HBA is indicated by
3585  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3586  * iteration, the function will restart the HBA again. The function returns
3587  * zero if HBA successfully restarted else returns negative error code.
3588  **/
3589 static int
3590 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3591 {
3592         uint32_t status, i = 0;
3593
3594         /* Read the HBA Host Status Register */
3595         status = readl(phba->HSregaddr);
3596
3597         /* Check status register to see what current state is */
3598         i = 0;
3599         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3600
3601                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3602                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
3603                  * 4.
3604                  */
3605                 if (i++ >= 20) {
3606                         /* Adapter failed to init, timeout, status reg
3607                            <status> */
3608                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3609                                         "0436 Adapter failed to init, "
3610                                         "timeout, status reg x%x, "
3611                                         "FW Data: A8 x%x AC x%x\n", status,
3612                                         readl(phba->MBslimaddr + 0xa8),
3613                                         readl(phba->MBslimaddr + 0xac));
3614                         phba->link_state = LPFC_HBA_ERROR;
3615                         return -ETIMEDOUT;
3616                 }
3617
3618                 /* Check to see if any errors occurred during init */
3619                 if (status & HS_FFERM) {
3620                         /* ERROR: During chipset initialization */
3621                         /* Adapter failed to init, chipset, status reg
3622                            <status> */
3623                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3624                                         "0437 Adapter failed to init, "
3625                                         "chipset, status reg x%x, "
3626                                         "FW Data: A8 x%x AC x%x\n", status,
3627                                         readl(phba->MBslimaddr + 0xa8),
3628                                         readl(phba->MBslimaddr + 0xac));
3629                         phba->link_state = LPFC_HBA_ERROR;
3630                         return -EIO;
3631                 }
3632
3633                 if (i <= 5) {
3634                         msleep(10);
3635                 } else if (i <= 10) {
3636                         msleep(500);
3637                 } else {
3638                         msleep(2500);
3639                 }
3640
3641                 if (i == 15) {
3642                                 /* Do post */
3643                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3644                         lpfc_sli_brdrestart(phba);
3645                 }
3646                 /* Read the HBA Host Status Register */
3647                 status = readl(phba->HSregaddr);
3648         }
3649
3650         /* Check to see if any errors occurred during init */
3651         if (status & HS_FFERM) {
3652                 /* ERROR: During chipset initialization */
3653                 /* Adapter failed to init, chipset, status reg <status> */
3654                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3655                                 "0438 Adapter failed to init, chipset, "
3656                                 "status reg x%x, "
3657                                 "FW Data: A8 x%x AC x%x\n", status,
3658                                 readl(phba->MBslimaddr + 0xa8),
3659                                 readl(phba->MBslimaddr + 0xac));
3660                 phba->link_state = LPFC_HBA_ERROR;
3661                 return -EIO;
3662         }
3663
3664         /* Clear all interrupt enable conditions */
3665         writel(0, phba->HCregaddr);
3666         readl(phba->HCregaddr); /* flush */
3667
3668         /* setup host attn register */
3669         writel(0xffffffff, phba->HAregaddr);
3670         readl(phba->HAregaddr); /* flush */
3671         return 0;
3672 }
3673
3674 /**
3675  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3676  *
3677  * This function calculates and returns the number of HBQs required to be
3678  * configured.
3679  **/
3680 int
3681 lpfc_sli_hbq_count(void)
3682 {
3683         return ARRAY_SIZE(lpfc_hbq_defs);
3684 }
3685
3686 /**
3687  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3688  *
3689  * This function adds the number of hbq entries in every HBQ to get
3690  * the total number of hbq entries required for the HBA and returns
3691  * the total count.
3692  **/
3693 static int
3694 lpfc_sli_hbq_entry_count(void)
3695 {
3696         int  hbq_count = lpfc_sli_hbq_count();
3697         int  count = 0;
3698         int  i;
3699
3700         for (i = 0; i < hbq_count; ++i)
3701                 count += lpfc_hbq_defs[i]->entry_count;
3702         return count;
3703 }
3704
3705 /**
3706  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3707  *
3708  * This function calculates amount of memory required for all hbq entries
3709  * to be configured and returns the total memory required.
3710  **/
3711 int
3712 lpfc_sli_hbq_size(void)
3713 {
3714         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3715 }
3716
3717 /**
3718  * lpfc_sli_hbq_setup - configure and initialize HBQs
3719  * @phba: Pointer to HBA context object.
3720  *
3721  * This function is called during the SLI initialization to configure
3722  * all the HBQs and post buffers to the HBQ. The caller is not
3723  * required to hold any locks. This function will return zero if successful
3724  * else it will return negative error code.
3725  **/
3726 static int
3727 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3728 {
3729         int  hbq_count = lpfc_sli_hbq_count();
3730         LPFC_MBOXQ_t *pmb;
3731         MAILBOX_t *pmbox;
3732         uint32_t hbqno;
3733         uint32_t hbq_entry_index;
3734
3735                                 /* Get a Mailbox buffer to setup mailbox
3736                                  * commands for HBA initialization
3737                                  */
3738         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3739
3740         if (!pmb)
3741                 return -ENOMEM;
3742
3743         pmbox = &pmb->u.mb;
3744
3745         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3746         phba->link_state = LPFC_INIT_MBX_CMDS;
3747         phba->hbq_in_use = 1;
3748
3749         hbq_entry_index = 0;
3750         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3751                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3752                 phba->hbqs[hbqno].hbqPutIdx      = 0;
3753                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3754                 phba->hbqs[hbqno].entry_count =
3755                         lpfc_hbq_defs[hbqno]->entry_count;
3756                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3757                         hbq_entry_index, pmb);
3758                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3759
3760                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3761                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3762                            mbxStatus <status>, ring <num> */
3763
3764                         lpfc_printf_log(phba, KERN_ERR,
3765                                         LOG_SLI | LOG_VPORT,
3766                                         "1805 Adapter failed to init. "
3767                                         "Data: x%x x%x x%x\n",
3768                                         pmbox->mbxCommand,
3769                                         pmbox->mbxStatus, hbqno);
3770
3771                         phba->link_state = LPFC_HBA_ERROR;
3772                         mempool_free(pmb, phba->mbox_mem_pool);
3773                         return ENXIO;
3774                 }
3775         }
3776         phba->hbq_count = hbq_count;
3777
3778         mempool_free(pmb, phba->mbox_mem_pool);
3779
3780         /* Initially populate or replenish the HBQs */
3781         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3782                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3783         return 0;
3784 }
3785
3786 /**
3787  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3788  * @phba: Pointer to HBA context object.
3789  *
3790  * This function is called during the SLI initialization to configure
3791  * all the HBQs and post buffers to the HBQ. The caller is not
3792  * required to hold any locks. This function will return zero if successful
3793  * else it will return negative error code.
3794  **/
3795 static int
3796 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3797 {
3798         phba->hbq_in_use = 1;
3799         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3800         phba->hbq_count = 1;
3801         /* Initially populate or replenish the HBQs */
3802         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3803         return 0;
3804 }
3805
3806 /**
3807  * lpfc_sli_config_port - Issue config port mailbox command
3808  * @phba: Pointer to HBA context object.
3809  * @sli_mode: sli mode - 2/3
3810  *
3811  * This function is called by the sli intialization code path
3812  * to issue config_port mailbox command. This function restarts the
3813  * HBA firmware and issues a config_port mailbox command to configure
3814  * the SLI interface in the sli mode specified by sli_mode
3815  * variable. The caller is not required to hold any locks.
3816  * The function returns 0 if successful, else returns negative error
3817  * code.
3818  **/
3819 int
3820 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3821 {
3822         LPFC_MBOXQ_t *pmb;
3823         uint32_t resetcount = 0, rc = 0, done = 0;
3824
3825         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3826         if (!pmb) {
3827                 phba->link_state = LPFC_HBA_ERROR;
3828                 return -ENOMEM;
3829         }
3830
3831         phba->sli_rev = sli_mode;
3832         while (resetcount < 2 && !done) {
3833                 spin_lock_irq(&phba->hbalock);
3834                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3835                 spin_unlock_irq(&phba->hbalock);
3836                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3837                 lpfc_sli_brdrestart(phba);
3838                 rc = lpfc_sli_chipset_init(phba);
3839                 if (rc)
3840                         break;
3841
3842                 spin_lock_irq(&phba->hbalock);
3843                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3844                 spin_unlock_irq(&phba->hbalock);
3845                 resetcount++;
3846
3847                 /* Call pre CONFIG_PORT mailbox command initialization.  A
3848                  * value of 0 means the call was successful.  Any other
3849                  * nonzero value is a failure, but if ERESTART is returned,
3850                  * the driver may reset the HBA and try again.
3851                  */
3852                 rc = lpfc_config_port_prep(phba);
3853                 if (rc == -ERESTART) {
3854                         phba->link_state = LPFC_LINK_UNKNOWN;
3855                         continue;
3856                 } else if (rc)
3857                         break;
3858                 phba->link_state = LPFC_INIT_MBX_CMDS;
3859                 lpfc_config_port(phba, pmb);
3860                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3861                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3862                                         LPFC_SLI3_HBQ_ENABLED |
3863                                         LPFC_SLI3_CRP_ENABLED |
3864                                         LPFC_SLI3_INB_ENABLED |
3865                                         LPFC_SLI3_BG_ENABLED);
3866                 if (rc != MBX_SUCCESS) {
3867                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3868                                 "0442 Adapter failed to init, mbxCmd x%x "
3869                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3870                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
3871                         spin_lock_irq(&phba->hbalock);
3872                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
3873                         spin_unlock_irq(&phba->hbalock);
3874                         rc = -ENXIO;
3875                 } else {
3876                         /* Allow asynchronous mailbox command to go through */
3877                         spin_lock_irq(&phba->hbalock);
3878                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3879                         spin_unlock_irq(&phba->hbalock);
3880                         done = 1;
3881                 }
3882         }
3883         if (!done) {
3884                 rc = -EINVAL;
3885                 goto do_prep_failed;
3886         }
3887         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3888                 if (!pmb->u.mb.un.varCfgPort.cMA) {
3889                         rc = -ENXIO;
3890                         goto do_prep_failed;
3891                 }
3892                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
3893                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3894                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3895                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3896                                 phba->max_vpi : phba->max_vports;
3897
3898                 } else
3899                         phba->max_vpi = 0;
3900                 if (pmb->u.mb.un.varCfgPort.gdss)
3901                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3902                 if (pmb->u.mb.un.varCfgPort.gerbm)
3903                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3904                 if (pmb->u.mb.un.varCfgPort.gcrp)
3905                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3906                 if (pmb->u.mb.un.varCfgPort.ginb) {
3907                         phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3908                         phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
3909                         phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3910                         phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3911                         phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3912                         phba->inb_last_counter =
3913                                         phba->mbox->us.s3_inb_pgp.counter;
3914                 } else {
3915                         phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3916                         phba->port_gp = phba->mbox->us.s3_pgp.port;
3917                         phba->inb_ha_copy = NULL;
3918                         phba->inb_counter = NULL;
3919                 }
3920
3921                 if (phba->cfg_enable_bg) {
3922                         if (pmb->u.mb.un.varCfgPort.gbg)
3923                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3924                         else
3925                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3926                                                 "0443 Adapter did not grant "
3927                                                 "BlockGuard\n");
3928                 }
3929         } else {
3930                 phba->hbq_get = NULL;
3931                 phba->port_gp = phba->mbox->us.s2.port;
3932                 phba->inb_ha_copy = NULL;
3933                 phba->inb_counter = NULL;
3934                 phba->max_vpi = 0;
3935         }
3936 do_prep_failed:
3937         mempool_free(pmb, phba->mbox_mem_pool);
3938         return rc;
3939 }
3940
3941
3942 /**
3943  * lpfc_sli_hba_setup - SLI intialization function
3944  * @phba: Pointer to HBA context object.
3945  *
3946  * This function is the main SLI intialization function. This function
3947  * is called by the HBA intialization code, HBA reset code and HBA
3948  * error attention handler code. Caller is not required to hold any
3949  * locks. This function issues config_port mailbox command to configure
3950  * the SLI, setup iocb rings and HBQ rings. In the end the function
3951  * calls the config_port_post function to issue init_link mailbox
3952  * command and to start the discovery. The function will return zero
3953  * if successful, else it will return negative error code.
3954  **/
3955 int
3956 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3957 {
3958         uint32_t rc;
3959         int  mode = 3;
3960
3961         switch (lpfc_sli_mode) {
3962         case 2:
3963                 if (phba->cfg_enable_npiv) {
3964                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3965                                 "1824 NPIV enabled: Override lpfc_sli_mode "
3966                                 "parameter (%d) to auto (0).\n",
3967                                 lpfc_sli_mode);
3968                         break;
3969                 }
3970                 mode = 2;
3971                 break;
3972         case 0:
3973         case 3:
3974                 break;
3975         default:
3976                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3977                                 "1819 Unrecognized lpfc_sli_mode "
3978                                 "parameter: %d.\n", lpfc_sli_mode);
3979
3980                 break;
3981         }
3982
3983         rc = lpfc_sli_config_port(phba, mode);
3984
3985         if (rc && lpfc_sli_mode == 3)
3986                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3987                                 "1820 Unable to select SLI-3.  "
3988                                 "Not supported by adapter.\n");
3989         if (rc && mode != 2)
3990                 rc = lpfc_sli_config_port(phba, 2);
3991         if (rc)
3992                 goto lpfc_sli_hba_setup_error;
3993
3994         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
3995         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
3996                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
3997                 if (!rc) {
3998                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3999                                         "2709 This device supports "
4000                                         "Advanced Error Reporting (AER)\n");
4001                         spin_lock_irq(&phba->hbalock);
4002                         phba->hba_flag |= HBA_AER_ENABLED;
4003                         spin_unlock_irq(&phba->hbalock);
4004                 } else {
4005                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4006                                         "2708 This device does not support "
4007                                         "Advanced Error Reporting (AER)\n");
4008                         phba->cfg_aer_support = 0;
4009                 }
4010         }
4011
4012         if (phba->sli_rev == 3) {
4013                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4014                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4015         } else {
4016                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4017                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4018                 phba->sli3_options = 0;
4019         }
4020
4021         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4022                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4023                         phba->sli_rev, phba->max_vpi);
4024         rc = lpfc_sli_ring_map(phba);
4025
4026         if (rc)
4027                 goto lpfc_sli_hba_setup_error;
4028
4029         /* Init HBQs */
4030         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4031                 rc = lpfc_sli_hbq_setup(phba);
4032                 if (rc)
4033                         goto lpfc_sli_hba_setup_error;
4034         }
4035         spin_lock_irq(&phba->hbalock);
4036         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4037         spin_unlock_irq(&phba->hbalock);
4038
4039         rc = lpfc_config_port_post(phba);
4040         if (rc)
4041                 goto lpfc_sli_hba_setup_error;
4042
4043         return rc;
4044
4045 lpfc_sli_hba_setup_error:
4046         phba->link_state = LPFC_HBA_ERROR;
4047         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4048                         "0445 Firmware initialization failed\n");
4049         return rc;
4050 }
4051
4052 /**
4053  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4054  * @phba: Pointer to HBA context object.
4055  * @mboxq: mailbox pointer.
4056  * This function issue a dump mailbox command to read config region
4057  * 23 and parse the records in the region and populate driver
4058  * data structure.
4059  **/
4060 static int
4061 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4062                 LPFC_MBOXQ_t *mboxq)
4063 {
4064         struct lpfc_dmabuf *mp;
4065         struct lpfc_mqe *mqe;
4066         uint32_t data_length;
4067         int rc;
4068
4069         /* Program the default value of vlan_id and fc_map */
4070         phba->valid_vlan = 0;
4071         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4072         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4073         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4074
4075         mqe = &mboxq->u.mqe;
4076         if (lpfc_dump_fcoe_param(phba, mboxq))
4077                 return -ENOMEM;
4078
4079         mp = (struct lpfc_dmabuf *) mboxq->context1;
4080         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4081
4082         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4083                         "(%d):2571 Mailbox cmd x%x Status x%x "
4084                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4085                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4086                         "CQ: x%x x%x x%x x%x\n",
4087                         mboxq->vport ? mboxq->vport->vpi : 0,
4088                         bf_get(lpfc_mqe_command, mqe),
4089                         bf_get(lpfc_mqe_status, mqe),
4090                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4091                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4092                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4093                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4094                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4095                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4096                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4097                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4098                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4099                         mboxq->mcqe.word0,
4100                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4101                         mboxq->mcqe.trailer);
4102
4103         if (rc) {
4104                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4105                 kfree(mp);
4106                 return -EIO;
4107         }
4108         data_length = mqe->un.mb_words[5];
4109         if (data_length > DMP_RGN23_SIZE) {
4110                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4111                 kfree(mp);
4112                 return -EIO;
4113         }
4114
4115         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4116         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4117         kfree(mp);
4118         return 0;
4119 }
4120
4121 /**
4122  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4123  * @phba: pointer to lpfc hba data structure.
4124  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4125  * @vpd: pointer to the memory to hold resulting port vpd data.
4126  * @vpd_size: On input, the number of bytes allocated to @vpd.
4127  *            On output, the number of data bytes in @vpd.
4128  *
4129  * This routine executes a READ_REV SLI4 mailbox command.  In
4130  * addition, this routine gets the port vpd data.
4131  *
4132  * Return codes
4133  *      0 - successful
4134  *      ENOMEM - could not allocated memory.
4135  **/
4136 static int
4137 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4138                     uint8_t *vpd, uint32_t *vpd_size)
4139 {
4140         int rc = 0;
4141         uint32_t dma_size;
4142         struct lpfc_dmabuf *dmabuf;
4143         struct lpfc_mqe *mqe;
4144
4145         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4146         if (!dmabuf)
4147                 return -ENOMEM;
4148
4149         /*
4150          * Get a DMA buffer for the vpd data resulting from the READ_REV
4151          * mailbox command.
4152          */
4153         dma_size = *vpd_size;
4154         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4155                                           dma_size,
4156                                           &dmabuf->phys,
4157                                           GFP_KERNEL);
4158         if (!dmabuf->virt) {
4159                 kfree(dmabuf);
4160                 return -ENOMEM;
4161         }
4162         memset(dmabuf->virt, 0, dma_size);
4163
4164         /*
4165          * The SLI4 implementation of READ_REV conflicts at word1,
4166          * bits 31:16 and SLI4 adds vpd functionality not present
4167          * in SLI3.  This code corrects the conflicts.
4168          */
4169         lpfc_read_rev(phba, mboxq);
4170         mqe = &mboxq->u.mqe;
4171         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4172         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4173         mqe->un.read_rev.word1 &= 0x0000FFFF;
4174         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4175         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4176
4177         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4178         if (rc) {
4179                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4180                                   dmabuf->virt, dmabuf->phys);
4181                 kfree(dmabuf);
4182                 return -EIO;
4183         }
4184
4185         /*
4186          * The available vpd length cannot be bigger than the
4187          * DMA buffer passed to the port.  Catch the less than
4188          * case and update the caller's size.
4189          */
4190         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4191                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4192
4193         lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4194         dma_free_coherent(&phba->pcidev->dev, dma_size,
4195                           dmabuf->virt, dmabuf->phys);
4196         kfree(dmabuf);
4197         return 0;
4198 }
4199
4200 /**
4201  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4202  * @phba: pointer to lpfc hba data structure.
4203  *
4204  * This routine is called to explicitly arm the SLI4 device's completion and
4205  * event queues
4206  **/
4207 static void
4208 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4209 {
4210         uint8_t fcp_eqidx;
4211
4212         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4213         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4214         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4215                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4216                                      LPFC_QUEUE_REARM);
4217         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4218         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4219                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4220                                      LPFC_QUEUE_REARM);
4221 }
4222
4223 /**
4224  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4225  * @phba: Pointer to HBA context object.
4226  *
4227  * This function is the main SLI4 device intialization PCI function. This
4228  * function is called by the HBA intialization code, HBA reset code and
4229  * HBA error attention handler code. Caller is not required to hold any
4230  * locks.
4231  **/
4232 int
4233 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4234 {
4235         int rc;
4236         LPFC_MBOXQ_t *mboxq;
4237         struct lpfc_mqe *mqe;
4238         uint8_t *vpd;
4239         uint32_t vpd_size;
4240         uint32_t ftr_rsp = 0;
4241         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4242         struct lpfc_vport *vport = phba->pport;
4243         struct lpfc_dmabuf *mp;
4244
4245         /* Perform a PCI function reset to start from clean */
4246         rc = lpfc_pci_function_reset(phba);
4247         if (unlikely(rc))
4248                 return -ENODEV;
4249
4250         /* Check the HBA Host Status Register for readyness */
4251         rc = lpfc_sli4_post_status_check(phba);
4252         if (unlikely(rc))
4253                 return -ENODEV;
4254         else {
4255                 spin_lock_irq(&phba->hbalock);
4256                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4257                 spin_unlock_irq(&phba->hbalock);
4258         }
4259
4260         /*
4261          * Allocate a single mailbox container for initializing the
4262          * port.
4263          */
4264         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4265         if (!mboxq)
4266                 return -ENOMEM;
4267
4268         /*
4269          * Continue initialization with default values even if driver failed
4270          * to read FCoE param config regions
4271          */
4272         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4273                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4274                         "2570 Failed to read FCoE parameters\n");
4275
4276         /* Issue READ_REV to collect vpd and FW information. */
4277         vpd_size = PAGE_SIZE;
4278         vpd = kzalloc(vpd_size, GFP_KERNEL);
4279         if (!vpd) {
4280                 rc = -ENOMEM;
4281                 goto out_free_mbox;
4282         }
4283
4284         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4285         if (unlikely(rc))
4286                 goto out_free_vpd;
4287
4288         mqe = &mboxq->u.mqe;
4289         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4290         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4291                 phba->hba_flag |= HBA_FCOE_SUPPORT;
4292
4293         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4294                 LPFC_DCBX_CEE_MODE)
4295                 phba->hba_flag |= HBA_FIP_SUPPORT;
4296         else
4297                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4298
4299         if (phba->sli_rev != LPFC_SLI_REV4 ||
4300             !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
4301                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4302                         "0376 READ_REV Error. SLI Level %d "
4303                         "FCoE enabled %d\n",
4304                         phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
4305                 rc = -EIO;
4306                 goto out_free_vpd;
4307         }
4308         /*
4309          * Evaluate the read rev and vpd data. Populate the driver
4310          * state with the results. If this routine fails, the failure
4311          * is not fatal as the driver will use generic values.
4312          */
4313         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4314         if (unlikely(!rc)) {
4315                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4316                                 "0377 Error %d parsing vpd. "
4317                                 "Using defaults.\n", rc);
4318                 rc = 0;
4319         }
4320
4321         /* Save information as VPD data */
4322         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4323         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4324         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4325         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4326                                          &mqe->un.read_rev);
4327         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4328                                        &mqe->un.read_rev);
4329         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4330                                             &mqe->un.read_rev);
4331         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4332                                            &mqe->un.read_rev);
4333         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4334         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4335         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4336         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4337         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4338         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4339         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4340                         "(%d):0380 READ_REV Status x%x "
4341                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4342                         mboxq->vport ? mboxq->vport->vpi : 0,
4343                         bf_get(lpfc_mqe_status, mqe),
4344                         phba->vpd.rev.opFwName,
4345                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4346                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4347
4348         /*
4349          * Discover the port's supported feature set and match it against the
4350          * hosts requests.
4351          */
4352         lpfc_request_features(phba, mboxq);
4353         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4354         if (unlikely(rc)) {
4355                 rc = -EIO;
4356                 goto out_free_vpd;
4357         }
4358
4359         /*
4360          * The port must support FCP initiator mode as this is the
4361          * only mode running in the host.
4362          */
4363         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4364                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4365                                 "0378 No support for fcpi mode.\n");
4366                 ftr_rsp++;
4367         }
4368
4369         /*
4370          * If the port cannot support the host's requested features
4371          * then turn off the global config parameters to disable the
4372          * feature in the driver.  This is not a fatal error.
4373          */
4374         if ((phba->cfg_enable_bg) &&
4375             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4376                 ftr_rsp++;
4377
4378         if (phba->max_vpi && phba->cfg_enable_npiv &&
4379             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4380                 ftr_rsp++;
4381
4382         if (ftr_rsp) {
4383                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4384                                 "0379 Feature Mismatch Data: x%08x %08x "
4385                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4386                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4387                                 phba->cfg_enable_npiv, phba->max_vpi);
4388                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4389                         phba->cfg_enable_bg = 0;
4390                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4391                         phba->cfg_enable_npiv = 0;
4392         }
4393
4394         /* These SLI3 features are assumed in SLI4 */
4395         spin_lock_irq(&phba->hbalock);
4396         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4397         spin_unlock_irq(&phba->hbalock);
4398
4399         /* Read the port's service parameters. */
4400         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4401         if (rc) {
4402                 phba->link_state = LPFC_HBA_ERROR;
4403                 rc = -ENOMEM;
4404                 goto out_free_vpd;
4405         }
4406
4407         mboxq->vport = vport;
4408         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4409         mp = (struct lpfc_dmabuf *) mboxq->context1;
4410         if (rc == MBX_SUCCESS) {
4411                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4412                 rc = 0;
4413         }
4414
4415         /*
4416          * This memory was allocated by the lpfc_read_sparam routine. Release
4417          * it to the mbuf pool.
4418          */
4419         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4420         kfree(mp);
4421         mboxq->context1 = NULL;
4422         if (unlikely(rc)) {
4423                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4424                                 "0382 READ_SPARAM command failed "
4425                                 "status %d, mbxStatus x%x\n",
4426                                 rc, bf_get(lpfc_mqe_status, mqe));
4427                 phba->link_state = LPFC_HBA_ERROR;
4428                 rc = -EIO;
4429                 goto out_free_vpd;
4430         }
4431
4432         if (phba->cfg_soft_wwnn)
4433                 u64_to_wwn(phba->cfg_soft_wwnn,
4434                            vport->fc_sparam.nodeName.u.wwn);
4435         if (phba->cfg_soft_wwpn)
4436                 u64_to_wwn(phba->cfg_soft_wwpn,
4437                            vport->fc_sparam.portName.u.wwn);
4438         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4439                sizeof(struct lpfc_name));
4440         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4441                sizeof(struct lpfc_name));
4442
4443         /* Update the fc_host data structures with new wwn. */
4444         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4445         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4446
4447         /* Register SGL pool to the device using non-embedded mailbox command */
4448         rc = lpfc_sli4_post_sgl_list(phba);
4449         if (unlikely(rc)) {
4450                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4451                                 "0582 Error %d during sgl post operation\n",
4452                                         rc);
4453                 rc = -ENODEV;
4454                 goto out_free_vpd;
4455         }
4456
4457         /* Register SCSI SGL pool to the device */
4458         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4459         if (unlikely(rc)) {
4460                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4461                                 "0383 Error %d during scsi sgl post "
4462                                 "operation\n", rc);
4463                 /* Some Scsi buffers were moved to the abort scsi list */
4464                 /* A pci function reset will repost them */
4465                 rc = -ENODEV;
4466                 goto out_free_vpd;
4467         }
4468
4469         /* Post the rpi header region to the device. */
4470         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4471         if (unlikely(rc)) {
4472                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4473                                 "0393 Error %d during rpi post operation\n",
4474                                 rc);
4475                 rc = -ENODEV;
4476                 goto out_free_vpd;
4477         }
4478
4479         /* Set up all the queues to the device */
4480         rc = lpfc_sli4_queue_setup(phba);
4481         if (unlikely(rc)) {
4482                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4483                                 "0381 Error %d during queue setup.\n ", rc);
4484                 goto out_stop_timers;
4485         }
4486
4487         /* Arm the CQs and then EQs on device */
4488         lpfc_sli4_arm_cqeq_intr(phba);
4489
4490         /* Indicate device interrupt mode */
4491         phba->sli4_hba.intr_enable = 1;
4492
4493         /* Allow asynchronous mailbox command to go through */
4494         spin_lock_irq(&phba->hbalock);
4495         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4496         spin_unlock_irq(&phba->hbalock);
4497
4498         /* Post receive buffers to the device */
4499         lpfc_sli4_rb_setup(phba);
4500
4501         /* Start the ELS watchdog timer */
4502         mod_timer(&vport->els_tmofunc,
4503                   jiffies + HZ * (phba->fc_ratov * 2));
4504
4505         /* Start heart beat timer */
4506         mod_timer(&phba->hb_tmofunc,
4507                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4508         phba->hb_outstanding = 0;
4509         phba->last_completion_time = jiffies;
4510
4511         /* Start error attention (ERATT) polling timer */
4512         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4513
4514         /*
4515          * The port is ready, set the host's link state to LINK_DOWN
4516          * in preparation for link interrupts.
4517          */
4518         lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4519         mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4520         lpfc_set_loopback_flag(phba);
4521         /* Change driver state to LPFC_LINK_DOWN right before init link */
4522         spin_lock_irq(&phba->hbalock);
4523         phba->link_state = LPFC_LINK_DOWN;
4524         spin_unlock_irq(&phba->hbalock);
4525         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4526         if (unlikely(rc != MBX_NOT_FINISHED)) {
4527                 kfree(vpd);
4528                 return 0;
4529         } else
4530                 rc = -EIO;
4531
4532         /* Unset all the queues set up in this routine when error out */
4533         if (rc)
4534                 lpfc_sli4_queue_unset(phba);
4535
4536 out_stop_timers:
4537         if (rc)
4538                 lpfc_stop_hba_timers(phba);
4539 out_free_vpd:
4540         kfree(vpd);
4541 out_free_mbox:
4542         mempool_free(mboxq, phba->mbox_mem_pool);
4543         return rc;
4544 }
4545
4546 /**
4547  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4548  * @ptr: context object - pointer to hba structure.
4549  *
4550  * This is the callback function for mailbox timer. The mailbox
4551  * timer is armed when a new mailbox command is issued and the timer
4552  * is deleted when the mailbox complete. The function is called by
4553  * the kernel timer code when a mailbox does not complete within
4554  * expected time. This function wakes up the worker thread to
4555  * process the mailbox timeout and returns. All the processing is
4556  * done by the worker thread function lpfc_mbox_timeout_handler.
4557  **/
4558 void
4559 lpfc_mbox_timeout(unsigned long ptr)
4560 {
4561         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4562         unsigned long iflag;
4563         uint32_t tmo_posted;
4564
4565         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4566         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4567         if (!tmo_posted)
4568                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4569         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4570
4571         if (!tmo_posted)
4572                 lpfc_worker_wake_up(phba);
4573         return;
4574 }
4575
4576
4577 /**
4578  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4579  * @phba: Pointer to HBA context object.
4580  *
4581  * This function is called from worker thread when a mailbox command times out.
4582  * The caller is not required to hold any locks. This function will reset the
4583  * HBA and recover all the pending commands.
4584  **/
4585 void
4586 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4587 {
4588         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4589         MAILBOX_t *mb = &pmbox->u.mb;
4590         struct lpfc_sli *psli = &phba->sli;
4591         struct lpfc_sli_ring *pring;
4592
4593         /* Check the pmbox pointer first.  There is a race condition
4594          * between the mbox timeout handler getting executed in the
4595          * worklist and the mailbox actually completing. When this
4596          * race condition occurs, the mbox_active will be NULL.
4597          */
4598         spin_lock_irq(&phba->hbalock);
4599         if (pmbox == NULL) {
4600                 lpfc_printf_log(phba, KERN_WARNING,
4601                                 LOG_MBOX | LOG_SLI,
4602                                 "0353 Active Mailbox cleared - mailbox timeout "
4603                                 "exiting\n");
4604                 spin_unlock_irq(&phba->hbalock);
4605                 return;
4606         }
4607
4608         /* Mbox cmd <mbxCommand> timeout */
4609         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4610                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4611                         mb->mbxCommand,
4612                         phba->pport->port_state,
4613                         phba->sli.sli_flag,
4614                         phba->sli.mbox_active);
4615         spin_unlock_irq(&phba->hbalock);
4616
4617         /* Setting state unknown so lpfc_sli_abort_iocb_ring
4618          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4619          * it to fail all oustanding SCSI IO.
4620          */
4621         spin_lock_irq(&phba->pport->work_port_lock);
4622         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4623         spin_unlock_irq(&phba->pport->work_port_lock);
4624         spin_lock_irq(&phba->hbalock);
4625         phba->link_state = LPFC_LINK_UNKNOWN;
4626         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4627         spin_unlock_irq(&phba->hbalock);
4628
4629         pring = &psli->ring[psli->fcp_ring];
4630         lpfc_sli_abort_iocb_ring(phba, pring);
4631
4632         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4633                         "0345 Resetting board due to mailbox timeout\n");
4634
4635         /* Reset the HBA device */
4636         lpfc_reset_hba(phba);
4637 }
4638
4639 /**
4640  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4641  * @phba: Pointer to HBA context object.
4642  * @pmbox: Pointer to mailbox object.
4643  * @flag: Flag indicating how the mailbox need to be processed.
4644  *
4645  * This function is called by discovery code and HBA management code
4646  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4647  * function gets the hbalock to protect the data structures.
4648  * The mailbox command can be submitted in polling mode, in which case
4649  * this function will wait in a polling loop for the completion of the
4650  * mailbox.
4651  * If the mailbox is submitted in no_wait mode (not polling) the
4652  * function will submit the command and returns immediately without waiting
4653  * for the mailbox completion. The no_wait is supported only when HBA
4654  * is in SLI2/SLI3 mode - interrupts are enabled.
4655  * The SLI interface allows only one mailbox pending at a time. If the
4656  * mailbox is issued in polling mode and there is already a mailbox
4657  * pending, then the function will return an error. If the mailbox is issued
4658  * in NO_WAIT mode and there is a mailbox pending already, the function
4659  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4660  * The sli layer owns the mailbox object until the completion of mailbox
4661  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4662  * return codes the caller owns the mailbox command after the return of
4663  * the function.
4664  **/
4665 static int
4666 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4667                        uint32_t flag)
4668 {
4669         MAILBOX_t *mb;
4670         struct lpfc_sli *psli = &phba->sli;
4671         uint32_t status, evtctr;
4672         uint32_t ha_copy;
4673         int i;
4674         unsigned long timeout;
4675         unsigned long drvr_flag = 0;
4676         uint32_t word0, ldata;
4677         void __iomem *to_slim;
4678         int processing_queue = 0;
4679
4680         spin_lock_irqsave(&phba->hbalock, drvr_flag);
4681         if (!pmbox) {
4682                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4683                 /* processing mbox queue from intr_handler */
4684                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4685                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4686                         return MBX_SUCCESS;
4687                 }
4688                 processing_queue = 1;
4689                 pmbox = lpfc_mbox_get(phba);
4690                 if (!pmbox) {
4691                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4692                         return MBX_SUCCESS;
4693                 }
4694         }
4695
4696         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4697                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4698                 if(!pmbox->vport) {
4699                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4700                         lpfc_printf_log(phba, KERN_ERR,
4701                                         LOG_MBOX | LOG_VPORT,
4702                                         "1806 Mbox x%x failed. No vport\n",
4703                                         pmbox->u.mb.mbxCommand);
4704                         dump_stack();
4705                         goto out_not_finished;
4706                 }
4707         }
4708
4709         /* If the PCI channel is in offline state, do not post mbox. */
4710         if (unlikely(pci_channel_offline(phba->pcidev))) {
4711                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4712                 goto out_not_finished;
4713         }
4714
4715         /* If HBA has a deferred error attention, fail the iocb. */
4716         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4717                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4718                 goto out_not_finished;
4719         }
4720
4721         psli = &phba->sli;
4722
4723         mb = &pmbox->u.mb;
4724         status = MBX_SUCCESS;
4725
4726         if (phba->link_state == LPFC_HBA_ERROR) {
4727                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4728
4729                 /* Mbox command <mbxCommand> cannot issue */
4730                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4731                                 "(%d):0311 Mailbox command x%x cannot "
4732                                 "issue Data: x%x x%x\n",
4733                                 pmbox->vport ? pmbox->vport->vpi : 0,
4734                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4735                 goto out_not_finished;
4736         }
4737
4738         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4739             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4740                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4741                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4742                                 "(%d):2528 Mailbox command x%x cannot "
4743                                 "issue Data: x%x x%x\n",
4744                                 pmbox->vport ? pmbox->vport->vpi : 0,
4745                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4746                 goto out_not_finished;
4747         }
4748
4749         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4750                 /* Polling for a mbox command when another one is already active
4751                  * is not allowed in SLI. Also, the driver must have established
4752                  * SLI2 mode to queue and process multiple mbox commands.
4753                  */
4754
4755                 if (flag & MBX_POLL) {
4756                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4757
4758                         /* Mbox command <mbxCommand> cannot issue */
4759                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4760                                         "(%d):2529 Mailbox command x%x "
4761                                         "cannot issue Data: x%x x%x\n",
4762                                         pmbox->vport ? pmbox->vport->vpi : 0,
4763                                         pmbox->u.mb.mbxCommand,
4764                                         psli->sli_flag, flag);
4765                         goto out_not_finished;
4766                 }
4767
4768                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4769                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4770                         /* Mbox command <mbxCommand> cannot issue */
4771                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4772                                         "(%d):2530 Mailbox command x%x "
4773                                         "cannot issue Data: x%x x%x\n",
4774                                         pmbox->vport ? pmbox->vport->vpi : 0,
4775                                         pmbox->u.mb.mbxCommand,
4776                                         psli->sli_flag, flag);
4777                         goto out_not_finished;
4778                 }
4779
4780                 /* Another mailbox command is still being processed, queue this
4781                  * command to be processed later.
4782                  */
4783                 lpfc_mbox_put(phba, pmbox);
4784
4785                 /* Mbox cmd issue - BUSY */
4786                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4787                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
4788                                 "x%x x%x x%x x%x\n",
4789                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4790                                 mb->mbxCommand, phba->pport->port_state,
4791                                 psli->sli_flag, flag);
4792
4793                 psli->slistat.mbox_busy++;
4794                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4795
4796                 if (pmbox->vport) {
4797                         lpfc_debugfs_disc_trc(pmbox->vport,
4798                                 LPFC_DISC_TRC_MBOX_VPORT,
4799                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4800                                 (uint32_t)mb->mbxCommand,
4801                                 mb->un.varWords[0], mb->un.varWords[1]);
4802                 }
4803                 else {
4804                         lpfc_debugfs_disc_trc(phba->pport,
4805                                 LPFC_DISC_TRC_MBOX,
4806                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
4807                                 (uint32_t)mb->mbxCommand,
4808                                 mb->un.varWords[0], mb->un.varWords[1]);
4809                 }
4810
4811                 return MBX_BUSY;
4812         }
4813
4814         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4815
4816         /* If we are not polling, we MUST be in SLI2 mode */
4817         if (flag != MBX_POLL) {
4818                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4819                     (mb->mbxCommand != MBX_KILL_BOARD)) {
4820                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4821                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4822                         /* Mbox command <mbxCommand> cannot issue */
4823                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4824                                         "(%d):2531 Mailbox command x%x "
4825                                         "cannot issue Data: x%x x%x\n",
4826                                         pmbox->vport ? pmbox->vport->vpi : 0,
4827                                         pmbox->u.mb.mbxCommand,
4828                                         psli->sli_flag, flag);
4829                         goto out_not_finished;
4830                 }
4831                 /* timeout active mbox command */
4832                 mod_timer(&psli->mbox_tmo, (jiffies +
4833                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4834         }
4835
4836         /* Mailbox cmd <cmd> issue */
4837         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4838                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4839                         "x%x\n",
4840                         pmbox->vport ? pmbox->vport->vpi : 0,
4841                         mb->mbxCommand, phba->pport->port_state,
4842                         psli->sli_flag, flag);
4843
4844         if (mb->mbxCommand != MBX_HEARTBEAT) {
4845                 if (pmbox->vport) {
4846                         lpfc_debugfs_disc_trc(pmbox->vport,
4847                                 LPFC_DISC_TRC_MBOX_VPORT,
4848                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4849                                 (uint32_t)mb->mbxCommand,
4850                                 mb->un.varWords[0], mb->un.varWords[1]);
4851                 }
4852                 else {
4853                         lpfc_debugfs_disc_trc(phba->pport,
4854                                 LPFC_DISC_TRC_MBOX,
4855                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
4856                                 (uint32_t)mb->mbxCommand,
4857                                 mb->un.varWords[0], mb->un.varWords[1]);
4858                 }
4859         }
4860
4861         psli->slistat.mbox_cmd++;
4862         evtctr = psli->slistat.mbox_event;
4863
4864         /* next set own bit for the adapter and copy over command word */
4865         mb->mbxOwner = OWN_CHIP;
4866
4867         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4868                 /* First copy command data to host SLIM area */
4869                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4870         } else {
4871                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4872                         /* copy command data into host mbox for cmpl */
4873                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4874                 }
4875
4876                 /* First copy mbox command data to HBA SLIM, skip past first
4877                    word */
4878                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4879                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4880                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
4881
4882                 /* Next copy over first word, with mbxOwner set */
4883                 ldata = *((uint32_t *)mb);
4884                 to_slim = phba->MBslimaddr;
4885                 writel(ldata, to_slim);
4886                 readl(to_slim); /* flush */
4887
4888                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4889                         /* switch over to host mailbox */
4890                         psli->sli_flag |= LPFC_SLI_ACTIVE;
4891                 }
4892         }
4893
4894         wmb();
4895
4896         switch (flag) {
4897         case MBX_NOWAIT:
4898                 /* Set up reference to mailbox command */
4899                 psli->mbox_active = pmbox;
4900                 /* Interrupt board to do it */
4901                 writel(CA_MBATT, phba->CAregaddr);
4902                 readl(phba->CAregaddr); /* flush */
4903                 /* Don't wait for it to finish, just return */
4904                 break;
4905
4906         case MBX_POLL:
4907                 /* Set up null reference to mailbox command */
4908                 psli->mbox_active = NULL;
4909                 /* Interrupt board to do it */
4910                 writel(CA_MBATT, phba->CAregaddr);
4911                 readl(phba->CAregaddr); /* flush */
4912
4913                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4914                         /* First read mbox status word */
4915                         word0 = *((uint32_t *)phba->mbox);
4916                         word0 = le32_to_cpu(word0);
4917                 } else {
4918                         /* First read mbox status word */
4919                         word0 = readl(phba->MBslimaddr);
4920                 }
4921
4922                 /* Read the HBA Host Attention Register */
4923                 ha_copy = readl(phba->HAregaddr);
4924                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4925                                                              mb->mbxCommand) *
4926                                            1000) + jiffies;
4927                 i = 0;
4928                 /* Wait for command to complete */
4929                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4930                        (!(ha_copy & HA_MBATT) &&
4931                         (phba->link_state > LPFC_WARM_START))) {
4932                         if (time_after(jiffies, timeout)) {
4933                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4934                                 spin_unlock_irqrestore(&phba->hbalock,
4935                                                        drvr_flag);
4936                                 goto out_not_finished;
4937                         }
4938
4939                         /* Check if we took a mbox interrupt while we were
4940                            polling */
4941                         if (((word0 & OWN_CHIP) != OWN_CHIP)
4942                             && (evtctr != psli->slistat.mbox_event))
4943                                 break;
4944
4945                         if (i++ > 10) {
4946                                 spin_unlock_irqrestore(&phba->hbalock,
4947                                                        drvr_flag);
4948                                 msleep(1);
4949                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4950                         }
4951
4952                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4953                                 /* First copy command data */
4954                                 word0 = *((uint32_t *)phba->mbox);
4955                                 word0 = le32_to_cpu(word0);
4956                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4957                                         MAILBOX_t *slimmb;
4958                                         uint32_t slimword0;
4959                                         /* Check real SLIM for any errors */
4960                                         slimword0 = readl(phba->MBslimaddr);
4961                                         slimmb = (MAILBOX_t *) & slimword0;
4962                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4963                                             && slimmb->mbxStatus) {
4964                                                 psli->sli_flag &=
4965                                                     ~LPFC_SLI_ACTIVE;
4966                                                 word0 = slimword0;
4967                                         }
4968                                 }
4969                         } else {
4970                                 /* First copy command data */
4971                                 word0 = readl(phba->MBslimaddr);
4972                         }
4973                         /* Read the HBA Host Attention Register */
4974                         ha_copy = readl(phba->HAregaddr);
4975                 }
4976
4977                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4978                         /* copy results back to user */
4979                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
4980                 } else {
4981                         /* First copy command data */
4982                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4983                                                         MAILBOX_CMD_SIZE);
4984                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4985                                 pmbox->context2) {
4986                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
4987                                       phba->MBslimaddr + DMP_RSP_OFFSET,
4988                                                       mb->un.varDmp.word_cnt);
4989                         }
4990                 }
4991
4992                 writel(HA_MBATT, phba->HAregaddr);
4993                 readl(phba->HAregaddr); /* flush */
4994
4995                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4996                 status = mb->mbxStatus;
4997         }
4998
4999         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5000         return status;
5001
5002 out_not_finished:
5003         if (processing_queue) {
5004                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5005                 lpfc_mbox_cmpl_put(phba, pmbox);
5006         }
5007         return MBX_NOT_FINISHED;
5008 }
5009
5010 /**
5011  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5012  * @phba: Pointer to HBA context object.
5013  *
5014  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5015  * the driver internal pending mailbox queue. It will then try to wait out the
5016  * possible outstanding mailbox command before return.
5017  *
5018  * Returns:
5019  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5020  *      the outstanding mailbox command timed out.
5021  **/
5022 static int
5023 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5024 {
5025         struct lpfc_sli *psli = &phba->sli;
5026         uint8_t actcmd = MBX_HEARTBEAT;
5027         int rc = 0;
5028         unsigned long timeout;
5029
5030         /* Mark the asynchronous mailbox command posting as blocked */
5031         spin_lock_irq(&phba->hbalock);
5032         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5033         if (phba->sli.mbox_active)
5034                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5035         spin_unlock_irq(&phba->hbalock);
5036         /* Determine how long we might wait for the active mailbox
5037          * command to be gracefully completed by firmware.
5038          */
5039         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5040                                    jiffies;
5041         /* Wait for the outstnading mailbox command to complete */
5042         while (phba->sli.mbox_active) {
5043                 /* Check active mailbox complete status every 2ms */
5044                 msleep(2);
5045                 if (time_after(jiffies, timeout)) {
5046                         /* Timeout, marked the outstanding cmd not complete */
5047                         rc = 1;
5048                         break;
5049                 }
5050         }
5051
5052         /* Can not cleanly block async mailbox command, fails it */
5053         if (rc) {
5054                 spin_lock_irq(&phba->hbalock);
5055                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5056                 spin_unlock_irq(&phba->hbalock);
5057         }
5058         return rc;
5059 }
5060
5061 /**
5062  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5063  * @phba: Pointer to HBA context object.
5064  *
5065  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5066  * commands from the driver internal pending mailbox queue. It makes sure
5067  * that there is no outstanding mailbox command before resuming posting
5068  * asynchronous mailbox commands. If, for any reason, there is outstanding
5069  * mailbox command, it will try to wait it out before resuming asynchronous
5070  * mailbox command posting.
5071  **/
5072 static void
5073 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5074 {
5075         struct lpfc_sli *psli = &phba->sli;
5076
5077         spin_lock_irq(&phba->hbalock);
5078         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5079                 /* Asynchronous mailbox posting is not blocked, do nothing */
5080                 spin_unlock_irq(&phba->hbalock);
5081                 return;
5082         }
5083
5084         /* Outstanding synchronous mailbox command is guaranteed to be done,
5085          * successful or timeout, after timing-out the outstanding mailbox
5086          * command shall always be removed, so just unblock posting async
5087          * mailbox command and resume
5088          */
5089         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5090         spin_unlock_irq(&phba->hbalock);
5091
5092         /* wake up worker thread to post asynchronlous mailbox command */
5093         lpfc_worker_wake_up(phba);
5094 }
5095
5096 /**
5097  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5098  * @phba: Pointer to HBA context object.
5099  * @mboxq: Pointer to mailbox object.
5100  *
5101  * The function posts a mailbox to the port.  The mailbox is expected
5102  * to be comletely filled in and ready for the port to operate on it.
5103  * This routine executes a synchronous completion operation on the
5104  * mailbox by polling for its completion.
5105  *
5106  * The caller must not be holding any locks when calling this routine.
5107  *
5108  * Returns:
5109  *      MBX_SUCCESS - mailbox posted successfully
5110  *      Any of the MBX error values.
5111  **/
5112 static int
5113 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5114 {
5115         int rc = MBX_SUCCESS;
5116         unsigned long iflag;
5117         uint32_t db_ready;
5118         uint32_t mcqe_status;
5119         uint32_t mbx_cmnd;
5120         unsigned long timeout;
5121         struct lpfc_sli *psli = &phba->sli;
5122         struct lpfc_mqe *mb = &mboxq->u.mqe;
5123         struct lpfc_bmbx_create *mbox_rgn;
5124         struct dma_address *dma_address;
5125         struct lpfc_register bmbx_reg;
5126
5127         /*
5128          * Only one mailbox can be active to the bootstrap mailbox region
5129          * at a time and there is no queueing provided.
5130          */
5131         spin_lock_irqsave(&phba->hbalock, iflag);
5132         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5133                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5134                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5135                                 "(%d):2532 Mailbox command x%x (x%x) "
5136                                 "cannot issue Data: x%x x%x\n",
5137                                 mboxq->vport ? mboxq->vport->vpi : 0,
5138                                 mboxq->u.mb.mbxCommand,
5139                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5140                                 psli->sli_flag, MBX_POLL);
5141                 return MBXERR_ERROR;
5142         }
5143         /* The server grabs the token and owns it until release */
5144         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5145         phba->sli.mbox_active = mboxq;
5146         spin_unlock_irqrestore(&phba->hbalock, iflag);
5147
5148         /*
5149          * Initialize the bootstrap memory region to avoid stale data areas
5150          * in the mailbox post.  Then copy the caller's mailbox contents to
5151          * the bmbx mailbox region.
5152          */
5153         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5154         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5155         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5156                               sizeof(struct lpfc_mqe));
5157
5158         /* Post the high mailbox dma address to the port and wait for ready. */
5159         dma_address = &phba->sli4_hba.bmbx.dma_address;
5160         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5161
5162         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5163                                    * 1000) + jiffies;
5164         do {
5165                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5166                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5167                 if (!db_ready)
5168                         msleep(2);
5169
5170                 if (time_after(jiffies, timeout)) {
5171                         rc = MBXERR_ERROR;
5172                         goto exit;
5173                 }
5174         } while (!db_ready);
5175
5176         /* Post the low mailbox dma address to the port. */
5177         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5178         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5179                                    * 1000) + jiffies;
5180         do {
5181                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5182                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5183                 if (!db_ready)
5184                         msleep(2);
5185
5186                 if (time_after(jiffies, timeout)) {
5187                         rc = MBXERR_ERROR;
5188                         goto exit;
5189                 }
5190         } while (!db_ready);
5191
5192         /*
5193          * Read the CQ to ensure the mailbox has completed.
5194          * If so, update the mailbox status so that the upper layers
5195          * can complete the request normally.
5196          */
5197         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5198                               sizeof(struct lpfc_mqe));
5199         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5200         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5201                               sizeof(struct lpfc_mcqe));
5202         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5203
5204         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5205         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5206                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5207                 rc = MBXERR_ERROR;
5208         }
5209
5210         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5211                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5212                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5213                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5214                         mboxq->vport ? mboxq->vport->vpi : 0,
5215                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5216                         bf_get(lpfc_mqe_status, mb),
5217                         mb->un.mb_words[0], mb->un.mb_words[1],
5218                         mb->un.mb_words[2], mb->un.mb_words[3],
5219                         mb->un.mb_words[4], mb->un.mb_words[5],
5220                         mb->un.mb_words[6], mb->un.mb_words[7],
5221                         mb->un.mb_words[8], mb->un.mb_words[9],
5222                         mb->un.mb_words[10], mb->un.mb_words[11],
5223                         mb->un.mb_words[12], mboxq->mcqe.word0,
5224                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5225                         mboxq->mcqe.trailer);
5226 exit:
5227         /* We are holding the token, no needed for lock when release */
5228         spin_lock_irqsave(&phba->hbalock, iflag);
5229         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5230         phba->sli.mbox_active = NULL;
5231         spin_unlock_irqrestore(&phba->hbalock, iflag);
5232         return rc;
5233 }
5234
5235 /**
5236  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5237  * @phba: Pointer to HBA context object.
5238  * @pmbox: Pointer to mailbox object.
5239  * @flag: Flag indicating how the mailbox need to be processed.
5240  *
5241  * This function is called by discovery code and HBA management code to submit
5242  * a mailbox command to firmware with SLI-4 interface spec.
5243  *
5244  * Return codes the caller owns the mailbox command after the return of the
5245  * function.
5246  **/
5247 static int
5248 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5249                        uint32_t flag)
5250 {
5251         struct lpfc_sli *psli = &phba->sli;
5252         unsigned long iflags;
5253         int rc;
5254
5255         rc = lpfc_mbox_dev_check(phba);
5256         if (unlikely(rc)) {
5257                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5258                                 "(%d):2544 Mailbox command x%x (x%x) "
5259                                 "cannot issue Data: x%x x%x\n",
5260                                 mboxq->vport ? mboxq->vport->vpi : 0,
5261                                 mboxq->u.mb.mbxCommand,
5262                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5263                                 psli->sli_flag, flag);
5264                 goto out_not_finished;
5265         }
5266
5267         /* Detect polling mode and jump to a handler */
5268         if (!phba->sli4_hba.intr_enable) {
5269                 if (flag == MBX_POLL)
5270                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5271                 else
5272                         rc = -EIO;
5273                 if (rc != MBX_SUCCESS)
5274                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5275                                         "(%d):2541 Mailbox command x%x "
5276                                         "(x%x) cannot issue Data: x%x x%x\n",
5277                                         mboxq->vport ? mboxq->vport->vpi : 0,
5278                                         mboxq->u.mb.mbxCommand,
5279                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5280                                         psli->sli_flag, flag);
5281                 return rc;
5282         } else if (flag == MBX_POLL) {
5283                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5284                                 "(%d):2542 Try to issue mailbox command "
5285                                 "x%x (x%x) synchronously ahead of async"
5286                                 "mailbox command queue: x%x x%x\n",
5287                                 mboxq->vport ? mboxq->vport->vpi : 0,
5288                                 mboxq->u.mb.mbxCommand,
5289                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5290                                 psli->sli_flag, flag);
5291                 /* Try to block the asynchronous mailbox posting */
5292                 rc = lpfc_sli4_async_mbox_block(phba);
5293                 if (!rc) {
5294                         /* Successfully blocked, now issue sync mbox cmd */
5295                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5296                         if (rc != MBX_SUCCESS)
5297                                 lpfc_printf_log(phba, KERN_ERR,
5298                                                 LOG_MBOX | LOG_SLI,
5299                                                 "(%d):2597 Mailbox command "
5300                                                 "x%x (x%x) cannot issue "
5301                                                 "Data: x%x x%x\n",
5302                                                 mboxq->vport ?
5303                                                 mboxq->vport->vpi : 0,
5304                                                 mboxq->u.mb.mbxCommand,
5305                                                 lpfc_sli4_mbox_opcode_get(phba,
5306                                                                 mboxq),
5307                                                 psli->sli_flag, flag);
5308                         /* Unblock the async mailbox posting afterward */
5309                         lpfc_sli4_async_mbox_unblock(phba);
5310                 }
5311                 return rc;
5312         }
5313
5314         /* Now, interrupt mode asynchrous mailbox command */
5315         rc = lpfc_mbox_cmd_check(phba, mboxq);
5316         if (rc) {
5317                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5318                                 "(%d):2543 Mailbox command x%x (x%x) "
5319                                 "cannot issue Data: x%x x%x\n",
5320                                 mboxq->vport ? mboxq->vport->vpi : 0,
5321                                 mboxq->u.mb.mbxCommand,
5322                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5323                                 psli->sli_flag, flag);
5324                 goto out_not_finished;
5325         }
5326
5327         /* Put the mailbox command to the driver internal FIFO */
5328         psli->slistat.mbox_busy++;
5329         spin_lock_irqsave(&phba->hbalock, iflags);
5330         lpfc_mbox_put(phba, mboxq);
5331         spin_unlock_irqrestore(&phba->hbalock, iflags);
5332         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5333                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5334                         "x%x (x%x) x%x x%x x%x\n",
5335                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5336                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5337                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5338                         phba->pport->port_state,
5339                         psli->sli_flag, MBX_NOWAIT);
5340         /* Wake up worker thread to transport mailbox command from head */
5341         lpfc_worker_wake_up(phba);
5342
5343         return MBX_BUSY;
5344
5345 out_not_finished:
5346         return MBX_NOT_FINISHED;
5347 }
5348
5349 /**
5350  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5351  * @phba: Pointer to HBA context object.
5352  *
5353  * This function is called by worker thread to send a mailbox command to
5354  * SLI4 HBA firmware.
5355  *
5356  **/
5357 int
5358 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5359 {
5360         struct lpfc_sli *psli = &phba->sli;
5361         LPFC_MBOXQ_t *mboxq;
5362         int rc = MBX_SUCCESS;
5363         unsigned long iflags;
5364         struct lpfc_mqe *mqe;
5365         uint32_t mbx_cmnd;
5366
5367         /* Check interrupt mode before post async mailbox command */
5368         if (unlikely(!phba->sli4_hba.intr_enable))
5369                 return MBX_NOT_FINISHED;
5370
5371         /* Check for mailbox command service token */
5372         spin_lock_irqsave(&phba->hbalock, iflags);
5373         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5374                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5375                 return MBX_NOT_FINISHED;
5376         }
5377         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5378                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5379                 return MBX_NOT_FINISHED;
5380         }
5381         if (unlikely(phba->sli.mbox_active)) {
5382                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5383                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5384                                 "0384 There is pending active mailbox cmd\n");
5385                 return MBX_NOT_FINISHED;
5386         }
5387         /* Take the mailbox command service token */
5388         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5389
5390         /* Get the next mailbox command from head of queue */
5391         mboxq = lpfc_mbox_get(phba);
5392
5393         /* If no more mailbox command waiting for post, we're done */
5394         if (!mboxq) {
5395                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5396                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5397                 return MBX_SUCCESS;
5398         }
5399         phba->sli.mbox_active = mboxq;
5400         spin_unlock_irqrestore(&phba->hbalock, iflags);
5401
5402         /* Check device readiness for posting mailbox command */
5403         rc = lpfc_mbox_dev_check(phba);
5404         if (unlikely(rc))
5405                 /* Driver clean routine will clean up pending mailbox */
5406                 goto out_not_finished;
5407
5408         /* Prepare the mbox command to be posted */
5409         mqe = &mboxq->u.mqe;
5410         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5411
5412         /* Start timer for the mbox_tmo and log some mailbox post messages */
5413         mod_timer(&psli->mbox_tmo, (jiffies +
5414                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5415
5416         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5417                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5418                         "x%x x%x\n",
5419                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5420                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5421                         phba->pport->port_state, psli->sli_flag);
5422
5423         if (mbx_cmnd != MBX_HEARTBEAT) {
5424                 if (mboxq->vport) {
5425                         lpfc_debugfs_disc_trc(mboxq->vport,
5426                                 LPFC_DISC_TRC_MBOX_VPORT,
5427                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5428                                 mbx_cmnd, mqe->un.mb_words[0],
5429                                 mqe->un.mb_words[1]);
5430                 } else {
5431                         lpfc_debugfs_disc_trc(phba->pport,
5432                                 LPFC_DISC_TRC_MBOX,
5433                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5434                                 mbx_cmnd, mqe->un.mb_words[0],
5435                                 mqe->un.mb_words[1]);
5436                 }
5437         }
5438         psli->slistat.mbox_cmd++;
5439
5440         /* Post the mailbox command to the port */
5441         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5442         if (rc != MBX_SUCCESS) {
5443                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5444                                 "(%d):2533 Mailbox command x%x (x%x) "
5445                                 "cannot issue Data: x%x x%x\n",
5446                                 mboxq->vport ? mboxq->vport->vpi : 0,
5447                                 mboxq->u.mb.mbxCommand,
5448                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5449                                 psli->sli_flag, MBX_NOWAIT);
5450                 goto out_not_finished;
5451         }
5452
5453         return rc;
5454
5455 out_not_finished:
5456         spin_lock_irqsave(&phba->hbalock, iflags);
5457         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5458         __lpfc_mbox_cmpl_put(phba, mboxq);
5459         /* Release the token */
5460         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5461         phba->sli.mbox_active = NULL;
5462         spin_unlock_irqrestore(&phba->hbalock, iflags);
5463
5464         return MBX_NOT_FINISHED;
5465 }
5466
5467 /**
5468  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5469  * @phba: Pointer to HBA context object.
5470  * @pmbox: Pointer to mailbox object.
5471  * @flag: Flag indicating how the mailbox need to be processed.
5472  *
5473  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5474  * the API jump table function pointer from the lpfc_hba struct.
5475  *
5476  * Return codes the caller owns the mailbox command after the return of the
5477  * function.
5478  **/
5479 int
5480 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5481 {
5482         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5483 }
5484
5485 /**
5486  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5487  * @phba: The hba struct for which this call is being executed.
5488  * @dev_grp: The HBA PCI-Device group number.
5489  *
5490  * This routine sets up the mbox interface API function jump table in @phba
5491  * struct.
5492  * Returns: 0 - success, -ENODEV - failure.
5493  **/
5494 int
5495 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5496 {
5497
5498         switch (dev_grp) {
5499         case LPFC_PCI_DEV_LP:
5500                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5501                 phba->lpfc_sli_handle_slow_ring_event =
5502                                 lpfc_sli_handle_slow_ring_event_s3;
5503                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5504                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5505                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5506                 break;
5507         case LPFC_PCI_DEV_OC:
5508                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5509                 phba->lpfc_sli_handle_slow_ring_event =
5510                                 lpfc_sli_handle_slow_ring_event_s4;
5511                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5512                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5513                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5514                 break;
5515         default:
5516                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5517                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
5518                                 dev_grp);
5519                 return -ENODEV;
5520                 break;
5521         }
5522         return 0;
5523 }
5524
5525 /**
5526  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5527  * @phba: Pointer to HBA context object.
5528  * @pring: Pointer to driver SLI ring object.
5529  * @piocb: Pointer to address of newly added command iocb.
5530  *
5531  * This function is called with hbalock held to add a command
5532  * iocb to the txq when SLI layer cannot submit the command iocb
5533  * to the ring.
5534  **/
5535 static void
5536 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5537                     struct lpfc_iocbq *piocb)
5538 {
5539         /* Insert the caller's iocb in the txq tail for later processing. */
5540         list_add_tail(&piocb->list, &pring->txq);
5541         pring->txq_cnt++;
5542 }
5543
5544 /**
5545  * lpfc_sli_next_iocb - Get the next iocb in the txq
5546  * @phba: Pointer to HBA context object.
5547  * @pring: Pointer to driver SLI ring object.
5548  * @piocb: Pointer to address of newly added command iocb.
5549  *
5550  * This function is called with hbalock held before a new
5551  * iocb is submitted to the firmware. This function checks
5552  * txq to flush the iocbs in txq to Firmware before
5553  * submitting new iocbs to the Firmware.
5554  * If there are iocbs in the txq which need to be submitted
5555  * to firmware, lpfc_sli_next_iocb returns the first element
5556  * of the txq after dequeuing it from txq.
5557  * If there is no iocb in the txq then the function will return
5558  * *piocb and *piocb is set to NULL. Caller needs to check
5559  * *piocb to find if there are more commands in the txq.
5560  **/
5561 static struct lpfc_iocbq *
5562 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5563                    struct lpfc_iocbq **piocb)
5564 {
5565         struct lpfc_iocbq * nextiocb;
5566
5567         nextiocb = lpfc_sli_ringtx_get(phba, pring);
5568         if (!nextiocb) {
5569                 nextiocb = *piocb;
5570                 *piocb = NULL;
5571         }
5572
5573         return nextiocb;
5574 }
5575
5576 /**
5577  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5578  * @phba: Pointer to HBA context object.
5579  * @ring_number: SLI ring number to issue iocb on.
5580  * @piocb: Pointer to command iocb.
5581  * @flag: Flag indicating if this command can be put into txq.
5582  *
5583  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5584  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5585  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5586  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5587  * this function allows only iocbs for posting buffers. This function finds
5588  * next available slot in the command ring and posts the command to the
5589  * available slot and writes the port attention register to request HBA start
5590  * processing new iocb. If there is no slot available in the ring and
5591  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5592  * the function returns IOCB_BUSY.
5593  *
5594  * This function is called with hbalock held. The function will return success
5595  * after it successfully submit the iocb to firmware or after adding to the
5596  * txq.
5597  **/
5598 static int
5599 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5600                     struct lpfc_iocbq *piocb, uint32_t flag)
5601 {
5602         struct lpfc_iocbq *nextiocb;
5603         IOCB_t *iocb;
5604         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5605
5606         if (piocb->iocb_cmpl && (!piocb->vport) &&
5607            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5608            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5609                 lpfc_printf_log(phba, KERN_ERR,
5610                                 LOG_SLI | LOG_VPORT,
5611                                 "1807 IOCB x%x failed. No vport\n",
5612                                 piocb->iocb.ulpCommand);
5613                 dump_stack();
5614                 return IOCB_ERROR;
5615         }
5616
5617
5618         /* If the PCI channel is in offline state, do not post iocbs. */
5619         if (unlikely(pci_channel_offline(phba->pcidev)))
5620                 return IOCB_ERROR;
5621
5622         /* If HBA has a deferred error attention, fail the iocb. */
5623         if (unlikely(phba->hba_flag & DEFER_ERATT))
5624                 return IOCB_ERROR;
5625
5626         /*
5627          * We should never get an IOCB if we are in a < LINK_DOWN state
5628          */
5629         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5630                 return IOCB_ERROR;
5631
5632         /*
5633          * Check to see if we are blocking IOCB processing because of a
5634          * outstanding event.
5635          */
5636         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5637                 goto iocb_busy;
5638
5639         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5640                 /*
5641                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5642                  * can be issued if the link is not up.
5643                  */
5644                 switch (piocb->iocb.ulpCommand) {
5645                 case CMD_GEN_REQUEST64_CR:
5646                 case CMD_GEN_REQUEST64_CX:
5647                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5648                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5649                                         FC_RCTL_DD_UNSOL_CMD) ||
5650                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5651                                         MENLO_TRANSPORT_TYPE))
5652
5653                                 goto iocb_busy;
5654                         break;
5655                 case CMD_QUE_RING_BUF_CN:
5656                 case CMD_QUE_RING_BUF64_CN:
5657                         /*
5658                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5659                          * completion, iocb_cmpl MUST be 0.
5660                          */
5661                         if (piocb->iocb_cmpl)
5662                                 piocb->iocb_cmpl = NULL;
5663                         /*FALLTHROUGH*/
5664                 case CMD_CREATE_XRI_CR:
5665                 case CMD_CLOSE_XRI_CN:
5666                 case CMD_CLOSE_XRI_CX:
5667                         break;
5668                 default:
5669                         goto iocb_busy;
5670                 }
5671
5672         /*
5673          * For FCP commands, we must be in a state where we can process link
5674          * attention events.
5675          */
5676         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5677                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5678                 goto iocb_busy;
5679         }
5680
5681         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5682                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5683                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5684
5685         if (iocb)
5686                 lpfc_sli_update_ring(phba, pring);
5687         else
5688                 lpfc_sli_update_full_ring(phba, pring);
5689
5690         if (!piocb)
5691                 return IOCB_SUCCESS;
5692
5693         goto out_busy;
5694
5695  iocb_busy:
5696         pring->stats.iocb_cmd_delay++;
5697
5698  out_busy:
5699
5700         if (!(flag & SLI_IOCB_RET_IOCB)) {
5701                 __lpfc_sli_ringtx_put(phba, pring, piocb);
5702                 return IOCB_SUCCESS;
5703         }
5704
5705         return IOCB_BUSY;
5706 }
5707
5708 /**
5709  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5710  * @phba: Pointer to HBA context object.
5711  * @piocb: Pointer to command iocb.
5712  * @sglq: Pointer to the scatter gather queue object.
5713  *
5714  * This routine converts the bpl or bde that is in the IOCB
5715  * to a sgl list for the sli4 hardware. The physical address
5716  * of the bpl/bde is converted back to a virtual address.
5717  * If the IOCB contains a BPL then the list of BDE's is
5718  * converted to sli4_sge's. If the IOCB contains a single
5719  * BDE then it is converted to a single sli_sge.
5720  * The IOCB is still in cpu endianess so the contents of
5721  * the bpl can be used without byte swapping.
5722  *
5723  * Returns valid XRI = Success, NO_XRI = Failure.
5724 **/
5725 static uint16_t
5726 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5727                 struct lpfc_sglq *sglq)
5728 {
5729         uint16_t xritag = NO_XRI;
5730         struct ulp_bde64 *bpl = NULL;
5731         struct ulp_bde64 bde;
5732         struct sli4_sge *sgl  = NULL;
5733         IOCB_t *icmd;
5734         int numBdes = 0;
5735         int i = 0;
5736
5737         if (!piocbq || !sglq)
5738                 return xritag;
5739
5740         sgl  = (struct sli4_sge *)sglq->sgl;
5741         icmd = &piocbq->iocb;
5742         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5743                 numBdes = icmd->un.genreq64.bdl.bdeSize /
5744                                 sizeof(struct ulp_bde64);
5745                 /* The addrHigh and addrLow fields within the IOCB
5746                  * have not been byteswapped yet so there is no
5747                  * need to swap them back.
5748                  */
5749                 bpl  = (struct ulp_bde64 *)
5750                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5751
5752                 if (!bpl)
5753                         return xritag;
5754
5755                 for (i = 0; i < numBdes; i++) {
5756                         /* Should already be byte swapped. */
5757                         sgl->addr_hi = bpl->addrHigh;
5758                         sgl->addr_lo = bpl->addrLow;
5759
5760                         if ((i+1) == numBdes)
5761                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
5762                         else
5763                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
5764                         sgl->word2 = cpu_to_le32(sgl->word2);
5765                         /* swap the size field back to the cpu so we
5766                          * can assign it to the sgl.
5767                          */
5768                         bde.tus.w = le32_to_cpu(bpl->tus.w);
5769                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
5770                         bpl++;
5771                         sgl++;
5772                 }
5773         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5774                         /* The addrHigh and addrLow fields of the BDE have not
5775                          * been byteswapped yet so they need to be swapped
5776                          * before putting them in the sgl.
5777                          */
5778                         sgl->addr_hi =
5779                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5780                         sgl->addr_lo =
5781                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5782                         bf_set(lpfc_sli4_sge_last, sgl, 1);
5783                         sgl->word2 = cpu_to_le32(sgl->word2);
5784                         sgl->sge_len =
5785                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
5786         }
5787         return sglq->sli4_xritag;
5788 }
5789
5790 /**
5791  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5792  * @phba: Pointer to HBA context object.
5793  *
5794  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5795  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5796  * held.
5797  *
5798  * Return: index into SLI4 fast-path FCP queue index.
5799  **/
5800 static uint32_t
5801 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
5802 {
5803         ++phba->fcp_qidx;
5804         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5805                 phba->fcp_qidx = 0;
5806
5807         return phba->fcp_qidx;
5808 }
5809
5810 /**
5811  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5812  * @phba: Pointer to HBA context object.
5813  * @piocb: Pointer to command iocb.
5814  * @wqe: Pointer to the work queue entry.
5815  *
5816  * This routine converts the iocb command to its Work Queue Entry
5817  * equivalent. The wqe pointer should not have any fields set when
5818  * this routine is called because it will memcpy over them.
5819  * This routine does not set the CQ_ID or the WQEC bits in the
5820  * wqe.
5821  *
5822  * Returns: 0 = Success, IOCB_ERROR = Failure.
5823  **/
5824 static int
5825 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5826                 union lpfc_wqe *wqe)
5827 {
5828         uint32_t xmit_len = 0, total_len = 0;
5829         uint8_t ct = 0;
5830         uint32_t fip;
5831         uint32_t abort_tag;
5832         uint8_t command_type = ELS_COMMAND_NON_FIP;
5833         uint8_t cmnd;
5834         uint16_t xritag;
5835         struct ulp_bde64 *bpl = NULL;
5836         uint32_t els_id = ELS_ID_DEFAULT;
5837         int numBdes, i;
5838         struct ulp_bde64 bde;
5839
5840         fip = phba->hba_flag & HBA_FIP_SUPPORT;
5841         /* The fcp commands will set command type */
5842         if (iocbq->iocb_flag &  LPFC_IO_FCP)
5843                 command_type = FCP_COMMAND;
5844         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
5845                 command_type = ELS_COMMAND_FIP;
5846         else
5847                 command_type = ELS_COMMAND_NON_FIP;
5848
5849         /* Some of the fields are in the right position already */
5850         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5851         abort_tag = (uint32_t) iocbq->iotag;
5852         xritag = iocbq->sli4_xritag;
5853         wqe->words[7] = 0; /* The ct field has moved so reset */
5854         /* words0-2 bpl convert bde */
5855         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5856                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5857                                 sizeof(struct ulp_bde64);
5858                 bpl  = (struct ulp_bde64 *)
5859                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5860                 if (!bpl)
5861                         return IOCB_ERROR;
5862
5863                 /* Should already be byte swapped. */
5864                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5865                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5866                 /* swap the size field back to the cpu so we
5867                  * can assign it to the sgl.
5868                  */
5869                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5870                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5871                 total_len = 0;
5872                 for (i = 0; i < numBdes; i++) {
5873                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
5874                         total_len += bde.tus.f.bdeSize;
5875                 }
5876         } else
5877                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5878
5879         iocbq->iocb.ulpIoTag = iocbq->iotag;
5880         cmnd = iocbq->iocb.ulpCommand;
5881
5882         switch (iocbq->iocb.ulpCommand) {
5883         case CMD_ELS_REQUEST64_CR:
5884                 if (!iocbq->iocb.ulpLe) {
5885                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5886                                 "2007 Only Limited Edition cmd Format"
5887                                 " supported 0x%x\n",
5888                                 iocbq->iocb.ulpCommand);
5889                         return IOCB_ERROR;
5890                 }
5891                 wqe->els_req.payload_len = xmit_len;
5892                 /* Els_reguest64 has a TMO */
5893                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5894                         iocbq->iocb.ulpTimeout);
5895                 /* Need a VF for word 4 set the vf bit*/
5896                 bf_set(els_req64_vf, &wqe->els_req, 0);
5897                 /* And a VFID for word 12 */
5898                 bf_set(els_req64_vfid, &wqe->els_req, 0);
5899                 /*
5900                  * Set ct field to 3, indicates that the context_tag field
5901                  * contains the FCFI and remote N_Port_ID is
5902                  * in word 5.
5903                  */
5904
5905                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5906                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5907                                 iocbq->iocb.ulpContext);
5908
5909                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5910                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5911                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5912
5913                 if (command_type == ELS_COMMAND_FIP) {
5914                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5915                                         >> LPFC_FIP_ELS_ID_SHIFT);
5916                 }
5917                 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5918
5919         break;
5920         case CMD_XMIT_SEQUENCE64_CX:
5921                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5922                                         iocbq->iocb.un.ulpWord[3]);
5923                 wqe->generic.word3 = 0;
5924                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5925                 /* The entire sequence is transmitted for this IOCB */
5926                 xmit_len = total_len;
5927                 cmnd = CMD_XMIT_SEQUENCE64_CR;
5928         case CMD_XMIT_SEQUENCE64_CR:
5929                 /* word3 iocb=io_tag32 wqe=payload_offset */
5930                 /* payload offset used for multilpe outstanding
5931                  * sequences on the same exchange
5932                  */
5933                 wqe->words[3] = 0;
5934                 /* word4 relative_offset memcpy */
5935                 /* word5 r_ctl/df_ctl memcpy */
5936                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5937                 wqe->xmit_sequence.xmit_len = xmit_len;
5938                 command_type = OTHER_COMMAND;
5939         break;
5940         case CMD_XMIT_BCAST64_CN:
5941                 /* word3 iocb=iotag32 wqe=payload_len */
5942                 wqe->words[3] = 0; /* no definition for this in wqe */
5943                 /* word4 iocb=rsvd wqe=rsvd */
5944                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5945                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5946                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5947                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5948         break;
5949         case CMD_FCP_IWRITE64_CR:
5950                 command_type = FCP_COMMAND_DATA_OUT;
5951                 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5952                  * confusing.
5953                  * word3 is payload_len: byte offset to the sgl entry for the
5954                  * fcp_command.
5955                  * word4 is total xfer len, same as the IOCB->ulpParameter.
5956                  * word5 is initial xfer len 0 = wait for xfer-ready
5957                  */
5958
5959                 /* Always wait for xfer-ready before sending data */
5960                 wqe->fcp_iwrite.initial_xfer_len = 0;
5961                 /* word 4 (xfer length) should have been set on the memcpy */
5962
5963         /* allow write to fall through to read */
5964         case CMD_FCP_IREAD64_CR:
5965                 /* FCP_CMD is always the 1st sgl entry */
5966                 wqe->fcp_iread.payload_len =
5967                         xmit_len + sizeof(struct fcp_rsp);
5968
5969                 /* word 4 (xfer length) should have been set on the memcpy */
5970
5971                 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5972                         iocbq->iocb.ulpFCP2Rcvy);
5973                 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5974                 /* The XC bit and the XS bit are similar. The driver never
5975                  * tracked whether or not the exchange was previouslly open.
5976                  * XC = Exchange create, 0 is create. 1 is already open.
5977                  * XS = link cmd: 1 do not close the exchange after command.
5978                  * XS = 0 close exchange when command completes.
5979                  * The only time we would not set the XC bit is when the XS bit
5980                  * is set and we are sending our 2nd or greater command on
5981                  * this exchange.
5982                  */
5983                 /* Always open the exchange */
5984                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5985
5986                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5987                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5988                 break;
5989         case CMD_FCP_ICMND64_CR:
5990                 /* Always open the exchange */
5991                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5992
5993                 wqe->words[4] = 0;
5994                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5995                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5996         break;
5997         case CMD_GEN_REQUEST64_CR:
5998                 /* word3 command length is described as byte offset to the
5999                  * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6000                  * sgl[0] = cmnd
6001                  * sgl[1] = rsp.
6002                  *
6003                  */
6004                 wqe->gen_req.command_len = xmit_len;
6005                 /* Word4 parameter  copied in the memcpy */
6006                 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6007                 /* word6 context tag copied in memcpy */
6008                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6009                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6010                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6011                                 "2015 Invalid CT %x command 0x%x\n",
6012                                 ct, iocbq->iocb.ulpCommand);
6013                         return IOCB_ERROR;
6014                 }
6015                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6016                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6017                         iocbq->iocb.ulpTimeout);
6018
6019                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6020                 command_type = OTHER_COMMAND;
6021         break;
6022         case CMD_XMIT_ELS_RSP64_CX:
6023                 /* words0-2 BDE memcpy */
6024                 /* word3 iocb=iotag32 wqe=rsvd */
6025                 wqe->words[3] = 0;
6026                 /* word4 iocb=did wge=rsvd. */
6027                 wqe->words[4] = 0;
6028                 /* word5 iocb=rsvd wge=did */
6029                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6030                          iocbq->iocb.un.elsreq64.remoteID);
6031
6032                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6033                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6034
6035                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6036                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6037                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6038                         bf_set(lpfc_wqe_gen_context, &wqe->generic,
6039                                iocbq->vport->vpi + phba->vpi_base);
6040                 command_type = OTHER_COMMAND;
6041         break;
6042         case CMD_CLOSE_XRI_CN:
6043         case CMD_ABORT_XRI_CN:
6044         case CMD_ABORT_XRI_CX:
6045                 /* words 0-2 memcpy should be 0 rserved */
6046                 /* port will send abts */
6047                 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6048                         /*
6049                          * The link is down so the fw does not need to send abts
6050                          * on the wire.
6051                          */
6052                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6053                 else
6054                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6055                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6056                 wqe->words[5] = 0;
6057                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6058                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6059                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6060                 /*
6061                  * The abort handler will send us CMD_ABORT_XRI_CN or
6062                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6063                  */
6064                 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6065                 cmnd = CMD_ABORT_XRI_CX;
6066                 command_type = OTHER_COMMAND;
6067                 xritag = 0;
6068         break;
6069         case CMD_XMIT_BLS_RSP64_CX:
6070                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6071                  * we re-construct this WQE here based on information in
6072                  * iocbq from scratch.
6073                  */
6074                 memset(wqe, 0, sizeof(union lpfc_wqe));
6075                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6076                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6077                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6078                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6079                     LPFC_ABTS_UNSOL_INT) {
6080                         /* ABTS sent by initiator to CT exchange, the
6081                          * RX_ID field will be filled with the newly
6082                          * allocated responder XRI.
6083                          */
6084                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6085                                iocbq->sli4_xritag);
6086                 } else {
6087                         /* ABTS sent by responder to CT exchange, the
6088                          * RX_ID field will be filled with the responder
6089                          * RX_ID from ABTS.
6090                          */
6091                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6092                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6093                 }
6094                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6095                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6096                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6097                        iocbq->iocb.ulpContext);
6098                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6099                 command_type = OTHER_COMMAND;
6100         break;
6101         case CMD_XRI_ABORTED_CX:
6102         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6103                 /* words0-2 are all 0's no bde */
6104                 /* word3 and word4 are rsvrd */
6105                 wqe->words[3] = 0;
6106                 wqe->words[4] = 0;
6107                 /* word5 iocb=rsvd wge=did */
6108                 /* There is no remote port id in the IOCB? */
6109                 /* Let this fall through and fail */
6110         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6111         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6112         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6113         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6114         default:
6115                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6116                                 "2014 Invalid command 0x%x\n",
6117                                 iocbq->iocb.ulpCommand);
6118                 return IOCB_ERROR;
6119         break;
6120
6121         }
6122         bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6123         bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6124         wqe->generic.abort_tag = abort_tag;
6125         bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6126         bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6127         bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6128         bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6129
6130         return 0;
6131 }
6132
6133 /**
6134  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6135  * @phba: Pointer to HBA context object.
6136  * @ring_number: SLI ring number to issue iocb on.
6137  * @piocb: Pointer to command iocb.
6138  * @flag: Flag indicating if this command can be put into txq.
6139  *
6140  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6141  * an iocb command to an HBA with SLI-4 interface spec.
6142  *
6143  * This function is called with hbalock held. The function will return success
6144  * after it successfully submit the iocb to firmware or after adding to the
6145  * txq.
6146  **/
6147 static int
6148 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6149                          struct lpfc_iocbq *piocb, uint32_t flag)
6150 {
6151         struct lpfc_sglq *sglq;
6152         uint16_t xritag;
6153         union lpfc_wqe wqe;
6154         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6155
6156         if (piocb->sli4_xritag == NO_XRI) {
6157                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6158                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6159                         sglq = NULL;
6160                 else {
6161                         sglq = __lpfc_sli_get_sglq(phba);
6162                         if (!sglq)
6163                                 return IOCB_ERROR;
6164                         piocb->sli4_xritag = sglq->sli4_xritag;
6165                 }
6166         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6167                 sglq = NULL; /* These IO's already have an XRI and
6168                               * a mapped sgl.
6169                               */
6170         } else {
6171                 /* This is a continuation of a commandi,(CX) so this
6172                  * sglq is on the active list
6173                  */
6174                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6175                 if (!sglq)
6176                         return IOCB_ERROR;
6177         }
6178
6179         if (sglq) {
6180                 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6181                 if (xritag != sglq->sli4_xritag)
6182                         return IOCB_ERROR;
6183         }
6184
6185         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6186                 return IOCB_ERROR;
6187
6188         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6189                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6190                 /*
6191                  * For FCP command IOCB, get a new WQ index to distribute
6192                  * WQE across the WQsr. On the other hand, for abort IOCB,
6193                  * it carries the same WQ index to the original command
6194                  * IOCB.
6195                  */
6196                 if (piocb->iocb_flag & LPFC_IO_FCP)
6197                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6198                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6199                                      &wqe))
6200                         return IOCB_ERROR;
6201         } else {
6202                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6203                         return IOCB_ERROR;
6204         }
6205         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6206
6207         return 0;
6208 }
6209
6210 /**
6211  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6212  *
6213  * This routine wraps the actual lockless version for issusing IOCB function
6214  * pointer from the lpfc_hba struct.
6215  *
6216  * Return codes:
6217  *      IOCB_ERROR - Error
6218  *      IOCB_SUCCESS - Success
6219  *      IOCB_BUSY - Busy
6220  **/
6221 static inline int
6222 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6223                 struct lpfc_iocbq *piocb, uint32_t flag)
6224 {
6225         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6226 }
6227
6228 /**
6229  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6230  * @phba: The hba struct for which this call is being executed.
6231  * @dev_grp: The HBA PCI-Device group number.
6232  *
6233  * This routine sets up the SLI interface API function jump table in @phba
6234  * struct.
6235  * Returns: 0 - success, -ENODEV - failure.
6236  **/
6237 int
6238 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6239 {
6240
6241         switch (dev_grp) {
6242         case LPFC_PCI_DEV_LP:
6243                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6244                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6245                 break;
6246         case LPFC_PCI_DEV_OC:
6247                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6248                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6249                 break;
6250         default:
6251                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6252                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6253                                 dev_grp);
6254                 return -ENODEV;
6255                 break;
6256         }
6257         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6258         return 0;
6259 }
6260
6261 /**
6262  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6263  * @phba: Pointer to HBA context object.
6264  * @pring: Pointer to driver SLI ring object.
6265  * @piocb: Pointer to command iocb.
6266  * @flag: Flag indicating if this command can be put into txq.
6267  *
6268  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6269  * function. This function gets the hbalock and calls
6270  * __lpfc_sli_issue_iocb function and will return the error returned
6271  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6272  * functions which do not hold hbalock.
6273  **/
6274 int
6275 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6276                     struct lpfc_iocbq *piocb, uint32_t flag)
6277 {
6278         unsigned long iflags;
6279         int rc;
6280
6281         spin_lock_irqsave(&phba->hbalock, iflags);
6282         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6283         spin_unlock_irqrestore(&phba->hbalock, iflags);
6284
6285         return rc;
6286 }
6287
6288 /**
6289  * lpfc_extra_ring_setup - Extra ring setup function
6290  * @phba: Pointer to HBA context object.
6291  *
6292  * This function is called while driver attaches with the
6293  * HBA to setup the extra ring. The extra ring is used
6294  * only when driver needs to support target mode functionality
6295  * or IP over FC functionalities.
6296  *
6297  * This function is called with no lock held.
6298  **/
6299 static int
6300 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6301 {
6302         struct lpfc_sli *psli;
6303         struct lpfc_sli_ring *pring;
6304
6305         psli = &phba->sli;
6306
6307         /* Adjust cmd/rsp ring iocb entries more evenly */
6308
6309         /* Take some away from the FCP ring */
6310         pring = &psli->ring[psli->fcp_ring];
6311         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6312         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6313         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6314         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6315
6316         /* and give them to the extra ring */
6317         pring = &psli->ring[psli->extra_ring];
6318
6319         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6320         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6321         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6322         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6323
6324         /* Setup default profile for this ring */
6325         pring->iotag_max = 4096;
6326         pring->num_mask = 1;
6327         pring->prt[0].profile = 0;      /* Mask 0 */
6328         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6329         pring->prt[0].type = phba->cfg_multi_ring_type;
6330         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6331         return 0;
6332 }
6333
6334 /**
6335  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6336  * @phba: Pointer to HBA context object.
6337  * @pring: Pointer to driver SLI ring object.
6338  * @iocbq: Pointer to iocb object.
6339  *
6340  * This function is called by the slow ring event handler
6341  * function when there is an ASYNC event iocb in the ring.
6342  * This function is called with no lock held.
6343  * Currently this function handles only temperature related
6344  * ASYNC events. The function decodes the temperature sensor
6345  * event message and posts events for the management applications.
6346  **/
6347 static void
6348 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6349         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6350 {
6351         IOCB_t *icmd;
6352         uint16_t evt_code;
6353         uint16_t temp;
6354         struct temp_event temp_event_data;
6355         struct Scsi_Host *shost;
6356         uint32_t *iocb_w;
6357
6358         icmd = &iocbq->iocb;
6359         evt_code = icmd->un.asyncstat.evt_code;
6360         temp = icmd->ulpContext;
6361
6362         if ((evt_code != ASYNC_TEMP_WARN) &&
6363                 (evt_code != ASYNC_TEMP_SAFE)) {
6364                 iocb_w = (uint32_t *) icmd;
6365                 lpfc_printf_log(phba,
6366                         KERN_ERR,
6367                         LOG_SLI,
6368                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6369                         " evt_code 0x%x\n"
6370                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6371                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6372                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6373                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6374                         pring->ringno,
6375                         icmd->un.asyncstat.evt_code,
6376                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6377                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6378                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6379                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6380
6381                 return;
6382         }
6383         temp_event_data.data = (uint32_t)temp;
6384         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6385         if (evt_code == ASYNC_TEMP_WARN) {
6386                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6387                 lpfc_printf_log(phba,
6388                                 KERN_ERR,
6389                                 LOG_TEMP,
6390                                 "0347 Adapter is very hot, please take "
6391                                 "corrective action. temperature : %d Celsius\n",
6392                                 temp);
6393         }
6394         if (evt_code == ASYNC_TEMP_SAFE) {
6395                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6396                 lpfc_printf_log(phba,
6397                                 KERN_ERR,
6398                                 LOG_TEMP,
6399                                 "0340 Adapter temperature is OK now. "
6400                                 "temperature : %d Celsius\n",
6401                                 temp);
6402         }
6403
6404         /* Send temperature change event to applications */
6405         shost = lpfc_shost_from_vport(phba->pport);
6406         fc_host_post_vendor_event(shost, fc_get_event_number(),
6407                 sizeof(temp_event_data), (char *) &temp_event_data,
6408                 LPFC_NL_VENDOR_ID);
6409
6410 }
6411
6412
6413 /**
6414  * lpfc_sli_setup - SLI ring setup function
6415  * @phba: Pointer to HBA context object.
6416  *
6417  * lpfc_sli_setup sets up rings of the SLI interface with
6418  * number of iocbs per ring and iotags. This function is
6419  * called while driver attach to the HBA and before the
6420  * interrupts are enabled. So there is no need for locking.
6421  *
6422  * This function always returns 0.
6423  **/
6424 int
6425 lpfc_sli_setup(struct lpfc_hba *phba)
6426 {
6427         int i, totiocbsize = 0;
6428         struct lpfc_sli *psli = &phba->sli;
6429         struct lpfc_sli_ring *pring;
6430
6431         psli->num_rings = MAX_CONFIGURED_RINGS;
6432         psli->sli_flag = 0;
6433         psli->fcp_ring = LPFC_FCP_RING;
6434         psli->next_ring = LPFC_FCP_NEXT_RING;
6435         psli->extra_ring = LPFC_EXTRA_RING;
6436
6437         psli->iocbq_lookup = NULL;
6438         psli->iocbq_lookup_len = 0;
6439         psli->last_iotag = 0;
6440
6441         for (i = 0; i < psli->num_rings; i++) {
6442                 pring = &psli->ring[i];
6443                 switch (i) {
6444                 case LPFC_FCP_RING:     /* ring 0 - FCP */
6445                         /* numCiocb and numRiocb are used in config_port */
6446                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6447                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6448                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6449                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6450                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6451                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6452                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6453                                                         SLI3_IOCB_CMD_SIZE :
6454                                                         SLI2_IOCB_CMD_SIZE;
6455                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6456                                                         SLI3_IOCB_RSP_SIZE :
6457                                                         SLI2_IOCB_RSP_SIZE;
6458                         pring->iotag_ctr = 0;
6459                         pring->iotag_max =
6460                             (phba->cfg_hba_queue_depth * 2);
6461                         pring->fast_iotag = pring->iotag_max;
6462                         pring->num_mask = 0;
6463                         break;
6464                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
6465                         /* numCiocb and numRiocb are used in config_port */
6466                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6467                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6468                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6469                                                         SLI3_IOCB_CMD_SIZE :
6470                                                         SLI2_IOCB_CMD_SIZE;
6471                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6472                                                         SLI3_IOCB_RSP_SIZE :
6473                                                         SLI2_IOCB_RSP_SIZE;
6474                         pring->iotag_max = phba->cfg_hba_queue_depth;
6475                         pring->num_mask = 0;
6476                         break;
6477                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
6478                         /* numCiocb and numRiocb are used in config_port */
6479                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6480                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6481                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6482                                                         SLI3_IOCB_CMD_SIZE :
6483                                                         SLI2_IOCB_CMD_SIZE;
6484                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6485                                                         SLI3_IOCB_RSP_SIZE :
6486                                                         SLI2_IOCB_RSP_SIZE;
6487                         pring->fast_iotag = 0;
6488                         pring->iotag_ctr = 0;
6489                         pring->iotag_max = 4096;
6490                         pring->lpfc_sli_rcv_async_status =
6491                                 lpfc_sli_async_event_handler;
6492                         pring->num_mask = LPFC_MAX_RING_MASK;
6493                         pring->prt[0].profile = 0;      /* Mask 0 */
6494                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6495                         pring->prt[0].type = FC_TYPE_ELS;
6496                         pring->prt[0].lpfc_sli_rcv_unsol_event =
6497                             lpfc_els_unsol_event;
6498                         pring->prt[1].profile = 0;      /* Mask 1 */
6499                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
6500                         pring->prt[1].type = FC_TYPE_ELS;
6501                         pring->prt[1].lpfc_sli_rcv_unsol_event =
6502                             lpfc_els_unsol_event;
6503                         pring->prt[2].profile = 0;      /* Mask 2 */
6504                         /* NameServer Inquiry */
6505                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
6506                         /* NameServer */
6507                         pring->prt[2].type = FC_TYPE_CT;
6508                         pring->prt[2].lpfc_sli_rcv_unsol_event =
6509                             lpfc_ct_unsol_event;
6510                         pring->prt[3].profile = 0;      /* Mask 3 */
6511                         /* NameServer response */
6512                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
6513                         /* NameServer */
6514                         pring->prt[3].type = FC_TYPE_CT;
6515                         pring->prt[3].lpfc_sli_rcv_unsol_event =
6516                             lpfc_ct_unsol_event;
6517                         /* abort unsolicited sequence */
6518                         pring->prt[4].profile = 0;      /* Mask 4 */
6519                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6520                         pring->prt[4].type = FC_TYPE_BLS;
6521                         pring->prt[4].lpfc_sli_rcv_unsol_event =
6522                             lpfc_sli4_ct_abort_unsol_event;
6523                         break;
6524                 }
6525                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6526                                 (pring->numRiocb * pring->sizeRiocb);
6527         }
6528         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6529                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6530                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6531                        "SLI2 SLIM Data: x%x x%lx\n",
6532                        phba->brd_no, totiocbsize,
6533                        (unsigned long) MAX_SLIM_IOCB_SIZE);
6534         }
6535         if (phba->cfg_multi_ring_support == 2)
6536                 lpfc_extra_ring_setup(phba);
6537
6538         return 0;
6539 }
6540
6541 /**
6542  * lpfc_sli_queue_setup - Queue initialization function
6543  * @phba: Pointer to HBA context object.
6544  *
6545  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6546  * ring. This function also initializes ring indices of each ring.
6547  * This function is called during the initialization of the SLI
6548  * interface of an HBA.
6549  * This function is called with no lock held and always returns
6550  * 1.
6551  **/
6552 int
6553 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6554 {
6555         struct lpfc_sli *psli;
6556         struct lpfc_sli_ring *pring;
6557         int i;
6558
6559         psli = &phba->sli;
6560         spin_lock_irq(&phba->hbalock);
6561         INIT_LIST_HEAD(&psli->mboxq);
6562         INIT_LIST_HEAD(&psli->mboxq_cmpl);
6563         /* Initialize list headers for txq and txcmplq as double linked lists */
6564         for (i = 0; i < psli->num_rings; i++) {
6565                 pring = &psli->ring[i];
6566                 pring->ringno = i;
6567                 pring->next_cmdidx  = 0;
6568                 pring->local_getidx = 0;
6569                 pring->cmdidx = 0;
6570                 INIT_LIST_HEAD(&pring->txq);
6571                 INIT_LIST_HEAD(&pring->txcmplq);
6572                 INIT_LIST_HEAD(&pring->iocb_continueq);
6573                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6574                 INIT_LIST_HEAD(&pring->postbufq);
6575         }
6576         spin_unlock_irq(&phba->hbalock);
6577         return 1;
6578 }
6579
6580 /**
6581  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6582  * @phba: Pointer to HBA context object.
6583  *
6584  * This routine flushes the mailbox command subsystem. It will unconditionally
6585  * flush all the mailbox commands in the three possible stages in the mailbox
6586  * command sub-system: pending mailbox command queue; the outstanding mailbox
6587  * command; and completed mailbox command queue. It is caller's responsibility
6588  * to make sure that the driver is in the proper state to flush the mailbox
6589  * command sub-system. Namely, the posting of mailbox commands into the
6590  * pending mailbox command queue from the various clients must be stopped;
6591  * either the HBA is in a state that it will never works on the outstanding
6592  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6593  * mailbox command has been completed.
6594  **/
6595 static void
6596 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6597 {
6598         LIST_HEAD(completions);
6599         struct lpfc_sli *psli = &phba->sli;
6600         LPFC_MBOXQ_t *pmb;
6601         unsigned long iflag;
6602
6603         /* Flush all the mailbox commands in the mbox system */
6604         spin_lock_irqsave(&phba->hbalock, iflag);
6605         /* The pending mailbox command queue */
6606         list_splice_init(&phba->sli.mboxq, &completions);
6607         /* The outstanding active mailbox command */
6608         if (psli->mbox_active) {
6609                 list_add_tail(&psli->mbox_active->list, &completions);
6610                 psli->mbox_active = NULL;
6611                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6612         }
6613         /* The completed mailbox command queue */
6614         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6615         spin_unlock_irqrestore(&phba->hbalock, iflag);
6616
6617         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6618         while (!list_empty(&completions)) {
6619                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6620                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6621                 if (pmb->mbox_cmpl)
6622                         pmb->mbox_cmpl(phba, pmb);
6623         }
6624 }
6625
6626 /**
6627  * lpfc_sli_host_down - Vport cleanup function
6628  * @vport: Pointer to virtual port object.
6629  *
6630  * lpfc_sli_host_down is called to clean up the resources
6631  * associated with a vport before destroying virtual
6632  * port data structures.
6633  * This function does following operations:
6634  * - Free discovery resources associated with this virtual
6635  *   port.
6636  * - Free iocbs associated with this virtual port in
6637  *   the txq.
6638  * - Send abort for all iocb commands associated with this
6639  *   vport in txcmplq.
6640  *
6641  * This function is called with no lock held and always returns 1.
6642  **/
6643 int
6644 lpfc_sli_host_down(struct lpfc_vport *vport)
6645 {
6646         LIST_HEAD(completions);
6647         struct lpfc_hba *phba = vport->phba;
6648         struct lpfc_sli *psli = &phba->sli;
6649         struct lpfc_sli_ring *pring;
6650         struct lpfc_iocbq *iocb, *next_iocb;
6651         int i;
6652         unsigned long flags = 0;
6653         uint16_t prev_pring_flag;
6654
6655         lpfc_cleanup_discovery_resources(vport);
6656
6657         spin_lock_irqsave(&phba->hbalock, flags);
6658         for (i = 0; i < psli->num_rings; i++) {
6659                 pring = &psli->ring[i];
6660                 prev_pring_flag = pring->flag;
6661                 /* Only slow rings */
6662                 if (pring->ringno == LPFC_ELS_RING) {
6663                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6664                         /* Set the lpfc data pending flag */
6665                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6666                 }
6667                 /*
6668                  * Error everything on the txq since these iocbs have not been
6669                  * given to the FW yet.
6670                  */
6671                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6672                         if (iocb->vport != vport)
6673                                 continue;
6674                         list_move_tail(&iocb->list, &completions);
6675                         pring->txq_cnt--;
6676                 }
6677
6678                 /* Next issue ABTS for everything on the txcmplq */
6679                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6680                                                                         list) {
6681                         if (iocb->vport != vport)
6682                                 continue;
6683                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6684                 }
6685
6686                 pring->flag = prev_pring_flag;
6687         }
6688
6689         spin_unlock_irqrestore(&phba->hbalock, flags);
6690
6691         /* Cancel all the IOCBs from the completions list */
6692         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6693                               IOERR_SLI_DOWN);
6694         return 1;
6695 }
6696
6697 /**
6698  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6699  * @phba: Pointer to HBA context object.
6700  *
6701  * This function cleans up all iocb, buffers, mailbox commands
6702  * while shutting down the HBA. This function is called with no
6703  * lock held and always returns 1.
6704  * This function does the following to cleanup driver resources:
6705  * - Free discovery resources for each virtual port
6706  * - Cleanup any pending fabric iocbs
6707  * - Iterate through the iocb txq and free each entry
6708  *   in the list.
6709  * - Free up any buffer posted to the HBA
6710  * - Free mailbox commands in the mailbox queue.
6711  **/
6712 int
6713 lpfc_sli_hba_down(struct lpfc_hba *phba)
6714 {
6715         LIST_HEAD(completions);
6716         struct lpfc_sli *psli = &phba->sli;
6717         struct lpfc_sli_ring *pring;
6718         struct lpfc_dmabuf *buf_ptr;
6719         unsigned long flags = 0;
6720         int i;
6721
6722         /* Shutdown the mailbox command sub-system */
6723         lpfc_sli_mbox_sys_shutdown(phba);
6724
6725         lpfc_hba_down_prep(phba);
6726
6727         lpfc_fabric_abort_hba(phba);
6728
6729         spin_lock_irqsave(&phba->hbalock, flags);
6730         for (i = 0; i < psli->num_rings; i++) {
6731                 pring = &psli->ring[i];
6732                 /* Only slow rings */
6733                 if (pring->ringno == LPFC_ELS_RING) {
6734                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6735                         /* Set the lpfc data pending flag */
6736                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6737                 }
6738
6739                 /*
6740                  * Error everything on the txq since these iocbs have not been
6741                  * given to the FW yet.
6742                  */
6743                 list_splice_init(&pring->txq, &completions);
6744                 pring->txq_cnt = 0;
6745
6746         }
6747         spin_unlock_irqrestore(&phba->hbalock, flags);
6748
6749         /* Cancel all the IOCBs from the completions list */
6750         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6751                               IOERR_SLI_DOWN);
6752
6753         spin_lock_irqsave(&phba->hbalock, flags);
6754         list_splice_init(&phba->elsbuf, &completions);
6755         phba->elsbuf_cnt = 0;
6756         phba->elsbuf_prev_cnt = 0;
6757         spin_unlock_irqrestore(&phba->hbalock, flags);
6758
6759         while (!list_empty(&completions)) {
6760                 list_remove_head(&completions, buf_ptr,
6761                         struct lpfc_dmabuf, list);
6762                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6763                 kfree(buf_ptr);
6764         }
6765
6766         /* Return any active mbox cmds */
6767         del_timer_sync(&psli->mbox_tmo);
6768
6769         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6770         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6771         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6772
6773         return 1;
6774 }
6775
6776 /**
6777  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6778  * @phba: Pointer to HBA context object.
6779  *
6780  * This function cleans up all queues, iocb, buffers, mailbox commands while
6781  * shutting down the SLI4 HBA FCoE function. This function is called with no
6782  * lock held and always returns 1.
6783  *
6784  * This function does the following to cleanup driver FCoE function resources:
6785  * - Free discovery resources for each virtual port
6786  * - Cleanup any pending fabric iocbs
6787  * - Iterate through the iocb txq and free each entry in the list.
6788  * - Free up any buffer posted to the HBA.
6789  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6790  * - Free mailbox commands in the mailbox queue.
6791  **/
6792 int
6793 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6794 {
6795         /* Stop the SLI4 device port */
6796         lpfc_stop_port(phba);
6797
6798         /* Tear down the queues in the HBA */
6799         lpfc_sli4_queue_unset(phba);
6800
6801         /* unregister default FCFI from the HBA */
6802         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6803
6804         return 1;
6805 }
6806
6807 /**
6808  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6809  * @srcp: Source memory pointer.
6810  * @destp: Destination memory pointer.
6811  * @cnt: Number of words required to be copied.
6812  *
6813  * This function is used for copying data between driver memory
6814  * and the SLI memory. This function also changes the endianness
6815  * of each word if native endianness is different from SLI
6816  * endianness. This function can be called with or without
6817  * lock.
6818  **/
6819 void
6820 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6821 {
6822         uint32_t *src = srcp;
6823         uint32_t *dest = destp;
6824         uint32_t ldata;
6825         int i;
6826
6827         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6828                 ldata = *src;
6829                 ldata = le32_to_cpu(ldata);
6830                 *dest = ldata;
6831                 src++;
6832                 dest++;
6833         }
6834 }
6835
6836
6837 /**
6838  * lpfc_sli_bemem_bcopy - SLI memory copy function
6839  * @srcp: Source memory pointer.
6840  * @destp: Destination memory pointer.
6841  * @cnt: Number of words required to be copied.
6842  *
6843  * This function is used for copying data between a data structure
6844  * with big endian representation to local endianness.
6845  * This function can be called with or without lock.
6846  **/
6847 void
6848 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6849 {
6850         uint32_t *src = srcp;
6851         uint32_t *dest = destp;
6852         uint32_t ldata;
6853         int i;
6854
6855         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6856                 ldata = *src;
6857                 ldata = be32_to_cpu(ldata);
6858                 *dest = ldata;
6859                 src++;
6860                 dest++;
6861         }
6862 }
6863
6864 /**
6865  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6866  * @phba: Pointer to HBA context object.
6867  * @pring: Pointer to driver SLI ring object.
6868  * @mp: Pointer to driver buffer object.
6869  *
6870  * This function is called with no lock held.
6871  * It always return zero after adding the buffer to the postbufq
6872  * buffer list.
6873  **/
6874 int
6875 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6876                          struct lpfc_dmabuf *mp)
6877 {
6878         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6879            later */
6880         spin_lock_irq(&phba->hbalock);
6881         list_add_tail(&mp->list, &pring->postbufq);
6882         pring->postbufq_cnt++;
6883         spin_unlock_irq(&phba->hbalock);
6884         return 0;
6885 }
6886
6887 /**
6888  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
6889  * @phba: Pointer to HBA context object.
6890  *
6891  * When HBQ is enabled, buffers are searched based on tags. This function
6892  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6893  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6894  * does not conflict with tags of buffer posted for unsolicited events.
6895  * The function returns the allocated tag. The function is called with
6896  * no locks held.
6897  **/
6898 uint32_t
6899 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6900 {
6901         spin_lock_irq(&phba->hbalock);
6902         phba->buffer_tag_count++;
6903         /*
6904          * Always set the QUE_BUFTAG_BIT to distiguish between
6905          * a tag assigned by HBQ.
6906          */
6907         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6908         spin_unlock_irq(&phba->hbalock);
6909         return phba->buffer_tag_count;
6910 }
6911
6912 /**
6913  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
6914  * @phba: Pointer to HBA context object.
6915  * @pring: Pointer to driver SLI ring object.
6916  * @tag: Buffer tag.
6917  *
6918  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6919  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6920  * iocb is posted to the response ring with the tag of the buffer.
6921  * This function searches the pring->postbufq list using the tag
6922  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6923  * iocb. If the buffer is found then lpfc_dmabuf object of the
6924  * buffer is returned to the caller else NULL is returned.
6925  * This function is called with no lock held.
6926  **/
6927 struct lpfc_dmabuf *
6928 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6929                         uint32_t tag)
6930 {
6931         struct lpfc_dmabuf *mp, *next_mp;
6932         struct list_head *slp = &pring->postbufq;
6933
6934         /* Search postbufq, from the begining, looking for a match on tag */
6935         spin_lock_irq(&phba->hbalock);
6936         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6937                 if (mp->buffer_tag == tag) {
6938                         list_del_init(&mp->list);
6939                         pring->postbufq_cnt--;
6940                         spin_unlock_irq(&phba->hbalock);
6941                         return mp;
6942                 }
6943         }
6944
6945         spin_unlock_irq(&phba->hbalock);
6946         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6947                         "0402 Cannot find virtual addr for buffer tag on "
6948                         "ring %d Data x%lx x%p x%p x%x\n",
6949                         pring->ringno, (unsigned long) tag,
6950                         slp->next, slp->prev, pring->postbufq_cnt);
6951
6952         return NULL;
6953 }
6954
6955 /**
6956  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
6957  * @phba: Pointer to HBA context object.
6958  * @pring: Pointer to driver SLI ring object.
6959  * @phys: DMA address of the buffer.
6960  *
6961  * This function searches the buffer list using the dma_address
6962  * of unsolicited event to find the driver's lpfc_dmabuf object
6963  * corresponding to the dma_address. The function returns the
6964  * lpfc_dmabuf object if a buffer is found else it returns NULL.
6965  * This function is called by the ct and els unsolicited event
6966  * handlers to get the buffer associated with the unsolicited
6967  * event.
6968  *
6969  * This function is called with no lock held.
6970  **/
6971 struct lpfc_dmabuf *
6972 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6973                          dma_addr_t phys)
6974 {
6975         struct lpfc_dmabuf *mp, *next_mp;
6976         struct list_head *slp = &pring->postbufq;
6977
6978         /* Search postbufq, from the begining, looking for a match on phys */
6979         spin_lock_irq(&phba->hbalock);
6980         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6981                 if (mp->phys == phys) {
6982                         list_del_init(&mp->list);
6983                         pring->postbufq_cnt--;
6984                         spin_unlock_irq(&phba->hbalock);
6985                         return mp;
6986                 }
6987         }
6988
6989         spin_unlock_irq(&phba->hbalock);
6990         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6991                         "0410 Cannot find virtual addr for mapped buf on "
6992                         "ring %d Data x%llx x%p x%p x%x\n",
6993                         pring->ringno, (unsigned long long)phys,
6994                         slp->next, slp->prev, pring->postbufq_cnt);
6995         return NULL;
6996 }
6997
6998 /**
6999  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7000  * @phba: Pointer to HBA context object.
7001  * @cmdiocb: Pointer to driver command iocb object.
7002  * @rspiocb: Pointer to driver response iocb object.
7003  *
7004  * This function is the completion handler for the abort iocbs for
7005  * ELS commands. This function is called from the ELS ring event
7006  * handler with no lock held. This function frees memory resources
7007  * associated with the abort iocb.
7008  **/
7009 static void
7010 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7011                         struct lpfc_iocbq *rspiocb)
7012 {
7013         IOCB_t *irsp = &rspiocb->iocb;
7014         uint16_t abort_iotag, abort_context;
7015         struct lpfc_iocbq *abort_iocb;
7016         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7017
7018         abort_iocb = NULL;
7019
7020         if (irsp->ulpStatus) {
7021                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7022                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7023
7024                 spin_lock_irq(&phba->hbalock);
7025                 if (phba->sli_rev < LPFC_SLI_REV4) {
7026                         if (abort_iotag != 0 &&
7027                                 abort_iotag <= phba->sli.last_iotag)
7028                                 abort_iocb =
7029                                         phba->sli.iocbq_lookup[abort_iotag];
7030                 } else
7031                         /* For sli4 the abort_tag is the XRI,
7032                          * so the abort routine puts the iotag  of the iocb
7033                          * being aborted in the context field of the abort
7034                          * IOCB.
7035                          */
7036                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7037
7038                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
7039                                 "0327 Cannot abort els iocb %p "
7040                                 "with tag %x context %x, abort status %x, "
7041                                 "abort code %x\n",
7042                                 abort_iocb, abort_iotag, abort_context,
7043                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7044
7045                 /*
7046                  *  If the iocb is not found in Firmware queue the iocb
7047                  *  might have completed already. Do not free it again.
7048                  */
7049                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7050                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7051                                 spin_unlock_irq(&phba->hbalock);
7052                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7053                                 return;
7054                         }
7055                         /* For SLI4 the ulpContext field for abort IOCB
7056                          * holds the iotag of the IOCB being aborted so
7057                          * the local abort_context needs to be reset to
7058                          * match the aborted IOCBs ulpContext.
7059                          */
7060                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7061                                 abort_context = abort_iocb->iocb.ulpContext;
7062                 }
7063                 /*
7064                  * make sure we have the right iocbq before taking it
7065                  * off the txcmplq and try to call completion routine.
7066                  */
7067                 if (!abort_iocb ||
7068                     abort_iocb->iocb.ulpContext != abort_context ||
7069                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7070                         spin_unlock_irq(&phba->hbalock);
7071                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7072                         /*
7073                          * leave the SLI4 aborted command on the txcmplq
7074                          * list and the command complete WCQE's XB bit
7075                          * will tell whether the SGL (XRI) can be released
7076                          * immediately or to the aborted SGL list for the
7077                          * following abort XRI from the HBA.
7078                          */
7079                         list_del_init(&abort_iocb->list);
7080                         pring->txcmplq_cnt--;
7081                         spin_unlock_irq(&phba->hbalock);
7082
7083                         /* Firmware could still be in progress of DMAing
7084                          * payload, so don't free data buffer till after
7085                          * a hbeat.
7086                          */
7087                         spin_lock_irq(&phba->hbalock);
7088                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7089                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7090                         spin_unlock_irq(&phba->hbalock);
7091
7092                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7093                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7094                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7095                 }
7096         }
7097
7098         lpfc_sli_release_iocbq(phba, cmdiocb);
7099         return;
7100 }
7101
7102 /**
7103  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7104  * @phba: Pointer to HBA context object.
7105  * @cmdiocb: Pointer to driver command iocb object.
7106  * @rspiocb: Pointer to driver response iocb object.
7107  *
7108  * The function is called from SLI ring event handler with no
7109  * lock held. This function is the completion handler for ELS commands
7110  * which are aborted. The function frees memory resources used for
7111  * the aborted ELS commands.
7112  **/
7113 static void
7114 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7115                      struct lpfc_iocbq *rspiocb)
7116 {
7117         IOCB_t *irsp = &rspiocb->iocb;
7118
7119         /* ELS cmd tag <ulpIoTag> completes */
7120         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7121                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7122                         "x%x x%x x%x\n",
7123                         irsp->ulpIoTag, irsp->ulpStatus,
7124                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7125         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7126                 lpfc_ct_free_iocb(phba, cmdiocb);
7127         else
7128                 lpfc_els_free_iocb(phba, cmdiocb);
7129         return;
7130 }
7131
7132 /**
7133  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7134  * @phba: Pointer to HBA context object.
7135  * @pring: Pointer to driver SLI ring object.
7136  * @cmdiocb: Pointer to driver command iocb object.
7137  *
7138  * This function issues an abort iocb for the provided command
7139  * iocb. This function is called with hbalock held.
7140  * The function returns 0 when it fails due to memory allocation
7141  * failure or when the command iocb is an abort request.
7142  **/
7143 int
7144 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7145                            struct lpfc_iocbq *cmdiocb)
7146 {
7147         struct lpfc_vport *vport = cmdiocb->vport;
7148         struct lpfc_iocbq *abtsiocbp;
7149         IOCB_t *icmd = NULL;
7150         IOCB_t *iabt = NULL;
7151         int retval = IOCB_ERROR;
7152
7153         /*
7154          * There are certain command types we don't want to abort.  And we
7155          * don't want to abort commands that are already in the process of
7156          * being aborted.
7157          */
7158         icmd = &cmdiocb->iocb;
7159         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7160             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7161             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7162                 return 0;
7163
7164         /* If we're unloading, don't abort iocb on the ELS ring, but change the
7165          * callback so that nothing happens when it finishes.
7166          */
7167         if ((vport->load_flag & FC_UNLOADING) &&
7168             (pring->ringno == LPFC_ELS_RING)) {
7169                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7170                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7171                 else
7172                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7173                 goto abort_iotag_exit;
7174         }
7175
7176         /* issue ABTS for this IOCB based on iotag */
7177         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7178         if (abtsiocbp == NULL)
7179                 return 0;
7180
7181         /* This signals the response to set the correct status
7182          * before calling the completion handler
7183          */
7184         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7185
7186         iabt = &abtsiocbp->iocb;
7187         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7188         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7189         if (phba->sli_rev == LPFC_SLI_REV4) {
7190                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7191                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7192         }
7193         else
7194                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7195         iabt->ulpLe = 1;
7196         iabt->ulpClass = icmd->ulpClass;
7197
7198         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7199         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7200         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7201                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7202
7203         if (phba->link_state >= LPFC_LINK_UP)
7204                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7205         else
7206                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7207
7208         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7209
7210         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7211                          "0339 Abort xri x%x, original iotag x%x, "
7212                          "abort cmd iotag x%x\n",
7213                          iabt->un.acxri.abortContextTag,
7214                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
7215         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7216
7217         if (retval)
7218                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7219 abort_iotag_exit:
7220         /*
7221          * Caller to this routine should check for IOCB_ERROR
7222          * and handle it properly.  This routine no longer removes
7223          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7224          */
7225         return retval;
7226 }
7227
7228 /**
7229  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7230  * @iocbq: Pointer to driver iocb object.
7231  * @vport: Pointer to driver virtual port object.
7232  * @tgt_id: SCSI ID of the target.
7233  * @lun_id: LUN ID of the scsi device.
7234  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7235  *
7236  * This function acts as an iocb filter for functions which abort or count
7237  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7238  * 0 if the filtering criteria is met for the given iocb and will return
7239  * 1 if the filtering criteria is not met.
7240  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7241  * given iocb is for the SCSI device specified by vport, tgt_id and
7242  * lun_id parameter.
7243  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7244  * given iocb is for the SCSI target specified by vport and tgt_id
7245  * parameters.
7246  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7247  * given iocb is for the SCSI host associated with the given vport.
7248  * This function is called with no locks held.
7249  **/
7250 static int
7251 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7252                            uint16_t tgt_id, uint64_t lun_id,
7253                            lpfc_ctx_cmd ctx_cmd)
7254 {
7255         struct lpfc_scsi_buf *lpfc_cmd;
7256         int rc = 1;
7257
7258         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7259                 return rc;
7260
7261         if (iocbq->vport != vport)
7262                 return rc;
7263
7264         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7265
7266         if (lpfc_cmd->pCmd == NULL)
7267                 return rc;
7268
7269         switch (ctx_cmd) {
7270         case LPFC_CTX_LUN:
7271                 if ((lpfc_cmd->rdata->pnode) &&
7272                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7273                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7274                         rc = 0;
7275                 break;
7276         case LPFC_CTX_TGT:
7277                 if ((lpfc_cmd->rdata->pnode) &&
7278                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7279                         rc = 0;
7280                 break;
7281         case LPFC_CTX_HOST:
7282                 rc = 0;
7283                 break;
7284         default:
7285                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7286                         __func__, ctx_cmd);
7287                 break;
7288         }
7289
7290         return rc;
7291 }
7292
7293 /**
7294  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7295  * @vport: Pointer to virtual port.
7296  * @tgt_id: SCSI ID of the target.
7297  * @lun_id: LUN ID of the scsi device.
7298  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7299  *
7300  * This function returns number of FCP commands pending for the vport.
7301  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7302  * commands pending on the vport associated with SCSI device specified
7303  * by tgt_id and lun_id parameters.
7304  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7305  * commands pending on the vport associated with SCSI target specified
7306  * by tgt_id parameter.
7307  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7308  * commands pending on the vport.
7309  * This function returns the number of iocbs which satisfy the filter.
7310  * This function is called without any lock held.
7311  **/
7312 int
7313 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7314                   lpfc_ctx_cmd ctx_cmd)
7315 {
7316         struct lpfc_hba *phba = vport->phba;
7317         struct lpfc_iocbq *iocbq;
7318         int sum, i;
7319
7320         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7321                 iocbq = phba->sli.iocbq_lookup[i];
7322
7323                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7324                                                 ctx_cmd) == 0)
7325                         sum++;
7326         }
7327
7328         return sum;
7329 }
7330
7331 /**
7332  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7333  * @phba: Pointer to HBA context object
7334  * @cmdiocb: Pointer to command iocb object.
7335  * @rspiocb: Pointer to response iocb object.
7336  *
7337  * This function is called when an aborted FCP iocb completes. This
7338  * function is called by the ring event handler with no lock held.
7339  * This function frees the iocb.
7340  **/
7341 void
7342 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7343                         struct lpfc_iocbq *rspiocb)
7344 {
7345         lpfc_sli_release_iocbq(phba, cmdiocb);
7346         return;
7347 }
7348
7349 /**
7350  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7351  * @vport: Pointer to virtual port.
7352  * @pring: Pointer to driver SLI ring object.
7353  * @tgt_id: SCSI ID of the target.
7354  * @lun_id: LUN ID of the scsi device.
7355  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7356  *
7357  * This function sends an abort command for every SCSI command
7358  * associated with the given virtual port pending on the ring
7359  * filtered by lpfc_sli_validate_fcp_iocb function.
7360  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7361  * FCP iocbs associated with lun specified by tgt_id and lun_id
7362  * parameters
7363  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7364  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7365  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7366  * FCP iocbs associated with virtual port.
7367  * This function returns number of iocbs it failed to abort.
7368  * This function is called with no locks held.
7369  **/
7370 int
7371 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7372                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7373 {
7374         struct lpfc_hba *phba = vport->phba;
7375         struct lpfc_iocbq *iocbq;
7376         struct lpfc_iocbq *abtsiocb;
7377         IOCB_t *cmd = NULL;
7378         int errcnt = 0, ret_val = 0;
7379         int i;
7380
7381         for (i = 1; i <= phba->sli.last_iotag; i++) {
7382                 iocbq = phba->sli.iocbq_lookup[i];
7383
7384                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7385                                                abort_cmd) != 0)
7386                         continue;
7387
7388                 /* issue ABTS for this IOCB based on iotag */
7389                 abtsiocb = lpfc_sli_get_iocbq(phba);
7390                 if (abtsiocb == NULL) {
7391                         errcnt++;
7392                         continue;
7393                 }
7394
7395                 cmd = &iocbq->iocb;
7396                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7397                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7398                 if (phba->sli_rev == LPFC_SLI_REV4)
7399                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7400                 else
7401                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7402                 abtsiocb->iocb.ulpLe = 1;
7403                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7404                 abtsiocb->vport = phba->pport;
7405
7406                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7407                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
7408                 if (iocbq->iocb_flag & LPFC_IO_FCP)
7409                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
7410
7411                 if (lpfc_is_link_up(phba))
7412                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7413                 else
7414                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7415
7416                 /* Setup callback routine and issue the command. */
7417                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7418                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7419                                               abtsiocb, 0);
7420                 if (ret_val == IOCB_ERROR) {
7421                         lpfc_sli_release_iocbq(phba, abtsiocb);
7422                         errcnt++;
7423                         continue;
7424                 }
7425         }
7426
7427         return errcnt;
7428 }
7429
7430 /**
7431  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7432  * @phba: Pointer to HBA context object.
7433  * @cmdiocbq: Pointer to command iocb.
7434  * @rspiocbq: Pointer to response iocb.
7435  *
7436  * This function is the completion handler for iocbs issued using
7437  * lpfc_sli_issue_iocb_wait function. This function is called by the
7438  * ring event handler function without any lock held. This function
7439  * can be called from both worker thread context and interrupt
7440  * context. This function also can be called from other thread which
7441  * cleans up the SLI layer objects.
7442  * This function copy the contents of the response iocb to the
7443  * response iocb memory object provided by the caller of
7444  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7445  * sleeps for the iocb completion.
7446  **/
7447 static void
7448 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7449                         struct lpfc_iocbq *cmdiocbq,
7450                         struct lpfc_iocbq *rspiocbq)
7451 {
7452         wait_queue_head_t *pdone_q;
7453         unsigned long iflags;
7454
7455         spin_lock_irqsave(&phba->hbalock, iflags);
7456         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7457         if (cmdiocbq->context2 && rspiocbq)
7458                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7459                        &rspiocbq->iocb, sizeof(IOCB_t));
7460
7461         pdone_q = cmdiocbq->context_un.wait_queue;
7462         if (pdone_q)
7463                 wake_up(pdone_q);
7464         spin_unlock_irqrestore(&phba->hbalock, iflags);
7465         return;
7466 }
7467
7468 /**
7469  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7470  * @phba: Pointer to HBA context object..
7471  * @piocbq: Pointer to command iocb.
7472  * @flag: Flag to test.
7473  *
7474  * This routine grabs the hbalock and then test the iocb_flag to
7475  * see if the passed in flag is set.
7476  * Returns:
7477  * 1 if flag is set.
7478  * 0 if flag is not set.
7479  **/
7480 static int
7481 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7482                  struct lpfc_iocbq *piocbq, uint32_t flag)
7483 {
7484         unsigned long iflags;
7485         int ret;
7486
7487         spin_lock_irqsave(&phba->hbalock, iflags);
7488         ret = piocbq->iocb_flag & flag;
7489         spin_unlock_irqrestore(&phba->hbalock, iflags);
7490         return ret;
7491
7492 }
7493
7494 /**
7495  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7496  * @phba: Pointer to HBA context object..
7497  * @pring: Pointer to sli ring.
7498  * @piocb: Pointer to command iocb.
7499  * @prspiocbq: Pointer to response iocb.
7500  * @timeout: Timeout in number of seconds.
7501  *
7502  * This function issues the iocb to firmware and waits for the
7503  * iocb to complete. If the iocb command is not
7504  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7505  * Caller should not free the iocb resources if this function
7506  * returns IOCB_TIMEDOUT.
7507  * The function waits for the iocb completion using an
7508  * non-interruptible wait.
7509  * This function will sleep while waiting for iocb completion.
7510  * So, this function should not be called from any context which
7511  * does not allow sleeping. Due to the same reason, this function
7512  * cannot be called with interrupt disabled.
7513  * This function assumes that the iocb completions occur while
7514  * this function sleep. So, this function cannot be called from
7515  * the thread which process iocb completion for this ring.
7516  * This function clears the iocb_flag of the iocb object before
7517  * issuing the iocb and the iocb completion handler sets this
7518  * flag and wakes this thread when the iocb completes.
7519  * The contents of the response iocb will be copied to prspiocbq
7520  * by the completion handler when the command completes.
7521  * This function returns IOCB_SUCCESS when success.
7522  * This function is called with no lock held.
7523  **/
7524 int
7525 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7526                          uint32_t ring_number,
7527                          struct lpfc_iocbq *piocb,
7528                          struct lpfc_iocbq *prspiocbq,
7529                          uint32_t timeout)
7530 {
7531         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7532         long timeleft, timeout_req = 0;
7533         int retval = IOCB_SUCCESS;
7534         uint32_t creg_val;
7535
7536         /*
7537          * If the caller has provided a response iocbq buffer, then context2
7538          * is NULL or its an error.
7539          */
7540         if (prspiocbq) {
7541                 if (piocb->context2)
7542                         return IOCB_ERROR;
7543                 piocb->context2 = prspiocbq;
7544         }
7545
7546         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7547         piocb->context_un.wait_queue = &done_q;
7548         piocb->iocb_flag &= ~LPFC_IO_WAKE;
7549
7550         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7551                 creg_val = readl(phba->HCregaddr);
7552                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7553                 writel(creg_val, phba->HCregaddr);
7554                 readl(phba->HCregaddr); /* flush */
7555         }
7556
7557         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
7558         if (retval == IOCB_SUCCESS) {
7559                 timeout_req = timeout * HZ;
7560                 timeleft = wait_event_timeout(done_q,
7561                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
7562                                 timeout_req);
7563
7564                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7565                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7566                                         "0331 IOCB wake signaled\n");
7567                 } else if (timeleft == 0) {
7568                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7569                                         "0338 IOCB wait timeout error - no "
7570                                         "wake response Data x%x\n", timeout);
7571                         retval = IOCB_TIMEDOUT;
7572                 } else {
7573                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7574                                         "0330 IOCB wake NOT set, "
7575                                         "Data x%x x%lx\n",
7576                                         timeout, (timeleft / jiffies));
7577                         retval = IOCB_TIMEDOUT;
7578                 }
7579         } else {
7580                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7581                                 "0332 IOCB wait issue failed, Data x%x\n",
7582                                 retval);
7583                 retval = IOCB_ERROR;
7584         }
7585
7586         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7587                 creg_val = readl(phba->HCregaddr);
7588                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7589                 writel(creg_val, phba->HCregaddr);
7590                 readl(phba->HCregaddr); /* flush */
7591         }
7592
7593         if (prspiocbq)
7594                 piocb->context2 = NULL;
7595
7596         piocb->context_un.wait_queue = NULL;
7597         piocb->iocb_cmpl = NULL;
7598         return retval;
7599 }
7600
7601 /**
7602  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7603  * @phba: Pointer to HBA context object.
7604  * @pmboxq: Pointer to driver mailbox object.
7605  * @timeout: Timeout in number of seconds.
7606  *
7607  * This function issues the mailbox to firmware and waits for the
7608  * mailbox command to complete. If the mailbox command is not
7609  * completed within timeout seconds, it returns MBX_TIMEOUT.
7610  * The function waits for the mailbox completion using an
7611  * interruptible wait. If the thread is woken up due to a
7612  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7613  * should not free the mailbox resources, if this function returns
7614  * MBX_TIMEOUT.
7615  * This function will sleep while waiting for mailbox completion.
7616  * So, this function should not be called from any context which
7617  * does not allow sleeping. Due to the same reason, this function
7618  * cannot be called with interrupt disabled.
7619  * This function assumes that the mailbox completion occurs while
7620  * this function sleep. So, this function cannot be called from
7621  * the worker thread which processes mailbox completion.
7622  * This function is called in the context of HBA management
7623  * applications.
7624  * This function returns MBX_SUCCESS when successful.
7625  * This function is called with no lock held.
7626  **/
7627 int
7628 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7629                          uint32_t timeout)
7630 {
7631         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7632         int retval;
7633         unsigned long flag;
7634
7635         /* The caller must leave context1 empty. */
7636         if (pmboxq->context1)
7637                 return MBX_NOT_FINISHED;
7638
7639         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7640         /* setup wake call as IOCB callback */
7641         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7642         /* setup context field to pass wait_queue pointer to wake function  */
7643         pmboxq->context1 = &done_q;
7644
7645         /* now issue the command */
7646         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7647
7648         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7649                 wait_event_interruptible_timeout(done_q,
7650                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7651                                 timeout * HZ);
7652
7653                 spin_lock_irqsave(&phba->hbalock, flag);
7654                 pmboxq->context1 = NULL;
7655                 /*
7656                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
7657                  * else do not free the resources.
7658                  */
7659                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
7660                         retval = MBX_SUCCESS;
7661                 else {
7662                         retval = MBX_TIMEOUT;
7663                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7664                 }
7665                 spin_unlock_irqrestore(&phba->hbalock, flag);
7666         }
7667
7668         return retval;
7669 }
7670
7671 /**
7672  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7673  * @phba: Pointer to HBA context.
7674  *
7675  * This function is called to shutdown the driver's mailbox sub-system.
7676  * It first marks the mailbox sub-system is in a block state to prevent
7677  * the asynchronous mailbox command from issued off the pending mailbox
7678  * command queue. If the mailbox command sub-system shutdown is due to
7679  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7680  * the mailbox sub-system flush routine to forcefully bring down the
7681  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7682  * as with offline or HBA function reset), this routine will wait for the
7683  * outstanding mailbox command to complete before invoking the mailbox
7684  * sub-system flush routine to gracefully bring down mailbox sub-system.
7685  **/
7686 void
7687 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7688 {
7689         struct lpfc_sli *psli = &phba->sli;
7690         uint8_t actcmd = MBX_HEARTBEAT;
7691         unsigned long timeout;
7692
7693         spin_lock_irq(&phba->hbalock);
7694         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7695         spin_unlock_irq(&phba->hbalock);
7696
7697         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7698                 spin_lock_irq(&phba->hbalock);
7699                 if (phba->sli.mbox_active)
7700                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7701                 spin_unlock_irq(&phba->hbalock);
7702                 /* Determine how long we might wait for the active mailbox
7703                  * command to be gracefully completed by firmware.
7704                  */
7705                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7706                                            1000) + jiffies;
7707                 while (phba->sli.mbox_active) {
7708                         /* Check active mailbox complete status every 2ms */
7709                         msleep(2);
7710                         if (time_after(jiffies, timeout))
7711                                 /* Timeout, let the mailbox flush routine to
7712                                  * forcefully release active mailbox command
7713                                  */
7714                                 break;
7715                 }
7716         }
7717         lpfc_sli_mbox_sys_flush(phba);
7718 }
7719
7720 /**
7721  * lpfc_sli_eratt_read - read sli-3 error attention events
7722  * @phba: Pointer to HBA context.
7723  *
7724  * This function is called to read the SLI3 device error attention registers
7725  * for possible error attention events. The caller must hold the hostlock
7726  * with spin_lock_irq().
7727  *
7728  * This fucntion returns 1 when there is Error Attention in the Host Attention
7729  * Register and returns 0 otherwise.
7730  **/
7731 static int
7732 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7733 {
7734         uint32_t ha_copy;
7735
7736         /* Read chip Host Attention (HA) register */
7737         ha_copy = readl(phba->HAregaddr);
7738         if (ha_copy & HA_ERATT) {
7739                 /* Read host status register to retrieve error event */
7740                 lpfc_sli_read_hs(phba);
7741
7742                 /* Check if there is a deferred error condition is active */
7743                 if ((HS_FFER1 & phba->work_hs) &&
7744                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7745                      HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7746                         phba->hba_flag |= DEFER_ERATT;
7747                         /* Clear all interrupt enable conditions */
7748                         writel(0, phba->HCregaddr);
7749                         readl(phba->HCregaddr);
7750                 }
7751
7752                 /* Set the driver HA work bitmap */
7753                 phba->work_ha |= HA_ERATT;
7754                 /* Indicate polling handles this ERATT */
7755                 phba->hba_flag |= HBA_ERATT_HANDLED;
7756                 return 1;
7757         }
7758         return 0;
7759 }
7760
7761 /**
7762  * lpfc_sli4_eratt_read - read sli-4 error attention events
7763  * @phba: Pointer to HBA context.
7764  *
7765  * This function is called to read the SLI4 device error attention registers
7766  * for possible error attention events. The caller must hold the hostlock
7767  * with spin_lock_irq().
7768  *
7769  * This fucntion returns 1 when there is Error Attention in the Host Attention
7770  * Register and returns 0 otherwise.
7771  **/
7772 static int
7773 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7774 {
7775         uint32_t uerr_sta_hi, uerr_sta_lo;
7776
7777         /* For now, use the SLI4 device internal unrecoverable error
7778          * registers for error attention. This can be changed later.
7779          */
7780         uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7781         uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7782         if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7783             (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7784                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7785                                 "1423 HBA Unrecoverable error: "
7786                                 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7787                                 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7788                                 uerr_sta_lo, uerr_sta_hi,
7789                                 phba->sli4_hba.ue_mask_lo,
7790                                 phba->sli4_hba.ue_mask_hi);
7791                 phba->work_status[0] = uerr_sta_lo;
7792                 phba->work_status[1] = uerr_sta_hi;
7793                 /* Set the driver HA work bitmap */
7794                 phba->work_ha |= HA_ERATT;
7795                 /* Indicate polling handles this ERATT */
7796                 phba->hba_flag |= HBA_ERATT_HANDLED;
7797                 return 1;
7798         }
7799         return 0;
7800 }
7801
7802 /**
7803  * lpfc_sli_check_eratt - check error attention events
7804  * @phba: Pointer to HBA context.
7805  *
7806  * This function is called from timer soft interrupt context to check HBA's
7807  * error attention register bit for error attention events.
7808  *
7809  * This fucntion returns 1 when there is Error Attention in the Host Attention
7810  * Register and returns 0 otherwise.
7811  **/
7812 int
7813 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7814 {
7815         uint32_t ha_copy;
7816
7817         /* If somebody is waiting to handle an eratt, don't process it
7818          * here. The brdkill function will do this.
7819          */
7820         if (phba->link_flag & LS_IGNORE_ERATT)
7821                 return 0;
7822
7823         /* Check if interrupt handler handles this ERATT */
7824         spin_lock_irq(&phba->hbalock);
7825         if (phba->hba_flag & HBA_ERATT_HANDLED) {
7826                 /* Interrupt handler has handled ERATT */
7827                 spin_unlock_irq(&phba->hbalock);
7828                 return 0;
7829         }
7830
7831         /*
7832          * If there is deferred error attention, do not check for error
7833          * attention
7834          */
7835         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7836                 spin_unlock_irq(&phba->hbalock);
7837                 return 0;
7838         }
7839
7840         /* If PCI channel is offline, don't process it */
7841         if (unlikely(pci_channel_offline(phba->pcidev))) {
7842                 spin_unlock_irq(&phba->hbalock);
7843                 return 0;
7844         }
7845
7846         switch (phba->sli_rev) {
7847         case LPFC_SLI_REV2:
7848         case LPFC_SLI_REV3:
7849                 /* Read chip Host Attention (HA) register */
7850                 ha_copy = lpfc_sli_eratt_read(phba);
7851                 break;
7852         case LPFC_SLI_REV4:
7853                 /* Read devcie Uncoverable Error (UERR) registers */
7854                 ha_copy = lpfc_sli4_eratt_read(phba);
7855                 break;
7856         default:
7857                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7858                                 "0299 Invalid SLI revision (%d)\n",
7859                                 phba->sli_rev);
7860                 ha_copy = 0;
7861                 break;
7862         }
7863         spin_unlock_irq(&phba->hbalock);
7864
7865         return ha_copy;
7866 }
7867
7868 /**
7869  * lpfc_intr_state_check - Check device state for interrupt handling
7870  * @phba: Pointer to HBA context.
7871  *
7872  * This inline routine checks whether a device or its PCI slot is in a state
7873  * that the interrupt should be handled.
7874  *
7875  * This function returns 0 if the device or the PCI slot is in a state that
7876  * interrupt should be handled, otherwise -EIO.
7877  */
7878 static inline int
7879 lpfc_intr_state_check(struct lpfc_hba *phba)
7880 {
7881         /* If the pci channel is offline, ignore all the interrupts */
7882         if (unlikely(pci_channel_offline(phba->pcidev)))
7883                 return -EIO;
7884
7885         /* Update device level interrupt statistics */
7886         phba->sli.slistat.sli_intr++;
7887
7888         /* Ignore all interrupts during initialization. */
7889         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7890                 return -EIO;
7891
7892         return 0;
7893 }
7894
7895 /**
7896  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
7897  * @irq: Interrupt number.
7898  * @dev_id: The device context pointer.
7899  *
7900  * This function is directly called from the PCI layer as an interrupt
7901  * service routine when device with SLI-3 interface spec is enabled with
7902  * MSI-X multi-message interrupt mode and there are slow-path events in
7903  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7904  * interrupt mode, this function is called as part of the device-level
7905  * interrupt handler. When the PCI slot is in error recovery or the HBA
7906  * is undergoing initialization, the interrupt handler will not process
7907  * the interrupt. The link attention and ELS ring attention events are
7908  * handled by the worker thread. The interrupt handler signals the worker
7909  * thread and returns for these events. This function is called without
7910  * any lock held. It gets the hbalock to access and update SLI data
7911  * structures.
7912  *
7913  * This function returns IRQ_HANDLED when interrupt is handled else it
7914  * returns IRQ_NONE.
7915  **/
7916 irqreturn_t
7917 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
7918 {
7919         struct lpfc_hba  *phba;
7920         uint32_t ha_copy, hc_copy;
7921         uint32_t work_ha_copy;
7922         unsigned long status;
7923         unsigned long iflag;
7924         uint32_t control;
7925
7926         MAILBOX_t *mbox, *pmbox;
7927         struct lpfc_vport *vport;
7928         struct lpfc_nodelist *ndlp;
7929         struct lpfc_dmabuf *mp;
7930         LPFC_MBOXQ_t *pmb;
7931         int rc;
7932
7933         /*
7934          * Get the driver's phba structure from the dev_id and
7935          * assume the HBA is not interrupting.
7936          */
7937         phba = (struct lpfc_hba *)dev_id;
7938
7939         if (unlikely(!phba))
7940                 return IRQ_NONE;
7941
7942         /*
7943          * Stuff needs to be attented to when this function is invoked as an
7944          * individual interrupt handler in MSI-X multi-message interrupt mode
7945          */
7946         if (phba->intr_type == MSIX) {
7947                 /* Check device state for handling interrupt */
7948                 if (lpfc_intr_state_check(phba))
7949                         return IRQ_NONE;
7950                 /* Need to read HA REG for slow-path events */
7951                 spin_lock_irqsave(&phba->hbalock, iflag);
7952                 ha_copy = readl(phba->HAregaddr);
7953                 /* If somebody is waiting to handle an eratt don't process it
7954                  * here. The brdkill function will do this.
7955                  */
7956                 if (phba->link_flag & LS_IGNORE_ERATT)
7957                         ha_copy &= ~HA_ERATT;
7958                 /* Check the need for handling ERATT in interrupt handler */
7959                 if (ha_copy & HA_ERATT) {
7960                         if (phba->hba_flag & HBA_ERATT_HANDLED)
7961                                 /* ERATT polling has handled ERATT */
7962                                 ha_copy &= ~HA_ERATT;
7963                         else
7964                                 /* Indicate interrupt handler handles ERATT */
7965                                 phba->hba_flag |= HBA_ERATT_HANDLED;
7966                 }
7967
7968                 /*
7969                  * If there is deferred error attention, do not check for any
7970                  * interrupt.
7971                  */
7972                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7973                         spin_unlock_irqrestore(&phba->hbalock, iflag);
7974                         return IRQ_NONE;
7975                 }
7976
7977                 /* Clear up only attention source related to slow-path */
7978                 hc_copy = readl(phba->HCregaddr);
7979                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
7980                         HC_LAINT_ENA | HC_ERINT_ENA),
7981                         phba->HCregaddr);
7982                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7983                         phba->HAregaddr);
7984                 writel(hc_copy, phba->HCregaddr);
7985                 readl(phba->HAregaddr); /* flush */
7986                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7987         } else
7988                 ha_copy = phba->ha_copy;
7989
7990         work_ha_copy = ha_copy & phba->work_ha_mask;
7991
7992         if (work_ha_copy) {
7993                 if (work_ha_copy & HA_LATT) {
7994                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7995                                 /*
7996                                  * Turn off Link Attention interrupts
7997                                  * until CLEAR_LA done
7998                                  */
7999                                 spin_lock_irqsave(&phba->hbalock, iflag);
8000                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8001                                 control = readl(phba->HCregaddr);
8002                                 control &= ~HC_LAINT_ENA;
8003                                 writel(control, phba->HCregaddr);
8004                                 readl(phba->HCregaddr); /* flush */
8005                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8006                         }
8007                         else
8008                                 work_ha_copy &= ~HA_LATT;
8009                 }
8010
8011                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8012                         /*
8013                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8014                          * the only slow ring.
8015                          */
8016                         status = (work_ha_copy &
8017                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8018                         status >>= (4*LPFC_ELS_RING);
8019                         if (status & HA_RXMASK) {
8020                                 spin_lock_irqsave(&phba->hbalock, iflag);
8021                                 control = readl(phba->HCregaddr);
8022
8023                                 lpfc_debugfs_slow_ring_trc(phba,
8024                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8025                                 control, status,
8026                                 (uint32_t)phba->sli.slistat.sli_intr);
8027
8028                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8029                                         lpfc_debugfs_slow_ring_trc(phba,
8030                                                 "ISR Disable ring:"
8031                                                 "pwork:x%x hawork:x%x wait:x%x",
8032                                                 phba->work_ha, work_ha_copy,
8033                                                 (uint32_t)((unsigned long)
8034                                                 &phba->work_waitq));
8035
8036                                         control &=
8037                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8038                                         writel(control, phba->HCregaddr);
8039                                         readl(phba->HCregaddr); /* flush */
8040                                 }
8041                                 else {
8042                                         lpfc_debugfs_slow_ring_trc(phba,
8043                                                 "ISR slow ring:   pwork:"
8044                                                 "x%x hawork:x%x wait:x%x",
8045                                                 phba->work_ha, work_ha_copy,
8046                                                 (uint32_t)((unsigned long)
8047                                                 &phba->work_waitq));
8048                                 }
8049                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8050                         }
8051                 }
8052                 spin_lock_irqsave(&phba->hbalock, iflag);
8053                 if (work_ha_copy & HA_ERATT) {
8054                         lpfc_sli_read_hs(phba);
8055                         /*
8056                          * Check if there is a deferred error condition
8057                          * is active
8058                          */
8059                         if ((HS_FFER1 & phba->work_hs) &&
8060                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8061                                 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8062                                 phba->hba_flag |= DEFER_ERATT;
8063                                 /* Clear all interrupt enable conditions */
8064                                 writel(0, phba->HCregaddr);
8065                                 readl(phba->HCregaddr);
8066                         }
8067                 }
8068
8069                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8070                         pmb = phba->sli.mbox_active;
8071                         pmbox = &pmb->u.mb;
8072                         mbox = phba->mbox;
8073                         vport = pmb->vport;
8074
8075                         /* First check out the status word */
8076                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8077                         if (pmbox->mbxOwner != OWN_HOST) {
8078                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8079                                 /*
8080                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8081                                  * mbxStatus <status>
8082                                  */
8083                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8084                                                 LOG_SLI,
8085                                                 "(%d):0304 Stray Mailbox "
8086                                                 "Interrupt mbxCommand x%x "
8087                                                 "mbxStatus x%x\n",
8088                                                 (vport ? vport->vpi : 0),
8089                                                 pmbox->mbxCommand,
8090                                                 pmbox->mbxStatus);
8091                                 /* clear mailbox attention bit */
8092                                 work_ha_copy &= ~HA_MBATT;
8093                         } else {
8094                                 phba->sli.mbox_active = NULL;
8095                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8096                                 phba->last_completion_time = jiffies;
8097                                 del_timer(&phba->sli.mbox_tmo);
8098                                 if (pmb->mbox_cmpl) {
8099                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8100                                                         MAILBOX_CMD_SIZE);
8101                                 }
8102                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8103                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8104
8105                                         lpfc_debugfs_disc_trc(vport,
8106                                                 LPFC_DISC_TRC_MBOX_VPORT,
8107                                                 "MBOX dflt rpi: : "
8108                                                 "status:x%x rpi:x%x",
8109                                                 (uint32_t)pmbox->mbxStatus,
8110                                                 pmbox->un.varWords[0], 0);
8111
8112                                         if (!pmbox->mbxStatus) {
8113                                                 mp = (struct lpfc_dmabuf *)
8114                                                         (pmb->context1);
8115                                                 ndlp = (struct lpfc_nodelist *)
8116                                                         pmb->context2;
8117
8118                                                 /* Reg_LOGIN of dflt RPI was
8119                                                  * successful. new lets get
8120                                                  * rid of the RPI using the
8121                                                  * same mbox buffer.
8122                                                  */
8123                                                 lpfc_unreg_login(phba,
8124                                                         vport->vpi,
8125                                                         pmbox->un.varWords[0],
8126                                                         pmb);
8127                                                 pmb->mbox_cmpl =
8128                                                         lpfc_mbx_cmpl_dflt_rpi;
8129                                                 pmb->context1 = mp;
8130                                                 pmb->context2 = ndlp;
8131                                                 pmb->vport = vport;
8132                                                 rc = lpfc_sli_issue_mbox(phba,
8133                                                                 pmb,
8134                                                                 MBX_NOWAIT);
8135                                                 if (rc != MBX_BUSY)
8136                                                         lpfc_printf_log(phba,
8137                                                         KERN_ERR,
8138                                                         LOG_MBOX | LOG_SLI,
8139                                                         "0350 rc should have"
8140                                                         "been MBX_BUSY\n");
8141                                                 if (rc != MBX_NOT_FINISHED)
8142                                                         goto send_current_mbox;
8143                                         }
8144                                 }
8145                                 spin_lock_irqsave(
8146                                                 &phba->pport->work_port_lock,
8147                                                 iflag);
8148                                 phba->pport->work_port_events &=
8149                                         ~WORKER_MBOX_TMO;
8150                                 spin_unlock_irqrestore(
8151                                                 &phba->pport->work_port_lock,
8152                                                 iflag);
8153                                 lpfc_mbox_cmpl_put(phba, pmb);
8154                         }
8155                 } else
8156                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8157
8158                 if ((work_ha_copy & HA_MBATT) &&
8159                     (phba->sli.mbox_active == NULL)) {
8160 send_current_mbox:
8161                         /* Process next mailbox command if there is one */
8162                         do {
8163                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8164                                                          MBX_NOWAIT);
8165                         } while (rc == MBX_NOT_FINISHED);
8166                         if (rc != MBX_SUCCESS)
8167                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8168                                                 LOG_SLI, "0349 rc should be "
8169                                                 "MBX_SUCCESS\n");
8170                 }
8171
8172                 spin_lock_irqsave(&phba->hbalock, iflag);
8173                 phba->work_ha |= work_ha_copy;
8174                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8175                 lpfc_worker_wake_up(phba);
8176         }
8177         return IRQ_HANDLED;
8178
8179 } /* lpfc_sli_sp_intr_handler */
8180
8181 /**
8182  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8183  * @irq: Interrupt number.
8184  * @dev_id: The device context pointer.
8185  *
8186  * This function is directly called from the PCI layer as an interrupt
8187  * service routine when device with SLI-3 interface spec is enabled with
8188  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8189  * ring event in the HBA. However, when the device is enabled with either
8190  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8191  * device-level interrupt handler. When the PCI slot is in error recovery
8192  * or the HBA is undergoing initialization, the interrupt handler will not
8193  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8194  * the intrrupt context. This function is called without any lock held.
8195  * It gets the hbalock to access and update SLI data structures.
8196  *
8197  * This function returns IRQ_HANDLED when interrupt is handled else it
8198  * returns IRQ_NONE.
8199  **/
8200 irqreturn_t
8201 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8202 {
8203         struct lpfc_hba  *phba;
8204         uint32_t ha_copy;
8205         unsigned long status;
8206         unsigned long iflag;
8207
8208         /* Get the driver's phba structure from the dev_id and
8209          * assume the HBA is not interrupting.
8210          */
8211         phba = (struct lpfc_hba *) dev_id;
8212
8213         if (unlikely(!phba))
8214                 return IRQ_NONE;
8215
8216         /*
8217          * Stuff needs to be attented to when this function is invoked as an
8218          * individual interrupt handler in MSI-X multi-message interrupt mode
8219          */
8220         if (phba->intr_type == MSIX) {
8221                 /* Check device state for handling interrupt */
8222                 if (lpfc_intr_state_check(phba))
8223                         return IRQ_NONE;
8224                 /* Need to read HA REG for FCP ring and other ring events */
8225                 ha_copy = readl(phba->HAregaddr);
8226                 /* Clear up only attention source related to fast-path */
8227                 spin_lock_irqsave(&phba->hbalock, iflag);
8228                 /*
8229                  * If there is deferred error attention, do not check for
8230                  * any interrupt.
8231                  */
8232                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8233                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8234                         return IRQ_NONE;
8235                 }
8236                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8237                         phba->HAregaddr);
8238                 readl(phba->HAregaddr); /* flush */
8239                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8240         } else
8241                 ha_copy = phba->ha_copy;
8242
8243         /*
8244          * Process all events on FCP ring. Take the optimized path for FCP IO.
8245          */
8246         ha_copy &= ~(phba->work_ha_mask);
8247
8248         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8249         status >>= (4*LPFC_FCP_RING);
8250         if (status & HA_RXMASK)
8251                 lpfc_sli_handle_fast_ring_event(phba,
8252                                                 &phba->sli.ring[LPFC_FCP_RING],
8253                                                 status);
8254
8255         if (phba->cfg_multi_ring_support == 2) {
8256                 /*
8257                  * Process all events on extra ring. Take the optimized path
8258                  * for extra ring IO.
8259                  */
8260                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8261                 status >>= (4*LPFC_EXTRA_RING);
8262                 if (status & HA_RXMASK) {
8263                         lpfc_sli_handle_fast_ring_event(phba,
8264                                         &phba->sli.ring[LPFC_EXTRA_RING],
8265                                         status);
8266                 }
8267         }
8268         return IRQ_HANDLED;
8269 }  /* lpfc_sli_fp_intr_handler */
8270
8271 /**
8272  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8273  * @irq: Interrupt number.
8274  * @dev_id: The device context pointer.
8275  *
8276  * This function is the HBA device-level interrupt handler to device with
8277  * SLI-3 interface spec, called from the PCI layer when either MSI or
8278  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8279  * requires driver attention. This function invokes the slow-path interrupt
8280  * attention handling function and fast-path interrupt attention handling
8281  * function in turn to process the relevant HBA attention events. This
8282  * function is called without any lock held. It gets the hbalock to access
8283  * and update SLI data structures.
8284  *
8285  * This function returns IRQ_HANDLED when interrupt is handled, else it
8286  * returns IRQ_NONE.
8287  **/
8288 irqreturn_t
8289 lpfc_sli_intr_handler(int irq, void *dev_id)
8290 {
8291         struct lpfc_hba  *phba;
8292         irqreturn_t sp_irq_rc, fp_irq_rc;
8293         unsigned long status1, status2;
8294         uint32_t hc_copy;
8295
8296         /*
8297          * Get the driver's phba structure from the dev_id and
8298          * assume the HBA is not interrupting.
8299          */
8300         phba = (struct lpfc_hba *) dev_id;
8301
8302         if (unlikely(!phba))
8303                 return IRQ_NONE;
8304
8305         /* Check device state for handling interrupt */
8306         if (lpfc_intr_state_check(phba))
8307                 return IRQ_NONE;
8308
8309         spin_lock(&phba->hbalock);
8310         phba->ha_copy = readl(phba->HAregaddr);
8311         if (unlikely(!phba->ha_copy)) {
8312                 spin_unlock(&phba->hbalock);
8313                 return IRQ_NONE;
8314         } else if (phba->ha_copy & HA_ERATT) {
8315                 if (phba->hba_flag & HBA_ERATT_HANDLED)
8316                         /* ERATT polling has handled ERATT */
8317                         phba->ha_copy &= ~HA_ERATT;
8318                 else
8319                         /* Indicate interrupt handler handles ERATT */
8320                         phba->hba_flag |= HBA_ERATT_HANDLED;
8321         }
8322
8323         /*
8324          * If there is deferred error attention, do not check for any interrupt.
8325          */
8326         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8327                 spin_unlock_irq(&phba->hbalock);
8328                 return IRQ_NONE;
8329         }
8330
8331         /* Clear attention sources except link and error attentions */
8332         hc_copy = readl(phba->HCregaddr);
8333         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8334                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8335                 phba->HCregaddr);
8336         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8337         writel(hc_copy, phba->HCregaddr);
8338         readl(phba->HAregaddr); /* flush */
8339         spin_unlock(&phba->hbalock);
8340
8341         /*
8342          * Invokes slow-path host attention interrupt handling as appropriate.
8343          */
8344
8345         /* status of events with mailbox and link attention */
8346         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8347
8348         /* status of events with ELS ring */
8349         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8350         status2 >>= (4*LPFC_ELS_RING);
8351
8352         if (status1 || (status2 & HA_RXMASK))
8353                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8354         else
8355                 sp_irq_rc = IRQ_NONE;
8356
8357         /*
8358          * Invoke fast-path host attention interrupt handling as appropriate.
8359          */
8360
8361         /* status of events with FCP ring */
8362         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8363         status1 >>= (4*LPFC_FCP_RING);
8364
8365         /* status of events with extra ring */
8366         if (phba->cfg_multi_ring_support == 2) {
8367                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8368                 status2 >>= (4*LPFC_EXTRA_RING);
8369         } else
8370                 status2 = 0;
8371
8372         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8373                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8374         else
8375                 fp_irq_rc = IRQ_NONE;
8376
8377         /* Return device-level interrupt handling status */
8378         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8379 }  /* lpfc_sli_intr_handler */
8380
8381 /**
8382  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8383  * @phba: pointer to lpfc hba data structure.
8384  *
8385  * This routine is invoked by the worker thread to process all the pending
8386  * SLI4 FCP abort XRI events.
8387  **/
8388 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8389 {
8390         struct lpfc_cq_event *cq_event;
8391
8392         /* First, declare the fcp xri abort event has been handled */
8393         spin_lock_irq(&phba->hbalock);
8394         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8395         spin_unlock_irq(&phba->hbalock);
8396         /* Now, handle all the fcp xri abort events */
8397         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8398                 /* Get the first event from the head of the event queue */
8399                 spin_lock_irq(&phba->hbalock);
8400                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8401                                  cq_event, struct lpfc_cq_event, list);
8402                 spin_unlock_irq(&phba->hbalock);
8403                 /* Notify aborted XRI for FCP work queue */
8404                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8405                 /* Free the event processed back to the free pool */
8406                 lpfc_sli4_cq_event_release(phba, cq_event);
8407         }
8408 }
8409
8410 /**
8411  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8412  * @phba: pointer to lpfc hba data structure.
8413  *
8414  * This routine is invoked by the worker thread to process all the pending
8415  * SLI4 els abort xri events.
8416  **/
8417 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8418 {
8419         struct lpfc_cq_event *cq_event;
8420
8421         /* First, declare the els xri abort event has been handled */
8422         spin_lock_irq(&phba->hbalock);
8423         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8424         spin_unlock_irq(&phba->hbalock);
8425         /* Now, handle all the els xri abort events */
8426         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8427                 /* Get the first event from the head of the event queue */
8428                 spin_lock_irq(&phba->hbalock);
8429                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8430                                  cq_event, struct lpfc_cq_event, list);
8431                 spin_unlock_irq(&phba->hbalock);
8432                 /* Notify aborted XRI for ELS work queue */
8433                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8434                 /* Free the event processed back to the free pool */
8435                 lpfc_sli4_cq_event_release(phba, cq_event);
8436         }
8437 }
8438
8439 /**
8440  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8441  * @phba: pointer to lpfc hba data structure
8442  * @pIocbIn: pointer to the rspiocbq
8443  * @pIocbOut: pointer to the cmdiocbq
8444  * @wcqe: pointer to the complete wcqe
8445  *
8446  * This routine transfers the fields of a command iocbq to a response iocbq
8447  * by copying all the IOCB fields from command iocbq and transferring the
8448  * completion status information from the complete wcqe.
8449  **/
8450 static void
8451 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8452                               struct lpfc_iocbq *pIocbIn,
8453                               struct lpfc_iocbq *pIocbOut,
8454                               struct lpfc_wcqe_complete *wcqe)
8455 {
8456         unsigned long iflags;
8457         size_t offset = offsetof(struct lpfc_iocbq, iocb);
8458
8459         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8460                sizeof(struct lpfc_iocbq) - offset);
8461         /* Map WCQE parameters into irspiocb parameters */
8462         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8463         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8464                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8465                         pIocbIn->iocb.un.fcpi.fcpi_parm =
8466                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
8467                                         wcqe->total_data_placed;
8468                 else
8469                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8470         else {
8471                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8472                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8473         }
8474
8475         /* Pick up HBA exchange busy condition */
8476         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8477                 spin_lock_irqsave(&phba->hbalock, iflags);
8478                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8479                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8480         }
8481 }
8482
8483 /**
8484  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8485  * @phba: Pointer to HBA context object.
8486  * @wcqe: Pointer to work-queue completion queue entry.
8487  *
8488  * This routine handles an ELS work-queue completion event and construct
8489  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8490  * discovery engine to handle.
8491  *
8492  * Return: Pointer to the receive IOCBQ, NULL otherwise.
8493  **/
8494 static struct lpfc_iocbq *
8495 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8496                                struct lpfc_iocbq *irspiocbq)
8497 {
8498         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8499         struct lpfc_iocbq *cmdiocbq;
8500         struct lpfc_wcqe_complete *wcqe;
8501         unsigned long iflags;
8502
8503         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8504         spin_lock_irqsave(&phba->hbalock, iflags);
8505         pring->stats.iocb_event++;
8506         /* Look up the ELS command IOCB and create pseudo response IOCB */
8507         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8508                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8509         spin_unlock_irqrestore(&phba->hbalock, iflags);
8510
8511         if (unlikely(!cmdiocbq)) {
8512                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8513                                 "0386 ELS complete with no corresponding "
8514                                 "cmdiocb: iotag (%d)\n",
8515                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8516                 lpfc_sli_release_iocbq(phba, irspiocbq);
8517                 return NULL;
8518         }
8519
8520         /* Fake the irspiocbq and copy necessary response information */
8521         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
8522
8523         return irspiocbq;
8524 }
8525
8526 /**
8527  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8528  * @phba: Pointer to HBA context object.
8529  * @cqe: Pointer to mailbox completion queue entry.
8530  *
8531  * This routine process a mailbox completion queue entry with asynchrous
8532  * event.
8533  *
8534  * Return: true if work posted to worker thread, otherwise false.
8535  **/
8536 static bool
8537 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8538 {
8539         struct lpfc_cq_event *cq_event;
8540         unsigned long iflags;
8541
8542         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8543                         "0392 Async Event: word0:x%x, word1:x%x, "
8544                         "word2:x%x, word3:x%x\n", mcqe->word0,
8545                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8546
8547         /* Allocate a new internal CQ_EVENT entry */
8548         cq_event = lpfc_sli4_cq_event_alloc(phba);
8549         if (!cq_event) {
8550                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8551                                 "0394 Failed to allocate CQ_EVENT entry\n");
8552                 return false;
8553         }
8554
8555         /* Move the CQE into an asynchronous event entry */
8556         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8557         spin_lock_irqsave(&phba->hbalock, iflags);
8558         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8559         /* Set the async event flag */
8560         phba->hba_flag |= ASYNC_EVENT;
8561         spin_unlock_irqrestore(&phba->hbalock, iflags);
8562
8563         return true;
8564 }
8565
8566 /**
8567  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8568  * @phba: Pointer to HBA context object.
8569  * @cqe: Pointer to mailbox completion queue entry.
8570  *
8571  * This routine process a mailbox completion queue entry with mailbox
8572  * completion event.
8573  *
8574  * Return: true if work posted to worker thread, otherwise false.
8575  **/
8576 static bool
8577 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8578 {
8579         uint32_t mcqe_status;
8580         MAILBOX_t *mbox, *pmbox;
8581         struct lpfc_mqe *mqe;
8582         struct lpfc_vport *vport;
8583         struct lpfc_nodelist *ndlp;
8584         struct lpfc_dmabuf *mp;
8585         unsigned long iflags;
8586         LPFC_MBOXQ_t *pmb;
8587         bool workposted = false;
8588         int rc;
8589
8590         /* If not a mailbox complete MCQE, out by checking mailbox consume */
8591         if (!bf_get(lpfc_trailer_completed, mcqe))
8592                 goto out_no_mqe_complete;
8593
8594         /* Get the reference to the active mbox command */
8595         spin_lock_irqsave(&phba->hbalock, iflags);
8596         pmb = phba->sli.mbox_active;
8597         if (unlikely(!pmb)) {
8598                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8599                                 "1832 No pending MBOX command to handle\n");
8600                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8601                 goto out_no_mqe_complete;
8602         }
8603         spin_unlock_irqrestore(&phba->hbalock, iflags);
8604         mqe = &pmb->u.mqe;
8605         pmbox = (MAILBOX_t *)&pmb->u.mqe;
8606         mbox = phba->mbox;
8607         vport = pmb->vport;
8608
8609         /* Reset heartbeat timer */
8610         phba->last_completion_time = jiffies;
8611         del_timer(&phba->sli.mbox_tmo);
8612
8613         /* Move mbox data to caller's mailbox region, do endian swapping */
8614         if (pmb->mbox_cmpl && mbox)
8615                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8616         /* Set the mailbox status with SLI4 range 0x4000 */
8617         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8618         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8619                 bf_set(lpfc_mqe_status, mqe,
8620                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
8621
8622         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8623                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8624                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8625                                       "MBOX dflt rpi: status:x%x rpi:x%x",
8626                                       mcqe_status,
8627                                       pmbox->un.varWords[0], 0);
8628                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8629                         mp = (struct lpfc_dmabuf *)(pmb->context1);
8630                         ndlp = (struct lpfc_nodelist *)pmb->context2;
8631                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
8632                          * RID of the PPI using the same mbox buffer.
8633                          */
8634                         lpfc_unreg_login(phba, vport->vpi,
8635                                          pmbox->un.varWords[0], pmb);
8636                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8637                         pmb->context1 = mp;
8638                         pmb->context2 = ndlp;
8639                         pmb->vport = vport;
8640                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8641                         if (rc != MBX_BUSY)
8642                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8643                                                 LOG_SLI, "0385 rc should "
8644                                                 "have been MBX_BUSY\n");
8645                         if (rc != MBX_NOT_FINISHED)
8646                                 goto send_current_mbox;
8647                 }
8648         }
8649         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8650         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8651         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8652
8653         /* There is mailbox completion work to do */
8654         spin_lock_irqsave(&phba->hbalock, iflags);
8655         __lpfc_mbox_cmpl_put(phba, pmb);
8656         phba->work_ha |= HA_MBATT;
8657         spin_unlock_irqrestore(&phba->hbalock, iflags);
8658         workposted = true;
8659
8660 send_current_mbox:
8661         spin_lock_irqsave(&phba->hbalock, iflags);
8662         /* Release the mailbox command posting token */
8663         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8664         /* Setting active mailbox pointer need to be in sync to flag clear */
8665         phba->sli.mbox_active = NULL;
8666         spin_unlock_irqrestore(&phba->hbalock, iflags);
8667         /* Wake up worker thread to post the next pending mailbox command */
8668         lpfc_worker_wake_up(phba);
8669 out_no_mqe_complete:
8670         if (bf_get(lpfc_trailer_consumed, mcqe))
8671                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8672         return workposted;
8673 }
8674
8675 /**
8676  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8677  * @phba: Pointer to HBA context object.
8678  * @cqe: Pointer to mailbox completion queue entry.
8679  *
8680  * This routine process a mailbox completion queue entry, it invokes the
8681  * proper mailbox complete handling or asynchrous event handling routine
8682  * according to the MCQE's async bit.
8683  *
8684  * Return: true if work posted to worker thread, otherwise false.
8685  **/
8686 static bool
8687 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8688 {
8689         struct lpfc_mcqe mcqe;
8690         bool workposted;
8691
8692         /* Copy the mailbox MCQE and convert endian order as needed */
8693         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8694
8695         /* Invoke the proper event handling routine */
8696         if (!bf_get(lpfc_trailer_async, &mcqe))
8697                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8698         else
8699                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8700         return workposted;
8701 }
8702
8703 /**
8704  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8705  * @phba: Pointer to HBA context object.
8706  * @wcqe: Pointer to work-queue completion queue entry.
8707  *
8708  * This routine handles an ELS work-queue completion event.
8709  *
8710  * Return: true if work posted to worker thread, otherwise false.
8711  **/
8712 static bool
8713 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8714                              struct lpfc_wcqe_complete *wcqe)
8715 {
8716         struct lpfc_iocbq *irspiocbq;
8717         unsigned long iflags;
8718
8719         /* Get an irspiocbq for later ELS response processing use */
8720         irspiocbq = lpfc_sli_get_iocbq(phba);
8721         if (!irspiocbq) {
8722                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8723                                 "0387 Failed to allocate an iocbq\n");
8724                 return false;
8725         }
8726
8727         /* Save off the slow-path queue event for work thread to process */
8728         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
8729         spin_lock_irqsave(&phba->hbalock, iflags);
8730         list_add_tail(&irspiocbq->cq_event.list,
8731                       &phba->sli4_hba.sp_queue_event);
8732         phba->hba_flag |= HBA_SP_QUEUE_EVT;
8733         spin_unlock_irqrestore(&phba->hbalock, iflags);
8734
8735         return true;
8736 }
8737
8738 /**
8739  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8740  * @phba: Pointer to HBA context object.
8741  * @wcqe: Pointer to work-queue completion queue entry.
8742  *
8743  * This routine handles slow-path WQ entry comsumed event by invoking the
8744  * proper WQ release routine to the slow-path WQ.
8745  **/
8746 static void
8747 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8748                              struct lpfc_wcqe_release *wcqe)
8749 {
8750         /* Check for the slow-path ELS work queue */
8751         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8752                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8753                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8754         else
8755                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8756                                 "2579 Slow-path wqe consume event carries "
8757                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8758                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8759                                 phba->sli4_hba.els_wq->queue_id);
8760 }
8761
8762 /**
8763  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8764  * @phba: Pointer to HBA context object.
8765  * @cq: Pointer to a WQ completion queue.
8766  * @wcqe: Pointer to work-queue completion queue entry.
8767  *
8768  * This routine handles an XRI abort event.
8769  *
8770  * Return: true if work posted to worker thread, otherwise false.
8771  **/
8772 static bool
8773 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8774                                    struct lpfc_queue *cq,
8775                                    struct sli4_wcqe_xri_aborted *wcqe)
8776 {
8777         bool workposted = false;
8778         struct lpfc_cq_event *cq_event;
8779         unsigned long iflags;
8780
8781         /* Allocate a new internal CQ_EVENT entry */
8782         cq_event = lpfc_sli4_cq_event_alloc(phba);
8783         if (!cq_event) {
8784                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8785                                 "0602 Failed to allocate CQ_EVENT entry\n");
8786                 return false;
8787         }
8788
8789         /* Move the CQE into the proper xri abort event list */
8790         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8791         switch (cq->subtype) {
8792         case LPFC_FCP:
8793                 spin_lock_irqsave(&phba->hbalock, iflags);
8794                 list_add_tail(&cq_event->list,
8795                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8796                 /* Set the fcp xri abort event flag */
8797                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8798                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8799                 workposted = true;
8800                 break;
8801         case LPFC_ELS:
8802                 spin_lock_irqsave(&phba->hbalock, iflags);
8803                 list_add_tail(&cq_event->list,
8804                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8805                 /* Set the els xri abort event flag */
8806                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8807                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8808                 workposted = true;
8809                 break;
8810         default:
8811                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8812                                 "0603 Invalid work queue CQE subtype (x%x)\n",
8813                                 cq->subtype);
8814                 workposted = false;
8815                 break;
8816         }
8817         return workposted;
8818 }
8819
8820 /**
8821  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8822  * @phba: Pointer to HBA context object.
8823  * @rcqe: Pointer to receive-queue completion queue entry.
8824  *
8825  * This routine process a receive-queue completion queue entry.
8826  *
8827  * Return: true if work posted to worker thread, otherwise false.
8828  **/
8829 static bool
8830 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8831 {
8832         bool workposted = false;
8833         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8834         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8835         struct hbq_dmabuf *dma_buf;
8836         uint32_t status;
8837         unsigned long iflags;
8838
8839         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8840                 goto out;
8841
8842         status = bf_get(lpfc_rcqe_status, rcqe);
8843         switch (status) {
8844         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8845                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8846                                 "2537 Receive Frame Truncated!!\n");
8847         case FC_STATUS_RQ_SUCCESS:
8848                 lpfc_sli4_rq_release(hrq, drq);
8849                 spin_lock_irqsave(&phba->hbalock, iflags);
8850                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8851                 if (!dma_buf) {
8852                         spin_unlock_irqrestore(&phba->hbalock, iflags);
8853                         goto out;
8854                 }
8855                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8856                 /* save off the frame for the word thread to process */
8857                 list_add_tail(&dma_buf->cq_event.list,
8858                               &phba->sli4_hba.sp_queue_event);
8859                 /* Frame received */
8860                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
8861                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8862                 workposted = true;
8863                 break;
8864         case FC_STATUS_INSUFF_BUF_NEED_BUF:
8865         case FC_STATUS_INSUFF_BUF_FRM_DISC:
8866                 /* Post more buffers if possible */
8867                 spin_lock_irqsave(&phba->hbalock, iflags);
8868                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8869                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8870                 workposted = true;
8871                 break;
8872         }
8873 out:
8874         return workposted;
8875 }
8876
8877 /**
8878  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8879  * @phba: Pointer to HBA context object.
8880  * @cq: Pointer to the completion queue.
8881  * @wcqe: Pointer to a completion queue entry.
8882  *
8883  * This routine process a slow-path work-queue or recieve queue completion queue
8884  * entry.
8885  *
8886  * Return: true if work posted to worker thread, otherwise false.
8887  **/
8888 static bool
8889 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8890                          struct lpfc_cqe *cqe)
8891 {
8892         struct lpfc_cqe cqevt;
8893         bool workposted = false;
8894
8895         /* Copy the work queue CQE and convert endian order if needed */
8896         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
8897
8898         /* Check and process for different type of WCQE and dispatch */
8899         switch (bf_get(lpfc_cqe_code, &cqevt)) {
8900         case CQE_CODE_COMPL_WQE:
8901                 /* Process the WQ/RQ complete event */
8902                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8903                                 (struct lpfc_wcqe_complete *)&cqevt);
8904                 break;
8905         case CQE_CODE_RELEASE_WQE:
8906                 /* Process the WQ release event */
8907                 lpfc_sli4_sp_handle_rel_wcqe(phba,
8908                                 (struct lpfc_wcqe_release *)&cqevt);
8909                 break;
8910         case CQE_CODE_XRI_ABORTED:
8911                 /* Process the WQ XRI abort event */
8912                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8913                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
8914                 break;
8915         case CQE_CODE_RECEIVE:
8916                 /* Process the RQ event */
8917                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8918                                 (struct lpfc_rcqe *)&cqevt);
8919                 break;
8920         default:
8921                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8922                                 "0388 Not a valid WCQE code: x%x\n",
8923                                 bf_get(lpfc_cqe_code, &cqevt));
8924                 break;
8925         }
8926         return workposted;
8927 }
8928
8929 /**
8930  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8931  * @phba: Pointer to HBA context object.
8932  * @eqe: Pointer to fast-path event queue entry.
8933  *
8934  * This routine process a event queue entry from the slow-path event queue.
8935  * It will check the MajorCode and MinorCode to determine this is for a
8936  * completion event on a completion queue, if not, an error shall be logged
8937  * and just return. Otherwise, it will get to the corresponding completion
8938  * queue and process all the entries on that completion queue, rearm the
8939  * completion queue, and then return.
8940  *
8941  **/
8942 static void
8943 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8944 {
8945         struct lpfc_queue *cq = NULL, *childq, *speq;
8946         struct lpfc_cqe *cqe;
8947         bool workposted = false;
8948         int ecount = 0;
8949         uint16_t cqid;
8950
8951         if (bf_get(lpfc_eqe_major_code, eqe) != 0) {
8952                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8953                                 "0359 Not a valid slow-path completion "
8954                                 "event: majorcode=x%x, minorcode=x%x\n",
8955                                 bf_get(lpfc_eqe_major_code, eqe),
8956                                 bf_get(lpfc_eqe_minor_code, eqe));
8957                 return;
8958         }
8959
8960         /* Get the reference to the corresponding CQ */
8961         cqid = bf_get(lpfc_eqe_resource_id, eqe);
8962
8963         /* Search for completion queue pointer matching this cqid */
8964         speq = phba->sli4_hba.sp_eq;
8965         list_for_each_entry(childq, &speq->child_list, list) {
8966                 if (childq->queue_id == cqid) {
8967                         cq = childq;
8968                         break;
8969                 }
8970         }
8971         if (unlikely(!cq)) {
8972                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8973                                 "0365 Slow-path CQ identifier (%d) does "
8974                                 "not exist\n", cqid);
8975                 return;
8976         }
8977
8978         /* Process all the entries to the CQ */
8979         switch (cq->type) {
8980         case LPFC_MCQ:
8981                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8982                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8983                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8984                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8985                 }
8986                 break;
8987         case LPFC_WCQ:
8988                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8989                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
8990                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8991                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8992                 }
8993                 break;
8994         default:
8995                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8996                                 "0370 Invalid completion queue type (%d)\n",
8997                                 cq->type);
8998                 return;
8999         }
9000
9001         /* Catch the no cq entry condition, log an error */
9002         if (unlikely(ecount == 0))
9003                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9004                                 "0371 No entry from the CQ: identifier "
9005                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9006
9007         /* In any case, flash and re-arm the RCQ */
9008         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9009
9010         /* wake up worker thread if there are works to be done */
9011         if (workposted)
9012                 lpfc_worker_wake_up(phba);
9013 }
9014
9015 /**
9016  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9017  * @eqe: Pointer to fast-path completion queue entry.
9018  *
9019  * This routine process a fast-path work queue completion entry from fast-path
9020  * event queue for FCP command response completion.
9021  **/
9022 static void
9023 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9024                              struct lpfc_wcqe_complete *wcqe)
9025 {
9026         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9027         struct lpfc_iocbq *cmdiocbq;
9028         struct lpfc_iocbq irspiocbq;
9029         unsigned long iflags;
9030
9031         spin_lock_irqsave(&phba->hbalock, iflags);
9032         pring->stats.iocb_event++;
9033         spin_unlock_irqrestore(&phba->hbalock, iflags);
9034
9035         /* Check for response status */
9036         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9037                 /* If resource errors reported from HBA, reduce queue
9038                  * depth of the SCSI device.
9039                  */
9040                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9041                      IOSTAT_LOCAL_REJECT) &&
9042                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9043                         phba->lpfc_rampdown_queue_depth(phba);
9044                 }
9045                 /* Log the error status */
9046                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9047                                 "0373 FCP complete error: status=x%x, "
9048                                 "hw_status=x%x, total_data_specified=%d, "
9049                                 "parameter=x%x, word3=x%x\n",
9050                                 bf_get(lpfc_wcqe_c_status, wcqe),
9051                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9052                                 wcqe->total_data_placed, wcqe->parameter,
9053                                 wcqe->word3);
9054         }
9055
9056         /* Look up the FCP command IOCB and create pseudo response IOCB */
9057         spin_lock_irqsave(&phba->hbalock, iflags);
9058         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9059                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9060         spin_unlock_irqrestore(&phba->hbalock, iflags);
9061         if (unlikely(!cmdiocbq)) {
9062                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9063                                 "0374 FCP complete with no corresponding "
9064                                 "cmdiocb: iotag (%d)\n",
9065                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9066                 return;
9067         }
9068         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9069                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9070                                 "0375 FCP cmdiocb not callback function "
9071                                 "iotag: (%d)\n",
9072                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9073                 return;
9074         }
9075
9076         /* Fake the irspiocb and copy necessary response information */
9077         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9078
9079         /* Pass the cmd_iocb and the rsp state to the upper layer */
9080         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9081 }
9082
9083 /**
9084  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9085  * @phba: Pointer to HBA context object.
9086  * @cq: Pointer to completion queue.
9087  * @wcqe: Pointer to work-queue completion queue entry.
9088  *
9089  * This routine handles an fast-path WQ entry comsumed event by invoking the
9090  * proper WQ release routine to the slow-path WQ.
9091  **/
9092 static void
9093 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9094                              struct lpfc_wcqe_release *wcqe)
9095 {
9096         struct lpfc_queue *childwq;
9097         bool wqid_matched = false;
9098         uint16_t fcp_wqid;
9099
9100         /* Check for fast-path FCP work queue release */
9101         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9102         list_for_each_entry(childwq, &cq->child_list, list) {
9103                 if (childwq->queue_id == fcp_wqid) {
9104                         lpfc_sli4_wq_release(childwq,
9105                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9106                         wqid_matched = true;
9107                         break;
9108                 }
9109         }
9110         /* Report warning log message if no match found */
9111         if (wqid_matched != true)
9112                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9113                                 "2580 Fast-path wqe consume event carries "
9114                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9115 }
9116
9117 /**
9118  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9119  * @cq: Pointer to the completion queue.
9120  * @eqe: Pointer to fast-path completion queue entry.
9121  *
9122  * This routine process a fast-path work queue completion entry from fast-path
9123  * event queue for FCP command response completion.
9124  **/
9125 static int
9126 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9127                          struct lpfc_cqe *cqe)
9128 {
9129         struct lpfc_wcqe_release wcqe;
9130         bool workposted = false;
9131
9132         /* Copy the work queue CQE and convert endian order if needed */
9133         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9134
9135         /* Check and process for different type of WCQE and dispatch */
9136         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9137         case CQE_CODE_COMPL_WQE:
9138                 /* Process the WQ complete event */
9139                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9140                                 (struct lpfc_wcqe_complete *)&wcqe);
9141                 break;
9142         case CQE_CODE_RELEASE_WQE:
9143                 /* Process the WQ release event */
9144                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9145                                 (struct lpfc_wcqe_release *)&wcqe);
9146                 break;
9147         case CQE_CODE_XRI_ABORTED:
9148                 /* Process the WQ XRI abort event */
9149                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9150                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9151                 break;
9152         default:
9153                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9154                                 "0144 Not a valid WCQE code: x%x\n",
9155                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9156                 break;
9157         }
9158         return workposted;
9159 }
9160
9161 /**
9162  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9163  * @phba: Pointer to HBA context object.
9164  * @eqe: Pointer to fast-path event queue entry.
9165  *
9166  * This routine process a event queue entry from the fast-path event queue.
9167  * It will check the MajorCode and MinorCode to determine this is for a
9168  * completion event on a completion queue, if not, an error shall be logged
9169  * and just return. Otherwise, it will get to the corresponding completion
9170  * queue and process all the entries on the completion queue, rearm the
9171  * completion queue, and then return.
9172  **/
9173 static void
9174 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9175                         uint32_t fcp_cqidx)
9176 {
9177         struct lpfc_queue *cq;
9178         struct lpfc_cqe *cqe;
9179         bool workposted = false;
9180         uint16_t cqid;
9181         int ecount = 0;
9182
9183         if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0)) {
9184                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9185                                 "0366 Not a valid fast-path completion "
9186                                 "event: majorcode=x%x, minorcode=x%x\n",
9187                                 bf_get(lpfc_eqe_major_code, eqe),
9188                                 bf_get(lpfc_eqe_minor_code, eqe));
9189                 return;
9190         }
9191
9192         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9193         if (unlikely(!cq)) {
9194                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9195                                 "0367 Fast-path completion queue does not "
9196                                 "exist\n");
9197                 return;
9198         }
9199
9200         /* Get the reference to the corresponding CQ */
9201         cqid = bf_get(lpfc_eqe_resource_id, eqe);
9202         if (unlikely(cqid != cq->queue_id)) {
9203                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9204                                 "0368 Miss-matched fast-path completion "
9205                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9206                                 cqid, cq->queue_id);
9207                 return;
9208         }
9209
9210         /* Process all the entries to the CQ */
9211         while ((cqe = lpfc_sli4_cq_get(cq))) {
9212                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9213                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9214                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9215         }
9216
9217         /* Catch the no cq entry condition */
9218         if (unlikely(ecount == 0))
9219                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9220                                 "0369 No entry from fast-path completion "
9221                                 "queue fcpcqid=%d\n", cq->queue_id);
9222
9223         /* In any case, flash and re-arm the CQ */
9224         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9225
9226         /* wake up worker thread if there are works to be done */
9227         if (workposted)
9228                 lpfc_worker_wake_up(phba);
9229 }
9230
9231 static void
9232 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9233 {
9234         struct lpfc_eqe *eqe;
9235
9236         /* walk all the EQ entries and drop on the floor */
9237         while ((eqe = lpfc_sli4_eq_get(eq)))
9238                 ;
9239
9240         /* Clear and re-arm the EQ */
9241         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9242 }
9243
9244 /**
9245  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9246  * @irq: Interrupt number.
9247  * @dev_id: The device context pointer.
9248  *
9249  * This function is directly called from the PCI layer as an interrupt
9250  * service routine when device with SLI-4 interface spec is enabled with
9251  * MSI-X multi-message interrupt mode and there are slow-path events in
9252  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9253  * interrupt mode, this function is called as part of the device-level
9254  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9255  * undergoing initialization, the interrupt handler will not process the
9256  * interrupt. The link attention and ELS ring attention events are handled
9257  * by the worker thread. The interrupt handler signals the worker thread
9258  * and returns for these events. This function is called without any lock
9259  * held. It gets the hbalock to access and update SLI data structures.
9260  *
9261  * This function returns IRQ_HANDLED when interrupt is handled else it
9262  * returns IRQ_NONE.
9263  **/
9264 irqreturn_t
9265 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9266 {
9267         struct lpfc_hba *phba;
9268         struct lpfc_queue *speq;
9269         struct lpfc_eqe *eqe;
9270         unsigned long iflag;
9271         int ecount = 0;
9272
9273         /*
9274          * Get the driver's phba structure from the dev_id
9275          */
9276         phba = (struct lpfc_hba *)dev_id;
9277
9278         if (unlikely(!phba))
9279                 return IRQ_NONE;
9280
9281         /* Get to the EQ struct associated with this vector */
9282         speq = phba->sli4_hba.sp_eq;
9283
9284         /* Check device state for handling interrupt */
9285         if (unlikely(lpfc_intr_state_check(phba))) {
9286                 /* Check again for link_state with lock held */
9287                 spin_lock_irqsave(&phba->hbalock, iflag);
9288                 if (phba->link_state < LPFC_LINK_DOWN)
9289                         /* Flush, clear interrupt, and rearm the EQ */
9290                         lpfc_sli4_eq_flush(phba, speq);
9291                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9292                 return IRQ_NONE;
9293         }
9294
9295         /*
9296          * Process all the event on FCP slow-path EQ
9297          */
9298         while ((eqe = lpfc_sli4_eq_get(speq))) {
9299                 lpfc_sli4_sp_handle_eqe(phba, eqe);
9300                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9301                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9302         }
9303
9304         /* Always clear and re-arm the slow-path EQ */
9305         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9306
9307         /* Catch the no cq entry condition */
9308         if (unlikely(ecount == 0)) {
9309                 if (phba->intr_type == MSIX)
9310                         /* MSI-X treated interrupt served as no EQ share INT */
9311                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9312                                         "0357 MSI-X interrupt with no EQE\n");
9313                 else
9314                         /* Non MSI-X treated on interrupt as EQ share INT */
9315                         return IRQ_NONE;
9316         }
9317
9318         return IRQ_HANDLED;
9319 } /* lpfc_sli4_sp_intr_handler */
9320
9321 /**
9322  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9323  * @irq: Interrupt number.
9324  * @dev_id: The device context pointer.
9325  *
9326  * This function is directly called from the PCI layer as an interrupt
9327  * service routine when device with SLI-4 interface spec is enabled with
9328  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9329  * ring event in the HBA. However, when the device is enabled with either
9330  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9331  * device-level interrupt handler. When the PCI slot is in error recovery
9332  * or the HBA is undergoing initialization, the interrupt handler will not
9333  * process the interrupt. The SCSI FCP fast-path ring event are handled in
9334  * the intrrupt context. This function is called without any lock held.
9335  * It gets the hbalock to access and update SLI data structures. Note that,
9336  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9337  * equal to that of FCP CQ index.
9338  *
9339  * This function returns IRQ_HANDLED when interrupt is handled else it
9340  * returns IRQ_NONE.
9341  **/
9342 irqreturn_t
9343 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9344 {
9345         struct lpfc_hba *phba;
9346         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9347         struct lpfc_queue *fpeq;
9348         struct lpfc_eqe *eqe;
9349         unsigned long iflag;
9350         int ecount = 0;
9351         uint32_t fcp_eqidx;
9352
9353         /* Get the driver's phba structure from the dev_id */
9354         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9355         phba = fcp_eq_hdl->phba;
9356         fcp_eqidx = fcp_eq_hdl->idx;
9357
9358         if (unlikely(!phba))
9359                 return IRQ_NONE;
9360
9361         /* Get to the EQ struct associated with this vector */
9362         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9363
9364         /* Check device state for handling interrupt */
9365         if (unlikely(lpfc_intr_state_check(phba))) {
9366                 /* Check again for link_state with lock held */
9367                 spin_lock_irqsave(&phba->hbalock, iflag);
9368                 if (phba->link_state < LPFC_LINK_DOWN)
9369                         /* Flush, clear interrupt, and rearm the EQ */
9370                         lpfc_sli4_eq_flush(phba, fpeq);
9371                 spin_unlock_irqrestore(&phba->hbalock, iflag);
9372                 return IRQ_NONE;
9373         }
9374
9375         /*
9376          * Process all the event on FCP fast-path EQ
9377          */
9378         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9379                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9380                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9381                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9382         }
9383
9384         /* Always clear and re-arm the fast-path EQ */
9385         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9386
9387         if (unlikely(ecount == 0)) {
9388                 if (phba->intr_type == MSIX)
9389                         /* MSI-X treated interrupt served as no EQ share INT */
9390                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9391                                         "0358 MSI-X interrupt with no EQE\n");
9392                 else
9393                         /* Non MSI-X treated on interrupt as EQ share INT */
9394                         return IRQ_NONE;
9395         }
9396
9397         return IRQ_HANDLED;
9398 } /* lpfc_sli4_fp_intr_handler */
9399
9400 /**
9401  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9402  * @irq: Interrupt number.
9403  * @dev_id: The device context pointer.
9404  *
9405  * This function is the device-level interrupt handler to device with SLI-4
9406  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9407  * interrupt mode is enabled and there is an event in the HBA which requires
9408  * driver attention. This function invokes the slow-path interrupt attention
9409  * handling function and fast-path interrupt attention handling function in
9410  * turn to process the relevant HBA attention events. This function is called
9411  * without any lock held. It gets the hbalock to access and update SLI data
9412  * structures.
9413  *
9414  * This function returns IRQ_HANDLED when interrupt is handled, else it
9415  * returns IRQ_NONE.
9416  **/
9417 irqreturn_t
9418 lpfc_sli4_intr_handler(int irq, void *dev_id)
9419 {
9420         struct lpfc_hba  *phba;
9421         irqreturn_t sp_irq_rc, fp_irq_rc;
9422         bool fp_handled = false;
9423         uint32_t fcp_eqidx;
9424
9425         /* Get the driver's phba structure from the dev_id */
9426         phba = (struct lpfc_hba *)dev_id;
9427
9428         if (unlikely(!phba))
9429                 return IRQ_NONE;
9430
9431         /*
9432          * Invokes slow-path host attention interrupt handling as appropriate.
9433          */
9434         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9435
9436         /*
9437          * Invoke fast-path host attention interrupt handling as appropriate.
9438          */
9439         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9440                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9441                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9442                 if (fp_irq_rc == IRQ_HANDLED)
9443                         fp_handled |= true;
9444         }
9445
9446         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9447 } /* lpfc_sli4_intr_handler */
9448
9449 /**
9450  * lpfc_sli4_queue_free - free a queue structure and associated memory
9451  * @queue: The queue structure to free.
9452  *
9453  * This function frees a queue structure and the DMAable memeory used for
9454  * the host resident queue. This function must be called after destroying the
9455  * queue on the HBA.
9456  **/
9457 void
9458 lpfc_sli4_queue_free(struct lpfc_queue *queue)
9459 {
9460         struct lpfc_dmabuf *dmabuf;
9461
9462         if (!queue)
9463                 return;
9464
9465         while (!list_empty(&queue->page_list)) {
9466                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9467                                  list);
9468                 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9469                                   dmabuf->virt, dmabuf->phys);
9470                 kfree(dmabuf);
9471         }
9472         kfree(queue);
9473         return;
9474 }
9475
9476 /**
9477  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9478  * @phba: The HBA that this queue is being created on.
9479  * @entry_size: The size of each queue entry for this queue.
9480  * @entry count: The number of entries that this queue will handle.
9481  *
9482  * This function allocates a queue structure and the DMAable memory used for
9483  * the host resident queue. This function must be called before creating the
9484  * queue on the HBA.
9485  **/
9486 struct lpfc_queue *
9487 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9488                       uint32_t entry_count)
9489 {
9490         struct lpfc_queue *queue;
9491         struct lpfc_dmabuf *dmabuf;
9492         int x, total_qe_count;
9493         void *dma_pointer;
9494
9495
9496         queue = kzalloc(sizeof(struct lpfc_queue) +
9497                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9498         if (!queue)
9499                 return NULL;
9500         queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9501         INIT_LIST_HEAD(&queue->list);
9502         INIT_LIST_HEAD(&queue->page_list);
9503         INIT_LIST_HEAD(&queue->child_list);
9504         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9505                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9506                 if (!dmabuf)
9507                         goto out_fail;
9508                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9509                                                   PAGE_SIZE, &dmabuf->phys,
9510                                                   GFP_KERNEL);
9511                 if (!dmabuf->virt) {
9512                         kfree(dmabuf);
9513                         goto out_fail;
9514                 }
9515                 memset(dmabuf->virt, 0, PAGE_SIZE);
9516                 dmabuf->buffer_tag = x;
9517                 list_add_tail(&dmabuf->list, &queue->page_list);
9518                 /* initialize queue's entry array */
9519                 dma_pointer = dmabuf->virt;
9520                 for (; total_qe_count < entry_count &&
9521                      dma_pointer < (PAGE_SIZE + dmabuf->virt);
9522                      total_qe_count++, dma_pointer += entry_size) {
9523                         queue->qe[total_qe_count].address = dma_pointer;
9524                 }
9525         }
9526         queue->entry_size = entry_size;
9527         queue->entry_count = entry_count;
9528         queue->phba = phba;
9529
9530         return queue;
9531 out_fail:
9532         lpfc_sli4_queue_free(queue);
9533         return NULL;
9534 }
9535
9536 /**
9537  * lpfc_eq_create - Create an Event Queue on the HBA
9538  * @phba: HBA structure that indicates port to create a queue on.
9539  * @eq: The queue structure to use to create the event queue.
9540  * @imax: The maximum interrupt per second limit.
9541  *
9542  * This function creates an event queue, as detailed in @eq, on a port,
9543  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9544  *
9545  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9546  * is used to get the entry count and entry size that are necessary to
9547  * determine the number of pages to allocate and use for this queue. This
9548  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9549  * event queue. This function is asynchronous and will wait for the mailbox
9550  * command to finish before continuing.
9551  *
9552  * On success this function will return a zero. If unable to allocate enough
9553  * memory this function will return ENOMEM. If the queue create mailbox command
9554  * fails this function will return ENXIO.
9555  **/
9556 uint32_t
9557 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9558 {
9559         struct lpfc_mbx_eq_create *eq_create;
9560         LPFC_MBOXQ_t *mbox;
9561         int rc, length, status = 0;
9562         struct lpfc_dmabuf *dmabuf;
9563         uint32_t shdr_status, shdr_add_status;
9564         union lpfc_sli4_cfg_shdr *shdr;
9565         uint16_t dmult;
9566
9567         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9568         if (!mbox)
9569                 return -ENOMEM;
9570         length = (sizeof(struct lpfc_mbx_eq_create) -
9571                   sizeof(struct lpfc_sli4_cfg_mhdr));
9572         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9573                          LPFC_MBOX_OPCODE_EQ_CREATE,
9574                          length, LPFC_SLI4_MBX_EMBED);
9575         eq_create = &mbox->u.mqe.un.eq_create;
9576         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9577                eq->page_count);
9578         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9579                LPFC_EQE_SIZE);
9580         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9581         /* Calculate delay multiper from maximum interrupt per second */
9582         dmult = LPFC_DMULT_CONST/imax - 1;
9583         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9584                dmult);
9585         switch (eq->entry_count) {
9586         default:
9587                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9588                                 "0360 Unsupported EQ count. (%d)\n",
9589                                 eq->entry_count);
9590                 if (eq->entry_count < 256)
9591                         return -EINVAL;
9592                 /* otherwise default to smallest count (drop through) */
9593         case 256:
9594                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9595                        LPFC_EQ_CNT_256);
9596                 break;
9597         case 512:
9598                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9599                        LPFC_EQ_CNT_512);
9600                 break;
9601         case 1024:
9602                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9603                        LPFC_EQ_CNT_1024);
9604                 break;
9605         case 2048:
9606                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9607                        LPFC_EQ_CNT_2048);
9608                 break;
9609         case 4096:
9610                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9611                        LPFC_EQ_CNT_4096);
9612                 break;
9613         }
9614         list_for_each_entry(dmabuf, &eq->page_list, list) {
9615                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9616                                         putPaddrLow(dmabuf->phys);
9617                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9618                                         putPaddrHigh(dmabuf->phys);
9619         }
9620         mbox->vport = phba->pport;
9621         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9622         mbox->context1 = NULL;
9623         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9624         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9625         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9626         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9627         if (shdr_status || shdr_add_status || rc) {
9628                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9629                                 "2500 EQ_CREATE mailbox failed with "
9630                                 "status x%x add_status x%x, mbx status x%x\n",
9631                                 shdr_status, shdr_add_status, rc);
9632                 status = -ENXIO;
9633         }
9634         eq->type = LPFC_EQ;
9635         eq->subtype = LPFC_NONE;
9636         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9637         if (eq->queue_id == 0xFFFF)
9638                 status = -ENXIO;
9639         eq->host_index = 0;
9640         eq->hba_index = 0;
9641
9642         mempool_free(mbox, phba->mbox_mem_pool);
9643         return status;
9644 }
9645
9646 /**
9647  * lpfc_cq_create - Create a Completion Queue on the HBA
9648  * @phba: HBA structure that indicates port to create a queue on.
9649  * @cq: The queue structure to use to create the completion queue.
9650  * @eq: The event queue to bind this completion queue to.
9651  *
9652  * This function creates a completion queue, as detailed in @wq, on a port,
9653  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9654  *
9655  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9656  * is used to get the entry count and entry size that are necessary to
9657  * determine the number of pages to allocate and use for this queue. The @eq
9658  * is used to indicate which event queue to bind this completion queue to. This
9659  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9660  * completion queue. This function is asynchronous and will wait for the mailbox
9661  * command to finish before continuing.
9662  *
9663  * On success this function will return a zero. If unable to allocate enough
9664  * memory this function will return ENOMEM. If the queue create mailbox command
9665  * fails this function will return ENXIO.
9666  **/
9667 uint32_t
9668 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9669                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9670 {
9671         struct lpfc_mbx_cq_create *cq_create;
9672         struct lpfc_dmabuf *dmabuf;
9673         LPFC_MBOXQ_t *mbox;
9674         int rc, length, status = 0;
9675         uint32_t shdr_status, shdr_add_status;
9676         union lpfc_sli4_cfg_shdr *shdr;
9677
9678         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9679         if (!mbox)
9680                 return -ENOMEM;
9681         length = (sizeof(struct lpfc_mbx_cq_create) -
9682                   sizeof(struct lpfc_sli4_cfg_mhdr));
9683         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9684                          LPFC_MBOX_OPCODE_CQ_CREATE,
9685                          length, LPFC_SLI4_MBX_EMBED);
9686         cq_create = &mbox->u.mqe.un.cq_create;
9687         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9688                     cq->page_count);
9689         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9690         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9691         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9692         switch (cq->entry_count) {
9693         default:
9694                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9695                                 "0361 Unsupported CQ count. (%d)\n",
9696                                 cq->entry_count);
9697                 if (cq->entry_count < 256)
9698                         return -EINVAL;
9699                 /* otherwise default to smallest count (drop through) */
9700         case 256:
9701                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9702                        LPFC_CQ_CNT_256);
9703                 break;
9704         case 512:
9705                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9706                        LPFC_CQ_CNT_512);
9707                 break;
9708         case 1024:
9709                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9710                        LPFC_CQ_CNT_1024);
9711                 break;
9712         }
9713         list_for_each_entry(dmabuf, &cq->page_list, list) {
9714                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9715                                         putPaddrLow(dmabuf->phys);
9716                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9717                                         putPaddrHigh(dmabuf->phys);
9718         }
9719         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9720
9721         /* The IOCTL status is embedded in the mailbox subheader. */
9722         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9723         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9724         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9725         if (shdr_status || shdr_add_status || rc) {
9726                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9727                                 "2501 CQ_CREATE mailbox failed with "
9728                                 "status x%x add_status x%x, mbx status x%x\n",
9729                                 shdr_status, shdr_add_status, rc);
9730                 status = -ENXIO;
9731                 goto out;
9732         }
9733         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9734         if (cq->queue_id == 0xFFFF) {
9735                 status = -ENXIO;
9736                 goto out;
9737         }
9738         /* link the cq onto the parent eq child list */
9739         list_add_tail(&cq->list, &eq->child_list);
9740         /* Set up completion queue's type and subtype */
9741         cq->type = type;
9742         cq->subtype = subtype;
9743         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9744         cq->host_index = 0;
9745         cq->hba_index = 0;
9746
9747 out:
9748         mempool_free(mbox, phba->mbox_mem_pool);
9749         return status;
9750 }
9751
9752 /**
9753  * lpfc_mq_create - Create a mailbox Queue on the HBA
9754  * @phba: HBA structure that indicates port to create a queue on.
9755  * @mq: The queue structure to use to create the mailbox queue.
9756  *
9757  * This function creates a mailbox queue, as detailed in @mq, on a port,
9758  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9759  *
9760  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9761  * is used to get the entry count and entry size that are necessary to
9762  * determine the number of pages to allocate and use for this queue. This
9763  * function will send the MQ_CREATE mailbox command to the HBA to setup the
9764  * mailbox queue. This function is asynchronous and will wait for the mailbox
9765  * command to finish before continuing.
9766  *
9767  * On success this function will return a zero. If unable to allocate enough
9768  * memory this function will return ENOMEM. If the queue create mailbox command
9769  * fails this function will return ENXIO.
9770  **/
9771 uint32_t
9772 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9773                struct lpfc_queue *cq, uint32_t subtype)
9774 {
9775         struct lpfc_mbx_mq_create *mq_create;
9776         struct lpfc_dmabuf *dmabuf;
9777         LPFC_MBOXQ_t *mbox;
9778         int rc, length, status = 0;
9779         uint32_t shdr_status, shdr_add_status;
9780         union lpfc_sli4_cfg_shdr *shdr;
9781
9782         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9783         if (!mbox)
9784                 return -ENOMEM;
9785         length = (sizeof(struct lpfc_mbx_mq_create) -
9786                   sizeof(struct lpfc_sli4_cfg_mhdr));
9787         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9788                          LPFC_MBOX_OPCODE_MQ_CREATE,
9789                          length, LPFC_SLI4_MBX_EMBED);
9790         mq_create = &mbox->u.mqe.un.mq_create;
9791         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9792                     mq->page_count);
9793         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9794                     cq->queue_id);
9795         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9796         switch (mq->entry_count) {
9797         default:
9798                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9799                                 "0362 Unsupported MQ count. (%d)\n",
9800                                 mq->entry_count);
9801                 if (mq->entry_count < 16)
9802                         return -EINVAL;
9803                 /* otherwise default to smallest count (drop through) */
9804         case 16:
9805                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9806                        LPFC_MQ_CNT_16);
9807                 break;
9808         case 32:
9809                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9810                        LPFC_MQ_CNT_32);
9811                 break;
9812         case 64:
9813                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9814                        LPFC_MQ_CNT_64);
9815                 break;
9816         case 128:
9817                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9818                        LPFC_MQ_CNT_128);
9819                 break;
9820         }
9821         list_for_each_entry(dmabuf, &mq->page_list, list) {
9822                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9823                                         putPaddrLow(dmabuf->phys);
9824                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9825                                         putPaddrHigh(dmabuf->phys);
9826         }
9827         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9828         /* The IOCTL status is embedded in the mailbox subheader. */
9829         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9830         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9831         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9832         if (shdr_status || shdr_add_status || rc) {
9833                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9834                                 "2502 MQ_CREATE mailbox failed with "
9835                                 "status x%x add_status x%x, mbx status x%x\n",
9836                                 shdr_status, shdr_add_status, rc);
9837                 status = -ENXIO;
9838                 goto out;
9839         }
9840         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9841         if (mq->queue_id == 0xFFFF) {
9842                 status = -ENXIO;
9843                 goto out;
9844         }
9845         mq->type = LPFC_MQ;
9846         mq->subtype = subtype;
9847         mq->host_index = 0;
9848         mq->hba_index = 0;
9849
9850         /* link the mq onto the parent cq child list */
9851         list_add_tail(&mq->list, &cq->child_list);
9852 out:
9853         mempool_free(mbox, phba->mbox_mem_pool);
9854         return status;
9855 }
9856
9857 /**
9858  * lpfc_wq_create - Create a Work Queue on the HBA
9859  * @phba: HBA structure that indicates port to create a queue on.
9860  * @wq: The queue structure to use to create the work queue.
9861  * @cq: The completion queue to bind this work queue to.
9862  * @subtype: The subtype of the work queue indicating its functionality.
9863  *
9864  * This function creates a work queue, as detailed in @wq, on a port, described
9865  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9866  *
9867  * The @phba struct is used to send mailbox command to HBA. The @wq struct
9868  * is used to get the entry count and entry size that are necessary to
9869  * determine the number of pages to allocate and use for this queue. The @cq
9870  * is used to indicate which completion queue to bind this work queue to. This
9871  * function will send the WQ_CREATE mailbox command to the HBA to setup the
9872  * work queue. This function is asynchronous and will wait for the mailbox
9873  * command to finish before continuing.
9874  *
9875  * On success this function will return a zero. If unable to allocate enough
9876  * memory this function will return ENOMEM. If the queue create mailbox command
9877  * fails this function will return ENXIO.
9878  **/
9879 uint32_t
9880 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9881                struct lpfc_queue *cq, uint32_t subtype)
9882 {
9883         struct lpfc_mbx_wq_create *wq_create;
9884         struct lpfc_dmabuf *dmabuf;
9885         LPFC_MBOXQ_t *mbox;
9886         int rc, length, status = 0;
9887         uint32_t shdr_status, shdr_add_status;
9888         union lpfc_sli4_cfg_shdr *shdr;
9889
9890         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9891         if (!mbox)
9892                 return -ENOMEM;
9893         length = (sizeof(struct lpfc_mbx_wq_create) -
9894                   sizeof(struct lpfc_sli4_cfg_mhdr));
9895         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9896                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9897                          length, LPFC_SLI4_MBX_EMBED);
9898         wq_create = &mbox->u.mqe.un.wq_create;
9899         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9900                     wq->page_count);
9901         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9902                     cq->queue_id);
9903         list_for_each_entry(dmabuf, &wq->page_list, list) {
9904                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9905                                         putPaddrLow(dmabuf->phys);
9906                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9907                                         putPaddrHigh(dmabuf->phys);
9908         }
9909         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9910         /* The IOCTL status is embedded in the mailbox subheader. */
9911         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9912         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9913         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9914         if (shdr_status || shdr_add_status || rc) {
9915                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9916                                 "2503 WQ_CREATE mailbox failed with "
9917                                 "status x%x add_status x%x, mbx status x%x\n",
9918                                 shdr_status, shdr_add_status, rc);
9919                 status = -ENXIO;
9920                 goto out;
9921         }
9922         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9923         if (wq->queue_id == 0xFFFF) {
9924                 status = -ENXIO;
9925                 goto out;
9926         }
9927         wq->type = LPFC_WQ;
9928         wq->subtype = subtype;
9929         wq->host_index = 0;
9930         wq->hba_index = 0;
9931
9932         /* link the wq onto the parent cq child list */
9933         list_add_tail(&wq->list, &cq->child_list);
9934 out:
9935         mempool_free(mbox, phba->mbox_mem_pool);
9936         return status;
9937 }
9938
9939 /**
9940  * lpfc_rq_create - Create a Receive Queue on the HBA
9941  * @phba: HBA structure that indicates port to create a queue on.
9942  * @hrq: The queue structure to use to create the header receive queue.
9943  * @drq: The queue structure to use to create the data receive queue.
9944  * @cq: The completion queue to bind this work queue to.
9945  *
9946  * This function creates a receive buffer queue pair , as detailed in @hrq and
9947  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9948  * to the HBA.
9949  *
9950  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9951  * struct is used to get the entry count that is necessary to determine the
9952  * number of pages to use for this queue. The @cq is used to indicate which
9953  * completion queue to bind received buffers that are posted to these queues to.
9954  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9955  * receive queue pair. This function is asynchronous and will wait for the
9956  * mailbox command to finish before continuing.
9957  *
9958  * On success this function will return a zero. If unable to allocate enough
9959  * memory this function will return ENOMEM. If the queue create mailbox command
9960  * fails this function will return ENXIO.
9961  **/
9962 uint32_t
9963 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9964                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9965 {
9966         struct lpfc_mbx_rq_create *rq_create;
9967         struct lpfc_dmabuf *dmabuf;
9968         LPFC_MBOXQ_t *mbox;
9969         int rc, length, status = 0;
9970         uint32_t shdr_status, shdr_add_status;
9971         union lpfc_sli4_cfg_shdr *shdr;
9972
9973         if (hrq->entry_count != drq->entry_count)
9974                 return -EINVAL;
9975         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9976         if (!mbox)
9977                 return -ENOMEM;
9978         length = (sizeof(struct lpfc_mbx_rq_create) -
9979                   sizeof(struct lpfc_sli4_cfg_mhdr));
9980         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9981                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9982                          length, LPFC_SLI4_MBX_EMBED);
9983         rq_create = &mbox->u.mqe.un.rq_create;
9984         switch (hrq->entry_count) {
9985         default:
9986                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9987                                 "2535 Unsupported RQ count. (%d)\n",
9988                                 hrq->entry_count);
9989                 if (hrq->entry_count < 512)
9990                         return -EINVAL;
9991                 /* otherwise default to smallest count (drop through) */
9992         case 512:
9993                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9994                        LPFC_RQ_RING_SIZE_512);
9995                 break;
9996         case 1024:
9997                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9998                        LPFC_RQ_RING_SIZE_1024);
9999                 break;
10000         case 2048:
10001                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10002                        LPFC_RQ_RING_SIZE_2048);
10003                 break;
10004         case 4096:
10005                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10006                        LPFC_RQ_RING_SIZE_4096);
10007                 break;
10008         }
10009         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10010                cq->queue_id);
10011         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10012                hrq->page_count);
10013         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10014                LPFC_HDR_BUF_SIZE);
10015         list_for_each_entry(dmabuf, &hrq->page_list, list) {
10016                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10017                                         putPaddrLow(dmabuf->phys);
10018                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10019                                         putPaddrHigh(dmabuf->phys);
10020         }
10021         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10022         /* The IOCTL status is embedded in the mailbox subheader. */
10023         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10024         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10025         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10026         if (shdr_status || shdr_add_status || rc) {
10027                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10028                                 "2504 RQ_CREATE mailbox failed with "
10029                                 "status x%x add_status x%x, mbx status x%x\n",
10030                                 shdr_status, shdr_add_status, rc);
10031                 status = -ENXIO;
10032                 goto out;
10033         }
10034         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10035         if (hrq->queue_id == 0xFFFF) {
10036                 status = -ENXIO;
10037                 goto out;
10038         }
10039         hrq->type = LPFC_HRQ;
10040         hrq->subtype = subtype;
10041         hrq->host_index = 0;
10042         hrq->hba_index = 0;
10043
10044         /* now create the data queue */
10045         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10046                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10047                          length, LPFC_SLI4_MBX_EMBED);
10048         switch (drq->entry_count) {
10049         default:
10050                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10051                                 "2536 Unsupported RQ count. (%d)\n",
10052                                 drq->entry_count);
10053                 if (drq->entry_count < 512)
10054                         return -EINVAL;
10055                 /* otherwise default to smallest count (drop through) */
10056         case 512:
10057                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10058                        LPFC_RQ_RING_SIZE_512);
10059                 break;
10060         case 1024:
10061                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10062                        LPFC_RQ_RING_SIZE_1024);
10063                 break;
10064         case 2048:
10065                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10066                        LPFC_RQ_RING_SIZE_2048);
10067                 break;
10068         case 4096:
10069                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10070                        LPFC_RQ_RING_SIZE_4096);
10071                 break;
10072         }
10073         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10074                cq->queue_id);
10075         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10076                drq->page_count);
10077         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10078                LPFC_DATA_BUF_SIZE);
10079         list_for_each_entry(dmabuf, &drq->page_list, list) {
10080                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10081                                         putPaddrLow(dmabuf->phys);
10082                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10083                                         putPaddrHigh(dmabuf->phys);
10084         }
10085         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10086         /* The IOCTL status is embedded in the mailbox subheader. */
10087         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10088         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10089         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10090         if (shdr_status || shdr_add_status || rc) {
10091                 status = -ENXIO;
10092                 goto out;
10093         }
10094         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10095         if (drq->queue_id == 0xFFFF) {
10096                 status = -ENXIO;
10097                 goto out;
10098         }
10099         drq->type = LPFC_DRQ;
10100         drq->subtype = subtype;
10101         drq->host_index = 0;
10102         drq->hba_index = 0;
10103
10104         /* link the header and data RQs onto the parent cq child list */
10105         list_add_tail(&hrq->list, &cq->child_list);
10106         list_add_tail(&drq->list, &cq->child_list);
10107
10108 out:
10109         mempool_free(mbox, phba->mbox_mem_pool);
10110         return status;
10111 }
10112
10113 /**
10114  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10115  * @eq: The queue structure associated with the queue to destroy.
10116  *
10117  * This function destroys a queue, as detailed in @eq by sending an mailbox
10118  * command, specific to the type of queue, to the HBA.
10119  *
10120  * The @eq struct is used to get the queue ID of the queue to destroy.
10121  *
10122  * On success this function will return a zero. If the queue destroy mailbox
10123  * command fails this function will return ENXIO.
10124  **/
10125 uint32_t
10126 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10127 {
10128         LPFC_MBOXQ_t *mbox;
10129         int rc, length, status = 0;
10130         uint32_t shdr_status, shdr_add_status;
10131         union lpfc_sli4_cfg_shdr *shdr;
10132
10133         if (!eq)
10134                 return -ENODEV;
10135         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10136         if (!mbox)
10137                 return -ENOMEM;
10138         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10139                   sizeof(struct lpfc_sli4_cfg_mhdr));
10140         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10141                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10142                          length, LPFC_SLI4_MBX_EMBED);
10143         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10144                eq->queue_id);
10145         mbox->vport = eq->phba->pport;
10146         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10147
10148         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10149         /* The IOCTL status is embedded in the mailbox subheader. */
10150         shdr = (union lpfc_sli4_cfg_shdr *)
10151                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10152         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10153         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10154         if (shdr_status || shdr_add_status || rc) {
10155                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10156                                 "2505 EQ_DESTROY mailbox failed with "
10157                                 "status x%x add_status x%x, mbx status x%x\n",
10158                                 shdr_status, shdr_add_status, rc);
10159                 status = -ENXIO;
10160         }
10161
10162         /* Remove eq from any list */
10163         list_del_init(&eq->list);
10164         mempool_free(mbox, eq->phba->mbox_mem_pool);
10165         return status;
10166 }
10167
10168 /**
10169  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10170  * @cq: The queue structure associated with the queue to destroy.
10171  *
10172  * This function destroys a queue, as detailed in @cq by sending an mailbox
10173  * command, specific to the type of queue, to the HBA.
10174  *
10175  * The @cq struct is used to get the queue ID of the queue to destroy.
10176  *
10177  * On success this function will return a zero. If the queue destroy mailbox
10178  * command fails this function will return ENXIO.
10179  **/
10180 uint32_t
10181 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10182 {
10183         LPFC_MBOXQ_t *mbox;
10184         int rc, length, status = 0;
10185         uint32_t shdr_status, shdr_add_status;
10186         union lpfc_sli4_cfg_shdr *shdr;
10187
10188         if (!cq)
10189                 return -ENODEV;
10190         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10191         if (!mbox)
10192                 return -ENOMEM;
10193         length = (sizeof(struct lpfc_mbx_cq_destroy) -
10194                   sizeof(struct lpfc_sli4_cfg_mhdr));
10195         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10196                          LPFC_MBOX_OPCODE_CQ_DESTROY,
10197                          length, LPFC_SLI4_MBX_EMBED);
10198         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10199                cq->queue_id);
10200         mbox->vport = cq->phba->pport;
10201         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10202         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10203         /* The IOCTL status is embedded in the mailbox subheader. */
10204         shdr = (union lpfc_sli4_cfg_shdr *)
10205                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10206         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10207         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10208         if (shdr_status || shdr_add_status || rc) {
10209                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10210                                 "2506 CQ_DESTROY mailbox failed with "
10211                                 "status x%x add_status x%x, mbx status x%x\n",
10212                                 shdr_status, shdr_add_status, rc);
10213                 status = -ENXIO;
10214         }
10215         /* Remove cq from any list */
10216         list_del_init(&cq->list);
10217         mempool_free(mbox, cq->phba->mbox_mem_pool);
10218         return status;
10219 }
10220
10221 /**
10222  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10223  * @qm: The queue structure associated with the queue to destroy.
10224  *
10225  * This function destroys a queue, as detailed in @mq by sending an mailbox
10226  * command, specific to the type of queue, to the HBA.
10227  *
10228  * The @mq struct is used to get the queue ID of the queue to destroy.
10229  *
10230  * On success this function will return a zero. If the queue destroy mailbox
10231  * command fails this function will return ENXIO.
10232  **/
10233 uint32_t
10234 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10235 {
10236         LPFC_MBOXQ_t *mbox;
10237         int rc, length, status = 0;
10238         uint32_t shdr_status, shdr_add_status;
10239         union lpfc_sli4_cfg_shdr *shdr;
10240
10241         if (!mq)
10242                 return -ENODEV;
10243         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10244         if (!mbox)
10245                 return -ENOMEM;
10246         length = (sizeof(struct lpfc_mbx_mq_destroy) -
10247                   sizeof(struct lpfc_sli4_cfg_mhdr));
10248         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10249                          LPFC_MBOX_OPCODE_MQ_DESTROY,
10250                          length, LPFC_SLI4_MBX_EMBED);
10251         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10252                mq->queue_id);
10253         mbox->vport = mq->phba->pport;
10254         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10255         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10256         /* The IOCTL status is embedded in the mailbox subheader. */
10257         shdr = (union lpfc_sli4_cfg_shdr *)
10258                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10259         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10260         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10261         if (shdr_status || shdr_add_status || rc) {
10262                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10263                                 "2507 MQ_DESTROY mailbox failed with "
10264                                 "status x%x add_status x%x, mbx status x%x\n",
10265                                 shdr_status, shdr_add_status, rc);
10266                 status = -ENXIO;
10267         }
10268         /* Remove mq from any list */
10269         list_del_init(&mq->list);
10270         mempool_free(mbox, mq->phba->mbox_mem_pool);
10271         return status;
10272 }
10273
10274 /**
10275  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10276  * @wq: The queue structure associated with the queue to destroy.
10277  *
10278  * This function destroys a queue, as detailed in @wq by sending an mailbox
10279  * command, specific to the type of queue, to the HBA.
10280  *
10281  * The @wq struct is used to get the queue ID of the queue to destroy.
10282  *
10283  * On success this function will return a zero. If the queue destroy mailbox
10284  * command fails this function will return ENXIO.
10285  **/
10286 uint32_t
10287 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10288 {
10289         LPFC_MBOXQ_t *mbox;
10290         int rc, length, status = 0;
10291         uint32_t shdr_status, shdr_add_status;
10292         union lpfc_sli4_cfg_shdr *shdr;
10293
10294         if (!wq)
10295                 return -ENODEV;
10296         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10297         if (!mbox)
10298                 return -ENOMEM;
10299         length = (sizeof(struct lpfc_mbx_wq_destroy) -
10300                   sizeof(struct lpfc_sli4_cfg_mhdr));
10301         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10302                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10303                          length, LPFC_SLI4_MBX_EMBED);
10304         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10305                wq->queue_id);
10306         mbox->vport = wq->phba->pport;
10307         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10308         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10309         shdr = (union lpfc_sli4_cfg_shdr *)
10310                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10311         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10312         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10313         if (shdr_status || shdr_add_status || rc) {
10314                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10315                                 "2508 WQ_DESTROY mailbox failed with "
10316                                 "status x%x add_status x%x, mbx status x%x\n",
10317                                 shdr_status, shdr_add_status, rc);
10318                 status = -ENXIO;
10319         }
10320         /* Remove wq from any list */
10321         list_del_init(&wq->list);
10322         mempool_free(mbox, wq->phba->mbox_mem_pool);
10323         return status;
10324 }
10325
10326 /**
10327  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10328  * @rq: The queue structure associated with the queue to destroy.
10329  *
10330  * This function destroys a queue, as detailed in @rq by sending an mailbox
10331  * command, specific to the type of queue, to the HBA.
10332  *
10333  * The @rq struct is used to get the queue ID of the queue to destroy.
10334  *
10335  * On success this function will return a zero. If the queue destroy mailbox
10336  * command fails this function will return ENXIO.
10337  **/
10338 uint32_t
10339 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10340                 struct lpfc_queue *drq)
10341 {
10342         LPFC_MBOXQ_t *mbox;
10343         int rc, length, status = 0;
10344         uint32_t shdr_status, shdr_add_status;
10345         union lpfc_sli4_cfg_shdr *shdr;
10346
10347         if (!hrq || !drq)
10348                 return -ENODEV;
10349         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10350         if (!mbox)
10351                 return -ENOMEM;
10352         length = (sizeof(struct lpfc_mbx_rq_destroy) -
10353                   sizeof(struct mbox_header));
10354         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10355                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10356                          length, LPFC_SLI4_MBX_EMBED);
10357         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10358                hrq->queue_id);
10359         mbox->vport = hrq->phba->pport;
10360         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10361         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10362         /* The IOCTL status is embedded in the mailbox subheader. */
10363         shdr = (union lpfc_sli4_cfg_shdr *)
10364                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10365         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10366         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10367         if (shdr_status || shdr_add_status || rc) {
10368                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10369                                 "2509 RQ_DESTROY mailbox failed with "
10370                                 "status x%x add_status x%x, mbx status x%x\n",
10371                                 shdr_status, shdr_add_status, rc);
10372                 if (rc != MBX_TIMEOUT)
10373                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10374                 return -ENXIO;
10375         }
10376         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10377                drq->queue_id);
10378         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10379         shdr = (union lpfc_sli4_cfg_shdr *)
10380                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10381         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10382         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10383         if (shdr_status || shdr_add_status || rc) {
10384                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10385                                 "2510 RQ_DESTROY mailbox failed with "
10386                                 "status x%x add_status x%x, mbx status x%x\n",
10387                                 shdr_status, shdr_add_status, rc);
10388                 status = -ENXIO;
10389         }
10390         list_del_init(&hrq->list);
10391         list_del_init(&drq->list);
10392         mempool_free(mbox, hrq->phba->mbox_mem_pool);
10393         return status;
10394 }
10395
10396 /**
10397  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10398  * @phba: The virtual port for which this call being executed.
10399  * @pdma_phys_addr0: Physical address of the 1st SGL page.
10400  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10401  * @xritag: the xritag that ties this io to the SGL pages.
10402  *
10403  * This routine will post the sgl pages for the IO that has the xritag
10404  * that is in the iocbq structure. The xritag is assigned during iocbq
10405  * creation and persists for as long as the driver is loaded.
10406  * if the caller has fewer than 256 scatter gather segments to map then
10407  * pdma_phys_addr1 should be 0.
10408  * If the caller needs to map more than 256 scatter gather segment then
10409  * pdma_phys_addr1 should be a valid physical address.
10410  * physical address for SGLs must be 64 byte aligned.
10411  * If you are going to map 2 SGL's then the first one must have 256 entries
10412  * the second sgl can have between 1 and 256 entries.
10413  *
10414  * Return codes:
10415  *      0 - Success
10416  *      -ENXIO, -ENOMEM - Failure
10417  **/
10418 int
10419 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10420                 dma_addr_t pdma_phys_addr0,
10421                 dma_addr_t pdma_phys_addr1,
10422                 uint16_t xritag)
10423 {
10424         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10425         LPFC_MBOXQ_t *mbox;
10426         int rc;
10427         uint32_t shdr_status, shdr_add_status;
10428         union lpfc_sli4_cfg_shdr *shdr;
10429
10430         if (xritag == NO_XRI) {
10431                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10432                                 "0364 Invalid param:\n");
10433                 return -EINVAL;
10434         }
10435
10436         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10437         if (!mbox)
10438                 return -ENOMEM;
10439
10440         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10441                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10442                         sizeof(struct lpfc_mbx_post_sgl_pages) -
10443                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10444
10445         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10446                                 &mbox->u.mqe.un.post_sgl_pages;
10447         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10448         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10449
10450         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10451                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10452         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10453                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10454
10455         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10456                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10457         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10458                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10459         if (!phba->sli4_hba.intr_enable)
10460                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10461         else
10462                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10463         /* The IOCTL status is embedded in the mailbox subheader. */
10464         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10465         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10466         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10467         if (rc != MBX_TIMEOUT)
10468                 mempool_free(mbox, phba->mbox_mem_pool);
10469         if (shdr_status || shdr_add_status || rc) {
10470                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10471                                 "2511 POST_SGL mailbox failed with "
10472                                 "status x%x add_status x%x, mbx status x%x\n",
10473                                 shdr_status, shdr_add_status, rc);
10474                 rc = -ENXIO;
10475         }
10476         return 0;
10477 }
10478 /**
10479  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10480  * @phba: The virtual port for which this call being executed.
10481  *
10482  * This routine will remove all of the sgl pages registered with the hba.
10483  *
10484  * Return codes:
10485  *      0 - Success
10486  *      -ENXIO, -ENOMEM - Failure
10487  **/
10488 int
10489 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10490 {
10491         LPFC_MBOXQ_t *mbox;
10492         int rc;
10493         uint32_t shdr_status, shdr_add_status;
10494         union lpfc_sli4_cfg_shdr *shdr;
10495
10496         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10497         if (!mbox)
10498                 return -ENOMEM;
10499
10500         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10501                         LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10502                         LPFC_SLI4_MBX_EMBED);
10503         if (!phba->sli4_hba.intr_enable)
10504                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10505         else
10506                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10507         /* The IOCTL status is embedded in the mailbox subheader. */
10508         shdr = (union lpfc_sli4_cfg_shdr *)
10509                 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10510         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10511         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10512         if (rc != MBX_TIMEOUT)
10513                 mempool_free(mbox, phba->mbox_mem_pool);
10514         if (shdr_status || shdr_add_status || rc) {
10515                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10516                                 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10517                                 "status x%x add_status x%x, mbx status x%x\n",
10518                                 shdr_status, shdr_add_status, rc);
10519                 rc = -ENXIO;
10520         }
10521         return rc;
10522 }
10523
10524 /**
10525  * lpfc_sli4_next_xritag - Get an xritag for the io
10526  * @phba: Pointer to HBA context object.
10527  *
10528  * This function gets an xritag for the iocb. If there is no unused xritag
10529  * it will return 0xffff.
10530  * The function returns the allocated xritag if successful, else returns zero.
10531  * Zero is not a valid xritag.
10532  * The caller is not required to hold any lock.
10533  **/
10534 uint16_t
10535 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10536 {
10537         uint16_t xritag;
10538
10539         spin_lock_irq(&phba->hbalock);
10540         xritag = phba->sli4_hba.next_xri;
10541         if ((xritag != (uint16_t) -1) && xritag <
10542                 (phba->sli4_hba.max_cfg_param.max_xri
10543                         + phba->sli4_hba.max_cfg_param.xri_base)) {
10544                 phba->sli4_hba.next_xri++;
10545                 phba->sli4_hba.max_cfg_param.xri_used++;
10546                 spin_unlock_irq(&phba->hbalock);
10547                 return xritag;
10548         }
10549         spin_unlock_irq(&phba->hbalock);
10550         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10551                         "2004 Failed to allocate XRI.last XRITAG is %d"
10552                         " Max XRI is %d, Used XRI is %d\n",
10553                         phba->sli4_hba.next_xri,
10554                         phba->sli4_hba.max_cfg_param.max_xri,
10555                         phba->sli4_hba.max_cfg_param.xri_used);
10556         return -1;
10557 }
10558
10559 /**
10560  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10561  * @phba: pointer to lpfc hba data structure.
10562  *
10563  * This routine is invoked to post a block of driver's sgl pages to the
10564  * HBA using non-embedded mailbox command. No Lock is held. This routine
10565  * is only called when the driver is loading and after all IO has been
10566  * stopped.
10567  **/
10568 int
10569 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10570 {
10571         struct lpfc_sglq *sglq_entry;
10572         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10573         struct sgl_page_pairs *sgl_pg_pairs;
10574         void *viraddr;
10575         LPFC_MBOXQ_t *mbox;
10576         uint32_t reqlen, alloclen, pg_pairs;
10577         uint32_t mbox_tmo;
10578         uint16_t xritag_start = 0;
10579         int els_xri_cnt, rc = 0;
10580         uint32_t shdr_status, shdr_add_status;
10581         union lpfc_sli4_cfg_shdr *shdr;
10582
10583         /* The number of sgls to be posted */
10584         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10585
10586         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10587                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10588         if (reqlen > PAGE_SIZE) {
10589                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10590                                 "2559 Block sgl registration required DMA "
10591                                 "size (%d) great than a page\n", reqlen);
10592                 return -ENOMEM;
10593         }
10594         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10595         if (!mbox) {
10596                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10597                                 "2560 Failed to allocate mbox cmd memory\n");
10598                 return -ENOMEM;
10599         }
10600
10601         /* Allocate DMA memory and set up the non-embedded mailbox command */
10602         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10603                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10604                          LPFC_SLI4_MBX_NEMBED);
10605
10606         if (alloclen < reqlen) {
10607                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10608                                 "0285 Allocated DMA memory size (%d) is "
10609                                 "less than the requested DMA memory "
10610                                 "size (%d)\n", alloclen, reqlen);
10611                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10612                 return -ENOMEM;
10613         }
10614         /* Get the first SGE entry from the non-embedded DMA memory */
10615         viraddr = mbox->sge_array->addr[0];
10616
10617         /* Set up the SGL pages in the non-embedded DMA pages */
10618         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10619         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10620
10621         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10622                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10623                 /* Set up the sge entry */
10624                 sgl_pg_pairs->sgl_pg0_addr_lo =
10625                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10626                 sgl_pg_pairs->sgl_pg0_addr_hi =
10627                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10628                 sgl_pg_pairs->sgl_pg1_addr_lo =
10629                                 cpu_to_le32(putPaddrLow(0));
10630                 sgl_pg_pairs->sgl_pg1_addr_hi =
10631                                 cpu_to_le32(putPaddrHigh(0));
10632                 /* Keep the first xritag on the list */
10633                 if (pg_pairs == 0)
10634                         xritag_start = sglq_entry->sli4_xritag;
10635                 sgl_pg_pairs++;
10636         }
10637         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10638         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
10639         /* Perform endian conversion if necessary */
10640         sgl->word0 = cpu_to_le32(sgl->word0);
10641
10642         if (!phba->sli4_hba.intr_enable)
10643                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10644         else {
10645                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10646                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10647         }
10648         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10649         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10650         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10651         if (rc != MBX_TIMEOUT)
10652                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10653         if (shdr_status || shdr_add_status || rc) {
10654                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10655                                 "2513 POST_SGL_BLOCK mailbox command failed "
10656                                 "status x%x add_status x%x mbx status x%x\n",
10657                                 shdr_status, shdr_add_status, rc);
10658                 rc = -ENXIO;
10659         }
10660         return rc;
10661 }
10662
10663 /**
10664  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10665  * @phba: pointer to lpfc hba data structure.
10666  * @sblist: pointer to scsi buffer list.
10667  * @count: number of scsi buffers on the list.
10668  *
10669  * This routine is invoked to post a block of @count scsi sgl pages from a
10670  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10671  * No Lock is held.
10672  *
10673  **/
10674 int
10675 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10676                               int cnt)
10677 {
10678         struct lpfc_scsi_buf *psb;
10679         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10680         struct sgl_page_pairs *sgl_pg_pairs;
10681         void *viraddr;
10682         LPFC_MBOXQ_t *mbox;
10683         uint32_t reqlen, alloclen, pg_pairs;
10684         uint32_t mbox_tmo;
10685         uint16_t xritag_start = 0;
10686         int rc = 0;
10687         uint32_t shdr_status, shdr_add_status;
10688         dma_addr_t pdma_phys_bpl1;
10689         union lpfc_sli4_cfg_shdr *shdr;
10690
10691         /* Calculate the requested length of the dma memory */
10692         reqlen = cnt * sizeof(struct sgl_page_pairs) +
10693                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10694         if (reqlen > PAGE_SIZE) {
10695                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10696                                 "0217 Block sgl registration required DMA "
10697                                 "size (%d) great than a page\n", reqlen);
10698                 return -ENOMEM;
10699         }
10700         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10701         if (!mbox) {
10702                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10703                                 "0283 Failed to allocate mbox cmd memory\n");
10704                 return -ENOMEM;
10705         }
10706
10707         /* Allocate DMA memory and set up the non-embedded mailbox command */
10708         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10709                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10710                                 LPFC_SLI4_MBX_NEMBED);
10711
10712         if (alloclen < reqlen) {
10713                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10714                                 "2561 Allocated DMA memory size (%d) is "
10715                                 "less than the requested DMA memory "
10716                                 "size (%d)\n", alloclen, reqlen);
10717                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10718                 return -ENOMEM;
10719         }
10720         /* Get the first SGE entry from the non-embedded DMA memory */
10721         viraddr = mbox->sge_array->addr[0];
10722
10723         /* Set up the SGL pages in the non-embedded DMA pages */
10724         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10725         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10726
10727         pg_pairs = 0;
10728         list_for_each_entry(psb, sblist, list) {
10729                 /* Set up the sge entry */
10730                 sgl_pg_pairs->sgl_pg0_addr_lo =
10731                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10732                 sgl_pg_pairs->sgl_pg0_addr_hi =
10733                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10734                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10735                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10736                 else
10737                         pdma_phys_bpl1 = 0;
10738                 sgl_pg_pairs->sgl_pg1_addr_lo =
10739                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10740                 sgl_pg_pairs->sgl_pg1_addr_hi =
10741                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10742                 /* Keep the first xritag on the list */
10743                 if (pg_pairs == 0)
10744                         xritag_start = psb->cur_iocbq.sli4_xritag;
10745                 sgl_pg_pairs++;
10746                 pg_pairs++;
10747         }
10748         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10749         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10750         /* Perform endian conversion if necessary */
10751         sgl->word0 = cpu_to_le32(sgl->word0);
10752
10753         if (!phba->sli4_hba.intr_enable)
10754                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10755         else {
10756                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10757                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10758         }
10759         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10760         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10761         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10762         if (rc != MBX_TIMEOUT)
10763                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10764         if (shdr_status || shdr_add_status || rc) {
10765                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10766                                 "2564 POST_SGL_BLOCK mailbox command failed "
10767                                 "status x%x add_status x%x mbx status x%x\n",
10768                                 shdr_status, shdr_add_status, rc);
10769                 rc = -ENXIO;
10770         }
10771         return rc;
10772 }
10773
10774 /**
10775  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10776  * @phba: pointer to lpfc_hba struct that the frame was received on
10777  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10778  *
10779  * This function checks the fields in the @fc_hdr to see if the FC frame is a
10780  * valid type of frame that the LPFC driver will handle. This function will
10781  * return a zero if the frame is a valid frame or a non zero value when the
10782  * frame does not pass the check.
10783  **/
10784 static int
10785 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10786 {
10787         char *rctl_names[] = FC_RCTL_NAMES_INIT;
10788         char *type_names[] = FC_TYPE_NAMES_INIT;
10789         struct fc_vft_header *fc_vft_hdr;
10790
10791         switch (fc_hdr->fh_r_ctl) {
10792         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
10793         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
10794         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
10795         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
10796         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
10797         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
10798         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
10799         case FC_RCTL_DD_CMD_STATUS:     /* command status */
10800         case FC_RCTL_ELS_REQ:   /* extended link services request */
10801         case FC_RCTL_ELS_REP:   /* extended link services reply */
10802         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
10803         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
10804         case FC_RCTL_BA_NOP:    /* basic link service NOP */
10805         case FC_RCTL_BA_ABTS:   /* basic link service abort */
10806         case FC_RCTL_BA_RMC:    /* remove connection */
10807         case FC_RCTL_BA_ACC:    /* basic accept */
10808         case FC_RCTL_BA_RJT:    /* basic reject */
10809         case FC_RCTL_BA_PRMT:
10810         case FC_RCTL_ACK_1:     /* acknowledge_1 */
10811         case FC_RCTL_ACK_0:     /* acknowledge_0 */
10812         case FC_RCTL_P_RJT:     /* port reject */
10813         case FC_RCTL_F_RJT:     /* fabric reject */
10814         case FC_RCTL_P_BSY:     /* port busy */
10815         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
10816         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
10817         case FC_RCTL_LCR:       /* link credit reset */
10818         case FC_RCTL_END:       /* end */
10819                 break;
10820         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
10821                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10822                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10823                 return lpfc_fc_frame_check(phba, fc_hdr);
10824         default:
10825                 goto drop;
10826         }
10827         switch (fc_hdr->fh_type) {
10828         case FC_TYPE_BLS:
10829         case FC_TYPE_ELS:
10830         case FC_TYPE_FCP:
10831         case FC_TYPE_CT:
10832                 break;
10833         case FC_TYPE_IP:
10834         case FC_TYPE_ILS:
10835         default:
10836                 goto drop;
10837         }
10838         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10839                         "2538 Received frame rctl:%s type:%s\n",
10840                         rctl_names[fc_hdr->fh_r_ctl],
10841                         type_names[fc_hdr->fh_type]);
10842         return 0;
10843 drop:
10844         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10845                         "2539 Dropped frame rctl:%s type:%s\n",
10846                         rctl_names[fc_hdr->fh_r_ctl],
10847                         type_names[fc_hdr->fh_type]);
10848         return 1;
10849 }
10850
10851 /**
10852  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10853  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10854  *
10855  * This function processes the FC header to retrieve the VFI from the VF
10856  * header, if one exists. This function will return the VFI if one exists
10857  * or 0 if no VSAN Header exists.
10858  **/
10859 static uint32_t
10860 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10861 {
10862         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10863
10864         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10865                 return 0;
10866         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10867 }
10868
10869 /**
10870  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10871  * @phba: Pointer to the HBA structure to search for the vport on
10872  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10873  * @fcfi: The FC Fabric ID that the frame came from
10874  *
10875  * This function searches the @phba for a vport that matches the content of the
10876  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10877  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10878  * returns the matching vport pointer or NULL if unable to match frame to a
10879  * vport.
10880  **/
10881 static struct lpfc_vport *
10882 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10883                        uint16_t fcfi)
10884 {
10885         struct lpfc_vport **vports;
10886         struct lpfc_vport *vport = NULL;
10887         int i;
10888         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10889                         fc_hdr->fh_d_id[1] << 8 |
10890                         fc_hdr->fh_d_id[2]);
10891
10892         vports = lpfc_create_vport_work_array(phba);
10893         if (vports != NULL)
10894                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10895                         if (phba->fcf.fcfi == fcfi &&
10896                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10897                             vports[i]->fc_myDID == did) {
10898                                 vport = vports[i];
10899                                 break;
10900                         }
10901                 }
10902         lpfc_destroy_vport_work_array(phba, vports);
10903         return vport;
10904 }
10905
10906 /**
10907  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10908  * @vport: The vport to work on.
10909  *
10910  * This function updates the receive sequence time stamp for this vport. The
10911  * receive sequence time stamp indicates the time that the last frame of the
10912  * the sequence that has been idle for the longest amount of time was received.
10913  * the driver uses this time stamp to indicate if any received sequences have
10914  * timed out.
10915  **/
10916 void
10917 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10918 {
10919         struct lpfc_dmabuf *h_buf;
10920         struct hbq_dmabuf *dmabuf = NULL;
10921
10922         /* get the oldest sequence on the rcv list */
10923         h_buf = list_get_first(&vport->rcv_buffer_list,
10924                                struct lpfc_dmabuf, list);
10925         if (!h_buf)
10926                 return;
10927         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10928         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10929 }
10930
10931 /**
10932  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10933  * @vport: The vport that the received sequences were sent to.
10934  *
10935  * This function cleans up all outstanding received sequences. This is called
10936  * by the driver when a link event or user action invalidates all the received
10937  * sequences.
10938  **/
10939 void
10940 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10941 {
10942         struct lpfc_dmabuf *h_buf, *hnext;
10943         struct lpfc_dmabuf *d_buf, *dnext;
10944         struct hbq_dmabuf *dmabuf = NULL;
10945
10946         /* start with the oldest sequence on the rcv list */
10947         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10948                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10949                 list_del_init(&dmabuf->hbuf.list);
10950                 list_for_each_entry_safe(d_buf, dnext,
10951                                          &dmabuf->dbuf.list, list) {
10952                         list_del_init(&d_buf->list);
10953                         lpfc_in_buf_free(vport->phba, d_buf);
10954                 }
10955                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10956         }
10957 }
10958
10959 /**
10960  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10961  * @vport: The vport that the received sequences were sent to.
10962  *
10963  * This function determines whether any received sequences have timed out by
10964  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
10965  * indicates that there is at least one timed out sequence this routine will
10966  * go through the received sequences one at a time from most inactive to most
10967  * active to determine which ones need to be cleaned up. Once it has determined
10968  * that a sequence needs to be cleaned up it will simply free up the resources
10969  * without sending an abort.
10970  **/
10971 void
10972 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
10973 {
10974         struct lpfc_dmabuf *h_buf, *hnext;
10975         struct lpfc_dmabuf *d_buf, *dnext;
10976         struct hbq_dmabuf *dmabuf = NULL;
10977         unsigned long timeout;
10978         int abort_count = 0;
10979
10980         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10981                    vport->rcv_buffer_time_stamp);
10982         if (list_empty(&vport->rcv_buffer_list) ||
10983             time_before(jiffies, timeout))
10984                 return;
10985         /* start with the oldest sequence on the rcv list */
10986         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10987                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10988                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10989                            dmabuf->time_stamp);
10990                 if (time_before(jiffies, timeout))
10991                         break;
10992                 abort_count++;
10993                 list_del_init(&dmabuf->hbuf.list);
10994                 list_for_each_entry_safe(d_buf, dnext,
10995                                          &dmabuf->dbuf.list, list) {
10996                         list_del_init(&d_buf->list);
10997                         lpfc_in_buf_free(vport->phba, d_buf);
10998                 }
10999                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11000         }
11001         if (abort_count)
11002                 lpfc_update_rcv_time_stamp(vport);
11003 }
11004
11005 /**
11006  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11007  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11008  *
11009  * This function searches through the existing incomplete sequences that have
11010  * been sent to this @vport. If the frame matches one of the incomplete
11011  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11012  * make up that sequence. If no sequence is found that matches this frame then
11013  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11014  * This function returns a pointer to the first dmabuf in the sequence list that
11015  * the frame was linked to.
11016  **/
11017 static struct hbq_dmabuf *
11018 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11019 {
11020         struct fc_frame_header *new_hdr;
11021         struct fc_frame_header *temp_hdr;
11022         struct lpfc_dmabuf *d_buf;
11023         struct lpfc_dmabuf *h_buf;
11024         struct hbq_dmabuf *seq_dmabuf = NULL;
11025         struct hbq_dmabuf *temp_dmabuf = NULL;
11026
11027         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11028         dmabuf->time_stamp = jiffies;
11029         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11030         /* Use the hdr_buf to find the sequence that this frame belongs to */
11031         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11032                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11033                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11034                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11035                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11036                         continue;
11037                 /* found a pending sequence that matches this frame */
11038                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11039                 break;
11040         }
11041         if (!seq_dmabuf) {
11042                 /*
11043                  * This indicates first frame received for this sequence.
11044                  * Queue the buffer on the vport's rcv_buffer_list.
11045                  */
11046                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11047                 lpfc_update_rcv_time_stamp(vport);
11048                 return dmabuf;
11049         }
11050         temp_hdr = seq_dmabuf->hbuf.virt;
11051         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11052                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11053                 list_del_init(&seq_dmabuf->hbuf.list);
11054                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11055                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11056                 lpfc_update_rcv_time_stamp(vport);
11057                 return dmabuf;
11058         }
11059         /* move this sequence to the tail to indicate a young sequence */
11060         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11061         seq_dmabuf->time_stamp = jiffies;
11062         lpfc_update_rcv_time_stamp(vport);
11063         if (list_empty(&seq_dmabuf->dbuf.list)) {
11064                 temp_hdr = dmabuf->hbuf.virt;
11065                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11066                 return seq_dmabuf;
11067         }
11068         /* find the correct place in the sequence to insert this frame */
11069         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11070                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11071                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11072                 /*
11073                  * If the frame's sequence count is greater than the frame on
11074                  * the list then insert the frame right after this frame
11075                  */
11076                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11077                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11078                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11079                         return seq_dmabuf;
11080                 }
11081         }
11082         return NULL;
11083 }
11084
11085 /**
11086  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11087  * @vport: pointer to a vitural port
11088  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11089  *
11090  * This function tries to abort from the partially assembed sequence, described
11091  * by the information from basic abbort @dmabuf. It checks to see whether such
11092  * partially assembled sequence held by the driver. If so, it shall free up all
11093  * the frames from the partially assembled sequence.
11094  *
11095  * Return
11096  * true  -- if there is matching partially assembled sequence present and all
11097  *          the frames freed with the sequence;
11098  * false -- if there is no matching partially assembled sequence present so
11099  *          nothing got aborted in the lower layer driver
11100  **/
11101 static bool
11102 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11103                             struct hbq_dmabuf *dmabuf)
11104 {
11105         struct fc_frame_header *new_hdr;
11106         struct fc_frame_header *temp_hdr;
11107         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11108         struct hbq_dmabuf *seq_dmabuf = NULL;
11109
11110         /* Use the hdr_buf to find the sequence that matches this frame */
11111         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11112         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11113         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11114         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11115                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11116                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11117                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11118                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11119                         continue;
11120                 /* found a pending sequence that matches this frame */
11121                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11122                 break;
11123         }
11124
11125         /* Free up all the frames from the partially assembled sequence */
11126         if (seq_dmabuf) {
11127                 list_for_each_entry_safe(d_buf, n_buf,
11128                                          &seq_dmabuf->dbuf.list, list) {
11129                         list_del_init(&d_buf->list);
11130                         lpfc_in_buf_free(vport->phba, d_buf);
11131                 }
11132                 return true;
11133         }
11134         return false;
11135 }
11136
11137 /**
11138  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11139  * @phba: Pointer to HBA context object.
11140  * @cmd_iocbq: pointer to the command iocbq structure.
11141  * @rsp_iocbq: pointer to the response iocbq structure.
11142  *
11143  * This function handles the sequence abort accept iocb command complete
11144  * event. It properly releases the memory allocated to the sequence abort
11145  * accept iocb.
11146  **/
11147 static void
11148 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11149                              struct lpfc_iocbq *cmd_iocbq,
11150                              struct lpfc_iocbq *rsp_iocbq)
11151 {
11152         if (cmd_iocbq)
11153                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11154 }
11155
11156 /**
11157  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11158  * @phba: Pointer to HBA context object.
11159  * @fc_hdr: pointer to a FC frame header.
11160  *
11161  * This function sends a basic accept to a previous unsol sequence abort
11162  * event after aborting the sequence handling.
11163  **/
11164 static void
11165 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11166                         struct fc_frame_header *fc_hdr)
11167 {
11168         struct lpfc_iocbq *ctiocb = NULL;
11169         struct lpfc_nodelist *ndlp;
11170         uint16_t oxid, rxid;
11171         uint32_t sid, fctl;
11172         IOCB_t *icmd;
11173
11174         if (!lpfc_is_link_up(phba))
11175                 return;
11176
11177         sid = sli4_sid_from_fc_hdr(fc_hdr);
11178         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11179         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11180
11181         ndlp = lpfc_findnode_did(phba->pport, sid);
11182         if (!ndlp) {
11183                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11184                                 "1268 Find ndlp returned NULL for oxid:x%x "
11185                                 "SID:x%x\n", oxid, sid);
11186                 return;
11187         }
11188
11189         /* Allocate buffer for acc iocb */
11190         ctiocb = lpfc_sli_get_iocbq(phba);
11191         if (!ctiocb)
11192                 return;
11193
11194         /* Extract the F_CTL field from FC_HDR */
11195         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11196
11197         icmd = &ctiocb->iocb;
11198         icmd->un.xseq64.bdl.bdeSize = 0;
11199         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11200         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11201         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11202         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11203
11204         /* Fill in the rest of iocb fields */
11205         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11206         icmd->ulpBdeCount = 0;
11207         icmd->ulpLe = 1;
11208         icmd->ulpClass = CLASS3;
11209         icmd->ulpContext = ndlp->nlp_rpi;
11210
11211         ctiocb->iocb_cmpl = NULL;
11212         ctiocb->vport = phba->pport;
11213         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11214
11215         if (fctl & FC_FC_EX_CTX) {
11216                 /* ABTS sent by responder to CT exchange, construction
11217                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11218                  * field and RX_ID from ABTS for RX_ID field.
11219                  */
11220                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11221                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11222                 ctiocb->sli4_xritag = oxid;
11223         } else {
11224                 /* ABTS sent by initiator to CT exchange, construction
11225                  * of BA_ACC will need to allocate a new XRI as for the
11226                  * XRI_TAG and RX_ID fields.
11227                  */
11228                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11229                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11230                 ctiocb->sli4_xritag = NO_XRI;
11231         }
11232         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11233
11234         /* Xmit CT abts accept on exchange <xid> */
11235         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11236                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11237                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11238         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11239 }
11240
11241 /**
11242  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11243  * @vport: Pointer to the vport on which this sequence was received
11244  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11245  *
11246  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11247  * receive sequence is only partially assembed by the driver, it shall abort
11248  * the partially assembled frames for the sequence. Otherwise, if the
11249  * unsolicited receive sequence has been completely assembled and passed to
11250  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11251  * unsolicited sequence has been aborted. After that, it will issue a basic
11252  * accept to accept the abort.
11253  **/
11254 void
11255 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11256                              struct hbq_dmabuf *dmabuf)
11257 {
11258         struct lpfc_hba *phba = vport->phba;
11259         struct fc_frame_header fc_hdr;
11260         uint32_t fctl;
11261         bool abts_par;
11262
11263         /* Make a copy of fc_hdr before the dmabuf being released */
11264         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11265         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
11266
11267         if (fctl & FC_FC_EX_CTX) {
11268                 /*
11269                  * ABTS sent by responder to exchange, just free the buffer
11270                  */
11271                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11272         } else {
11273                 /*
11274                  * ABTS sent by initiator to exchange, need to do cleanup
11275                  */
11276                 /* Try to abort partially assembled seq */
11277                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11278
11279                 /* Send abort to ULP if partially seq abort failed */
11280                 if (abts_par == false)
11281                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11282                 else
11283                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
11284         }
11285         /* Send basic accept (BA_ACC) to the abort requester */
11286         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11287 }
11288
11289 /**
11290  * lpfc_seq_complete - Indicates if a sequence is complete
11291  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11292  *
11293  * This function checks the sequence, starting with the frame described by
11294  * @dmabuf, to see if all the frames associated with this sequence are present.
11295  * the frames associated with this sequence are linked to the @dmabuf using the
11296  * dbuf list. This function looks for two major things. 1) That the first frame
11297  * has a sequence count of zero. 2) There is a frame with last frame of sequence
11298  * set. 3) That there are no holes in the sequence count. The function will
11299  * return 1 when the sequence is complete, otherwise it will return 0.
11300  **/
11301 static int
11302 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11303 {
11304         struct fc_frame_header *hdr;
11305         struct lpfc_dmabuf *d_buf;
11306         struct hbq_dmabuf *seq_dmabuf;
11307         uint32_t fctl;
11308         int seq_count = 0;
11309
11310         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11311         /* make sure first fame of sequence has a sequence count of zero */
11312         if (hdr->fh_seq_cnt != seq_count)
11313                 return 0;
11314         fctl = (hdr->fh_f_ctl[0] << 16 |
11315                 hdr->fh_f_ctl[1] << 8 |
11316                 hdr->fh_f_ctl[2]);
11317         /* If last frame of sequence we can return success. */
11318         if (fctl & FC_FC_END_SEQ)
11319                 return 1;
11320         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11321                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11322                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11323                 /* If there is a hole in the sequence count then fail. */
11324                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
11325                         return 0;
11326                 fctl = (hdr->fh_f_ctl[0] << 16 |
11327                         hdr->fh_f_ctl[1] << 8 |
11328                         hdr->fh_f_ctl[2]);
11329                 /* If last frame of sequence we can return success. */
11330                 if (fctl & FC_FC_END_SEQ)
11331                         return 1;
11332         }
11333         return 0;
11334 }
11335
11336 /**
11337  * lpfc_prep_seq - Prep sequence for ULP processing
11338  * @vport: Pointer to the vport on which this sequence was received
11339  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11340  *
11341  * This function takes a sequence, described by a list of frames, and creates
11342  * a list of iocbq structures to describe the sequence. This iocbq list will be
11343  * used to issue to the generic unsolicited sequence handler. This routine
11344  * returns a pointer to the first iocbq in the list. If the function is unable
11345  * to allocate an iocbq then it throw out the received frames that were not
11346  * able to be described and return a pointer to the first iocbq. If unable to
11347  * allocate any iocbqs (including the first) this function will return NULL.
11348  **/
11349 static struct lpfc_iocbq *
11350 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11351 {
11352         struct lpfc_dmabuf *d_buf, *n_buf;
11353         struct lpfc_iocbq *first_iocbq, *iocbq;
11354         struct fc_frame_header *fc_hdr;
11355         uint32_t sid;
11356         struct ulp_bde64 *pbde;
11357
11358         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11359         /* remove from receive buffer list */
11360         list_del_init(&seq_dmabuf->hbuf.list);
11361         lpfc_update_rcv_time_stamp(vport);
11362         /* get the Remote Port's SID */
11363         sid = sli4_sid_from_fc_hdr(fc_hdr);
11364         /* Get an iocbq struct to fill in. */
11365         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11366         if (first_iocbq) {
11367                 /* Initialize the first IOCB. */
11368                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
11369                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11370                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11371                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11372                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11373                                         vport->vpi + vport->phba->vpi_base;
11374                 /* put the first buffer into the first IOCBq */
11375                 first_iocbq->context2 = &seq_dmabuf->dbuf;
11376                 first_iocbq->context3 = NULL;
11377                 first_iocbq->iocb.ulpBdeCount = 1;
11378                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11379                                                         LPFC_DATA_BUF_SIZE;
11380                 first_iocbq->iocb.un.rcvels.remoteID = sid;
11381                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11382                                 bf_get(lpfc_rcqe_length,
11383                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11384         }
11385         iocbq = first_iocbq;
11386         /*
11387          * Each IOCBq can have two Buffers assigned, so go through the list
11388          * of buffers for this sequence and save two buffers in each IOCBq
11389          */
11390         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11391                 if (!iocbq) {
11392                         lpfc_in_buf_free(vport->phba, d_buf);
11393                         continue;
11394                 }
11395                 if (!iocbq->context3) {
11396                         iocbq->context3 = d_buf;
11397                         iocbq->iocb.ulpBdeCount++;
11398                         pbde = (struct ulp_bde64 *)
11399                                         &iocbq->iocb.unsli3.sli3Words[4];
11400                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
11401                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11402                                 bf_get(lpfc_rcqe_length,
11403                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11404                 } else {
11405                         iocbq = lpfc_sli_get_iocbq(vport->phba);
11406                         if (!iocbq) {
11407                                 if (first_iocbq) {
11408                                         first_iocbq->iocb.ulpStatus =
11409                                                         IOSTAT_FCP_RSP_ERROR;
11410                                         first_iocbq->iocb.un.ulpWord[4] =
11411                                                         IOERR_NO_RESOURCES;
11412                                 }
11413                                 lpfc_in_buf_free(vport->phba, d_buf);
11414                                 continue;
11415                         }
11416                         iocbq->context2 = d_buf;
11417                         iocbq->context3 = NULL;
11418                         iocbq->iocb.ulpBdeCount = 1;
11419                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11420                                                         LPFC_DATA_BUF_SIZE;
11421                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
11422                                 bf_get(lpfc_rcqe_length,
11423                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
11424                         iocbq->iocb.un.rcvels.remoteID = sid;
11425                         list_add_tail(&iocbq->list, &first_iocbq->list);
11426                 }
11427         }
11428         return first_iocbq;
11429 }
11430
11431 static void
11432 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11433                           struct hbq_dmabuf *seq_dmabuf)
11434 {
11435         struct fc_frame_header *fc_hdr;
11436         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11437         struct lpfc_hba *phba = vport->phba;
11438
11439         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11440         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11441         if (!iocbq) {
11442                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11443                                 "2707 Ring %d handler: Failed to allocate "
11444                                 "iocb Rctl x%x Type x%x received\n",
11445                                 LPFC_ELS_RING,
11446                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11447                 return;
11448         }
11449         if (!lpfc_complete_unsol_iocb(phba,
11450                                       &phba->sli.ring[LPFC_ELS_RING],
11451                                       iocbq, fc_hdr->fh_r_ctl,
11452                                       fc_hdr->fh_type))
11453                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11454                                 "2540 Ring %d handler: unexpected Rctl "
11455                                 "x%x Type x%x received\n",
11456                                 LPFC_ELS_RING,
11457                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11458
11459         /* Free iocb created in lpfc_prep_seq */
11460         list_for_each_entry_safe(curr_iocb, next_iocb,
11461                 &iocbq->list, list) {
11462                 list_del_init(&curr_iocb->list);
11463                 lpfc_sli_release_iocbq(phba, curr_iocb);
11464         }
11465         lpfc_sli_release_iocbq(phba, iocbq);
11466 }
11467
11468 /**
11469  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11470  * @phba: Pointer to HBA context object.
11471  *
11472  * This function is called with no lock held. This function processes all
11473  * the received buffers and gives it to upper layers when a received buffer
11474  * indicates that it is the final frame in the sequence. The interrupt
11475  * service routine processes received buffers at interrupt contexts and adds
11476  * received dma buffers to the rb_pend_list queue and signals the worker thread.
11477  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11478  * appropriate receive function when the final frame in a sequence is received.
11479  **/
11480 void
11481 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11482                                  struct hbq_dmabuf *dmabuf)
11483 {
11484         struct hbq_dmabuf *seq_dmabuf;
11485         struct fc_frame_header *fc_hdr;
11486         struct lpfc_vport *vport;
11487         uint32_t fcfi;
11488
11489         /* Process each received buffer */
11490         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11491         /* check to see if this a valid type of frame */
11492         if (lpfc_fc_frame_check(phba, fc_hdr)) {
11493                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11494                 return;
11495         }
11496         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11497         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11498         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
11499                 /* throw out the frame */
11500                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11501                 return;
11502         }
11503         /* Handle the basic abort sequence (BA_ABTS) event */
11504         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11505                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11506                 return;
11507         }
11508
11509         /* Link this frame */
11510         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11511         if (!seq_dmabuf) {
11512                 /* unable to add frame to vport - throw it out */
11513                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11514                 return;
11515         }
11516         /* If not last frame in sequence continue processing frames. */
11517         if (!lpfc_seq_complete(seq_dmabuf))
11518                 return;
11519
11520         /* Send the complete sequence to the upper layer protocol */
11521         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
11522 }
11523
11524 /**
11525  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11526  * @phba: pointer to lpfc hba data structure.
11527  *
11528  * This routine is invoked to post rpi header templates to the
11529  * HBA consistent with the SLI-4 interface spec.  This routine
11530  * posts a PAGE_SIZE memory region to the port to hold up to
11531  * PAGE_SIZE modulo 64 rpi context headers.
11532  *
11533  * This routine does not require any locks.  It's usage is expected
11534  * to be driver load or reset recovery when the driver is
11535  * sequential.
11536  *
11537  * Return codes
11538  *      0 - successful
11539  *      EIO - The mailbox failed to complete successfully.
11540  *      When this error occurs, the driver is not guaranteed
11541  *      to have any rpi regions posted to the device and
11542  *      must either attempt to repost the regions or take a
11543  *      fatal error.
11544  **/
11545 int
11546 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11547 {
11548         struct lpfc_rpi_hdr *rpi_page;
11549         uint32_t rc = 0;
11550
11551         /* Post all rpi memory regions to the port. */
11552         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11553                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11554                 if (rc != MBX_SUCCESS) {
11555                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11556                                         "2008 Error %d posting all rpi "
11557                                         "headers\n", rc);
11558                         rc = -EIO;
11559                         break;
11560                 }
11561         }
11562
11563         return rc;
11564 }
11565
11566 /**
11567  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11568  * @phba: pointer to lpfc hba data structure.
11569  * @rpi_page:  pointer to the rpi memory region.
11570  *
11571  * This routine is invoked to post a single rpi header to the
11572  * HBA consistent with the SLI-4 interface spec.  This memory region
11573  * maps up to 64 rpi context regions.
11574  *
11575  * Return codes
11576  *      0 - successful
11577  *      ENOMEM - No available memory
11578  *      EIO - The mailbox failed to complete successfully.
11579  **/
11580 int
11581 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11582 {
11583         LPFC_MBOXQ_t *mboxq;
11584         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11585         uint32_t rc = 0;
11586         uint32_t mbox_tmo;
11587         uint32_t shdr_status, shdr_add_status;
11588         union lpfc_sli4_cfg_shdr *shdr;
11589
11590         /* The port is notified of the header region via a mailbox command. */
11591         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11592         if (!mboxq) {
11593                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11594                                 "2001 Unable to allocate memory for issuing "
11595                                 "SLI_CONFIG_SPECIAL mailbox command\n");
11596                 return -ENOMEM;
11597         }
11598
11599         /* Post all rpi memory regions to the port. */
11600         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11601         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11602         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11603                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11604                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11605                          sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11606         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11607                hdr_tmpl, rpi_page->page_count);
11608         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11609                rpi_page->start_rpi);
11610         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11611         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
11612         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
11613         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11614         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11615         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11616         if (rc != MBX_TIMEOUT)
11617                 mempool_free(mboxq, phba->mbox_mem_pool);
11618         if (shdr_status || shdr_add_status || rc) {
11619                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11620                                 "2514 POST_RPI_HDR mailbox failed with "
11621                                 "status x%x add_status x%x, mbx status x%x\n",
11622                                 shdr_status, shdr_add_status, rc);
11623                 rc = -ENXIO;
11624         }
11625         return rc;
11626 }
11627
11628 /**
11629  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11630  * @phba: pointer to lpfc hba data structure.
11631  *
11632  * This routine is invoked to post rpi header templates to the
11633  * HBA consistent with the SLI-4 interface spec.  This routine
11634  * posts a PAGE_SIZE memory region to the port to hold up to
11635  * PAGE_SIZE modulo 64 rpi context headers.
11636  *
11637  * Returns
11638  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
11639  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
11640  **/
11641 int
11642 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11643 {
11644         int rpi;
11645         uint16_t max_rpi, rpi_base, rpi_limit;
11646         uint16_t rpi_remaining;
11647         struct lpfc_rpi_hdr *rpi_hdr;
11648
11649         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11650         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11651         rpi_limit = phba->sli4_hba.next_rpi;
11652
11653         /*
11654          * The valid rpi range is not guaranteed to be zero-based.  Start
11655          * the search at the rpi_base as reported by the port.
11656          */
11657         spin_lock_irq(&phba->hbalock);
11658         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11659         if (rpi >= rpi_limit || rpi < rpi_base)
11660                 rpi = LPFC_RPI_ALLOC_ERROR;
11661         else {
11662                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11663                 phba->sli4_hba.max_cfg_param.rpi_used++;
11664                 phba->sli4_hba.rpi_count++;
11665         }
11666
11667         /*
11668          * Don't try to allocate more rpi header regions if the device limit
11669          * on available rpis max has been exhausted.
11670          */
11671         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11672             (phba->sli4_hba.rpi_count >= max_rpi)) {
11673                 spin_unlock_irq(&phba->hbalock);
11674                 return rpi;
11675         }
11676
11677         /*
11678          * If the driver is running low on rpi resources, allocate another
11679          * page now.  Note that the next_rpi value is used because
11680          * it represents how many are actually in use whereas max_rpi notes
11681          * how many are supported max by the device.
11682          */
11683         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11684                 phba->sli4_hba.rpi_count;
11685         spin_unlock_irq(&phba->hbalock);
11686         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11687                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11688                 if (!rpi_hdr) {
11689                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11690                                         "2002 Error Could not grow rpi "
11691                                         "count\n");
11692                 } else {
11693                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11694                 }
11695         }
11696
11697         return rpi;
11698 }
11699
11700 /**
11701  * lpfc_sli4_free_rpi - Release an rpi for reuse.
11702  * @phba: pointer to lpfc hba data structure.
11703  *
11704  * This routine is invoked to release an rpi to the pool of
11705  * available rpis maintained by the driver.
11706  **/
11707 void
11708 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11709 {
11710         spin_lock_irq(&phba->hbalock);
11711         clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11712         phba->sli4_hba.rpi_count--;
11713         phba->sli4_hba.max_cfg_param.rpi_used--;
11714         spin_unlock_irq(&phba->hbalock);
11715 }
11716
11717 /**
11718  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11719  * @phba: pointer to lpfc hba data structure.
11720  *
11721  * This routine is invoked to remove the memory region that
11722  * provided rpi via a bitmask.
11723  **/
11724 void
11725 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11726 {
11727         kfree(phba->sli4_hba.rpi_bmask);
11728 }
11729
11730 /**
11731  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11732  * @phba: pointer to lpfc hba data structure.
11733  *
11734  * This routine is invoked to remove the memory region that
11735  * provided rpi via a bitmask.
11736  **/
11737 int
11738 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11739 {
11740         LPFC_MBOXQ_t *mboxq;
11741         struct lpfc_hba *phba = ndlp->phba;
11742         int rc;
11743
11744         /* The port is notified of the header region via a mailbox command. */
11745         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11746         if (!mboxq)
11747                 return -ENOMEM;
11748
11749         /* Post all rpi memory regions to the port. */
11750         lpfc_resume_rpi(mboxq, ndlp);
11751         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11752         if (rc == MBX_NOT_FINISHED) {
11753                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11754                                 "2010 Resume RPI Mailbox failed "
11755                                 "status %d, mbxStatus x%x\n", rc,
11756                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11757                 mempool_free(mboxq, phba->mbox_mem_pool);
11758                 return -EIO;
11759         }
11760         return 0;
11761 }
11762
11763 /**
11764  * lpfc_sli4_init_vpi - Initialize a vpi with the port
11765  * @phba: pointer to lpfc hba data structure.
11766  * @vpi: vpi value to activate with the port.
11767  *
11768  * This routine is invoked to activate a vpi with the
11769  * port when the host intends to use vports with a
11770  * nonzero vpi.
11771  *
11772  * Returns:
11773  *    0 success
11774  *    -Evalue otherwise
11775  **/
11776 int
11777 lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11778 {
11779         LPFC_MBOXQ_t *mboxq;
11780         int rc = 0;
11781         int retval = MBX_SUCCESS;
11782         uint32_t mbox_tmo;
11783
11784         if (vpi == 0)
11785                 return -EINVAL;
11786         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11787         if (!mboxq)
11788                 return -ENOMEM;
11789         lpfc_init_vpi(phba, mboxq, vpi);
11790         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11791         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11792         if (rc != MBX_SUCCESS) {
11793                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11794                                 "2022 INIT VPI Mailbox failed "
11795                                 "status %d, mbxStatus x%x\n", rc,
11796                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11797                 retval = -EIO;
11798         }
11799         if (rc != MBX_TIMEOUT)
11800                 mempool_free(mboxq, phba->mbox_mem_pool);
11801
11802         return retval;
11803 }
11804
11805 /**
11806  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11807  * @phba: pointer to lpfc hba data structure.
11808  * @mboxq: Pointer to mailbox object.
11809  *
11810  * This routine is invoked to manually add a single FCF record. The caller
11811  * must pass a completely initialized FCF_Record.  This routine takes
11812  * care of the nonembedded mailbox operations.
11813  **/
11814 static void
11815 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11816 {
11817         void *virt_addr;
11818         union lpfc_sli4_cfg_shdr *shdr;
11819         uint32_t shdr_status, shdr_add_status;
11820
11821         virt_addr = mboxq->sge_array->addr[0];
11822         /* The IOCTL status is embedded in the mailbox subheader. */
11823         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11824         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11825         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11826
11827         if ((shdr_status || shdr_add_status) &&
11828                 (shdr_status != STATUS_FCF_IN_USE))
11829                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11830                         "2558 ADD_FCF_RECORD mailbox failed with "
11831                         "status x%x add_status x%x\n",
11832                         shdr_status, shdr_add_status);
11833
11834         lpfc_sli4_mbox_cmd_free(phba, mboxq);
11835 }
11836
11837 /**
11838  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11839  * @phba: pointer to lpfc hba data structure.
11840  * @fcf_record:  pointer to the initialized fcf record to add.
11841  *
11842  * This routine is invoked to manually add a single FCF record. The caller
11843  * must pass a completely initialized FCF_Record.  This routine takes
11844  * care of the nonembedded mailbox operations.
11845  **/
11846 int
11847 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11848 {
11849         int rc = 0;
11850         LPFC_MBOXQ_t *mboxq;
11851         uint8_t *bytep;
11852         void *virt_addr;
11853         dma_addr_t phys_addr;
11854         struct lpfc_mbx_sge sge;
11855         uint32_t alloc_len, req_len;
11856         uint32_t fcfindex;
11857
11858         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11859         if (!mboxq) {
11860                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11861                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11862                 return -ENOMEM;
11863         }
11864
11865         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11866                   sizeof(uint32_t);
11867
11868         /* Allocate DMA memory and set up the non-embedded mailbox command */
11869         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11870                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11871                                      req_len, LPFC_SLI4_MBX_NEMBED);
11872         if (alloc_len < req_len) {
11873                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11874                         "2523 Allocated DMA memory size (x%x) is "
11875                         "less than the requested DMA memory "
11876                         "size (x%x)\n", alloc_len, req_len);
11877                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11878                 return -ENOMEM;
11879         }
11880
11881         /*
11882          * Get the first SGE entry from the non-embedded DMA memory.  This
11883          * routine only uses a single SGE.
11884          */
11885         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11886         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11887         virt_addr = mboxq->sge_array->addr[0];
11888         /*
11889          * Configure the FCF record for FCFI 0.  This is the driver's
11890          * hardcoded default and gets used in nonFIP mode.
11891          */
11892         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11893         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11894         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11895
11896         /*
11897          * Copy the fcf_index and the FCF Record Data. The data starts after
11898          * the FCoE header plus word10. The data copy needs to be endian
11899          * correct.
11900          */
11901         bytep += sizeof(uint32_t);
11902         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11903         mboxq->vport = phba->pport;
11904         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11905         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11906         if (rc == MBX_NOT_FINISHED) {
11907                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11908                         "2515 ADD_FCF_RECORD mailbox failed with "
11909                         "status 0x%x\n", rc);
11910                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11911                 rc = -EIO;
11912         } else
11913                 rc = 0;
11914
11915         return rc;
11916 }
11917
11918 /**
11919  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11920  * @phba: pointer to lpfc hba data structure.
11921  * @fcf_record:  pointer to the fcf record to write the default data.
11922  * @fcf_index: FCF table entry index.
11923  *
11924  * This routine is invoked to build the driver's default FCF record.  The
11925  * values used are hardcoded.  This routine handles memory initialization.
11926  *
11927  **/
11928 void
11929 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11930                                 struct fcf_record *fcf_record,
11931                                 uint16_t fcf_index)
11932 {
11933         memset(fcf_record, 0, sizeof(struct fcf_record));
11934         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11935         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11936         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11937         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11938         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11939         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11940         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11941         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11942         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11943         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11944         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11945         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11946         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
11947         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
11948         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11949         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11950                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11951         /* Set the VLAN bit map */
11952         if (phba->valid_vlan) {
11953                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11954                         = 1 << (phba->vlan_id % 8);
11955         }
11956 }
11957
11958 /**
11959  * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11960  * @phba: pointer to lpfc hba data structure.
11961  * @fcf_index: FCF table entry offset.
11962  *
11963  * This routine is invoked to read up to @fcf_num of FCF record from the
11964  * device starting with the given @fcf_index.
11965  **/
11966 int
11967 lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11968 {
11969         int rc = 0, error;
11970         LPFC_MBOXQ_t *mboxq;
11971
11972         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
11973         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11974         if (!mboxq) {
11975                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11976                                 "2000 Failed to allocate mbox for "
11977                                 "READ_FCF cmd\n");
11978                 error = -ENOMEM;
11979                 goto fail_fcfscan;
11980         }
11981         /* Construct the read FCF record mailbox command */
11982         rc = lpfc_sli4_mbx_read_fcf_record(phba, mboxq, fcf_index);
11983         if (rc) {
11984                 error = -EINVAL;
11985                 goto fail_fcfscan;
11986         }
11987         /* Issue the mailbox command asynchronously */
11988         mboxq->vport = phba->pport;
11989         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11990         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11991         if (rc == MBX_NOT_FINISHED)
11992                 error = -EIO;
11993         else {
11994                 spin_lock_irq(&phba->hbalock);
11995                 phba->hba_flag |= FCF_DISC_INPROGRESS;
11996                 spin_unlock_irq(&phba->hbalock);
11997                 error = 0;
11998         }
11999 fail_fcfscan:
12000         if (error) {
12001                 if (mboxq)
12002                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12003                 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12004                 spin_lock_irq(&phba->hbalock);
12005                 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12006                 spin_unlock_irq(&phba->hbalock);
12007         }
12008         return error;
12009 }
12010
12011 /**
12012  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12013  * @phba: pointer to lpfc hba data structure.
12014  *
12015  * This routine is the completion routine for the rediscover FCF table mailbox
12016  * command. If the mailbox command returned failure, it will try to stop the
12017  * FCF rediscover wait timer.
12018  **/
12019 void
12020 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12021 {
12022         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12023         uint32_t shdr_status, shdr_add_status;
12024
12025         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12026
12027         shdr_status = bf_get(lpfc_mbox_hdr_status,
12028                              &redisc_fcf->header.cfg_shdr.response);
12029         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12030                              &redisc_fcf->header.cfg_shdr.response);
12031         if (shdr_status || shdr_add_status) {
12032                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12033                                 "2746 Requesting for FCF rediscovery failed "
12034                                 "status x%x add_status x%x\n",
12035                                 shdr_status, shdr_add_status);
12036                 /*
12037                  * Request failed, last resort to re-try current
12038                  * registered FCF entry
12039                  */
12040                 lpfc_retry_pport_discovery(phba);
12041         } else
12042                 /*
12043                  * Start FCF rediscovery wait timer for pending FCF
12044                  * before rescan FCF record table.
12045                  */
12046                 lpfc_fcf_redisc_wait_start_timer(phba);
12047
12048         mempool_free(mbox, phba->mbox_mem_pool);
12049 }
12050
12051 /**
12052  * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12053  * @phba: pointer to lpfc hba data structure.
12054  *
12055  * This routine is invoked to request for rediscovery of the entire FCF table
12056  * by the port.
12057  **/
12058 int
12059 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12060 {
12061         LPFC_MBOXQ_t *mbox;
12062         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12063         int rc, length;
12064
12065         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12066         if (!mbox) {
12067                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12068                                 "2745 Failed to allocate mbox for "
12069                                 "requesting FCF rediscover.\n");
12070                 return -ENOMEM;
12071         }
12072
12073         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12074                   sizeof(struct lpfc_sli4_cfg_mhdr));
12075         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12076                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12077                          length, LPFC_SLI4_MBX_EMBED);
12078
12079         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12080         /* Set count to 0 for invalidating the entire FCF database */
12081         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12082
12083         /* Issue the mailbox command asynchronously */
12084         mbox->vport = phba->pport;
12085         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12086         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12087
12088         if (rc == MBX_NOT_FINISHED) {
12089                 mempool_free(mbox, phba->mbox_mem_pool);
12090                 return -EIO;
12091         }
12092         return 0;
12093 }
12094
12095 /**
12096  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12097  * @phba: pointer to lpfc hba data structure.
12098  *
12099  * This function read region 23 and parse TLV for port status to
12100  * decide if the user disaled the port. If the TLV indicates the
12101  * port is disabled, the hba_flag is set accordingly.
12102  **/
12103 void
12104 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12105 {
12106         LPFC_MBOXQ_t *pmb = NULL;
12107         MAILBOX_t *mb;
12108         uint8_t *rgn23_data = NULL;
12109         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12110         int rc;
12111
12112         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12113         if (!pmb) {
12114                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12115                         "2600 lpfc_sli_read_serdes_param failed to"
12116                         " allocate mailbox memory\n");
12117                 goto out;
12118         }
12119         mb = &pmb->u.mb;
12120
12121         /* Get adapter Region 23 data */
12122         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12123         if (!rgn23_data)
12124                 goto out;
12125
12126         do {
12127                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12128                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12129
12130                 if (rc != MBX_SUCCESS) {
12131                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12132                                 "2601 lpfc_sli_read_link_ste failed to"
12133                                 " read config region 23 rc 0x%x Status 0x%x\n",
12134                                 rc, mb->mbxStatus);
12135                         mb->un.varDmp.word_cnt = 0;
12136                 }
12137                 /*
12138                  * dump mem may return a zero when finished or we got a
12139                  * mailbox error, either way we are done.
12140                  */
12141                 if (mb->un.varDmp.word_cnt == 0)
12142                         break;
12143                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12144                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12145
12146                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12147                         rgn23_data + offset,
12148                         mb->un.varDmp.word_cnt);
12149                 offset += mb->un.varDmp.word_cnt;
12150         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12151
12152         data_size = offset;
12153         offset = 0;
12154
12155         if (!data_size)
12156                 goto out;
12157
12158         /* Check the region signature first */
12159         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12160                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12161                         "2619 Config region 23 has bad signature\n");
12162                         goto out;
12163         }
12164         offset += 4;
12165
12166         /* Check the data structure version */
12167         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12168                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12169                         "2620 Config region 23 has bad version\n");
12170                 goto out;
12171         }
12172         offset += 4;
12173
12174         /* Parse TLV entries in the region */
12175         while (offset < data_size) {
12176                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12177                         break;
12178                 /*
12179                  * If the TLV is not driver specific TLV or driver id is
12180                  * not linux driver id, skip the record.
12181                  */
12182                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12183                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12184                     (rgn23_data[offset + 3] != 0)) {
12185                         offset += rgn23_data[offset + 1] * 4 + 4;
12186                         continue;
12187                 }
12188
12189                 /* Driver found a driver specific TLV in the config region */
12190                 sub_tlv_len = rgn23_data[offset + 1] * 4;
12191                 offset += 4;
12192                 tlv_offset = 0;
12193
12194                 /*
12195                  * Search for configured port state sub-TLV.
12196                  */
12197                 while ((offset < data_size) &&
12198                         (tlv_offset < sub_tlv_len)) {
12199                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12200                                 offset += 4;
12201                                 tlv_offset += 4;
12202                                 break;
12203                         }
12204                         if (rgn23_data[offset] != PORT_STE_TYPE) {
12205                                 offset += rgn23_data[offset + 1] * 4 + 4;
12206                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12207                                 continue;
12208                         }
12209
12210                         /* This HBA contains PORT_STE configured */
12211                         if (!rgn23_data[offset + 2])
12212                                 phba->hba_flag |= LINK_DISABLED;
12213
12214                         goto out;
12215                 }
12216         }
12217 out:
12218         if (pmb)
12219                 mempool_free(pmb, phba->mbox_mem_pool);
12220         kfree(rgn23_data);
12221         return;
12222 }
12223
12224 /**
12225  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12226  * @vport: pointer to vport data structure.
12227  *
12228  * This function iterate through the mailboxq and clean up all REG_LOGIN
12229  * and REG_VPI mailbox commands associated with the vport. This function
12230  * is called when driver want to restart discovery of the vport due to
12231  * a Clear Virtual Link event.
12232  **/
12233 void
12234 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12235 {
12236         struct lpfc_hba *phba = vport->phba;
12237         LPFC_MBOXQ_t *mb, *nextmb;
12238         struct lpfc_dmabuf *mp;
12239
12240         spin_lock_irq(&phba->hbalock);
12241         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12242                 if (mb->vport != vport)
12243                         continue;
12244
12245                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12246                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
12247                         continue;
12248
12249                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12250                         mp = (struct lpfc_dmabuf *) (mb->context1);
12251                         if (mp) {
12252                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12253                                 kfree(mp);
12254                         }
12255                 }
12256                 list_del(&mb->list);
12257                 mempool_free(mb, phba->mbox_mem_pool);
12258         }
12259         mb = phba->sli.mbox_active;
12260         if (mb && (mb->vport == vport)) {
12261                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12262                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
12263                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12264         }
12265         spin_unlock_irq(&phba->hbalock);
12266 }
12267