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