2ee0374a99087a651020c4ca47595f110160fd7e
[pandora-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52         LPFC_UNKNOWN_IOCB,
53         LPFC_UNSOL_IOCB,
54         LPFC_SOL_IOCB,
55         LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61                                   uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63                               uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65                                                          struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67                                       struct hbq_dmabuf *);
68 static IOCB_t *
69 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70 {
71         return &iocbq->iocb;
72 }
73
74 /**
75  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76  * @q: The Work Queue to operate on.
77  * @wqe: The work Queue Entry to put on the Work queue.
78  *
79  * This routine will copy the contents of @wqe to the next available entry on
80  * the @q. This function will then ring the Work Queue Doorbell to signal the
81  * HBA to start processing the Work Queue Entry. This function returns 0 if
82  * successful. If no entries are available on @q then this function will return
83  * -ENOMEM.
84  * The caller is expected to hold the hbalock when calling this routine.
85  **/
86 static uint32_t
87 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88 {
89         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90         struct lpfc_register doorbell;
91         uint32_t host_index;
92
93         /* If the host has not yet processed the next entry then we are done */
94         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95                 return -ENOMEM;
96         /* set consumption flag every once in a while */
97         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98                 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
99         if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
100                 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
101         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
102
103         /* Update the host index before invoking device */
104         host_index = q->host_index;
105         q->host_index = ((q->host_index + 1) % q->entry_count);
106
107         /* Ring Doorbell */
108         doorbell.word0 = 0;
109         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
110         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
111         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
112         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
113         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
114
115         return 0;
116 }
117
118 /**
119  * lpfc_sli4_wq_release - Updates internal hba index for WQ
120  * @q: The Work Queue to operate on.
121  * @index: The index to advance the hba index to.
122  *
123  * This routine will update the HBA index of a queue to reflect consumption of
124  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
125  * an entry the host calls this function to update the queue's internal
126  * pointers. This routine returns the number of entries that were consumed by
127  * the HBA.
128  **/
129 static uint32_t
130 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
131 {
132         uint32_t released = 0;
133
134         if (q->hba_index == index)
135                 return 0;
136         do {
137                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
138                 released++;
139         } while (q->hba_index != index);
140         return released;
141 }
142
143 /**
144  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
145  * @q: The Mailbox Queue to operate on.
146  * @wqe: The Mailbox Queue Entry to put on the Work queue.
147  *
148  * This routine will copy the contents of @mqe to the next available entry on
149  * the @q. This function will then ring the Work Queue Doorbell to signal the
150  * HBA to start processing the Work Queue Entry. This function returns 0 if
151  * successful. If no entries are available on @q then this function will return
152  * -ENOMEM.
153  * The caller is expected to hold the hbalock when calling this routine.
154  **/
155 static uint32_t
156 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
157 {
158         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
159         struct lpfc_register doorbell;
160         uint32_t host_index;
161
162         /* If the host has not yet processed the next entry then we are done */
163         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
164                 return -ENOMEM;
165         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
166         /* Save off the mailbox pointer for completion */
167         q->phba->mbox = (MAILBOX_t *)temp_mqe;
168
169         /* Update the host index before invoking device */
170         host_index = q->host_index;
171         q->host_index = ((q->host_index + 1) % q->entry_count);
172
173         /* Ring Doorbell */
174         doorbell.word0 = 0;
175         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
176         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
177         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
178         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
179         return 0;
180 }
181
182 /**
183  * lpfc_sli4_mq_release - Updates internal hba index for MQ
184  * @q: The Mailbox Queue to operate on.
185  *
186  * This routine will update the HBA index of a queue to reflect consumption of
187  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
188  * an entry the host calls this function to update the queue's internal
189  * pointers. This routine returns the number of entries that were consumed by
190  * the HBA.
191  **/
192 static uint32_t
193 lpfc_sli4_mq_release(struct lpfc_queue *q)
194 {
195         /* Clear the mailbox pointer for completion */
196         q->phba->mbox = NULL;
197         q->hba_index = ((q->hba_index + 1) % q->entry_count);
198         return 1;
199 }
200
201 /**
202  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
203  * @q: The Event Queue to get the first valid EQE from
204  *
205  * This routine will get the first valid Event Queue Entry from @q, update
206  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
207  * the Queue (no more work to do), or the Queue is full of EQEs that have been
208  * processed, but not popped back to the HBA then this routine will return NULL.
209  **/
210 static struct lpfc_eqe *
211 lpfc_sli4_eq_get(struct lpfc_queue *q)
212 {
213         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
214
215         /* If the next EQE is not valid then we are done */
216         if (!bf_get_le32(lpfc_eqe_valid, eqe))
217                 return NULL;
218         /* If the host has not yet processed the next entry then we are done */
219         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
220                 return NULL;
221
222         q->hba_index = ((q->hba_index + 1) % q->entry_count);
223         return eqe;
224 }
225
226 /**
227  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
228  * @q: The Event Queue that the host has completed processing for.
229  * @arm: Indicates whether the host wants to arms this CQ.
230  *
231  * This routine will mark all Event Queue Entries on @q, from the last
232  * known completed entry to the last entry that was processed, as completed
233  * by clearing the valid bit for each completion queue entry. Then it will
234  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
235  * The internal host index in the @q will be updated by this routine to indicate
236  * that the host has finished processing the entries. The @arm parameter
237  * indicates that the queue should be rearmed when ringing the doorbell.
238  *
239  * This function will return the number of EQEs that were popped.
240  **/
241 uint32_t
242 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
243 {
244         uint32_t released = 0;
245         struct lpfc_eqe *temp_eqe;
246         struct lpfc_register doorbell;
247
248         /* while there are valid entries */
249         while (q->hba_index != q->host_index) {
250                 temp_eqe = q->qe[q->host_index].eqe;
251                 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
252                 released++;
253                 q->host_index = ((q->host_index + 1) % q->entry_count);
254         }
255         if (unlikely(released == 0 && !arm))
256                 return 0;
257
258         /* ring doorbell for number popped */
259         doorbell.word0 = 0;
260         if (arm) {
261                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
262                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
263         }
264         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
265         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
266         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
267         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
268         /* PCI read to flush PCI pipeline on re-arming for INTx mode */
269         if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
270                 readl(q->phba->sli4_hba.EQCQDBregaddr);
271         return released;
272 }
273
274 /**
275  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
276  * @q: The Completion Queue to get the first valid CQE from
277  *
278  * This routine will get the first valid Completion Queue Entry from @q, update
279  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
280  * the Queue (no more work to do), or the Queue is full of CQEs that have been
281  * processed, but not popped back to the HBA then this routine will return NULL.
282  **/
283 static struct lpfc_cqe *
284 lpfc_sli4_cq_get(struct lpfc_queue *q)
285 {
286         struct lpfc_cqe *cqe;
287
288         /* If the next CQE is not valid then we are done */
289         if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
290                 return NULL;
291         /* If the host has not yet processed the next entry then we are done */
292         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
293                 return NULL;
294
295         cqe = q->qe[q->hba_index].cqe;
296         q->hba_index = ((q->hba_index + 1) % q->entry_count);
297         return cqe;
298 }
299
300 /**
301  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
302  * @q: The Completion Queue that the host has completed processing for.
303  * @arm: Indicates whether the host wants to arms this CQ.
304  *
305  * This routine will mark all Completion queue entries on @q, from the last
306  * known completed entry to the last entry that was processed, as completed
307  * by clearing the valid bit for each completion queue entry. Then it will
308  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
309  * The internal host index in the @q will be updated by this routine to indicate
310  * that the host has finished processing the entries. The @arm parameter
311  * indicates that the queue should be rearmed when ringing the doorbell.
312  *
313  * This function will return the number of CQEs that were released.
314  **/
315 uint32_t
316 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
317 {
318         uint32_t released = 0;
319         struct lpfc_cqe *temp_qe;
320         struct lpfc_register doorbell;
321
322         /* while there are valid entries */
323         while (q->hba_index != q->host_index) {
324                 temp_qe = q->qe[q->host_index].cqe;
325                 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
326                 released++;
327                 q->host_index = ((q->host_index + 1) % q->entry_count);
328         }
329         if (unlikely(released == 0 && !arm))
330                 return 0;
331
332         /* ring doorbell for number popped */
333         doorbell.word0 = 0;
334         if (arm)
335                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
336         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
337         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
338         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
339         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
340         return released;
341 }
342
343 /**
344  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
345  * @q: The Header Receive Queue to operate on.
346  * @wqe: The Receive Queue Entry to put on the Receive queue.
347  *
348  * This routine will copy the contents of @wqe to the next available entry on
349  * the @q. This function will then ring the Receive Queue Doorbell to signal the
350  * HBA to start processing the Receive Queue Entry. This function returns the
351  * index that the rqe was copied to if successful. If no entries are available
352  * on @q then this function will return -ENOMEM.
353  * The caller is expected to hold the hbalock when calling this routine.
354  **/
355 static int
356 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
357                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
358 {
359         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
360         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
361         struct lpfc_register doorbell;
362         int put_index = hq->host_index;
363
364         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
365                 return -EINVAL;
366         if (hq->host_index != dq->host_index)
367                 return -EINVAL;
368         /* If the host has not yet processed the next entry then we are done */
369         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
370                 return -EBUSY;
371         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
372         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
373
374         /* Update the host index to point to the next slot */
375         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
376         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
377
378         /* Ring The Header Receive Queue Doorbell */
379         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
380                 doorbell.word0 = 0;
381                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
382                        LPFC_RQ_POST_BATCH);
383                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
384                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
385         }
386         return put_index;
387 }
388
389 /**
390  * lpfc_sli4_rq_release - Updates internal hba index for RQ
391  * @q: The Header Receive Queue to operate on.
392  *
393  * This routine will update the HBA index of a queue to reflect consumption of
394  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
395  * consumed an entry the host calls this function to update the queue's
396  * internal pointers. This routine returns the number of entries that were
397  * consumed by the HBA.
398  **/
399 static uint32_t
400 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
401 {
402         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
403                 return 0;
404         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
405         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
406         return 1;
407 }
408
409 /**
410  * lpfc_cmd_iocb - Get next command iocb entry in the ring
411  * @phba: Pointer to HBA context object.
412  * @pring: Pointer to driver SLI ring object.
413  *
414  * This function returns pointer to next command iocb entry
415  * in the command ring. The caller must hold hbalock to prevent
416  * other threads consume the next command iocb.
417  * SLI-2/SLI-3 provide different sized iocbs.
418  **/
419 static inline IOCB_t *
420 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
421 {
422         return (IOCB_t *) (((char *) pring->cmdringaddr) +
423                            pring->cmdidx * phba->iocb_cmd_size);
424 }
425
426 /**
427  * lpfc_resp_iocb - Get next response iocb entry in the ring
428  * @phba: Pointer to HBA context object.
429  * @pring: Pointer to driver SLI ring object.
430  *
431  * This function returns pointer to next response iocb entry
432  * in the response ring. The caller must hold hbalock to make sure
433  * that no other thread consume the next response iocb.
434  * SLI-2/SLI-3 provide different sized iocbs.
435  **/
436 static inline IOCB_t *
437 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
438 {
439         return (IOCB_t *) (((char *) pring->rspringaddr) +
440                            pring->rspidx * phba->iocb_rsp_size);
441 }
442
443 /**
444  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
445  * @phba: Pointer to HBA context object.
446  *
447  * This function is called with hbalock held. This function
448  * allocates a new driver iocb object from the iocb pool. If the
449  * allocation is successful, it returns pointer to the newly
450  * allocated iocb object else it returns NULL.
451  **/
452 static struct lpfc_iocbq *
453 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
454 {
455         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
456         struct lpfc_iocbq * iocbq = NULL;
457
458         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
459
460         if (iocbq)
461                 phba->iocb_cnt++;
462         if (phba->iocb_cnt > phba->iocb_max)
463                 phba->iocb_max = phba->iocb_cnt;
464         return iocbq;
465 }
466
467 /**
468  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
469  * @phba: Pointer to HBA context object.
470  * @xritag: XRI value.
471  *
472  * This function clears the sglq pointer from the array of acive
473  * sglq's. The xritag that is passed in is used to index into the
474  * array. Before the xritag can be used it needs to be adjusted
475  * by subtracting the xribase.
476  *
477  * Returns sglq ponter = success, NULL = Failure.
478  **/
479 static struct lpfc_sglq *
480 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
481 {
482         uint16_t adj_xri;
483         struct lpfc_sglq *sglq;
484         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
485         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
486                 return NULL;
487         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
488         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
489         return sglq;
490 }
491
492 /**
493  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
494  * @phba: Pointer to HBA context object.
495  * @xritag: XRI value.
496  *
497  * This function returns the sglq pointer from the array of acive
498  * sglq's. The xritag that is passed in is used to index into the
499  * array. Before the xritag can be used it needs to be adjusted
500  * by subtracting the xribase.
501  *
502  * Returns sglq ponter = success, NULL = Failure.
503  **/
504 struct lpfc_sglq *
505 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
506 {
507         uint16_t adj_xri;
508         struct lpfc_sglq *sglq;
509         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
510         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
511                 return NULL;
512         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
513         return sglq;
514 }
515
516 /**
517  * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
518  * @phba: Pointer to HBA context object.
519  * @ndlp: nodelist pointer for this target.
520  * @xritag: xri used in this exchange.
521  * @rxid: Remote Exchange ID.
522  * @send_rrq: Flag used to determine if we should send rrq els cmd.
523  *
524  * This function is called with hbalock held.
525  * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
526  * rrq struct and adds it to the active_rrq_list.
527  *
528  * returns  0 for rrq slot for this xri
529  *         < 0  Were not able to get rrq mem or invalid parameter.
530  **/
531 static int
532 __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
533                 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
534 {
535         uint16_t adj_xri;
536         struct lpfc_node_rrq *rrq;
537         int empty;
538         uint32_t did = 0;
539
540
541         if (!ndlp)
542                 return -EINVAL;
543
544         if (!phba->cfg_enable_rrq)
545                 return -EINVAL;
546
547         if (phba->pport->load_flag & FC_UNLOADING) {
548                 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
549                 goto out;
550         }
551         did = ndlp->nlp_DID;
552
553         /*
554          * set the active bit even if there is no mem available.
555          */
556         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
557
558         if (NLP_CHK_FREE_REQ(ndlp))
559                 goto out;
560
561         if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
562                 goto out;
563
564         if (test_and_set_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
565                 goto out;
566
567         rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
568         if (rrq) {
569                 rrq->send_rrq = send_rrq;
570                 rrq->xritag = xritag;
571                 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
572                 rrq->ndlp = ndlp;
573                 rrq->nlp_DID = ndlp->nlp_DID;
574                 rrq->vport = ndlp->vport;
575                 rrq->rxid = rxid;
576                 empty = list_empty(&phba->active_rrq_list);
577                 rrq->send_rrq = send_rrq;
578                 list_add_tail(&rrq->list, &phba->active_rrq_list);
579                 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
580                         phba->hba_flag |= HBA_RRQ_ACTIVE;
581                         if (empty)
582                                 lpfc_worker_wake_up(phba);
583                 }
584                 return 0;
585         }
586 out:
587         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
588                         "2921 Can't set rrq active xri:0x%x rxid:0x%x"
589                         " DID:0x%x Send:%d\n",
590                         xritag, rxid, did, send_rrq);
591         return -EINVAL;
592 }
593
594 /**
595  * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
596  * @phba: Pointer to HBA context object.
597  * @xritag: xri used in this exchange.
598  * @rrq: The RRQ to be cleared.
599  *
600  **/
601 void
602 lpfc_clr_rrq_active(struct lpfc_hba *phba,
603                     uint16_t xritag,
604                     struct lpfc_node_rrq *rrq)
605 {
606         uint16_t adj_xri;
607         struct lpfc_nodelist *ndlp = NULL;
608
609         if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
610                 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
611
612         /* The target DID could have been swapped (cable swap)
613          * we should use the ndlp from the findnode if it is
614          * available.
615          */
616         if ((!ndlp) && rrq->ndlp)
617                 ndlp = rrq->ndlp;
618
619         if (!ndlp)
620                 goto out;
621
622         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
623         if (test_and_clear_bit(adj_xri, ndlp->active_rrqs.xri_bitmap)) {
624                 rrq->send_rrq = 0;
625                 rrq->xritag = 0;
626                 rrq->rrq_stop_time = 0;
627         }
628 out:
629         mempool_free(rrq, phba->rrq_pool);
630 }
631
632 /**
633  * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
634  * @phba: Pointer to HBA context object.
635  *
636  * This function is called with hbalock held. This function
637  * Checks if stop_time (ratov from setting rrq active) has
638  * been reached, if it has and the send_rrq flag is set then
639  * it will call lpfc_send_rrq. If the send_rrq flag is not set
640  * then it will just call the routine to clear the rrq and
641  * free the rrq resource.
642  * The timer is set to the next rrq that is going to expire before
643  * leaving the routine.
644  *
645  **/
646 void
647 lpfc_handle_rrq_active(struct lpfc_hba *phba)
648 {
649         struct lpfc_node_rrq *rrq;
650         struct lpfc_node_rrq *nextrrq;
651         unsigned long next_time;
652         unsigned long iflags;
653         LIST_HEAD(send_rrq);
654
655         spin_lock_irqsave(&phba->hbalock, iflags);
656         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
657         next_time = jiffies + HZ * (phba->fc_ratov + 1);
658         list_for_each_entry_safe(rrq, nextrrq,
659                                  &phba->active_rrq_list, list) {
660                 if (time_after(jiffies, rrq->rrq_stop_time))
661                         list_move(&rrq->list, &send_rrq);
662                 else if (time_before(rrq->rrq_stop_time, next_time))
663                         next_time = rrq->rrq_stop_time;
664         }
665         spin_unlock_irqrestore(&phba->hbalock, iflags);
666         if (!list_empty(&phba->active_rrq_list))
667                 mod_timer(&phba->rrq_tmr, next_time);
668         list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
669                 list_del(&rrq->list);
670                 if (!rrq->send_rrq)
671                         /* this call will free the rrq */
672                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
673                 else if (lpfc_send_rrq(phba, rrq)) {
674                         /* if we send the rrq then the completion handler
675                         *  will clear the bit in the xribitmap.
676                         */
677                         lpfc_clr_rrq_active(phba, rrq->xritag,
678                                             rrq);
679                 }
680         }
681 }
682
683 /**
684  * lpfc_get_active_rrq - Get the active RRQ for this exchange.
685  * @vport: Pointer to vport context object.
686  * @xri: The xri used in the exchange.
687  * @did: The targets DID for this exchange.
688  *
689  * returns NULL = rrq not found in the phba->active_rrq_list.
690  *         rrq = rrq for this xri and target.
691  **/
692 struct lpfc_node_rrq *
693 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
694 {
695         struct lpfc_hba *phba = vport->phba;
696         struct lpfc_node_rrq *rrq;
697         struct lpfc_node_rrq *nextrrq;
698         unsigned long iflags;
699
700         if (phba->sli_rev != LPFC_SLI_REV4)
701                 return NULL;
702         spin_lock_irqsave(&phba->hbalock, iflags);
703         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
704                 if (rrq->vport == vport && rrq->xritag == xri &&
705                                 rrq->nlp_DID == did){
706                         list_del(&rrq->list);
707                         spin_unlock_irqrestore(&phba->hbalock, iflags);
708                         return rrq;
709                 }
710         }
711         spin_unlock_irqrestore(&phba->hbalock, iflags);
712         return NULL;
713 }
714
715 /**
716  * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
717  * @vport: Pointer to vport context object.
718  * @ndlp: Pointer to the lpfc_node_list structure.
719  * If ndlp is NULL Remove all active RRQs for this vport from the
720  * phba->active_rrq_list and clear the rrq.
721  * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
722  **/
723 void
724 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
725
726 {
727         struct lpfc_hba *phba = vport->phba;
728         struct lpfc_node_rrq *rrq;
729         struct lpfc_node_rrq *nextrrq;
730         unsigned long iflags;
731         LIST_HEAD(rrq_list);
732
733         if (phba->sli_rev != LPFC_SLI_REV4)
734                 return;
735         if (!ndlp) {
736                 lpfc_sli4_vport_delete_els_xri_aborted(vport);
737                 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
738         }
739         spin_lock_irqsave(&phba->hbalock, iflags);
740         list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
741                 if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
742                         list_move(&rrq->list, &rrq_list);
743         spin_unlock_irqrestore(&phba->hbalock, iflags);
744
745         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
746                 list_del(&rrq->list);
747                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
748         }
749 }
750
751 /**
752  * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
753  * @phba: Pointer to HBA context object.
754  *
755  * Remove all rrqs from the phba->active_rrq_list and free them by
756  * calling __lpfc_clr_active_rrq
757  *
758  **/
759 void
760 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
761 {
762         struct lpfc_node_rrq *rrq;
763         struct lpfc_node_rrq *nextrrq;
764         unsigned long next_time;
765         unsigned long iflags;
766         LIST_HEAD(rrq_list);
767
768         if (phba->sli_rev != LPFC_SLI_REV4)
769                 return;
770         spin_lock_irqsave(&phba->hbalock, iflags);
771         phba->hba_flag &= ~HBA_RRQ_ACTIVE;
772         next_time = jiffies + HZ * (phba->fc_ratov * 2);
773         list_splice_init(&phba->active_rrq_list, &rrq_list);
774         spin_unlock_irqrestore(&phba->hbalock, iflags);
775
776         list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
777                 list_del(&rrq->list);
778                 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
779         }
780         if (!list_empty(&phba->active_rrq_list))
781                 mod_timer(&phba->rrq_tmr, next_time);
782 }
783
784
785 /**
786  * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
787  * @phba: Pointer to HBA context object.
788  * @ndlp: Targets nodelist pointer for this exchange.
789  * @xritag the xri in the bitmap to test.
790  *
791  * This function is called with hbalock held. This function
792  * returns 0 = rrq not active for this xri
793  *         1 = rrq is valid for this xri.
794  **/
795 int
796 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
797                         uint16_t  xritag)
798 {
799         uint16_t adj_xri;
800
801         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
802         if (!ndlp)
803                 return 0;
804         if (test_bit(adj_xri, ndlp->active_rrqs.xri_bitmap))
805                         return 1;
806         else
807                 return 0;
808 }
809
810 /**
811  * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
812  * @phba: Pointer to HBA context object.
813  * @ndlp: nodelist pointer for this target.
814  * @xritag: xri used in this exchange.
815  * @rxid: Remote Exchange ID.
816  * @send_rrq: Flag used to determine if we should send rrq els cmd.
817  *
818  * This function takes the hbalock.
819  * The active bit is always set in the active rrq xri_bitmap even
820  * if there is no slot avaiable for the other rrq information.
821  *
822  * returns 0 rrq actived for this xri
823  *         < 0 No memory or invalid ndlp.
824  **/
825 int
826 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
827                         uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
828 {
829         int ret;
830         unsigned long iflags;
831
832         spin_lock_irqsave(&phba->hbalock, iflags);
833         ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
834         spin_unlock_irqrestore(&phba->hbalock, iflags);
835         return ret;
836 }
837
838 /**
839  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
840  * @phba: Pointer to HBA context object.
841  * @piocb: Pointer to the iocbq.
842  *
843  * This function is called with hbalock held. This function
844  * Gets a new driver sglq object from the sglq list. If the
845  * list is not empty then it is successful, it returns pointer to the newly
846  * allocated sglq object else it returns NULL.
847  **/
848 static struct lpfc_sglq *
849 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
850 {
851         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
852         struct lpfc_sglq *sglq = NULL;
853         struct lpfc_sglq *start_sglq = NULL;
854         uint16_t adj_xri;
855         struct lpfc_scsi_buf *lpfc_cmd;
856         struct lpfc_nodelist *ndlp;
857         int found = 0;
858
859         if (piocbq->iocb_flag &  LPFC_IO_FCP) {
860                 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
861                 ndlp = lpfc_cmd->rdata->pnode;
862         } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
863                         !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
864                 ndlp = piocbq->context_un.ndlp;
865         else
866                 ndlp = piocbq->context1;
867
868         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
869         start_sglq = sglq;
870         while (!found) {
871                 if (!sglq)
872                         return NULL;
873                 adj_xri = sglq->sli4_xritag -
874                                 phba->sli4_hba.max_cfg_param.xri_base;
875                 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
876                         /* This xri has an rrq outstanding for this DID.
877                          * put it back in the list and get another xri.
878                          */
879                         list_add_tail(&sglq->list, lpfc_sgl_list);
880                         sglq = NULL;
881                         list_remove_head(lpfc_sgl_list, sglq,
882                                                 struct lpfc_sglq, list);
883                         if (sglq == start_sglq) {
884                                 sglq = NULL;
885                                 break;
886                         } else
887                                 continue;
888                 }
889                 sglq->ndlp = ndlp;
890                 found = 1;
891                 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
892                 sglq->state = SGL_ALLOCATED;
893         }
894         return sglq;
895 }
896
897 /**
898  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
899  * @phba: Pointer to HBA context object.
900  *
901  * This function is called with no lock held. This function
902  * allocates a new driver iocb object from the iocb pool. If the
903  * allocation is successful, it returns pointer to the newly
904  * allocated iocb object else it returns NULL.
905  **/
906 struct lpfc_iocbq *
907 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
908 {
909         struct lpfc_iocbq * iocbq = NULL;
910         unsigned long iflags;
911
912         spin_lock_irqsave(&phba->hbalock, iflags);
913         iocbq = __lpfc_sli_get_iocbq(phba);
914         spin_unlock_irqrestore(&phba->hbalock, iflags);
915         return iocbq;
916 }
917
918 /**
919  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
920  * @phba: Pointer to HBA context object.
921  * @iocbq: Pointer to driver iocb object.
922  *
923  * This function is called with hbalock held to release driver
924  * iocb object to the iocb pool. The iotag in the iocb object
925  * does not change for each use of the iocb object. This function
926  * clears all other fields of the iocb object when it is freed.
927  * The sqlq structure that holds the xritag and phys and virtual
928  * mappings for the scatter gather list is retrieved from the
929  * active array of sglq. The get of the sglq pointer also clears
930  * the entry in the array. If the status of the IO indiactes that
931  * this IO was aborted then the sglq entry it put on the
932  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
933  * IO has good status or fails for any other reason then the sglq
934  * entry is added to the free list (lpfc_sgl_list).
935  **/
936 static void
937 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
938 {
939         struct lpfc_sglq *sglq;
940         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
941         unsigned long iflag = 0;
942         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
943
944         if (iocbq->sli4_xritag == NO_XRI)
945                 sglq = NULL;
946         else
947                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
948         if (sglq)  {
949                 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
950                         (sglq->state != SGL_XRI_ABORTED)) {
951                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
952                                         iflag);
953                         list_add(&sglq->list,
954                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
955                         spin_unlock_irqrestore(
956                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
957                 } else {
958                         sglq->state = SGL_FREED;
959                         sglq->ndlp = NULL;
960                         list_add_tail(&sglq->list,
961                                 &phba->sli4_hba.lpfc_sgl_list);
962
963                         /* Check if TXQ queue needs to be serviced */
964                         if (pring->txq_cnt)
965                                 lpfc_worker_wake_up(phba);
966                 }
967         }
968
969
970         /*
971          * Clean all volatile data fields, preserve iotag and node struct.
972          */
973         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
974         iocbq->sli4_xritag = NO_XRI;
975         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
976 }
977
978
979 /**
980  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
981  * @phba: Pointer to HBA context object.
982  * @iocbq: Pointer to driver iocb object.
983  *
984  * This function is called with hbalock held to release driver
985  * iocb object to the iocb pool. The iotag in the iocb object
986  * does not change for each use of the iocb object. This function
987  * clears all other fields of the iocb object when it is freed.
988  **/
989 static void
990 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
991 {
992         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
993
994         /*
995          * Clean all volatile data fields, preserve iotag and node struct.
996          */
997         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
998         iocbq->sli4_xritag = NO_XRI;
999         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1000 }
1001
1002 /**
1003  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1004  * @phba: Pointer to HBA context object.
1005  * @iocbq: Pointer to driver iocb object.
1006  *
1007  * This function is called with hbalock held to release driver
1008  * iocb object to the iocb pool. The iotag in the iocb object
1009  * does not change for each use of the iocb object. This function
1010  * clears all other fields of the iocb object when it is freed.
1011  **/
1012 static void
1013 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1014 {
1015         phba->__lpfc_sli_release_iocbq(phba, iocbq);
1016         phba->iocb_cnt--;
1017 }
1018
1019 /**
1020  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1021  * @phba: Pointer to HBA context object.
1022  * @iocbq: Pointer to driver iocb object.
1023  *
1024  * This function is called with no lock held to release the iocb to
1025  * iocb pool.
1026  **/
1027 void
1028 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1029 {
1030         unsigned long iflags;
1031
1032         /*
1033          * Clean all volatile data fields, preserve iotag and node struct.
1034          */
1035         spin_lock_irqsave(&phba->hbalock, iflags);
1036         __lpfc_sli_release_iocbq(phba, iocbq);
1037         spin_unlock_irqrestore(&phba->hbalock, iflags);
1038 }
1039
1040 /**
1041  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1042  * @phba: Pointer to HBA context object.
1043  * @iocblist: List of IOCBs.
1044  * @ulpstatus: ULP status in IOCB command field.
1045  * @ulpWord4: ULP word-4 in IOCB command field.
1046  *
1047  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1048  * on the list by invoking the complete callback function associated with the
1049  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1050  * fields.
1051  **/
1052 void
1053 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1054                       uint32_t ulpstatus, uint32_t ulpWord4)
1055 {
1056         struct lpfc_iocbq *piocb;
1057
1058         while (!list_empty(iocblist)) {
1059                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1060
1061                 if (!piocb->iocb_cmpl)
1062                         lpfc_sli_release_iocbq(phba, piocb);
1063                 else {
1064                         piocb->iocb.ulpStatus = ulpstatus;
1065                         piocb->iocb.un.ulpWord[4] = ulpWord4;
1066                         (piocb->iocb_cmpl) (phba, piocb, piocb);
1067                 }
1068         }
1069         return;
1070 }
1071
1072 /**
1073  * lpfc_sli_iocb_cmd_type - Get the iocb type
1074  * @iocb_cmnd: iocb command code.
1075  *
1076  * This function is called by ring event handler function to get the iocb type.
1077  * This function translates the iocb command to an iocb command type used to
1078  * decide the final disposition of each completed IOCB.
1079  * The function returns
1080  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1081  * LPFC_SOL_IOCB     if it is a solicited iocb completion
1082  * LPFC_ABORT_IOCB   if it is an abort iocb
1083  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1084  *
1085  * The caller is not required to hold any lock.
1086  **/
1087 static lpfc_iocb_type
1088 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1089 {
1090         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1091
1092         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1093                 return 0;
1094
1095         switch (iocb_cmnd) {
1096         case CMD_XMIT_SEQUENCE_CR:
1097         case CMD_XMIT_SEQUENCE_CX:
1098         case CMD_XMIT_BCAST_CN:
1099         case CMD_XMIT_BCAST_CX:
1100         case CMD_ELS_REQUEST_CR:
1101         case CMD_ELS_REQUEST_CX:
1102         case CMD_CREATE_XRI_CR:
1103         case CMD_CREATE_XRI_CX:
1104         case CMD_GET_RPI_CN:
1105         case CMD_XMIT_ELS_RSP_CX:
1106         case CMD_GET_RPI_CR:
1107         case CMD_FCP_IWRITE_CR:
1108         case CMD_FCP_IWRITE_CX:
1109         case CMD_FCP_IREAD_CR:
1110         case CMD_FCP_IREAD_CX:
1111         case CMD_FCP_ICMND_CR:
1112         case CMD_FCP_ICMND_CX:
1113         case CMD_FCP_TSEND_CX:
1114         case CMD_FCP_TRSP_CX:
1115         case CMD_FCP_TRECEIVE_CX:
1116         case CMD_FCP_AUTO_TRSP_CX:
1117         case CMD_ADAPTER_MSG:
1118         case CMD_ADAPTER_DUMP:
1119         case CMD_XMIT_SEQUENCE64_CR:
1120         case CMD_XMIT_SEQUENCE64_CX:
1121         case CMD_XMIT_BCAST64_CN:
1122         case CMD_XMIT_BCAST64_CX:
1123         case CMD_ELS_REQUEST64_CR:
1124         case CMD_ELS_REQUEST64_CX:
1125         case CMD_FCP_IWRITE64_CR:
1126         case CMD_FCP_IWRITE64_CX:
1127         case CMD_FCP_IREAD64_CR:
1128         case CMD_FCP_IREAD64_CX:
1129         case CMD_FCP_ICMND64_CR:
1130         case CMD_FCP_ICMND64_CX:
1131         case CMD_FCP_TSEND64_CX:
1132         case CMD_FCP_TRSP64_CX:
1133         case CMD_FCP_TRECEIVE64_CX:
1134         case CMD_GEN_REQUEST64_CR:
1135         case CMD_GEN_REQUEST64_CX:
1136         case CMD_XMIT_ELS_RSP64_CX:
1137         case DSSCMD_IWRITE64_CR:
1138         case DSSCMD_IWRITE64_CX:
1139         case DSSCMD_IREAD64_CR:
1140         case DSSCMD_IREAD64_CX:
1141                 type = LPFC_SOL_IOCB;
1142                 break;
1143         case CMD_ABORT_XRI_CN:
1144         case CMD_ABORT_XRI_CX:
1145         case CMD_CLOSE_XRI_CN:
1146         case CMD_CLOSE_XRI_CX:
1147         case CMD_XRI_ABORTED_CX:
1148         case CMD_ABORT_MXRI64_CN:
1149         case CMD_XMIT_BLS_RSP64_CX:
1150                 type = LPFC_ABORT_IOCB;
1151                 break;
1152         case CMD_RCV_SEQUENCE_CX:
1153         case CMD_RCV_ELS_REQ_CX:
1154         case CMD_RCV_SEQUENCE64_CX:
1155         case CMD_RCV_ELS_REQ64_CX:
1156         case CMD_ASYNC_STATUS:
1157         case CMD_IOCB_RCV_SEQ64_CX:
1158         case CMD_IOCB_RCV_ELS64_CX:
1159         case CMD_IOCB_RCV_CONT64_CX:
1160         case CMD_IOCB_RET_XRI64_CX:
1161                 type = LPFC_UNSOL_IOCB;
1162                 break;
1163         case CMD_IOCB_XMIT_MSEQ64_CR:
1164         case CMD_IOCB_XMIT_MSEQ64_CX:
1165         case CMD_IOCB_RCV_SEQ_LIST64_CX:
1166         case CMD_IOCB_RCV_ELS_LIST64_CX:
1167         case CMD_IOCB_CLOSE_EXTENDED_CN:
1168         case CMD_IOCB_ABORT_EXTENDED_CN:
1169         case CMD_IOCB_RET_HBQE64_CN:
1170         case CMD_IOCB_FCP_IBIDIR64_CR:
1171         case CMD_IOCB_FCP_IBIDIR64_CX:
1172         case CMD_IOCB_FCP_ITASKMGT64_CX:
1173         case CMD_IOCB_LOGENTRY_CN:
1174         case CMD_IOCB_LOGENTRY_ASYNC_CN:
1175                 printk("%s - Unhandled SLI-3 Command x%x\n",
1176                                 __func__, iocb_cmnd);
1177                 type = LPFC_UNKNOWN_IOCB;
1178                 break;
1179         default:
1180                 type = LPFC_UNKNOWN_IOCB;
1181                 break;
1182         }
1183
1184         return type;
1185 }
1186
1187 /**
1188  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1189  * @phba: Pointer to HBA context object.
1190  *
1191  * This function is called from SLI initialization code
1192  * to configure every ring of the HBA's SLI interface. The
1193  * caller is not required to hold any lock. This function issues
1194  * a config_ring mailbox command for each ring.
1195  * This function returns zero if successful else returns a negative
1196  * error code.
1197  **/
1198 static int
1199 lpfc_sli_ring_map(struct lpfc_hba *phba)
1200 {
1201         struct lpfc_sli *psli = &phba->sli;
1202         LPFC_MBOXQ_t *pmb;
1203         MAILBOX_t *pmbox;
1204         int i, rc, ret = 0;
1205
1206         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1207         if (!pmb)
1208                 return -ENOMEM;
1209         pmbox = &pmb->u.mb;
1210         phba->link_state = LPFC_INIT_MBX_CMDS;
1211         for (i = 0; i < psli->num_rings; i++) {
1212                 lpfc_config_ring(phba, i, pmb);
1213                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1214                 if (rc != MBX_SUCCESS) {
1215                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1216                                         "0446 Adapter failed to init (%d), "
1217                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1218                                         "ring %d\n",
1219                                         rc, pmbox->mbxCommand,
1220                                         pmbox->mbxStatus, i);
1221                         phba->link_state = LPFC_HBA_ERROR;
1222                         ret = -ENXIO;
1223                         break;
1224                 }
1225         }
1226         mempool_free(pmb, phba->mbox_mem_pool);
1227         return ret;
1228 }
1229
1230 /**
1231  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1232  * @phba: Pointer to HBA context object.
1233  * @pring: Pointer to driver SLI ring object.
1234  * @piocb: Pointer to the driver iocb object.
1235  *
1236  * This function is called with hbalock held. The function adds the
1237  * new iocb to txcmplq of the given ring. This function always returns
1238  * 0. If this function is called for ELS ring, this function checks if
1239  * there is a vport associated with the ELS command. This function also
1240  * starts els_tmofunc timer if this is an ELS command.
1241  **/
1242 static int
1243 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1244                         struct lpfc_iocbq *piocb)
1245 {
1246         list_add_tail(&piocb->list, &pring->txcmplq);
1247         piocb->iocb_flag |= LPFC_IO_ON_Q;
1248         pring->txcmplq_cnt++;
1249         if (pring->txcmplq_cnt > pring->txcmplq_max)
1250                 pring->txcmplq_max = pring->txcmplq_cnt;
1251
1252         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1253            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1254            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1255                 if (!piocb->vport)
1256                         BUG();
1257                 else
1258                         mod_timer(&piocb->vport->els_tmofunc,
1259                                   jiffies + HZ * (phba->fc_ratov << 1));
1260         }
1261
1262
1263         return 0;
1264 }
1265
1266 /**
1267  * lpfc_sli_ringtx_get - Get first element of the txq
1268  * @phba: Pointer to HBA context object.
1269  * @pring: Pointer to driver SLI ring object.
1270  *
1271  * This function is called with hbalock held to get next
1272  * iocb in txq of the given ring. If there is any iocb in
1273  * the txq, the function returns first iocb in the list after
1274  * removing the iocb from the list, else it returns NULL.
1275  **/
1276 struct lpfc_iocbq *
1277 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1278 {
1279         struct lpfc_iocbq *cmd_iocb;
1280
1281         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1282         if (cmd_iocb != NULL)
1283                 pring->txq_cnt--;
1284         return cmd_iocb;
1285 }
1286
1287 /**
1288  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1289  * @phba: Pointer to HBA context object.
1290  * @pring: Pointer to driver SLI ring object.
1291  *
1292  * This function is called with hbalock held and the caller must post the
1293  * iocb without releasing the lock. If the caller releases the lock,
1294  * iocb slot returned by the function is not guaranteed to be available.
1295  * The function returns pointer to the next available iocb slot if there
1296  * is available slot in the ring, else it returns NULL.
1297  * If the get index of the ring is ahead of the put index, the function
1298  * will post an error attention event to the worker thread to take the
1299  * HBA to offline state.
1300  **/
1301 static IOCB_t *
1302 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1303 {
1304         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1305         uint32_t  max_cmd_idx = pring->numCiocb;
1306         if ((pring->next_cmdidx == pring->cmdidx) &&
1307            (++pring->next_cmdidx >= max_cmd_idx))
1308                 pring->next_cmdidx = 0;
1309
1310         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1311
1312                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1313
1314                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1315                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1316                                         "0315 Ring %d issue: portCmdGet %d "
1317                                         "is bigger than cmd ring %d\n",
1318                                         pring->ringno,
1319                                         pring->local_getidx, max_cmd_idx);
1320
1321                         phba->link_state = LPFC_HBA_ERROR;
1322                         /*
1323                          * All error attention handlers are posted to
1324                          * worker thread
1325                          */
1326                         phba->work_ha |= HA_ERATT;
1327                         phba->work_hs = HS_FFER3;
1328
1329                         lpfc_worker_wake_up(phba);
1330
1331                         return NULL;
1332                 }
1333
1334                 if (pring->local_getidx == pring->next_cmdidx)
1335                         return NULL;
1336         }
1337
1338         return lpfc_cmd_iocb(phba, pring);
1339 }
1340
1341 /**
1342  * lpfc_sli_next_iotag - Get an iotag for the iocb
1343  * @phba: Pointer to HBA context object.
1344  * @iocbq: Pointer to driver iocb object.
1345  *
1346  * This function gets an iotag for the iocb. If there is no unused iotag and
1347  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1348  * array and assigns a new iotag.
1349  * The function returns the allocated iotag if successful, else returns zero.
1350  * Zero is not a valid iotag.
1351  * The caller is not required to hold any lock.
1352  **/
1353 uint16_t
1354 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1355 {
1356         struct lpfc_iocbq **new_arr;
1357         struct lpfc_iocbq **old_arr;
1358         size_t new_len;
1359         struct lpfc_sli *psli = &phba->sli;
1360         uint16_t iotag;
1361
1362         spin_lock_irq(&phba->hbalock);
1363         iotag = psli->last_iotag;
1364         if(++iotag < psli->iocbq_lookup_len) {
1365                 psli->last_iotag = iotag;
1366                 psli->iocbq_lookup[iotag] = iocbq;
1367                 spin_unlock_irq(&phba->hbalock);
1368                 iocbq->iotag = iotag;
1369                 return iotag;
1370         } else if (psli->iocbq_lookup_len < (0xffff
1371                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1372                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1373                 spin_unlock_irq(&phba->hbalock);
1374                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1375                                   GFP_KERNEL);
1376                 if (new_arr) {
1377                         spin_lock_irq(&phba->hbalock);
1378                         old_arr = psli->iocbq_lookup;
1379                         if (new_len <= psli->iocbq_lookup_len) {
1380                                 /* highly unprobable case */
1381                                 kfree(new_arr);
1382                                 iotag = psli->last_iotag;
1383                                 if(++iotag < psli->iocbq_lookup_len) {
1384                                         psli->last_iotag = iotag;
1385                                         psli->iocbq_lookup[iotag] = iocbq;
1386                                         spin_unlock_irq(&phba->hbalock);
1387                                         iocbq->iotag = iotag;
1388                                         return iotag;
1389                                 }
1390                                 spin_unlock_irq(&phba->hbalock);
1391                                 return 0;
1392                         }
1393                         if (psli->iocbq_lookup)
1394                                 memcpy(new_arr, old_arr,
1395                                        ((psli->last_iotag  + 1) *
1396                                         sizeof (struct lpfc_iocbq *)));
1397                         psli->iocbq_lookup = new_arr;
1398                         psli->iocbq_lookup_len = new_len;
1399                         psli->last_iotag = iotag;
1400                         psli->iocbq_lookup[iotag] = iocbq;
1401                         spin_unlock_irq(&phba->hbalock);
1402                         iocbq->iotag = iotag;
1403                         kfree(old_arr);
1404                         return iotag;
1405                 }
1406         } else
1407                 spin_unlock_irq(&phba->hbalock);
1408
1409         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1410                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1411                         psli->last_iotag);
1412
1413         return 0;
1414 }
1415
1416 /**
1417  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1418  * @phba: Pointer to HBA context object.
1419  * @pring: Pointer to driver SLI ring object.
1420  * @iocb: Pointer to iocb slot in the ring.
1421  * @nextiocb: Pointer to driver iocb object which need to be
1422  *            posted to firmware.
1423  *
1424  * This function is called with hbalock held to post a new iocb to
1425  * the firmware. This function copies the new iocb to ring iocb slot and
1426  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1427  * a completion call back for this iocb else the function will free the
1428  * iocb object.
1429  **/
1430 static void
1431 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1432                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1433 {
1434         /*
1435          * Set up an iotag
1436          */
1437         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1438
1439
1440         if (pring->ringno == LPFC_ELS_RING) {
1441                 lpfc_debugfs_slow_ring_trc(phba,
1442                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1443                         *(((uint32_t *) &nextiocb->iocb) + 4),
1444                         *(((uint32_t *) &nextiocb->iocb) + 6),
1445                         *(((uint32_t *) &nextiocb->iocb) + 7));
1446         }
1447
1448         /*
1449          * Issue iocb command to adapter
1450          */
1451         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1452         wmb();
1453         pring->stats.iocb_cmd++;
1454
1455         /*
1456          * If there is no completion routine to call, we can release the
1457          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1458          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1459          */
1460         if (nextiocb->iocb_cmpl)
1461                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1462         else
1463                 __lpfc_sli_release_iocbq(phba, nextiocb);
1464
1465         /*
1466          * Let the HBA know what IOCB slot will be the next one the
1467          * driver will put a command into.
1468          */
1469         pring->cmdidx = pring->next_cmdidx;
1470         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1471 }
1472
1473 /**
1474  * lpfc_sli_update_full_ring - Update the chip attention register
1475  * @phba: Pointer to HBA context object.
1476  * @pring: Pointer to driver SLI ring object.
1477  *
1478  * The caller is not required to hold any lock for calling this function.
1479  * This function updates the chip attention bits for the ring to inform firmware
1480  * that there are pending work to be done for this ring and requests an
1481  * interrupt when there is space available in the ring. This function is
1482  * called when the driver is unable to post more iocbs to the ring due
1483  * to unavailability of space in the ring.
1484  **/
1485 static void
1486 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1487 {
1488         int ringno = pring->ringno;
1489
1490         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1491
1492         wmb();
1493
1494         /*
1495          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1496          * The HBA will tell us when an IOCB entry is available.
1497          */
1498         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1499         readl(phba->CAregaddr); /* flush */
1500
1501         pring->stats.iocb_cmd_full++;
1502 }
1503
1504 /**
1505  * lpfc_sli_update_ring - Update chip attention register
1506  * @phba: Pointer to HBA context object.
1507  * @pring: Pointer to driver SLI ring object.
1508  *
1509  * This function updates the chip attention register bit for the
1510  * given ring to inform HBA that there is more work to be done
1511  * in this ring. The caller is not required to hold any lock.
1512  **/
1513 static void
1514 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1515 {
1516         int ringno = pring->ringno;
1517
1518         /*
1519          * Tell the HBA that there is work to do in this ring.
1520          */
1521         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1522                 wmb();
1523                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1524                 readl(phba->CAregaddr); /* flush */
1525         }
1526 }
1527
1528 /**
1529  * lpfc_sli_resume_iocb - Process iocbs in the txq
1530  * @phba: Pointer to HBA context object.
1531  * @pring: Pointer to driver SLI ring object.
1532  *
1533  * This function is called with hbalock held to post pending iocbs
1534  * in the txq to the firmware. This function is called when driver
1535  * detects space available in the ring.
1536  **/
1537 static void
1538 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1539 {
1540         IOCB_t *iocb;
1541         struct lpfc_iocbq *nextiocb;
1542
1543         /*
1544          * Check to see if:
1545          *  (a) there is anything on the txq to send
1546          *  (b) link is up
1547          *  (c) link attention events can be processed (fcp ring only)
1548          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1549          */
1550         if (pring->txq_cnt &&
1551             lpfc_is_link_up(phba) &&
1552             (pring->ringno != phba->sli.fcp_ring ||
1553              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1554
1555                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1556                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1557                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1558
1559                 if (iocb)
1560                         lpfc_sli_update_ring(phba, pring);
1561                 else
1562                         lpfc_sli_update_full_ring(phba, pring);
1563         }
1564
1565         return;
1566 }
1567
1568 /**
1569  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1570  * @phba: Pointer to HBA context object.
1571  * @hbqno: HBQ number.
1572  *
1573  * This function is called with hbalock held to get the next
1574  * available slot for the given HBQ. If there is free slot
1575  * available for the HBQ it will return pointer to the next available
1576  * HBQ entry else it will return NULL.
1577  **/
1578 static struct lpfc_hbq_entry *
1579 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1580 {
1581         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1582
1583         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1584             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1585                 hbqp->next_hbqPutIdx = 0;
1586
1587         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1588                 uint32_t raw_index = phba->hbq_get[hbqno];
1589                 uint32_t getidx = le32_to_cpu(raw_index);
1590
1591                 hbqp->local_hbqGetIdx = getidx;
1592
1593                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1594                         lpfc_printf_log(phba, KERN_ERR,
1595                                         LOG_SLI | LOG_VPORT,
1596                                         "1802 HBQ %d: local_hbqGetIdx "
1597                                         "%u is > than hbqp->entry_count %u\n",
1598                                         hbqno, hbqp->local_hbqGetIdx,
1599                                         hbqp->entry_count);
1600
1601                         phba->link_state = LPFC_HBA_ERROR;
1602                         return NULL;
1603                 }
1604
1605                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1606                         return NULL;
1607         }
1608
1609         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1610                         hbqp->hbqPutIdx;
1611 }
1612
1613 /**
1614  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1615  * @phba: Pointer to HBA context object.
1616  *
1617  * This function is called with no lock held to free all the
1618  * hbq buffers while uninitializing the SLI interface. It also
1619  * frees the HBQ buffers returned by the firmware but not yet
1620  * processed by the upper layers.
1621  **/
1622 void
1623 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1624 {
1625         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1626         struct hbq_dmabuf *hbq_buf;
1627         unsigned long flags;
1628         int i, hbq_count;
1629         uint32_t hbqno;
1630
1631         hbq_count = lpfc_sli_hbq_count();
1632         /* Return all memory used by all HBQs */
1633         spin_lock_irqsave(&phba->hbalock, flags);
1634         for (i = 0; i < hbq_count; ++i) {
1635                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1636                                 &phba->hbqs[i].hbq_buffer_list, list) {
1637                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1638                         list_del(&hbq_buf->dbuf.list);
1639                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1640                 }
1641                 phba->hbqs[i].buffer_count = 0;
1642         }
1643         /* Return all HBQ buffer that are in-fly */
1644         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1645                                  list) {
1646                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1647                 list_del(&hbq_buf->dbuf.list);
1648                 if (hbq_buf->tag == -1) {
1649                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1650                                 (phba, hbq_buf);
1651                 } else {
1652                         hbqno = hbq_buf->tag >> 16;
1653                         if (hbqno >= LPFC_MAX_HBQS)
1654                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1655                                         (phba, hbq_buf);
1656                         else
1657                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1658                                         hbq_buf);
1659                 }
1660         }
1661
1662         /* Mark the HBQs not in use */
1663         phba->hbq_in_use = 0;
1664         spin_unlock_irqrestore(&phba->hbalock, flags);
1665 }
1666
1667 /**
1668  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1669  * @phba: Pointer to HBA context object.
1670  * @hbqno: HBQ number.
1671  * @hbq_buf: Pointer to HBQ buffer.
1672  *
1673  * This function is called with the hbalock held to post a
1674  * hbq buffer to the firmware. If the function finds an empty
1675  * slot in the HBQ, it will post the buffer. The function will return
1676  * pointer to the hbq entry if it successfully post the buffer
1677  * else it will return NULL.
1678  **/
1679 static int
1680 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1681                          struct hbq_dmabuf *hbq_buf)
1682 {
1683         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1684 }
1685
1686 /**
1687  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1688  * @phba: Pointer to HBA context object.
1689  * @hbqno: HBQ number.
1690  * @hbq_buf: Pointer to HBQ buffer.
1691  *
1692  * This function is called with the hbalock held to post a hbq buffer to the
1693  * firmware. If the function finds an empty slot in the HBQ, it will post the
1694  * buffer and place it on the hbq_buffer_list. The function will return zero if
1695  * it successfully post the buffer else it will return an error.
1696  **/
1697 static int
1698 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1699                             struct hbq_dmabuf *hbq_buf)
1700 {
1701         struct lpfc_hbq_entry *hbqe;
1702         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1703
1704         /* Get next HBQ entry slot to use */
1705         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1706         if (hbqe) {
1707                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1708
1709                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1710                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1711                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1712                 hbqe->bde.tus.f.bdeFlags = 0;
1713                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1714                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1715                                 /* Sync SLIM */
1716                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1717                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1718                                 /* flush */
1719                 readl(phba->hbq_put + hbqno);
1720                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1721                 return 0;
1722         } else
1723                 return -ENOMEM;
1724 }
1725
1726 /**
1727  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1728  * @phba: Pointer to HBA context object.
1729  * @hbqno: HBQ number.
1730  * @hbq_buf: Pointer to HBQ buffer.
1731  *
1732  * This function is called with the hbalock held to post an RQE to the SLI4
1733  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1734  * the hbq_buffer_list and return zero, otherwise it will return an error.
1735  **/
1736 static int
1737 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1738                             struct hbq_dmabuf *hbq_buf)
1739 {
1740         int rc;
1741         struct lpfc_rqe hrqe;
1742         struct lpfc_rqe drqe;
1743
1744         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1745         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1746         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1747         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1748         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1749                               &hrqe, &drqe);
1750         if (rc < 0)
1751                 return rc;
1752         hbq_buf->tag = rc;
1753         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1754         return 0;
1755 }
1756
1757 /* HBQ for ELS and CT traffic. */
1758 static struct lpfc_hbq_init lpfc_els_hbq = {
1759         .rn = 1,
1760         .entry_count = 256,
1761         .mask_count = 0,
1762         .profile = 0,
1763         .ring_mask = (1 << LPFC_ELS_RING),
1764         .buffer_count = 0,
1765         .init_count = 40,
1766         .add_count = 40,
1767 };
1768
1769 /* HBQ for the extra ring if needed */
1770 static struct lpfc_hbq_init lpfc_extra_hbq = {
1771         .rn = 1,
1772         .entry_count = 200,
1773         .mask_count = 0,
1774         .profile = 0,
1775         .ring_mask = (1 << LPFC_EXTRA_RING),
1776         .buffer_count = 0,
1777         .init_count = 0,
1778         .add_count = 5,
1779 };
1780
1781 /* Array of HBQs */
1782 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1783         &lpfc_els_hbq,
1784         &lpfc_extra_hbq,
1785 };
1786
1787 /**
1788  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1789  * @phba: Pointer to HBA context object.
1790  * @hbqno: HBQ number.
1791  * @count: Number of HBQ buffers to be posted.
1792  *
1793  * This function is called with no lock held to post more hbq buffers to the
1794  * given HBQ. The function returns the number of HBQ buffers successfully
1795  * posted.
1796  **/
1797 static int
1798 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1799 {
1800         uint32_t i, posted = 0;
1801         unsigned long flags;
1802         struct hbq_dmabuf *hbq_buffer;
1803         LIST_HEAD(hbq_buf_list);
1804         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1805                 return 0;
1806
1807         if ((phba->hbqs[hbqno].buffer_count + count) >
1808             lpfc_hbq_defs[hbqno]->entry_count)
1809                 count = lpfc_hbq_defs[hbqno]->entry_count -
1810                                         phba->hbqs[hbqno].buffer_count;
1811         if (!count)
1812                 return 0;
1813         /* Allocate HBQ entries */
1814         for (i = 0; i < count; i++) {
1815                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1816                 if (!hbq_buffer)
1817                         break;
1818                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1819         }
1820         /* Check whether HBQ is still in use */
1821         spin_lock_irqsave(&phba->hbalock, flags);
1822         if (!phba->hbq_in_use)
1823                 goto err;
1824         while (!list_empty(&hbq_buf_list)) {
1825                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1826                                  dbuf.list);
1827                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1828                                       (hbqno << 16));
1829                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1830                         phba->hbqs[hbqno].buffer_count++;
1831                         posted++;
1832                 } else
1833                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1834         }
1835         spin_unlock_irqrestore(&phba->hbalock, flags);
1836         return posted;
1837 err:
1838         spin_unlock_irqrestore(&phba->hbalock, flags);
1839         while (!list_empty(&hbq_buf_list)) {
1840                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1841                                  dbuf.list);
1842                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1843         }
1844         return 0;
1845 }
1846
1847 /**
1848  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1849  * @phba: Pointer to HBA context object.
1850  * @qno: HBQ number.
1851  *
1852  * This function posts more buffers to the HBQ. This function
1853  * is called with no lock held. The function returns the number of HBQ entries
1854  * successfully allocated.
1855  **/
1856 int
1857 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1858 {
1859         if (phba->sli_rev == LPFC_SLI_REV4)
1860                 return 0;
1861         else
1862                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1863                                          lpfc_hbq_defs[qno]->add_count);
1864 }
1865
1866 /**
1867  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1868  * @phba: Pointer to HBA context object.
1869  * @qno:  HBQ queue number.
1870  *
1871  * This function is called from SLI initialization code path with
1872  * no lock held to post initial HBQ buffers to firmware. The
1873  * function returns the number of HBQ entries successfully allocated.
1874  **/
1875 static int
1876 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1877 {
1878         if (phba->sli_rev == LPFC_SLI_REV4)
1879                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1880                                          lpfc_hbq_defs[qno]->entry_count);
1881         else
1882                 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1883                                          lpfc_hbq_defs[qno]->init_count);
1884 }
1885
1886 /**
1887  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1888  * @phba: Pointer to HBA context object.
1889  * @hbqno: HBQ number.
1890  *
1891  * This function removes the first hbq buffer on an hbq list and returns a
1892  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1893  **/
1894 static struct hbq_dmabuf *
1895 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1896 {
1897         struct lpfc_dmabuf *d_buf;
1898
1899         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1900         if (!d_buf)
1901                 return NULL;
1902         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1903 }
1904
1905 /**
1906  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1907  * @phba: Pointer to HBA context object.
1908  * @tag: Tag of the hbq buffer.
1909  *
1910  * This function is called with hbalock held. This function searches
1911  * for the hbq buffer associated with the given tag in the hbq buffer
1912  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1913  * it returns NULL.
1914  **/
1915 static struct hbq_dmabuf *
1916 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1917 {
1918         struct lpfc_dmabuf *d_buf;
1919         struct hbq_dmabuf *hbq_buf;
1920         uint32_t hbqno;
1921
1922         hbqno = tag >> 16;
1923         if (hbqno >= LPFC_MAX_HBQS)
1924                 return NULL;
1925
1926         spin_lock_irq(&phba->hbalock);
1927         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1928                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1929                 if (hbq_buf->tag == tag) {
1930                         spin_unlock_irq(&phba->hbalock);
1931                         return hbq_buf;
1932                 }
1933         }
1934         spin_unlock_irq(&phba->hbalock);
1935         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1936                         "1803 Bad hbq tag. Data: x%x x%x\n",
1937                         tag, phba->hbqs[tag >> 16].buffer_count);
1938         return NULL;
1939 }
1940
1941 /**
1942  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1943  * @phba: Pointer to HBA context object.
1944  * @hbq_buffer: Pointer to HBQ buffer.
1945  *
1946  * This function is called with hbalock. This function gives back
1947  * the hbq buffer to firmware. If the HBQ does not have space to
1948  * post the buffer, it will free the buffer.
1949  **/
1950 void
1951 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1952 {
1953         uint32_t hbqno;
1954
1955         if (hbq_buffer) {
1956                 hbqno = hbq_buffer->tag >> 16;
1957                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1958                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1959         }
1960 }
1961
1962 /**
1963  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1964  * @mbxCommand: mailbox command code.
1965  *
1966  * This function is called by the mailbox event handler function to verify
1967  * that the completed mailbox command is a legitimate mailbox command. If the
1968  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1969  * and the mailbox event handler will take the HBA offline.
1970  **/
1971 static int
1972 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1973 {
1974         uint8_t ret;
1975
1976         switch (mbxCommand) {
1977         case MBX_LOAD_SM:
1978         case MBX_READ_NV:
1979         case MBX_WRITE_NV:
1980         case MBX_WRITE_VPARMS:
1981         case MBX_RUN_BIU_DIAG:
1982         case MBX_INIT_LINK:
1983         case MBX_DOWN_LINK:
1984         case MBX_CONFIG_LINK:
1985         case MBX_CONFIG_RING:
1986         case MBX_RESET_RING:
1987         case MBX_READ_CONFIG:
1988         case MBX_READ_RCONFIG:
1989         case MBX_READ_SPARM:
1990         case MBX_READ_STATUS:
1991         case MBX_READ_RPI:
1992         case MBX_READ_XRI:
1993         case MBX_READ_REV:
1994         case MBX_READ_LNK_STAT:
1995         case MBX_REG_LOGIN:
1996         case MBX_UNREG_LOGIN:
1997         case MBX_CLEAR_LA:
1998         case MBX_DUMP_MEMORY:
1999         case MBX_DUMP_CONTEXT:
2000         case MBX_RUN_DIAGS:
2001         case MBX_RESTART:
2002         case MBX_UPDATE_CFG:
2003         case MBX_DOWN_LOAD:
2004         case MBX_DEL_LD_ENTRY:
2005         case MBX_RUN_PROGRAM:
2006         case MBX_SET_MASK:
2007         case MBX_SET_VARIABLE:
2008         case MBX_UNREG_D_ID:
2009         case MBX_KILL_BOARD:
2010         case MBX_CONFIG_FARP:
2011         case MBX_BEACON:
2012         case MBX_LOAD_AREA:
2013         case MBX_RUN_BIU_DIAG64:
2014         case MBX_CONFIG_PORT:
2015         case MBX_READ_SPARM64:
2016         case MBX_READ_RPI64:
2017         case MBX_REG_LOGIN64:
2018         case MBX_READ_TOPOLOGY:
2019         case MBX_WRITE_WWN:
2020         case MBX_SET_DEBUG:
2021         case MBX_LOAD_EXP_ROM:
2022         case MBX_ASYNCEVT_ENABLE:
2023         case MBX_REG_VPI:
2024         case MBX_UNREG_VPI:
2025         case MBX_HEARTBEAT:
2026         case MBX_PORT_CAPABILITIES:
2027         case MBX_PORT_IOV_CONTROL:
2028         case MBX_SLI4_CONFIG:
2029         case MBX_SLI4_REQ_FTRS:
2030         case MBX_REG_FCFI:
2031         case MBX_UNREG_FCFI:
2032         case MBX_REG_VFI:
2033         case MBX_UNREG_VFI:
2034         case MBX_INIT_VPI:
2035         case MBX_INIT_VFI:
2036         case MBX_RESUME_RPI:
2037         case MBX_READ_EVENT_LOG_STATUS:
2038         case MBX_READ_EVENT_LOG:
2039         case MBX_SECURITY_MGMT:
2040         case MBX_AUTH_PORT:
2041                 ret = mbxCommand;
2042                 break;
2043         default:
2044                 ret = MBX_SHUTDOWN;
2045                 break;
2046         }
2047         return ret;
2048 }
2049
2050 /**
2051  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2052  * @phba: Pointer to HBA context object.
2053  * @pmboxq: Pointer to mailbox command.
2054  *
2055  * This is completion handler function for mailbox commands issued from
2056  * lpfc_sli_issue_mbox_wait function. This function is called by the
2057  * mailbox event handler function with no lock held. This function
2058  * will wake up thread waiting on the wait queue pointed by context1
2059  * of the mailbox.
2060  **/
2061 void
2062 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2063 {
2064         wait_queue_head_t *pdone_q;
2065         unsigned long drvr_flag;
2066
2067         /*
2068          * If pdone_q is empty, the driver thread gave up waiting and
2069          * continued running.
2070          */
2071         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2072         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2073         pdone_q = (wait_queue_head_t *) pmboxq->context1;
2074         if (pdone_q)
2075                 wake_up_interruptible(pdone_q);
2076         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2077         return;
2078 }
2079
2080
2081 /**
2082  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2083  * @phba: Pointer to HBA context object.
2084  * @pmb: Pointer to mailbox object.
2085  *
2086  * This function is the default mailbox completion handler. It
2087  * frees the memory resources associated with the completed mailbox
2088  * command. If the completed command is a REG_LOGIN mailbox command,
2089  * this function will issue a UREG_LOGIN to re-claim the RPI.
2090  **/
2091 void
2092 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2093 {
2094         struct lpfc_vport  *vport = pmb->vport;
2095         struct lpfc_dmabuf *mp;
2096         struct lpfc_nodelist *ndlp;
2097         struct Scsi_Host *shost;
2098         uint16_t rpi, vpi;
2099         int rc;
2100
2101         mp = (struct lpfc_dmabuf *) (pmb->context1);
2102
2103         if (mp) {
2104                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2105                 kfree(mp);
2106         }
2107
2108         /*
2109          * If a REG_LOGIN succeeded  after node is destroyed or node
2110          * is in re-discovery driver need to cleanup the RPI.
2111          */
2112         if (!(phba->pport->load_flag & FC_UNLOADING) &&
2113             pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2114             !pmb->u.mb.mbxStatus) {
2115                 rpi = pmb->u.mb.un.varWords[0];
2116                 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
2117                 lpfc_unreg_login(phba, vpi, rpi, pmb);
2118                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2119                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2120                 if (rc != MBX_NOT_FINISHED)
2121                         return;
2122         }
2123
2124         if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2125                 !(phba->pport->load_flag & FC_UNLOADING) &&
2126                 !pmb->u.mb.mbxStatus) {
2127                 shost = lpfc_shost_from_vport(vport);
2128                 spin_lock_irq(shost->host_lock);
2129                 vport->vpi_state |= LPFC_VPI_REGISTERED;
2130                 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2131                 spin_unlock_irq(shost->host_lock);
2132         }
2133
2134         if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2135                 ndlp = (struct lpfc_nodelist *)pmb->context2;
2136                 lpfc_nlp_put(ndlp);
2137                 pmb->context2 = NULL;
2138         }
2139
2140         /* Check security permission status on INIT_LINK mailbox command */
2141         if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2142             (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2143                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2144                                 "2860 SLI authentication is required "
2145                                 "for INIT_LINK but has not done yet\n");
2146
2147         if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2148                 lpfc_sli4_mbox_cmd_free(phba, pmb);
2149         else
2150                 mempool_free(pmb, phba->mbox_mem_pool);
2151 }
2152
2153 /**
2154  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2155  * @phba: Pointer to HBA context object.
2156  *
2157  * This function is called with no lock held. This function processes all
2158  * the completed mailbox commands and gives it to upper layers. The interrupt
2159  * service routine processes mailbox completion interrupt and adds completed
2160  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2161  * Worker thread call lpfc_sli_handle_mb_event, which will return the
2162  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2163  * function returns the mailbox commands to the upper layer by calling the
2164  * completion handler function of each mailbox.
2165  **/
2166 int
2167 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2168 {
2169         MAILBOX_t *pmbox;
2170         LPFC_MBOXQ_t *pmb;
2171         int rc;
2172         LIST_HEAD(cmplq);
2173
2174         phba->sli.slistat.mbox_event++;
2175
2176         /* Get all completed mailboxe buffers into the cmplq */
2177         spin_lock_irq(&phba->hbalock);
2178         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2179         spin_unlock_irq(&phba->hbalock);
2180
2181         /* Get a Mailbox buffer to setup mailbox commands for callback */
2182         do {
2183                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2184                 if (pmb == NULL)
2185                         break;
2186
2187                 pmbox = &pmb->u.mb;
2188
2189                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2190                         if (pmb->vport) {
2191                                 lpfc_debugfs_disc_trc(pmb->vport,
2192                                         LPFC_DISC_TRC_MBOX_VPORT,
2193                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2194                                         (uint32_t)pmbox->mbxCommand,
2195                                         pmbox->un.varWords[0],
2196                                         pmbox->un.varWords[1]);
2197                         }
2198                         else {
2199                                 lpfc_debugfs_disc_trc(phba->pport,
2200                                         LPFC_DISC_TRC_MBOX,
2201                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2202                                         (uint32_t)pmbox->mbxCommand,
2203                                         pmbox->un.varWords[0],
2204                                         pmbox->un.varWords[1]);
2205                         }
2206                 }
2207
2208                 /*
2209                  * It is a fatal error if unknown mbox command completion.
2210                  */
2211                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2212                     MBX_SHUTDOWN) {
2213                         /* Unknown mailbox command compl */
2214                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2215                                         "(%d):0323 Unknown Mailbox command "
2216                                         "x%x (x%x) Cmpl\n",
2217                                         pmb->vport ? pmb->vport->vpi : 0,
2218                                         pmbox->mbxCommand,
2219                                         lpfc_sli4_mbox_opcode_get(phba, pmb));
2220                         phba->link_state = LPFC_HBA_ERROR;
2221                         phba->work_hs = HS_FFER3;
2222                         lpfc_handle_eratt(phba);
2223                         continue;
2224                 }
2225
2226                 if (pmbox->mbxStatus) {
2227                         phba->sli.slistat.mbox_stat_err++;
2228                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2229                                 /* Mbox cmd cmpl error - RETRYing */
2230                                 lpfc_printf_log(phba, KERN_INFO,
2231                                                 LOG_MBOX | LOG_SLI,
2232                                                 "(%d):0305 Mbox cmd cmpl "
2233                                                 "error - RETRYing Data: x%x "
2234                                                 "(x%x) x%x x%x x%x\n",
2235                                                 pmb->vport ? pmb->vport->vpi :0,
2236                                                 pmbox->mbxCommand,
2237                                                 lpfc_sli4_mbox_opcode_get(phba,
2238                                                                           pmb),
2239                                                 pmbox->mbxStatus,
2240                                                 pmbox->un.varWords[0],
2241                                                 pmb->vport->port_state);
2242                                 pmbox->mbxStatus = 0;
2243                                 pmbox->mbxOwner = OWN_HOST;
2244                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2245                                 if (rc != MBX_NOT_FINISHED)
2246                                         continue;
2247                         }
2248                 }
2249
2250                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2251                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2252                                 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
2253                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2254                                 pmb->vport ? pmb->vport->vpi : 0,
2255                                 pmbox->mbxCommand,
2256                                 lpfc_sli4_mbox_opcode_get(phba, pmb),
2257                                 pmb->mbox_cmpl,
2258                                 *((uint32_t *) pmbox),
2259                                 pmbox->un.varWords[0],
2260                                 pmbox->un.varWords[1],
2261                                 pmbox->un.varWords[2],
2262                                 pmbox->un.varWords[3],
2263                                 pmbox->un.varWords[4],
2264                                 pmbox->un.varWords[5],
2265                                 pmbox->un.varWords[6],
2266                                 pmbox->un.varWords[7]);
2267
2268                 if (pmb->mbox_cmpl)
2269                         pmb->mbox_cmpl(phba,pmb);
2270         } while (1);
2271         return 0;
2272 }
2273
2274 /**
2275  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2276  * @phba: Pointer to HBA context object.
2277  * @pring: Pointer to driver SLI ring object.
2278  * @tag: buffer tag.
2279  *
2280  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2281  * is set in the tag the buffer is posted for a particular exchange,
2282  * the function will return the buffer without replacing the buffer.
2283  * If the buffer is for unsolicited ELS or CT traffic, this function
2284  * returns the buffer and also posts another buffer to the firmware.
2285  **/
2286 static struct lpfc_dmabuf *
2287 lpfc_sli_get_buff(struct lpfc_hba *phba,
2288                   struct lpfc_sli_ring *pring,
2289                   uint32_t tag)
2290 {
2291         struct hbq_dmabuf *hbq_entry;
2292
2293         if (tag & QUE_BUFTAG_BIT)
2294                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2295         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2296         if (!hbq_entry)
2297                 return NULL;
2298         return &hbq_entry->dbuf;
2299 }
2300
2301 /**
2302  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2303  * @phba: Pointer to HBA context object.
2304  * @pring: Pointer to driver SLI ring object.
2305  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2306  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2307  * @fch_type: the type for the first frame of the sequence.
2308  *
2309  * This function is called with no lock held. This function uses the r_ctl and
2310  * type of the received sequence to find the correct callback function to call
2311  * to process the sequence.
2312  **/
2313 static int
2314 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2315                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2316                          uint32_t fch_type)
2317 {
2318         int i;
2319
2320         /* unSolicited Responses */
2321         if (pring->prt[0].profile) {
2322                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2323                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2324                                                                         saveq);
2325                 return 1;
2326         }
2327         /* We must search, based on rctl / type
2328            for the right routine */
2329         for (i = 0; i < pring->num_mask; i++) {
2330                 if ((pring->prt[i].rctl == fch_r_ctl) &&
2331                     (pring->prt[i].type == fch_type)) {
2332                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2333                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2334                                                 (phba, pring, saveq);
2335                         return 1;
2336                 }
2337         }
2338         return 0;
2339 }
2340
2341 /**
2342  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2343  * @phba: Pointer to HBA context object.
2344  * @pring: Pointer to driver SLI ring object.
2345  * @saveq: Pointer to the unsolicited iocb.
2346  *
2347  * This function is called with no lock held by the ring event handler
2348  * when there is an unsolicited iocb posted to the response ring by the
2349  * firmware. This function gets the buffer associated with the iocbs
2350  * and calls the event handler for the ring. This function handles both
2351  * qring buffers and hbq buffers.
2352  * When the function returns 1 the caller can free the iocb object otherwise
2353  * upper layer functions will free the iocb objects.
2354  **/
2355 static int
2356 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2357                             struct lpfc_iocbq *saveq)
2358 {
2359         IOCB_t           * irsp;
2360         WORD5            * w5p;
2361         uint32_t           Rctl, Type;
2362         uint32_t           match;
2363         struct lpfc_iocbq *iocbq;
2364         struct lpfc_dmabuf *dmzbuf;
2365
2366         match = 0;
2367         irsp = &(saveq->iocb);
2368
2369         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2370                 if (pring->lpfc_sli_rcv_async_status)
2371                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2372                 else
2373                         lpfc_printf_log(phba,
2374                                         KERN_WARNING,
2375                                         LOG_SLI,
2376                                         "0316 Ring %d handler: unexpected "
2377                                         "ASYNC_STATUS iocb received evt_code "
2378                                         "0x%x\n",
2379                                         pring->ringno,
2380                                         irsp->un.asyncstat.evt_code);
2381                 return 1;
2382         }
2383
2384         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2385                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2386                 if (irsp->ulpBdeCount > 0) {
2387                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2388                                         irsp->un.ulpWord[3]);
2389                         lpfc_in_buf_free(phba, dmzbuf);
2390                 }
2391
2392                 if (irsp->ulpBdeCount > 1) {
2393                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2394                                         irsp->unsli3.sli3Words[3]);
2395                         lpfc_in_buf_free(phba, dmzbuf);
2396                 }
2397
2398                 if (irsp->ulpBdeCount > 2) {
2399                         dmzbuf = lpfc_sli_get_buff(phba, pring,
2400                                 irsp->unsli3.sli3Words[7]);
2401                         lpfc_in_buf_free(phba, dmzbuf);
2402                 }
2403
2404                 return 1;
2405         }
2406
2407         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2408                 if (irsp->ulpBdeCount != 0) {
2409                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
2410                                                 irsp->un.ulpWord[3]);
2411                         if (!saveq->context2)
2412                                 lpfc_printf_log(phba,
2413                                         KERN_ERR,
2414                                         LOG_SLI,
2415                                         "0341 Ring %d Cannot find buffer for "
2416                                         "an unsolicited iocb. tag 0x%x\n",
2417                                         pring->ringno,
2418                                         irsp->un.ulpWord[3]);
2419                 }
2420                 if (irsp->ulpBdeCount == 2) {
2421                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2422                                                 irsp->unsli3.sli3Words[7]);
2423                         if (!saveq->context3)
2424                                 lpfc_printf_log(phba,
2425                                         KERN_ERR,
2426                                         LOG_SLI,
2427                                         "0342 Ring %d Cannot find buffer for an"
2428                                         " unsolicited iocb. tag 0x%x\n",
2429                                         pring->ringno,
2430                                         irsp->unsli3.sli3Words[7]);
2431                 }
2432                 list_for_each_entry(iocbq, &saveq->list, list) {
2433                         irsp = &(iocbq->iocb);
2434                         if (irsp->ulpBdeCount != 0) {
2435                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2436                                                         irsp->un.ulpWord[3]);
2437                                 if (!iocbq->context2)
2438                                         lpfc_printf_log(phba,
2439                                                 KERN_ERR,
2440                                                 LOG_SLI,
2441                                                 "0343 Ring %d Cannot find "
2442                                                 "buffer for an unsolicited iocb"
2443                                                 ". tag 0x%x\n", pring->ringno,
2444                                                 irsp->un.ulpWord[3]);
2445                         }
2446                         if (irsp->ulpBdeCount == 2) {
2447                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2448                                                 irsp->unsli3.sli3Words[7]);
2449                                 if (!iocbq->context3)
2450                                         lpfc_printf_log(phba,
2451                                                 KERN_ERR,
2452                                                 LOG_SLI,
2453                                                 "0344 Ring %d Cannot find "
2454                                                 "buffer for an unsolicited "
2455                                                 "iocb. tag 0x%x\n",
2456                                                 pring->ringno,
2457                                                 irsp->unsli3.sli3Words[7]);
2458                         }
2459                 }
2460         }
2461         if (irsp->ulpBdeCount != 0 &&
2462             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2463              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2464                 int found = 0;
2465
2466                 /* search continue save q for same XRI */
2467                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2468                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2469                                 list_add_tail(&saveq->list, &iocbq->list);
2470                                 found = 1;
2471                                 break;
2472                         }
2473                 }
2474                 if (!found)
2475                         list_add_tail(&saveq->clist,
2476                                       &pring->iocb_continue_saveq);
2477                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2478                         list_del_init(&iocbq->clist);
2479                         saveq = iocbq;
2480                         irsp = &(saveq->iocb);
2481                 } else
2482                         return 0;
2483         }
2484         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2485             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2486             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2487                 Rctl = FC_RCTL_ELS_REQ;
2488                 Type = FC_TYPE_ELS;
2489         } else {
2490                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2491                 Rctl = w5p->hcsw.Rctl;
2492                 Type = w5p->hcsw.Type;
2493
2494                 /* Firmware Workaround */
2495                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2496                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2497                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2498                         Rctl = FC_RCTL_ELS_REQ;
2499                         Type = FC_TYPE_ELS;
2500                         w5p->hcsw.Rctl = Rctl;
2501                         w5p->hcsw.Type = Type;
2502                 }
2503         }
2504
2505         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2506                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2507                                 "0313 Ring %d handler: unexpected Rctl x%x "
2508                                 "Type x%x received\n",
2509                                 pring->ringno, Rctl, Type);
2510
2511         return 1;
2512 }
2513
2514 /**
2515  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2516  * @phba: Pointer to HBA context object.
2517  * @pring: Pointer to driver SLI ring object.
2518  * @prspiocb: Pointer to response iocb object.
2519  *
2520  * This function looks up the iocb_lookup table to get the command iocb
2521  * corresponding to the given response iocb using the iotag of the
2522  * response iocb. This function is called with the hbalock held.
2523  * This function returns the command iocb object if it finds the command
2524  * iocb else returns NULL.
2525  **/
2526 static struct lpfc_iocbq *
2527 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2528                       struct lpfc_sli_ring *pring,
2529                       struct lpfc_iocbq *prspiocb)
2530 {
2531         struct lpfc_iocbq *cmd_iocb = NULL;
2532         uint16_t iotag;
2533
2534         iotag = prspiocb->iocb.ulpIoTag;
2535
2536         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2537                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2538                 list_del_init(&cmd_iocb->list);
2539                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2540                         pring->txcmplq_cnt--;
2541                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2542                 }
2543                 return cmd_iocb;
2544         }
2545
2546         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2547                         "0317 iotag x%x is out off "
2548                         "range: max iotag x%x wd0 x%x\n",
2549                         iotag, phba->sli.last_iotag,
2550                         *(((uint32_t *) &prspiocb->iocb) + 7));
2551         return NULL;
2552 }
2553
2554 /**
2555  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2556  * @phba: Pointer to HBA context object.
2557  * @pring: Pointer to driver SLI ring object.
2558  * @iotag: IOCB tag.
2559  *
2560  * This function looks up the iocb_lookup table to get the command iocb
2561  * corresponding to the given iotag. This function is called with the
2562  * hbalock held.
2563  * This function returns the command iocb object if it finds the command
2564  * iocb else returns NULL.
2565  **/
2566 static struct lpfc_iocbq *
2567 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2568                              struct lpfc_sli_ring *pring, uint16_t iotag)
2569 {
2570         struct lpfc_iocbq *cmd_iocb;
2571
2572         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2573                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2574                 list_del_init(&cmd_iocb->list);
2575                 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2576                         cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2577                         pring->txcmplq_cnt--;
2578                 }
2579                 return cmd_iocb;
2580         }
2581
2582         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2583                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2584                         iotag, phba->sli.last_iotag);
2585         return NULL;
2586 }
2587
2588 /**
2589  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2590  * @phba: Pointer to HBA context object.
2591  * @pring: Pointer to driver SLI ring object.
2592  * @saveq: Pointer to the response iocb to be processed.
2593  *
2594  * This function is called by the ring event handler for non-fcp
2595  * rings when there is a new response iocb in the response ring.
2596  * The caller is not required to hold any locks. This function
2597  * gets the command iocb associated with the response iocb and
2598  * calls the completion handler for the command iocb. If there
2599  * is no completion handler, the function will free the resources
2600  * associated with command iocb. If the response iocb is for
2601  * an already aborted command iocb, the status of the completion
2602  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2603  * This function always returns 1.
2604  **/
2605 static int
2606 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2607                           struct lpfc_iocbq *saveq)
2608 {
2609         struct lpfc_iocbq *cmdiocbp;
2610         int rc = 1;
2611         unsigned long iflag;
2612
2613         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2614         spin_lock_irqsave(&phba->hbalock, iflag);
2615         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2616         spin_unlock_irqrestore(&phba->hbalock, iflag);
2617
2618         if (cmdiocbp) {
2619                 if (cmdiocbp->iocb_cmpl) {
2620                         /*
2621                          * If an ELS command failed send an event to mgmt
2622                          * application.
2623                          */
2624                         if (saveq->iocb.ulpStatus &&
2625                              (pring->ringno == LPFC_ELS_RING) &&
2626                              (cmdiocbp->iocb.ulpCommand ==
2627                                 CMD_ELS_REQUEST64_CR))
2628                                 lpfc_send_els_failure_event(phba,
2629                                         cmdiocbp, saveq);
2630
2631                         /*
2632                          * Post all ELS completions to the worker thread.
2633                          * All other are passed to the completion callback.
2634                          */
2635                         if (pring->ringno == LPFC_ELS_RING) {
2636                                 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2637                                     (cmdiocbp->iocb_flag &
2638                                                         LPFC_DRIVER_ABORTED)) {
2639                                         spin_lock_irqsave(&phba->hbalock,
2640                                                           iflag);
2641                                         cmdiocbp->iocb_flag &=
2642                                                 ~LPFC_DRIVER_ABORTED;
2643                                         spin_unlock_irqrestore(&phba->hbalock,
2644                                                                iflag);
2645                                         saveq->iocb.ulpStatus =
2646                                                 IOSTAT_LOCAL_REJECT;
2647                                         saveq->iocb.un.ulpWord[4] =
2648                                                 IOERR_SLI_ABORTED;
2649
2650                                         /* Firmware could still be in progress
2651                                          * of DMAing payload, so don't free data
2652                                          * buffer till after a hbeat.
2653                                          */
2654                                         spin_lock_irqsave(&phba->hbalock,
2655                                                           iflag);
2656                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2657                                         spin_unlock_irqrestore(&phba->hbalock,
2658                                                                iflag);
2659                                 }
2660                                 if (phba->sli_rev == LPFC_SLI_REV4) {
2661                                         if (saveq->iocb_flag &
2662                                             LPFC_EXCHANGE_BUSY) {
2663                                                 /* Set cmdiocb flag for the
2664                                                  * exchange busy so sgl (xri)
2665                                                  * will not be released until
2666                                                  * the abort xri is received
2667                                                  * from hba.
2668                                                  */
2669                                                 spin_lock_irqsave(
2670                                                         &phba->hbalock, iflag);
2671                                                 cmdiocbp->iocb_flag |=
2672                                                         LPFC_EXCHANGE_BUSY;
2673                                                 spin_unlock_irqrestore(
2674                                                         &phba->hbalock, iflag);
2675                                         }
2676                                         if (cmdiocbp->iocb_flag &
2677                                             LPFC_DRIVER_ABORTED) {
2678                                                 /*
2679                                                  * Clear LPFC_DRIVER_ABORTED
2680                                                  * bit in case it was driver
2681                                                  * initiated abort.
2682                                                  */
2683                                                 spin_lock_irqsave(
2684                                                         &phba->hbalock, iflag);
2685                                                 cmdiocbp->iocb_flag &=
2686                                                         ~LPFC_DRIVER_ABORTED;
2687                                                 spin_unlock_irqrestore(
2688                                                         &phba->hbalock, iflag);
2689                                                 cmdiocbp->iocb.ulpStatus =
2690                                                         IOSTAT_LOCAL_REJECT;
2691                                                 cmdiocbp->iocb.un.ulpWord[4] =
2692                                                         IOERR_ABORT_REQUESTED;
2693                                                 /*
2694                                                  * For SLI4, irsiocb contains
2695                                                  * NO_XRI in sli_xritag, it
2696                                                  * shall not affect releasing
2697                                                  * sgl (xri) process.
2698                                                  */
2699                                                 saveq->iocb.ulpStatus =
2700                                                         IOSTAT_LOCAL_REJECT;
2701                                                 saveq->iocb.un.ulpWord[4] =
2702                                                         IOERR_SLI_ABORTED;
2703                                                 spin_lock_irqsave(
2704                                                         &phba->hbalock, iflag);
2705                                                 saveq->iocb_flag |=
2706                                                         LPFC_DELAY_MEM_FREE;
2707                                                 spin_unlock_irqrestore(
2708                                                         &phba->hbalock, iflag);
2709                                         }
2710                                 }
2711                         }
2712                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2713                 } else
2714                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2715         } else {
2716                 /*
2717                  * Unknown initiating command based on the response iotag.
2718                  * This could be the case on the ELS ring because of
2719                  * lpfc_els_abort().
2720                  */
2721                 if (pring->ringno != LPFC_ELS_RING) {
2722                         /*
2723                          * Ring <ringno> handler: unexpected completion IoTag
2724                          * <IoTag>
2725                          */
2726                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2727                                          "0322 Ring %d handler: "
2728                                          "unexpected completion IoTag x%x "
2729                                          "Data: x%x x%x x%x x%x\n",
2730                                          pring->ringno,
2731                                          saveq->iocb.ulpIoTag,
2732                                          saveq->iocb.ulpStatus,
2733                                          saveq->iocb.un.ulpWord[4],
2734                                          saveq->iocb.ulpCommand,
2735                                          saveq->iocb.ulpContext);
2736                 }
2737         }
2738
2739         return rc;
2740 }
2741
2742 /**
2743  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2744  * @phba: Pointer to HBA context object.
2745  * @pring: Pointer to driver SLI ring object.
2746  *
2747  * This function is called from the iocb ring event handlers when
2748  * put pointer is ahead of the get pointer for a ring. This function signal
2749  * an error attention condition to the worker thread and the worker
2750  * thread will transition the HBA to offline state.
2751  **/
2752 static void
2753 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2754 {
2755         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2756         /*
2757          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2758          * rsp ring <portRspMax>
2759          */
2760         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2761                         "0312 Ring %d handler: portRspPut %d "
2762                         "is bigger than rsp ring %d\n",
2763                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2764                         pring->numRiocb);
2765
2766         phba->link_state = LPFC_HBA_ERROR;
2767
2768         /*
2769          * All error attention handlers are posted to
2770          * worker thread
2771          */
2772         phba->work_ha |= HA_ERATT;
2773         phba->work_hs = HS_FFER3;
2774
2775         lpfc_worker_wake_up(phba);
2776
2777         return;
2778 }
2779
2780 /**
2781  * lpfc_poll_eratt - Error attention polling timer timeout handler
2782  * @ptr: Pointer to address of HBA context object.
2783  *
2784  * This function is invoked by the Error Attention polling timer when the
2785  * timer times out. It will check the SLI Error Attention register for
2786  * possible attention events. If so, it will post an Error Attention event
2787  * and wake up worker thread to process it. Otherwise, it will set up the
2788  * Error Attention polling timer for the next poll.
2789  **/
2790 void lpfc_poll_eratt(unsigned long ptr)
2791 {
2792         struct lpfc_hba *phba;
2793         uint32_t eratt = 0;
2794
2795         phba = (struct lpfc_hba *)ptr;
2796
2797         /* Check chip HA register for error event */
2798         eratt = lpfc_sli_check_eratt(phba);
2799
2800         if (eratt)
2801                 /* Tell the worker thread there is work to do */
2802                 lpfc_worker_wake_up(phba);
2803         else
2804                 /* Restart the timer for next eratt poll */
2805                 mod_timer(&phba->eratt_poll, jiffies +
2806                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2807         return;
2808 }
2809
2810
2811 /**
2812  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2813  * @phba: Pointer to HBA context object.
2814  * @pring: Pointer to driver SLI ring object.
2815  * @mask: Host attention register mask for this ring.
2816  *
2817  * This function is called from the interrupt context when there is a ring
2818  * event for the fcp ring. The caller does not hold any lock.
2819  * The function processes each response iocb in the response ring until it
2820  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2821  * LE bit set. The function will call the completion handler of the command iocb
2822  * if the response iocb indicates a completion for a command iocb or it is
2823  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2824  * function if this is an unsolicited iocb.
2825  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2826  * to check it explicitly.
2827  */
2828 int
2829 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2830                                 struct lpfc_sli_ring *pring, uint32_t mask)
2831 {
2832         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2833         IOCB_t *irsp = NULL;
2834         IOCB_t *entry = NULL;
2835         struct lpfc_iocbq *cmdiocbq = NULL;
2836         struct lpfc_iocbq rspiocbq;
2837         uint32_t status;
2838         uint32_t portRspPut, portRspMax;
2839         int rc = 1;
2840         lpfc_iocb_type type;
2841         unsigned long iflag;
2842         uint32_t rsp_cmpl = 0;
2843
2844         spin_lock_irqsave(&phba->hbalock, iflag);
2845         pring->stats.iocb_event++;
2846
2847         /*
2848          * The next available response entry should never exceed the maximum
2849          * entries.  If it does, treat it as an adapter hardware error.
2850          */
2851         portRspMax = pring->numRiocb;
2852         portRspPut = le32_to_cpu(pgp->rspPutInx);
2853         if (unlikely(portRspPut >= portRspMax)) {
2854                 lpfc_sli_rsp_pointers_error(phba, pring);
2855                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2856                 return 1;
2857         }
2858         if (phba->fcp_ring_in_use) {
2859                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2860                 return 1;
2861         } else
2862                 phba->fcp_ring_in_use = 1;
2863
2864         rmb();
2865         while (pring->rspidx != portRspPut) {
2866                 /*
2867                  * Fetch an entry off the ring and copy it into a local data
2868                  * structure.  The copy involves a byte-swap since the
2869                  * network byte order and pci byte orders are different.
2870                  */
2871                 entry = lpfc_resp_iocb(phba, pring);
2872                 phba->last_completion_time = jiffies;
2873
2874                 if (++pring->rspidx >= portRspMax)
2875                         pring->rspidx = 0;
2876
2877                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2878                                       (uint32_t *) &rspiocbq.iocb,
2879                                       phba->iocb_rsp_size);
2880                 INIT_LIST_HEAD(&(rspiocbq.list));
2881                 irsp = &rspiocbq.iocb;
2882
2883                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2884                 pring->stats.iocb_rsp++;
2885                 rsp_cmpl++;
2886
2887                 if (unlikely(irsp->ulpStatus)) {
2888                         /*
2889                          * If resource errors reported from HBA, reduce
2890                          * queuedepths of the SCSI device.
2891                          */
2892                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2893                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2894                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2895                                 phba->lpfc_rampdown_queue_depth(phba);
2896                                 spin_lock_irqsave(&phba->hbalock, iflag);
2897                         }
2898
2899                         /* Rsp ring <ringno> error: IOCB */
2900                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2901                                         "0336 Rsp Ring %d error: IOCB Data: "
2902                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2903                                         pring->ringno,
2904                                         irsp->un.ulpWord[0],
2905                                         irsp->un.ulpWord[1],
2906                                         irsp->un.ulpWord[2],
2907                                         irsp->un.ulpWord[3],
2908                                         irsp->un.ulpWord[4],
2909                                         irsp->un.ulpWord[5],
2910                                         *(uint32_t *)&irsp->un1,
2911                                         *((uint32_t *)&irsp->un1 + 1));
2912                 }
2913
2914                 switch (type) {
2915                 case LPFC_ABORT_IOCB:
2916                 case LPFC_SOL_IOCB:
2917                         /*
2918                          * Idle exchange closed via ABTS from port.  No iocb
2919                          * resources need to be recovered.
2920                          */
2921                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2922                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2923                                                 "0333 IOCB cmd 0x%x"
2924                                                 " processed. Skipping"
2925                                                 " completion\n",
2926                                                 irsp->ulpCommand);
2927                                 break;
2928                         }
2929
2930                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2931                                                          &rspiocbq);
2932                         if (unlikely(!cmdiocbq))
2933                                 break;
2934                         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2935                                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2936                         if (cmdiocbq->iocb_cmpl) {
2937                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2938                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2939                                                       &rspiocbq);
2940                                 spin_lock_irqsave(&phba->hbalock, iflag);
2941                         }
2942                         break;
2943                 case LPFC_UNSOL_IOCB:
2944                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2945                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2946                         spin_lock_irqsave(&phba->hbalock, iflag);
2947                         break;
2948                 default:
2949                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2950                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2951                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2952                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2953                                        MAX_MSG_DATA);
2954                                 dev_warn(&((phba->pcidev)->dev),
2955                                          "lpfc%d: %s\n",
2956                                          phba->brd_no, adaptermsg);
2957                         } else {
2958                                 /* Unknown IOCB command */
2959                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2960                                                 "0334 Unknown IOCB command "
2961                                                 "Data: x%x, x%x x%x x%x x%x\n",
2962                                                 type, irsp->ulpCommand,
2963                                                 irsp->ulpStatus,
2964                                                 irsp->ulpIoTag,
2965                                                 irsp->ulpContext);
2966                         }
2967                         break;
2968                 }
2969
2970                 /*
2971                  * The response IOCB has been processed.  Update the ring
2972                  * pointer in SLIM.  If the port response put pointer has not
2973                  * been updated, sync the pgp->rspPutInx and fetch the new port
2974                  * response put pointer.
2975                  */
2976                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2977
2978                 if (pring->rspidx == portRspPut)
2979                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2980         }
2981
2982         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2983                 pring->stats.iocb_rsp_full++;
2984                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2985                 writel(status, phba->CAregaddr);
2986                 readl(phba->CAregaddr);
2987         }
2988         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2989                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2990                 pring->stats.iocb_cmd_empty++;
2991
2992                 /* Force update of the local copy of cmdGetInx */
2993                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2994                 lpfc_sli_resume_iocb(phba, pring);
2995
2996                 if ((pring->lpfc_sli_cmd_available))
2997                         (pring->lpfc_sli_cmd_available) (phba, pring);
2998
2999         }
3000
3001         phba->fcp_ring_in_use = 0;
3002         spin_unlock_irqrestore(&phba->hbalock, iflag);
3003         return rc;
3004 }
3005
3006 /**
3007  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3008  * @phba: Pointer to HBA context object.
3009  * @pring: Pointer to driver SLI ring object.
3010  * @rspiocbp: Pointer to driver response IOCB object.
3011  *
3012  * This function is called from the worker thread when there is a slow-path
3013  * response IOCB to process. This function chains all the response iocbs until
3014  * seeing the iocb with the LE bit set. The function will call
3015  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3016  * completion of a command iocb. The function will call the
3017  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3018  * The function frees the resources or calls the completion handler if this
3019  * iocb is an abort completion. The function returns NULL when the response
3020  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3021  * this function shall chain the iocb on to the iocb_continueq and return the
3022  * response iocb passed in.
3023  **/
3024 static struct lpfc_iocbq *
3025 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3026                         struct lpfc_iocbq *rspiocbp)
3027 {
3028         struct lpfc_iocbq *saveq;
3029         struct lpfc_iocbq *cmdiocbp;
3030         struct lpfc_iocbq *next_iocb;
3031         IOCB_t *irsp = NULL;
3032         uint32_t free_saveq;
3033         uint8_t iocb_cmd_type;
3034         lpfc_iocb_type type;
3035         unsigned long iflag;
3036         int rc;
3037
3038         spin_lock_irqsave(&phba->hbalock, iflag);
3039         /* First add the response iocb to the countinueq list */
3040         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3041         pring->iocb_continueq_cnt++;
3042
3043         /* Now, determine whetehr the list is completed for processing */
3044         irsp = &rspiocbp->iocb;
3045         if (irsp->ulpLe) {
3046                 /*
3047                  * By default, the driver expects to free all resources
3048                  * associated with this iocb completion.
3049                  */
3050                 free_saveq = 1;
3051                 saveq = list_get_first(&pring->iocb_continueq,
3052                                        struct lpfc_iocbq, list);
3053                 irsp = &(saveq->iocb);
3054                 list_del_init(&pring->iocb_continueq);
3055                 pring->iocb_continueq_cnt = 0;
3056
3057                 pring->stats.iocb_rsp++;
3058
3059                 /*
3060                  * If resource errors reported from HBA, reduce
3061                  * queuedepths of the SCSI device.
3062                  */
3063                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3064                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3065                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3066                         phba->lpfc_rampdown_queue_depth(phba);
3067                         spin_lock_irqsave(&phba->hbalock, iflag);
3068                 }
3069
3070                 if (irsp->ulpStatus) {
3071                         /* Rsp ring <ringno> error: IOCB */
3072                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3073                                         "0328 Rsp Ring %d error: "
3074                                         "IOCB Data: "
3075                                         "x%x x%x x%x x%x "
3076                                         "x%x x%x x%x x%x "
3077                                         "x%x x%x x%x x%x "
3078                                         "x%x x%x x%x x%x\n",
3079                                         pring->ringno,
3080                                         irsp->un.ulpWord[0],
3081                                         irsp->un.ulpWord[1],
3082                                         irsp->un.ulpWord[2],
3083                                         irsp->un.ulpWord[3],
3084                                         irsp->un.ulpWord[4],
3085                                         irsp->un.ulpWord[5],
3086                                         *(((uint32_t *) irsp) + 6),
3087                                         *(((uint32_t *) irsp) + 7),
3088                                         *(((uint32_t *) irsp) + 8),
3089                                         *(((uint32_t *) irsp) + 9),
3090                                         *(((uint32_t *) irsp) + 10),
3091                                         *(((uint32_t *) irsp) + 11),
3092                                         *(((uint32_t *) irsp) + 12),
3093                                         *(((uint32_t *) irsp) + 13),
3094                                         *(((uint32_t *) irsp) + 14),
3095                                         *(((uint32_t *) irsp) + 15));
3096                 }
3097
3098                 /*
3099                  * Fetch the IOCB command type and call the correct completion
3100                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
3101                  * get freed back to the lpfc_iocb_list by the discovery
3102                  * kernel thread.
3103                  */
3104                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3105                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3106                 switch (type) {
3107                 case LPFC_SOL_IOCB:
3108                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3109                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3110                         spin_lock_irqsave(&phba->hbalock, iflag);
3111                         break;
3112
3113                 case LPFC_UNSOL_IOCB:
3114                         spin_unlock_irqrestore(&phba->hbalock, iflag);
3115                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3116                         spin_lock_irqsave(&phba->hbalock, iflag);
3117                         if (!rc)
3118                                 free_saveq = 0;
3119                         break;
3120
3121                 case LPFC_ABORT_IOCB:
3122                         cmdiocbp = NULL;
3123                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3124                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3125                                                                  saveq);
3126                         if (cmdiocbp) {
3127                                 /* Call the specified completion routine */
3128                                 if (cmdiocbp->iocb_cmpl) {
3129                                         spin_unlock_irqrestore(&phba->hbalock,
3130                                                                iflag);
3131                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3132                                                               saveq);
3133                                         spin_lock_irqsave(&phba->hbalock,
3134                                                           iflag);
3135                                 } else
3136                                         __lpfc_sli_release_iocbq(phba,
3137                                                                  cmdiocbp);
3138                         }
3139                         break;
3140
3141                 case LPFC_UNKNOWN_IOCB:
3142                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3143                                 char adaptermsg[LPFC_MAX_ADPTMSG];
3144                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3145                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3146                                        MAX_MSG_DATA);
3147                                 dev_warn(&((phba->pcidev)->dev),
3148                                          "lpfc%d: %s\n",
3149                                          phba->brd_no, adaptermsg);
3150                         } else {
3151                                 /* Unknown IOCB command */
3152                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3153                                                 "0335 Unknown IOCB "
3154                                                 "command Data: x%x "
3155                                                 "x%x x%x x%x\n",
3156                                                 irsp->ulpCommand,
3157                                                 irsp->ulpStatus,
3158                                                 irsp->ulpIoTag,
3159                                                 irsp->ulpContext);
3160                         }
3161                         break;
3162                 }
3163
3164                 if (free_saveq) {
3165                         list_for_each_entry_safe(rspiocbp, next_iocb,
3166                                                  &saveq->list, list) {
3167                                 list_del(&rspiocbp->list);
3168                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
3169                         }
3170                         __lpfc_sli_release_iocbq(phba, saveq);
3171                 }
3172                 rspiocbp = NULL;
3173         }
3174         spin_unlock_irqrestore(&phba->hbalock, iflag);
3175         return rspiocbp;
3176 }
3177
3178 /**
3179  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3180  * @phba: Pointer to HBA context object.
3181  * @pring: Pointer to driver SLI ring object.
3182  * @mask: Host attention register mask for this ring.
3183  *
3184  * This routine wraps the actual slow_ring event process routine from the
3185  * API jump table function pointer from the lpfc_hba struct.
3186  **/
3187 void
3188 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3189                                 struct lpfc_sli_ring *pring, uint32_t mask)
3190 {
3191         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3192 }
3193
3194 /**
3195  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3196  * @phba: Pointer to HBA context object.
3197  * @pring: Pointer to driver SLI ring object.
3198  * @mask: Host attention register mask for this ring.
3199  *
3200  * This function is called from the worker thread when there is a ring event
3201  * for non-fcp rings. The caller does not hold any lock. The function will
3202  * remove each response iocb in the response ring and calls the handle
3203  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3204  **/
3205 static void
3206 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3207                                    struct lpfc_sli_ring *pring, uint32_t mask)
3208 {
3209         struct lpfc_pgp *pgp;
3210         IOCB_t *entry;
3211         IOCB_t *irsp = NULL;
3212         struct lpfc_iocbq *rspiocbp = NULL;
3213         uint32_t portRspPut, portRspMax;
3214         unsigned long iflag;
3215         uint32_t status;
3216
3217         pgp = &phba->port_gp[pring->ringno];
3218         spin_lock_irqsave(&phba->hbalock, iflag);
3219         pring->stats.iocb_event++;
3220
3221         /*
3222          * The next available response entry should never exceed the maximum
3223          * entries.  If it does, treat it as an adapter hardware error.
3224          */
3225         portRspMax = pring->numRiocb;
3226         portRspPut = le32_to_cpu(pgp->rspPutInx);
3227         if (portRspPut >= portRspMax) {
3228                 /*
3229                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3230                  * rsp ring <portRspMax>
3231                  */
3232                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3233                                 "0303 Ring %d handler: portRspPut %d "
3234                                 "is bigger than rsp ring %d\n",
3235                                 pring->ringno, portRspPut, portRspMax);
3236
3237                 phba->link_state = LPFC_HBA_ERROR;
3238                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3239
3240                 phba->work_hs = HS_FFER3;
3241                 lpfc_handle_eratt(phba);
3242
3243                 return;
3244         }
3245
3246         rmb();
3247         while (pring->rspidx != portRspPut) {
3248                 /*
3249                  * Build a completion list and call the appropriate handler.
3250                  * The process is to get the next available response iocb, get
3251                  * a free iocb from the list, copy the response data into the
3252                  * free iocb, insert to the continuation list, and update the
3253                  * next response index to slim.  This process makes response
3254                  * iocb's in the ring available to DMA as fast as possible but
3255                  * pays a penalty for a copy operation.  Since the iocb is
3256                  * only 32 bytes, this penalty is considered small relative to
3257                  * the PCI reads for register values and a slim write.  When
3258                  * the ulpLe field is set, the entire Command has been
3259                  * received.
3260                  */
3261                 entry = lpfc_resp_iocb(phba, pring);
3262
3263                 phba->last_completion_time = jiffies;
3264                 rspiocbp = __lpfc_sli_get_iocbq(phba);
3265                 if (rspiocbp == NULL) {
3266                         printk(KERN_ERR "%s: out of buffers! Failing "
3267                                "completion.\n", __func__);
3268                         break;
3269                 }
3270
3271                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3272                                       phba->iocb_rsp_size);
3273                 irsp = &rspiocbp->iocb;
3274
3275                 if (++pring->rspidx >= portRspMax)
3276                         pring->rspidx = 0;
3277
3278                 if (pring->ringno == LPFC_ELS_RING) {
3279                         lpfc_debugfs_slow_ring_trc(phba,
3280                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3281                                 *(((uint32_t *) irsp) + 4),
3282                                 *(((uint32_t *) irsp) + 6),
3283                                 *(((uint32_t *) irsp) + 7));
3284                 }
3285
3286                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3287
3288                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3289                 /* Handle the response IOCB */
3290                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3291                 spin_lock_irqsave(&phba->hbalock, iflag);
3292
3293                 /*
3294                  * If the port response put pointer has not been updated, sync
3295                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3296                  * response put pointer.
3297                  */
3298                 if (pring->rspidx == portRspPut) {
3299                         portRspPut = le32_to_cpu(pgp->rspPutInx);
3300                 }
3301         } /* while (pring->rspidx != portRspPut) */
3302
3303         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3304                 /* At least one response entry has been freed */
3305                 pring->stats.iocb_rsp_full++;
3306                 /* SET RxRE_RSP in Chip Att register */
3307                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3308                 writel(status, phba->CAregaddr);
3309                 readl(phba->CAregaddr); /* flush */
3310         }
3311         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3312                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3313                 pring->stats.iocb_cmd_empty++;
3314
3315                 /* Force update of the local copy of cmdGetInx */
3316                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3317                 lpfc_sli_resume_iocb(phba, pring);
3318
3319                 if ((pring->lpfc_sli_cmd_available))
3320                         (pring->lpfc_sli_cmd_available) (phba, pring);
3321
3322         }
3323
3324         spin_unlock_irqrestore(&phba->hbalock, iflag);
3325         return;
3326 }
3327
3328 /**
3329  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3330  * @phba: Pointer to HBA context object.
3331  * @pring: Pointer to driver SLI ring object.
3332  * @mask: Host attention register mask for this ring.
3333  *
3334  * This function is called from the worker thread when there is a pending
3335  * ELS response iocb on the driver internal slow-path response iocb worker
3336  * queue. The caller does not hold any lock. The function will remove each
3337  * response iocb from the response worker queue and calls the handle
3338  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3339  **/
3340 static void
3341 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3342                                    struct lpfc_sli_ring *pring, uint32_t mask)
3343 {
3344         struct lpfc_iocbq *irspiocbq;
3345         struct hbq_dmabuf *dmabuf;
3346         struct lpfc_cq_event *cq_event;
3347         unsigned long iflag;
3348
3349         spin_lock_irqsave(&phba->hbalock, iflag);
3350         phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3351         spin_unlock_irqrestore(&phba->hbalock, iflag);
3352         while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3353                 /* Get the response iocb from the head of work queue */
3354                 spin_lock_irqsave(&phba->hbalock, iflag);
3355                 list_remove_head(&phba->sli4_hba.sp_queue_event,
3356                                  cq_event, struct lpfc_cq_event, list);
3357                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3358
3359                 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3360                 case CQE_CODE_COMPL_WQE:
3361                         irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3362                                                  cq_event);
3363                         /* Translate ELS WCQE to response IOCBQ */
3364                         irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3365                                                                    irspiocbq);
3366                         if (irspiocbq)
3367                                 lpfc_sli_sp_handle_rspiocb(phba, pring,
3368                                                            irspiocbq);
3369                         break;
3370                 case CQE_CODE_RECEIVE:
3371                         dmabuf = container_of(cq_event, struct hbq_dmabuf,
3372                                               cq_event);
3373                         lpfc_sli4_handle_received_buffer(phba, dmabuf);
3374                         break;
3375                 default:
3376                         break;
3377                 }
3378         }
3379 }
3380
3381 /**
3382  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3383  * @phba: Pointer to HBA context object.
3384  * @pring: Pointer to driver SLI ring object.
3385  *
3386  * This function aborts all iocbs in the given ring and frees all the iocb
3387  * objects in txq. This function issues an abort iocb for all the iocb commands
3388  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3389  * the return of this function. The caller is not required to hold any locks.
3390  **/
3391 void
3392 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3393 {
3394         LIST_HEAD(completions);
3395         struct lpfc_iocbq *iocb, *next_iocb;
3396
3397         if (pring->ringno == LPFC_ELS_RING) {
3398                 lpfc_fabric_abort_hba(phba);
3399         }
3400
3401         /* Error everything on txq and txcmplq
3402          * First do the txq.
3403          */
3404         spin_lock_irq(&phba->hbalock);
3405         list_splice_init(&pring->txq, &completions);
3406         pring->txq_cnt = 0;
3407
3408         /* Next issue ABTS for everything on the txcmplq */
3409         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3410                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3411
3412         spin_unlock_irq(&phba->hbalock);
3413
3414         /* Cancel all the IOCBs from the completions list */
3415         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3416                               IOERR_SLI_ABORTED);
3417 }
3418
3419 /**
3420  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3421  * @phba: Pointer to HBA context object.
3422  *
3423  * This function flushes all iocbs in the fcp ring and frees all the iocb
3424  * objects in txq and txcmplq. This function will not issue abort iocbs
3425  * for all the iocb commands in txcmplq, they will just be returned with
3426  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3427  * slot has been permanently disabled.
3428  **/
3429 void
3430 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3431 {
3432         LIST_HEAD(txq);
3433         LIST_HEAD(txcmplq);
3434         struct lpfc_sli *psli = &phba->sli;
3435         struct lpfc_sli_ring  *pring;
3436
3437         /* Currently, only one fcp ring */
3438         pring = &psli->ring[psli->fcp_ring];
3439
3440         spin_lock_irq(&phba->hbalock);
3441         /* Retrieve everything on txq */
3442         list_splice_init(&pring->txq, &txq);
3443         pring->txq_cnt = 0;
3444
3445         /* Retrieve everything on the txcmplq */
3446         list_splice_init(&pring->txcmplq, &txcmplq);
3447         pring->txcmplq_cnt = 0;
3448         spin_unlock_irq(&phba->hbalock);
3449
3450         /* Flush the txq */
3451         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3452                               IOERR_SLI_DOWN);
3453
3454         /* Flush the txcmpq */
3455         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3456                               IOERR_SLI_DOWN);
3457 }
3458
3459 /**
3460  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3461  * @phba: Pointer to HBA context object.
3462  * @mask: Bit mask to be checked.
3463  *
3464  * This function reads the host status register and compares
3465  * with the provided bit mask to check if HBA completed
3466  * the restart. This function will wait in a loop for the
3467  * HBA to complete restart. If the HBA does not restart within
3468  * 15 iterations, the function will reset the HBA again. The
3469  * function returns 1 when HBA fail to restart otherwise returns
3470  * zero.
3471  **/
3472 static int
3473 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3474 {
3475         uint32_t status;
3476         int i = 0;
3477         int retval = 0;
3478
3479         /* Read the HBA Host Status Register */
3480         status = readl(phba->HSregaddr);
3481
3482         /*
3483          * Check status register every 100ms for 5 retries, then every
3484          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3485          * every 2.5 sec for 4.
3486          * Break our of the loop if errors occurred during init.
3487          */
3488         while (((status & mask) != mask) &&
3489                !(status & HS_FFERM) &&
3490                i++ < 20) {
3491
3492                 if (i <= 5)
3493                         msleep(10);
3494                 else if (i <= 10)
3495                         msleep(500);
3496                 else
3497                         msleep(2500);
3498
3499                 if (i == 15) {
3500                                 /* Do post */
3501                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3502                         lpfc_sli_brdrestart(phba);
3503                 }
3504                 /* Read the HBA Host Status Register */
3505                 status = readl(phba->HSregaddr);
3506         }
3507
3508         /* Check to see if any errors occurred during init */
3509         if ((status & HS_FFERM) || (i >= 20)) {
3510                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3511                                 "2751 Adapter failed to restart, "
3512                                 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3513                                 status,
3514                                 readl(phba->MBslimaddr + 0xa8),
3515                                 readl(phba->MBslimaddr + 0xac));
3516                 phba->link_state = LPFC_HBA_ERROR;
3517                 retval = 1;
3518         }
3519
3520         return retval;
3521 }
3522
3523 /**
3524  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3525  * @phba: Pointer to HBA context object.
3526  * @mask: Bit mask to be checked.
3527  *
3528  * This function checks the host status register to check if HBA is
3529  * ready. This function will wait in a loop for the HBA to be ready
3530  * If the HBA is not ready , the function will will reset the HBA PCI
3531  * function again. The function returns 1 when HBA fail to be ready
3532  * otherwise returns zero.
3533  **/
3534 static int
3535 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3536 {
3537         uint32_t status;
3538         int retval = 0;
3539
3540         /* Read the HBA Host Status Register */
3541         status = lpfc_sli4_post_status_check(phba);
3542
3543         if (status) {
3544                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3545                 lpfc_sli_brdrestart(phba);
3546                 status = lpfc_sli4_post_status_check(phba);
3547         }
3548
3549         /* Check to see if any errors occurred during init */
3550         if (status) {
3551                 phba->link_state = LPFC_HBA_ERROR;
3552                 retval = 1;
3553         } else
3554                 phba->sli4_hba.intr_enable = 0;
3555
3556         return retval;
3557 }
3558
3559 /**
3560  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3561  * @phba: Pointer to HBA context object.
3562  * @mask: Bit mask to be checked.
3563  *
3564  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3565  * from the API jump table function pointer from the lpfc_hba struct.
3566  **/
3567 int
3568 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3569 {
3570         return phba->lpfc_sli_brdready(phba, mask);
3571 }
3572
3573 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3574
3575 /**
3576  * lpfc_reset_barrier - Make HBA ready for HBA reset
3577  * @phba: Pointer to HBA context object.
3578  *
3579  * This function is called before resetting an HBA. This
3580  * function requests HBA to quiesce DMAs before a reset.
3581  **/
3582 void lpfc_reset_barrier(struct lpfc_hba *phba)
3583 {
3584         uint32_t __iomem *resp_buf;
3585         uint32_t __iomem *mbox_buf;
3586         volatile uint32_t mbox;
3587         uint32_t hc_copy;
3588         int  i;
3589         uint8_t hdrtype;
3590
3591         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3592         if (hdrtype != 0x80 ||
3593             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3594              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3595                 return;
3596
3597         /*
3598          * Tell the other part of the chip to suspend temporarily all
3599          * its DMA activity.
3600          */
3601         resp_buf = phba->MBslimaddr;
3602
3603         /* Disable the error attention */
3604         hc_copy = readl(phba->HCregaddr);
3605         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3606         readl(phba->HCregaddr); /* flush */
3607         phba->link_flag |= LS_IGNORE_ERATT;
3608
3609         if (readl(phba->HAregaddr) & HA_ERATT) {
3610                 /* Clear Chip error bit */
3611                 writel(HA_ERATT, phba->HAregaddr);
3612                 phba->pport->stopped = 1;
3613         }
3614
3615         mbox = 0;
3616         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3617         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3618
3619         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3620         mbox_buf = phba->MBslimaddr;
3621         writel(mbox, mbox_buf);
3622
3623         for (i = 0;
3624              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3625                 mdelay(1);
3626
3627         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3628                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3629                     phba->pport->stopped)
3630                         goto restore_hc;
3631                 else
3632                         goto clear_errat;
3633         }
3634
3635         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3636         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3637                 mdelay(1);
3638
3639 clear_errat:
3640
3641         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3642                 mdelay(1);
3643
3644         if (readl(phba->HAregaddr) & HA_ERATT) {
3645                 writel(HA_ERATT, phba->HAregaddr);
3646                 phba->pport->stopped = 1;
3647         }
3648
3649 restore_hc:
3650         phba->link_flag &= ~LS_IGNORE_ERATT;
3651         writel(hc_copy, phba->HCregaddr);
3652         readl(phba->HCregaddr); /* flush */
3653 }
3654
3655 /**
3656  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3657  * @phba: Pointer to HBA context object.
3658  *
3659  * This function issues a kill_board mailbox command and waits for
3660  * the error attention interrupt. This function is called for stopping
3661  * the firmware processing. The caller is not required to hold any
3662  * locks. This function calls lpfc_hba_down_post function to free
3663  * any pending commands after the kill. The function will return 1 when it
3664  * fails to kill the board else will return 0.
3665  **/
3666 int
3667 lpfc_sli_brdkill(struct lpfc_hba *phba)
3668 {
3669         struct lpfc_sli *psli;
3670         LPFC_MBOXQ_t *pmb;
3671         uint32_t status;
3672         uint32_t ha_copy;
3673         int retval;
3674         int i = 0;
3675
3676         psli = &phba->sli;
3677
3678         /* Kill HBA */
3679         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3680                         "0329 Kill HBA Data: x%x x%x\n",
3681                         phba->pport->port_state, psli->sli_flag);
3682
3683         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3684         if (!pmb)
3685                 return 1;
3686
3687         /* Disable the error attention */
3688         spin_lock_irq(&phba->hbalock);
3689         status = readl(phba->HCregaddr);
3690         status &= ~HC_ERINT_ENA;
3691         writel(status, phba->HCregaddr);
3692         readl(phba->HCregaddr); /* flush */
3693         phba->link_flag |= LS_IGNORE_ERATT;
3694         spin_unlock_irq(&phba->hbalock);
3695
3696         lpfc_kill_board(phba, pmb);
3697         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3698         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3699
3700         if (retval != MBX_SUCCESS) {
3701                 if (retval != MBX_BUSY)
3702                         mempool_free(pmb, phba->mbox_mem_pool);
3703                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3704                                 "2752 KILL_BOARD command failed retval %d\n",
3705                                 retval);
3706                 spin_lock_irq(&phba->hbalock);
3707                 phba->link_flag &= ~LS_IGNORE_ERATT;
3708                 spin_unlock_irq(&phba->hbalock);
3709                 return 1;
3710         }
3711
3712         spin_lock_irq(&phba->hbalock);
3713         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3714         spin_unlock_irq(&phba->hbalock);
3715
3716         mempool_free(pmb, phba->mbox_mem_pool);
3717
3718         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3719          * attention every 100ms for 3 seconds. If we don't get ERATT after
3720          * 3 seconds we still set HBA_ERROR state because the status of the
3721          * board is now undefined.
3722          */
3723         ha_copy = readl(phba->HAregaddr);
3724
3725         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3726                 mdelay(100);
3727                 ha_copy = readl(phba->HAregaddr);
3728         }
3729
3730         del_timer_sync(&psli->mbox_tmo);
3731         if (ha_copy & HA_ERATT) {
3732                 writel(HA_ERATT, phba->HAregaddr);
3733                 phba->pport->stopped = 1;
3734         }
3735         spin_lock_irq(&phba->hbalock);
3736         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3737         psli->mbox_active = NULL;
3738         phba->link_flag &= ~LS_IGNORE_ERATT;
3739         spin_unlock_irq(&phba->hbalock);
3740
3741         lpfc_hba_down_post(phba);
3742         phba->link_state = LPFC_HBA_ERROR;
3743
3744         return ha_copy & HA_ERATT ? 0 : 1;
3745 }
3746
3747 /**
3748  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3749  * @phba: Pointer to HBA context object.
3750  *
3751  * This function resets the HBA by writing HC_INITFF to the control
3752  * register. After the HBA resets, this function resets all the iocb ring
3753  * indices. This function disables PCI layer parity checking during
3754  * the reset.
3755  * This function returns 0 always.
3756  * The caller is not required to hold any locks.
3757  **/
3758 int
3759 lpfc_sli_brdreset(struct lpfc_hba *phba)
3760 {
3761         struct lpfc_sli *psli;
3762         struct lpfc_sli_ring *pring;
3763         uint16_t cfg_value;
3764         int i;
3765
3766         psli = &phba->sli;
3767
3768         /* Reset HBA */
3769         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3770                         "0325 Reset HBA Data: x%x x%x\n",
3771                         phba->pport->port_state, psli->sli_flag);
3772
3773         /* perform board reset */
3774         phba->fc_eventTag = 0;
3775         phba->link_events = 0;
3776         phba->pport->fc_myDID = 0;
3777         phba->pport->fc_prevDID = 0;
3778
3779         /* Turn off parity checking and serr during the physical reset */
3780         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3781         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3782                               (cfg_value &
3783                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3784
3785         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3786
3787         /* Now toggle INITFF bit in the Host Control Register */
3788         writel(HC_INITFF, phba->HCregaddr);
3789         mdelay(1);
3790         readl(phba->HCregaddr); /* flush */
3791         writel(0, phba->HCregaddr);
3792         readl(phba->HCregaddr); /* flush */
3793
3794         /* Restore PCI cmd register */
3795         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3796
3797         /* Initialize relevant SLI info */
3798         for (i = 0; i < psli->num_rings; i++) {
3799                 pring = &psli->ring[i];
3800                 pring->flag = 0;
3801                 pring->rspidx = 0;
3802                 pring->next_cmdidx  = 0;
3803                 pring->local_getidx = 0;
3804                 pring->cmdidx = 0;
3805                 pring->missbufcnt = 0;
3806         }
3807
3808         phba->link_state = LPFC_WARM_START;
3809         return 0;
3810 }
3811
3812 /**
3813  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3814  * @phba: Pointer to HBA context object.
3815  *
3816  * This function resets a SLI4 HBA. This function disables PCI layer parity
3817  * checking during resets the device. The caller is not required to hold
3818  * any locks.
3819  *
3820  * This function returns 0 always.
3821  **/
3822 int
3823 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3824 {
3825         struct lpfc_sli *psli = &phba->sli;
3826         uint16_t cfg_value;
3827         uint8_t qindx;
3828
3829         /* Reset HBA */
3830         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3831                         "0295 Reset HBA Data: x%x x%x\n",
3832                         phba->pport->port_state, psli->sli_flag);
3833
3834         /* perform board reset */
3835         phba->fc_eventTag = 0;
3836         phba->link_events = 0;
3837         phba->pport->fc_myDID = 0;
3838         phba->pport->fc_prevDID = 0;
3839
3840         spin_lock_irq(&phba->hbalock);
3841         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3842         phba->fcf.fcf_flag = 0;
3843         /* Clean up the child queue list for the CQs */
3844         list_del_init(&phba->sli4_hba.mbx_wq->list);
3845         list_del_init(&phba->sli4_hba.els_wq->list);
3846         list_del_init(&phba->sli4_hba.hdr_rq->list);
3847         list_del_init(&phba->sli4_hba.dat_rq->list);
3848         list_del_init(&phba->sli4_hba.mbx_cq->list);
3849         list_del_init(&phba->sli4_hba.els_cq->list);
3850         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3851                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3852         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3853                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3854         spin_unlock_irq(&phba->hbalock);
3855
3856         /* Now physically reset the device */
3857         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3858                         "0389 Performing PCI function reset!\n");
3859
3860         /* Turn off parity checking and serr during the physical reset */
3861         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3862         pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3863                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3864
3865         /* Perform FCoE PCI function reset */
3866         lpfc_pci_function_reset(phba);
3867
3868         /* Restore PCI cmd register */
3869         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3870
3871         return 0;
3872 }
3873
3874 /**
3875  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3876  * @phba: Pointer to HBA context object.
3877  *
3878  * This function is called in the SLI initialization code path to
3879  * restart the HBA. The caller is not required to hold any lock.
3880  * This function writes MBX_RESTART mailbox command to the SLIM and
3881  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3882  * function to free any pending commands. The function enables
3883  * POST only during the first initialization. The function returns zero.
3884  * The function does not guarantee completion of MBX_RESTART mailbox
3885  * command before the return of this function.
3886  **/
3887 static int
3888 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3889 {
3890         MAILBOX_t *mb;
3891         struct lpfc_sli *psli;
3892         volatile uint32_t word0;
3893         void __iomem *to_slim;
3894         uint32_t hba_aer_enabled;
3895
3896         spin_lock_irq(&phba->hbalock);
3897
3898         /* Take PCIe device Advanced Error Reporting (AER) state */
3899         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3900
3901         psli = &phba->sli;
3902
3903         /* Restart HBA */
3904         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3905                         "0337 Restart HBA Data: x%x x%x\n",
3906                         phba->pport->port_state, psli->sli_flag);
3907
3908         word0 = 0;
3909         mb = (MAILBOX_t *) &word0;
3910         mb->mbxCommand = MBX_RESTART;
3911         mb->mbxHc = 1;
3912
3913         lpfc_reset_barrier(phba);
3914
3915         to_slim = phba->MBslimaddr;
3916         writel(*(uint32_t *) mb, to_slim);
3917         readl(to_slim); /* flush */
3918
3919         /* Only skip post after fc_ffinit is completed */
3920         if (phba->pport->port_state)
3921                 word0 = 1;      /* This is really setting up word1 */
3922         else
3923                 word0 = 0;      /* This is really setting up word1 */
3924         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3925         writel(*(uint32_t *) mb, to_slim);
3926         readl(to_slim); /* flush */
3927
3928         lpfc_sli_brdreset(phba);
3929         phba->pport->stopped = 0;
3930         phba->link_state = LPFC_INIT_START;
3931         phba->hba_flag = 0;
3932         spin_unlock_irq(&phba->hbalock);
3933
3934         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3935         psli->stats_start = get_seconds();
3936
3937         /* Give the INITFF and Post time to settle. */
3938         mdelay(100);
3939
3940         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3941         if (hba_aer_enabled)
3942                 pci_disable_pcie_error_reporting(phba->pcidev);
3943
3944         lpfc_hba_down_post(phba);
3945
3946         return 0;
3947 }
3948
3949 /**
3950  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3951  * @phba: Pointer to HBA context object.
3952  *
3953  * This function is called in the SLI initialization code path to restart
3954  * a SLI4 HBA. The caller is not required to hold any lock.
3955  * At the end of the function, it calls lpfc_hba_down_post function to
3956  * free any pending commands.
3957  **/
3958 static int
3959 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3960 {
3961         struct lpfc_sli *psli = &phba->sli;
3962         uint32_t hba_aer_enabled;
3963
3964         /* Restart HBA */
3965         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3966                         "0296 Restart HBA Data: x%x x%x\n",
3967                         phba->pport->port_state, psli->sli_flag);
3968
3969         /* Take PCIe device Advanced Error Reporting (AER) state */
3970         hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3971
3972         lpfc_sli4_brdreset(phba);
3973
3974         spin_lock_irq(&phba->hbalock);
3975         phba->pport->stopped = 0;
3976         phba->link_state = LPFC_INIT_START;
3977         phba->hba_flag = 0;
3978         spin_unlock_irq(&phba->hbalock);
3979
3980         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3981         psli->stats_start = get_seconds();
3982
3983         /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3984         if (hba_aer_enabled)
3985                 pci_disable_pcie_error_reporting(phba->pcidev);
3986
3987         lpfc_hba_down_post(phba);
3988
3989         return 0;
3990 }
3991
3992 /**
3993  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3994  * @phba: Pointer to HBA context object.
3995  *
3996  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3997  * API jump table function pointer from the lpfc_hba struct.
3998 **/
3999 int
4000 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4001 {
4002         return phba->lpfc_sli_brdrestart(phba);
4003 }
4004
4005 /**
4006  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4007  * @phba: Pointer to HBA context object.
4008  *
4009  * This function is called after a HBA restart to wait for successful
4010  * restart of the HBA. Successful restart of the HBA is indicated by
4011  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4012  * iteration, the function will restart the HBA again. The function returns
4013  * zero if HBA successfully restarted else returns negative error code.
4014  **/
4015 static int
4016 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4017 {
4018         uint32_t status, i = 0;
4019
4020         /* Read the HBA Host Status Register */
4021         status = readl(phba->HSregaddr);
4022
4023         /* Check status register to see what current state is */
4024         i = 0;
4025         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4026
4027                 /* Check every 10ms for 10 retries, then every 100ms for 90
4028                  * retries, then every 1 sec for 50 retires for a total of
4029                  * ~60 seconds before reset the board again and check every
4030                  * 1 sec for 50 retries. The up to 60 seconds before the
4031                  * board ready is required by the Falcon FIPS zeroization
4032                  * complete, and any reset the board in between shall cause
4033                  * restart of zeroization, further delay the board ready.
4034                  */
4035                 if (i++ >= 200) {
4036                         /* Adapter failed to init, timeout, status reg
4037                            <status> */
4038                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4039                                         "0436 Adapter failed to init, "
4040                                         "timeout, status reg x%x, "
4041                                         "FW Data: A8 x%x AC x%x\n", status,
4042                                         readl(phba->MBslimaddr + 0xa8),
4043                                         readl(phba->MBslimaddr + 0xac));
4044                         phba->link_state = LPFC_HBA_ERROR;
4045                         return -ETIMEDOUT;
4046                 }
4047
4048                 /* Check to see if any errors occurred during init */
4049                 if (status & HS_FFERM) {
4050                         /* ERROR: During chipset initialization */
4051                         /* Adapter failed to init, chipset, status reg
4052                            <status> */
4053                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4054                                         "0437 Adapter failed to init, "
4055                                         "chipset, status reg x%x, "
4056                                         "FW Data: A8 x%x AC x%x\n", status,
4057                                         readl(phba->MBslimaddr + 0xa8),
4058                                         readl(phba->MBslimaddr + 0xac));
4059                         phba->link_state = LPFC_HBA_ERROR;
4060                         return -EIO;
4061                 }
4062
4063                 if (i <= 10)
4064                         msleep(10);
4065                 else if (i <= 100)
4066                         msleep(100);
4067                 else
4068                         msleep(1000);
4069
4070                 if (i == 150) {
4071                         /* Do post */
4072                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4073                         lpfc_sli_brdrestart(phba);
4074                 }
4075                 /* Read the HBA Host Status Register */
4076                 status = readl(phba->HSregaddr);
4077         }
4078
4079         /* Check to see if any errors occurred during init */
4080         if (status & HS_FFERM) {
4081                 /* ERROR: During chipset initialization */
4082                 /* Adapter failed to init, chipset, status reg <status> */
4083                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4084                                 "0438 Adapter failed to init, chipset, "
4085                                 "status reg x%x, "
4086                                 "FW Data: A8 x%x AC x%x\n", status,
4087                                 readl(phba->MBslimaddr + 0xa8),
4088                                 readl(phba->MBslimaddr + 0xac));
4089                 phba->link_state = LPFC_HBA_ERROR;
4090                 return -EIO;
4091         }
4092
4093         /* Clear all interrupt enable conditions */
4094         writel(0, phba->HCregaddr);
4095         readl(phba->HCregaddr); /* flush */
4096
4097         /* setup host attn register */
4098         writel(0xffffffff, phba->HAregaddr);
4099         readl(phba->HAregaddr); /* flush */
4100         return 0;
4101 }
4102
4103 /**
4104  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4105  *
4106  * This function calculates and returns the number of HBQs required to be
4107  * configured.
4108  **/
4109 int
4110 lpfc_sli_hbq_count(void)
4111 {
4112         return ARRAY_SIZE(lpfc_hbq_defs);
4113 }
4114
4115 /**
4116  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4117  *
4118  * This function adds the number of hbq entries in every HBQ to get
4119  * the total number of hbq entries required for the HBA and returns
4120  * the total count.
4121  **/
4122 static int
4123 lpfc_sli_hbq_entry_count(void)
4124 {
4125         int  hbq_count = lpfc_sli_hbq_count();
4126         int  count = 0;
4127         int  i;
4128
4129         for (i = 0; i < hbq_count; ++i)
4130                 count += lpfc_hbq_defs[i]->entry_count;
4131         return count;
4132 }
4133
4134 /**
4135  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4136  *
4137  * This function calculates amount of memory required for all hbq entries
4138  * to be configured and returns the total memory required.
4139  **/
4140 int
4141 lpfc_sli_hbq_size(void)
4142 {
4143         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4144 }
4145
4146 /**
4147  * lpfc_sli_hbq_setup - configure and initialize HBQs
4148  * @phba: Pointer to HBA context object.
4149  *
4150  * This function is called during the SLI initialization to configure
4151  * all the HBQs and post buffers to the HBQ. The caller is not
4152  * required to hold any locks. This function will return zero if successful
4153  * else it will return negative error code.
4154  **/
4155 static int
4156 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4157 {
4158         int  hbq_count = lpfc_sli_hbq_count();
4159         LPFC_MBOXQ_t *pmb;
4160         MAILBOX_t *pmbox;
4161         uint32_t hbqno;
4162         uint32_t hbq_entry_index;
4163
4164                                 /* Get a Mailbox buffer to setup mailbox
4165                                  * commands for HBA initialization
4166                                  */
4167         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4168
4169         if (!pmb)
4170                 return -ENOMEM;
4171
4172         pmbox = &pmb->u.mb;
4173
4174         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4175         phba->link_state = LPFC_INIT_MBX_CMDS;
4176         phba->hbq_in_use = 1;
4177
4178         hbq_entry_index = 0;
4179         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4180                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4181                 phba->hbqs[hbqno].hbqPutIdx      = 0;
4182                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4183                 phba->hbqs[hbqno].entry_count =
4184                         lpfc_hbq_defs[hbqno]->entry_count;
4185                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4186                         hbq_entry_index, pmb);
4187                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4188
4189                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4190                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4191                            mbxStatus <status>, ring <num> */
4192
4193                         lpfc_printf_log(phba, KERN_ERR,
4194                                         LOG_SLI | LOG_VPORT,
4195                                         "1805 Adapter failed to init. "
4196                                         "Data: x%x x%x x%x\n",
4197                                         pmbox->mbxCommand,
4198                                         pmbox->mbxStatus, hbqno);
4199
4200                         phba->link_state = LPFC_HBA_ERROR;
4201                         mempool_free(pmb, phba->mbox_mem_pool);
4202                         return -ENXIO;
4203                 }
4204         }
4205         phba->hbq_count = hbq_count;
4206
4207         mempool_free(pmb, phba->mbox_mem_pool);
4208
4209         /* Initially populate or replenish the HBQs */
4210         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4211                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4212         return 0;
4213 }
4214
4215 /**
4216  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4217  * @phba: Pointer to HBA context object.
4218  *
4219  * This function is called during the SLI initialization to configure
4220  * all the HBQs and post buffers to the HBQ. The caller is not
4221  * required to hold any locks. This function will return zero if successful
4222  * else it will return negative error code.
4223  **/
4224 static int
4225 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4226 {
4227         phba->hbq_in_use = 1;
4228         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4229         phba->hbq_count = 1;
4230         /* Initially populate or replenish the HBQs */
4231         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4232         return 0;
4233 }
4234
4235 /**
4236  * lpfc_sli_config_port - Issue config port mailbox command
4237  * @phba: Pointer to HBA context object.
4238  * @sli_mode: sli mode - 2/3
4239  *
4240  * This function is called by the sli intialization code path
4241  * to issue config_port mailbox command. This function restarts the
4242  * HBA firmware and issues a config_port mailbox command to configure
4243  * the SLI interface in the sli mode specified by sli_mode
4244  * variable. The caller is not required to hold any locks.
4245  * The function returns 0 if successful, else returns negative error
4246  * code.
4247  **/
4248 int
4249 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4250 {
4251         LPFC_MBOXQ_t *pmb;
4252         uint32_t resetcount = 0, rc = 0, done = 0;
4253
4254         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4255         if (!pmb) {
4256                 phba->link_state = LPFC_HBA_ERROR;
4257                 return -ENOMEM;
4258         }
4259
4260         phba->sli_rev = sli_mode;
4261         while (resetcount < 2 && !done) {
4262                 spin_lock_irq(&phba->hbalock);
4263                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4264                 spin_unlock_irq(&phba->hbalock);
4265                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4266                 lpfc_sli_brdrestart(phba);
4267                 rc = lpfc_sli_chipset_init(phba);
4268                 if (rc)
4269                         break;
4270
4271                 spin_lock_irq(&phba->hbalock);
4272                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4273                 spin_unlock_irq(&phba->hbalock);
4274                 resetcount++;
4275
4276                 /* Call pre CONFIG_PORT mailbox command initialization.  A
4277                  * value of 0 means the call was successful.  Any other
4278                  * nonzero value is a failure, but if ERESTART is returned,
4279                  * the driver may reset the HBA and try again.
4280                  */
4281                 rc = lpfc_config_port_prep(phba);
4282                 if (rc == -ERESTART) {
4283                         phba->link_state = LPFC_LINK_UNKNOWN;
4284                         continue;
4285                 } else if (rc)
4286                         break;
4287                 phba->link_state = LPFC_INIT_MBX_CMDS;
4288                 lpfc_config_port(phba, pmb);
4289                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4290                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4291                                         LPFC_SLI3_HBQ_ENABLED |
4292                                         LPFC_SLI3_CRP_ENABLED |
4293                                         LPFC_SLI3_BG_ENABLED |
4294                                         LPFC_SLI3_DSS_ENABLED);
4295                 if (rc != MBX_SUCCESS) {
4296                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4297                                 "0442 Adapter failed to init, mbxCmd x%x "
4298                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4299                                 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4300                         spin_lock_irq(&phba->hbalock);
4301                         phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4302                         spin_unlock_irq(&phba->hbalock);
4303                         rc = -ENXIO;
4304                 } else {
4305                         /* Allow asynchronous mailbox command to go through */
4306                         spin_lock_irq(&phba->hbalock);
4307                         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4308                         spin_unlock_irq(&phba->hbalock);
4309                         done = 1;
4310                 }
4311         }
4312         if (!done) {
4313                 rc = -EINVAL;
4314                 goto do_prep_failed;
4315         }
4316         if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4317                 if (!pmb->u.mb.un.varCfgPort.cMA) {
4318                         rc = -ENXIO;
4319                         goto do_prep_failed;
4320                 }
4321                 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4322                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4323                         phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4324                         phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4325                                 phba->max_vpi : phba->max_vports;
4326
4327                 } else
4328                         phba->max_vpi = 0;
4329                 phba->fips_level = 0;
4330                 phba->fips_spec_rev = 0;
4331                 if (pmb->u.mb.un.varCfgPort.gdss) {
4332                         phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4333                         phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4334                         phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4335                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4336                                         "2850 Security Crypto Active. FIPS x%d "
4337                                         "(Spec Rev: x%d)",
4338                                         phba->fips_level, phba->fips_spec_rev);
4339                 }
4340                 if (pmb->u.mb.un.varCfgPort.sec_err) {
4341                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4342                                         "2856 Config Port Security Crypto "
4343                                         "Error: x%x ",
4344                                         pmb->u.mb.un.varCfgPort.sec_err);
4345                 }
4346                 if (pmb->u.mb.un.varCfgPort.gerbm)
4347                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4348                 if (pmb->u.mb.un.varCfgPort.gcrp)
4349                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4350
4351                 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4352                 phba->port_gp = phba->mbox->us.s3_pgp.port;
4353
4354                 if (phba->cfg_enable_bg) {
4355                         if (pmb->u.mb.un.varCfgPort.gbg)
4356                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4357                         else
4358                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4359                                                 "0443 Adapter did not grant "
4360                                                 "BlockGuard\n");
4361                 }
4362         } else {
4363                 phba->hbq_get = NULL;
4364                 phba->port_gp = phba->mbox->us.s2.port;
4365                 phba->max_vpi = 0;
4366         }
4367 do_prep_failed:
4368         mempool_free(pmb, phba->mbox_mem_pool);
4369         return rc;
4370 }
4371
4372
4373 /**
4374  * lpfc_sli_hba_setup - SLI intialization function
4375  * @phba: Pointer to HBA context object.
4376  *
4377  * This function is the main SLI intialization function. This function
4378  * is called by the HBA intialization code, HBA reset code and HBA
4379  * error attention handler code. Caller is not required to hold any
4380  * locks. This function issues config_port mailbox command to configure
4381  * the SLI, setup iocb rings and HBQ rings. In the end the function
4382  * calls the config_port_post function to issue init_link mailbox
4383  * command and to start the discovery. The function will return zero
4384  * if successful, else it will return negative error code.
4385  **/
4386 int
4387 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4388 {
4389         uint32_t rc;
4390         int  mode = 3;
4391
4392         switch (lpfc_sli_mode) {
4393         case 2:
4394                 if (phba->cfg_enable_npiv) {
4395                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4396                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4397                                 "parameter (%d) to auto (0).\n",
4398                                 lpfc_sli_mode);
4399                         break;
4400                 }
4401                 mode = 2;
4402                 break;
4403         case 0:
4404         case 3:
4405                 break;
4406         default:
4407                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4408                                 "1819 Unrecognized lpfc_sli_mode "
4409                                 "parameter: %d.\n", lpfc_sli_mode);
4410
4411                 break;
4412         }
4413
4414         rc = lpfc_sli_config_port(phba, mode);
4415
4416         if (rc && lpfc_sli_mode == 3)
4417                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4418                                 "1820 Unable to select SLI-3.  "
4419                                 "Not supported by adapter.\n");
4420         if (rc && mode != 2)
4421                 rc = lpfc_sli_config_port(phba, 2);
4422         if (rc)
4423                 goto lpfc_sli_hba_setup_error;
4424
4425         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4426         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4427                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4428                 if (!rc) {
4429                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4430                                         "2709 This device supports "
4431                                         "Advanced Error Reporting (AER)\n");
4432                         spin_lock_irq(&phba->hbalock);
4433                         phba->hba_flag |= HBA_AER_ENABLED;
4434                         spin_unlock_irq(&phba->hbalock);
4435                 } else {
4436                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4437                                         "2708 This device does not support "
4438                                         "Advanced Error Reporting (AER)\n");
4439                         phba->cfg_aer_support = 0;
4440                 }
4441         }
4442
4443         if (phba->sli_rev == 3) {
4444                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4445                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4446         } else {
4447                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4448                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4449                 phba->sli3_options = 0;
4450         }
4451
4452         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4453                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4454                         phba->sli_rev, phba->max_vpi);
4455         rc = lpfc_sli_ring_map(phba);
4456
4457         if (rc)
4458                 goto lpfc_sli_hba_setup_error;
4459
4460         /* Init HBQs */
4461         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4462                 rc = lpfc_sli_hbq_setup(phba);
4463                 if (rc)
4464                         goto lpfc_sli_hba_setup_error;
4465         }
4466         spin_lock_irq(&phba->hbalock);
4467         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4468         spin_unlock_irq(&phba->hbalock);
4469
4470         rc = lpfc_config_port_post(phba);
4471         if (rc)
4472                 goto lpfc_sli_hba_setup_error;
4473
4474         return rc;
4475
4476 lpfc_sli_hba_setup_error:
4477         phba->link_state = LPFC_HBA_ERROR;
4478         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4479                         "0445 Firmware initialization failed\n");
4480         return rc;
4481 }
4482
4483 /**
4484  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4485  * @phba: Pointer to HBA context object.
4486  * @mboxq: mailbox pointer.
4487  * This function issue a dump mailbox command to read config region
4488  * 23 and parse the records in the region and populate driver
4489  * data structure.
4490  **/
4491 static int
4492 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4493                 LPFC_MBOXQ_t *mboxq)
4494 {
4495         struct lpfc_dmabuf *mp;
4496         struct lpfc_mqe *mqe;
4497         uint32_t data_length;
4498         int rc;
4499
4500         /* Program the default value of vlan_id and fc_map */
4501         phba->valid_vlan = 0;
4502         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4503         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4504         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4505
4506         mqe = &mboxq->u.mqe;
4507         if (lpfc_dump_fcoe_param(phba, mboxq))
4508                 return -ENOMEM;
4509
4510         mp = (struct lpfc_dmabuf *) mboxq->context1;
4511         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4512
4513         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4514                         "(%d):2571 Mailbox cmd x%x Status x%x "
4515                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4516                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4517                         "CQ: x%x x%x x%x x%x\n",
4518                         mboxq->vport ? mboxq->vport->vpi : 0,
4519                         bf_get(lpfc_mqe_command, mqe),
4520                         bf_get(lpfc_mqe_status, mqe),
4521                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4522                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4523                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4524                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4525                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4526                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4527                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4528                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4529                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4530                         mboxq->mcqe.word0,
4531                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4532                         mboxq->mcqe.trailer);
4533
4534         if (rc) {
4535                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4536                 kfree(mp);
4537                 return -EIO;
4538         }
4539         data_length = mqe->un.mb_words[5];
4540         if (data_length > DMP_RGN23_SIZE) {
4541                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4542                 kfree(mp);
4543                 return -EIO;
4544         }
4545
4546         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4547         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4548         kfree(mp);
4549         return 0;
4550 }
4551
4552 /**
4553  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4554  * @phba: pointer to lpfc hba data structure.
4555  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4556  * @vpd: pointer to the memory to hold resulting port vpd data.
4557  * @vpd_size: On input, the number of bytes allocated to @vpd.
4558  *            On output, the number of data bytes in @vpd.
4559  *
4560  * This routine executes a READ_REV SLI4 mailbox command.  In
4561  * addition, this routine gets the port vpd data.
4562  *
4563  * Return codes
4564  *      0 - successful
4565  *      -ENOMEM - could not allocated memory.
4566  **/
4567 static int
4568 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4569                     uint8_t *vpd, uint32_t *vpd_size)
4570 {
4571         int rc = 0;
4572         uint32_t dma_size;
4573         struct lpfc_dmabuf *dmabuf;
4574         struct lpfc_mqe *mqe;
4575
4576         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4577         if (!dmabuf)
4578                 return -ENOMEM;
4579
4580         /*
4581          * Get a DMA buffer for the vpd data resulting from the READ_REV
4582          * mailbox command.
4583          */
4584         dma_size = *vpd_size;
4585         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4586                                           dma_size,
4587                                           &dmabuf->phys,
4588                                           GFP_KERNEL);
4589         if (!dmabuf->virt) {
4590                 kfree(dmabuf);
4591                 return -ENOMEM;
4592         }
4593         memset(dmabuf->virt, 0, dma_size);
4594
4595         /*
4596          * The SLI4 implementation of READ_REV conflicts at word1,
4597          * bits 31:16 and SLI4 adds vpd functionality not present
4598          * in SLI3.  This code corrects the conflicts.
4599          */
4600         lpfc_read_rev(phba, mboxq);
4601         mqe = &mboxq->u.mqe;
4602         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4603         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4604         mqe->un.read_rev.word1 &= 0x0000FFFF;
4605         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4606         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4607
4608         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4609         if (rc) {
4610                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4611                                   dmabuf->virt, dmabuf->phys);
4612                 kfree(dmabuf);
4613                 return -EIO;
4614         }
4615
4616         /*
4617          * The available vpd length cannot be bigger than the
4618          * DMA buffer passed to the port.  Catch the less than
4619          * case and update the caller's size.
4620          */
4621         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4622                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4623
4624         memcpy(vpd, dmabuf->virt, *vpd_size);
4625
4626         dma_free_coherent(&phba->pcidev->dev, dma_size,
4627                           dmabuf->virt, dmabuf->phys);
4628         kfree(dmabuf);
4629         return 0;
4630 }
4631
4632 /**
4633  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4634  * @phba: pointer to lpfc hba data structure.
4635  *
4636  * This routine is called to explicitly arm the SLI4 device's completion and
4637  * event queues
4638  **/
4639 static void
4640 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4641 {
4642         uint8_t fcp_eqidx;
4643
4644         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4645         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4646         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4647                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4648                                      LPFC_QUEUE_REARM);
4649         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4650         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4651                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4652                                      LPFC_QUEUE_REARM);
4653 }
4654
4655 /**
4656  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4657  * @phba: Pointer to HBA context object.
4658  *
4659  * This function is the main SLI4 device intialization PCI function. This
4660  * function is called by the HBA intialization code, HBA reset code and
4661  * HBA error attention handler code. Caller is not required to hold any
4662  * locks.
4663  **/
4664 int
4665 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4666 {
4667         int rc;
4668         LPFC_MBOXQ_t *mboxq;
4669         struct lpfc_mqe *mqe;
4670         uint8_t *vpd;
4671         uint32_t vpd_size;
4672         uint32_t ftr_rsp = 0;
4673         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4674         struct lpfc_vport *vport = phba->pport;
4675         struct lpfc_dmabuf *mp;
4676
4677         /*
4678          * TODO:  Why does this routine execute these task in a different
4679          * order from probe?
4680          */
4681         /* Perform a PCI function reset to start from clean */
4682         rc = lpfc_pci_function_reset(phba);
4683         if (unlikely(rc))
4684                 return -ENODEV;
4685
4686         /* Check the HBA Host Status Register for readyness */
4687         rc = lpfc_sli4_post_status_check(phba);
4688         if (unlikely(rc))
4689                 return -ENODEV;
4690         else {
4691                 spin_lock_irq(&phba->hbalock);
4692                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4693                 spin_unlock_irq(&phba->hbalock);
4694         }
4695
4696         /*
4697          * Allocate a single mailbox container for initializing the
4698          * port.
4699          */
4700         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4701         if (!mboxq)
4702                 return -ENOMEM;
4703
4704         /*
4705          * Continue initialization with default values even if driver failed
4706          * to read FCoE param config regions
4707          */
4708         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4709                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4710                         "2570 Failed to read FCoE parameters\n");
4711
4712         /* Issue READ_REV to collect vpd and FW information. */
4713         vpd_size = SLI4_PAGE_SIZE;
4714         vpd = kzalloc(vpd_size, GFP_KERNEL);
4715         if (!vpd) {
4716                 rc = -ENOMEM;
4717                 goto out_free_mbox;
4718         }
4719
4720         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4721         if (unlikely(rc)) {
4722                 kfree(vpd);
4723                 goto out_free_mbox;
4724         }
4725         mqe = &mboxq->u.mqe;
4726         phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4727         if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4728                 phba->hba_flag |= HBA_FCOE_MODE;
4729         else
4730                 phba->hba_flag &= ~HBA_FCOE_MODE;
4731
4732         if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4733                 LPFC_DCBX_CEE_MODE)
4734                 phba->hba_flag |= HBA_FIP_SUPPORT;
4735         else
4736                 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4737
4738         if (phba->sli_rev != LPFC_SLI_REV4 ||
4739             !(phba->hba_flag & HBA_FCOE_MODE)) {
4740                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4741                         "0376 READ_REV Error. SLI Level %d "
4742                         "FCoE enabled %d\n",
4743                         phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
4744                 rc = -EIO;
4745                 kfree(vpd);
4746                 goto out_free_mbox;
4747         }
4748         /*
4749          * Evaluate the read rev and vpd data. Populate the driver
4750          * state with the results. If this routine fails, the failure
4751          * is not fatal as the driver will use generic values.
4752          */
4753         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4754         if (unlikely(!rc)) {
4755                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4756                                 "0377 Error %d parsing vpd. "
4757                                 "Using defaults.\n", rc);
4758                 rc = 0;
4759         }
4760         kfree(vpd);
4761
4762         /* Save information as VPD data */
4763         phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4764         phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4765         phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4766         phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4767                                          &mqe->un.read_rev);
4768         phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4769                                        &mqe->un.read_rev);
4770         phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4771                                             &mqe->un.read_rev);
4772         phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4773                                            &mqe->un.read_rev);
4774         phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4775         memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4776         phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4777         memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4778         phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4779         memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4780         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4781                         "(%d):0380 READ_REV Status x%x "
4782                         "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4783                         mboxq->vport ? mboxq->vport->vpi : 0,
4784                         bf_get(lpfc_mqe_status, mqe),
4785                         phba->vpd.rev.opFwName,
4786                         phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4787                         phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
4788
4789         /*
4790          * Discover the port's supported feature set and match it against the
4791          * hosts requests.
4792          */
4793         lpfc_request_features(phba, mboxq);
4794         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4795         if (unlikely(rc)) {
4796                 rc = -EIO;
4797                 goto out_free_mbox;
4798         }
4799
4800         /*
4801          * The port must support FCP initiator mode as this is the
4802          * only mode running in the host.
4803          */
4804         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4805                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4806                                 "0378 No support for fcpi mode.\n");
4807                 ftr_rsp++;
4808         }
4809         if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
4810                 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
4811         else
4812                 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
4813         /*
4814          * If the port cannot support the host's requested features
4815          * then turn off the global config parameters to disable the
4816          * feature in the driver.  This is not a fatal error.
4817          */
4818         if ((phba->cfg_enable_bg) &&
4819             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4820                 ftr_rsp++;
4821
4822         if (phba->max_vpi && phba->cfg_enable_npiv &&
4823             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4824                 ftr_rsp++;
4825
4826         if (ftr_rsp) {
4827                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4828                                 "0379 Feature Mismatch Data: x%08x %08x "
4829                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4830                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4831                                 phba->cfg_enable_npiv, phba->max_vpi);
4832                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4833                         phba->cfg_enable_bg = 0;
4834                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4835                         phba->cfg_enable_npiv = 0;
4836         }
4837
4838         /* These SLI3 features are assumed in SLI4 */
4839         spin_lock_irq(&phba->hbalock);
4840         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4841         spin_unlock_irq(&phba->hbalock);
4842
4843         /* Read the port's service parameters. */
4844         rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4845         if (rc) {
4846                 phba->link_state = LPFC_HBA_ERROR;
4847                 rc = -ENOMEM;
4848                 goto out_free_mbox;
4849         }
4850
4851         mboxq->vport = vport;
4852         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4853         mp = (struct lpfc_dmabuf *) mboxq->context1;
4854         if (rc == MBX_SUCCESS) {
4855                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4856                 rc = 0;
4857         }
4858
4859         /*
4860          * This memory was allocated by the lpfc_read_sparam routine. Release
4861          * it to the mbuf pool.
4862          */
4863         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4864         kfree(mp);
4865         mboxq->context1 = NULL;
4866         if (unlikely(rc)) {
4867                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4868                                 "0382 READ_SPARAM command failed "
4869                                 "status %d, mbxStatus x%x\n",
4870                                 rc, bf_get(lpfc_mqe_status, mqe));
4871                 phba->link_state = LPFC_HBA_ERROR;
4872                 rc = -EIO;
4873                 goto out_free_mbox;
4874         }
4875
4876         if (phba->cfg_soft_wwnn)
4877                 u64_to_wwn(phba->cfg_soft_wwnn,
4878                            vport->fc_sparam.nodeName.u.wwn);
4879         if (phba->cfg_soft_wwpn)
4880                 u64_to_wwn(phba->cfg_soft_wwpn,
4881                            vport->fc_sparam.portName.u.wwn);
4882         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4883                sizeof(struct lpfc_name));
4884         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4885                sizeof(struct lpfc_name));
4886
4887         /* Update the fc_host data structures with new wwn. */
4888         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4889         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4890
4891         /* Register SGL pool to the device using non-embedded mailbox command */
4892         rc = lpfc_sli4_post_sgl_list(phba);
4893         if (unlikely(rc)) {
4894                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4895                                 "0582 Error %d during sgl post operation\n",
4896                                         rc);
4897                 rc = -ENODEV;
4898                 goto out_free_mbox;
4899         }
4900
4901         /* Register SCSI SGL pool to the device */
4902         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4903         if (unlikely(rc)) {
4904                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4905                                 "0383 Error %d during scsi sgl post "
4906                                 "operation\n", rc);
4907                 /* Some Scsi buffers were moved to the abort scsi list */
4908                 /* A pci function reset will repost them */
4909                 rc = -ENODEV;
4910                 goto out_free_mbox;
4911         }
4912
4913         /* Post the rpi header region to the device. */
4914         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4915         if (unlikely(rc)) {
4916                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4917                                 "0393 Error %d during rpi post operation\n",
4918                                 rc);
4919                 rc = -ENODEV;
4920                 goto out_free_mbox;
4921         }
4922
4923         /* Set up all the queues to the device */
4924         rc = lpfc_sli4_queue_setup(phba);
4925         if (unlikely(rc)) {
4926                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4927                                 "0381 Error %d during queue setup.\n ", rc);
4928                 goto out_stop_timers;
4929         }
4930
4931         /* Arm the CQs and then EQs on device */
4932         lpfc_sli4_arm_cqeq_intr(phba);
4933
4934         /* Indicate device interrupt mode */
4935         phba->sli4_hba.intr_enable = 1;
4936
4937         /* Allow asynchronous mailbox command to go through */
4938         spin_lock_irq(&phba->hbalock);
4939         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4940         spin_unlock_irq(&phba->hbalock);
4941
4942         /* Post receive buffers to the device */
4943         lpfc_sli4_rb_setup(phba);
4944
4945         /* Reset HBA FCF states after HBA reset */
4946         phba->fcf.fcf_flag = 0;
4947         phba->fcf.current_rec.flag = 0;
4948
4949         /* Start the ELS watchdog timer */
4950         mod_timer(&vport->els_tmofunc,
4951                   jiffies + HZ * (phba->fc_ratov * 2));
4952
4953         /* Start heart beat timer */
4954         mod_timer(&phba->hb_tmofunc,
4955                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4956         phba->hb_outstanding = 0;
4957         phba->last_completion_time = jiffies;
4958
4959         /* Start error attention (ERATT) polling timer */
4960         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4961
4962         /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4963         if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4964                 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4965                 if (!rc) {
4966                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4967                                         "2829 This device supports "
4968                                         "Advanced Error Reporting (AER)\n");
4969                         spin_lock_irq(&phba->hbalock);
4970                         phba->hba_flag |= HBA_AER_ENABLED;
4971                         spin_unlock_irq(&phba->hbalock);
4972                 } else {
4973                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4974                                         "2830 This device does not support "
4975                                         "Advanced Error Reporting (AER)\n");
4976                         phba->cfg_aer_support = 0;
4977                 }
4978         }
4979
4980         if (!(phba->hba_flag & HBA_FCOE_MODE)) {
4981                 /*
4982                  * The FC Port needs to register FCFI (index 0)
4983                  */
4984                 lpfc_reg_fcfi(phba, mboxq);
4985                 mboxq->vport = phba->pport;
4986                 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4987                 if (rc == MBX_SUCCESS)
4988                         rc = 0;
4989                 else
4990                         goto out_unset_queue;
4991         }
4992         /*
4993          * The port is ready, set the host's link state to LINK_DOWN
4994          * in preparation for link interrupts.
4995          */
4996         spin_lock_irq(&phba->hbalock);
4997         phba->link_state = LPFC_LINK_DOWN;
4998         spin_unlock_irq(&phba->hbalock);
4999         if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK)
5000                 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5001 out_unset_queue:
5002         /* Unset all the queues set up in this routine when error out */
5003         if (rc)
5004                 lpfc_sli4_queue_unset(phba);
5005 out_stop_timers:
5006         if (rc)
5007                 lpfc_stop_hba_timers(phba);
5008 out_free_mbox:
5009         mempool_free(mboxq, phba->mbox_mem_pool);
5010         return rc;
5011 }
5012
5013 /**
5014  * lpfc_mbox_timeout - Timeout call back function for mbox timer
5015  * @ptr: context object - pointer to hba structure.
5016  *
5017  * This is the callback function for mailbox timer. The mailbox
5018  * timer is armed when a new mailbox command is issued and the timer
5019  * is deleted when the mailbox complete. The function is called by
5020  * the kernel timer code when a mailbox does not complete within
5021  * expected time. This function wakes up the worker thread to
5022  * process the mailbox timeout and returns. All the processing is
5023  * done by the worker thread function lpfc_mbox_timeout_handler.
5024  **/
5025 void
5026 lpfc_mbox_timeout(unsigned long ptr)
5027 {
5028         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
5029         unsigned long iflag;
5030         uint32_t tmo_posted;
5031
5032         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
5033         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
5034         if (!tmo_posted)
5035                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
5036         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
5037
5038         if (!tmo_posted)
5039                 lpfc_worker_wake_up(phba);
5040         return;
5041 }
5042
5043
5044 /**
5045  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
5046  * @phba: Pointer to HBA context object.
5047  *
5048  * This function is called from worker thread when a mailbox command times out.
5049  * The caller is not required to hold any locks. This function will reset the
5050  * HBA and recover all the pending commands.
5051  **/
5052 void
5053 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
5054 {
5055         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
5056         MAILBOX_t *mb = &pmbox->u.mb;
5057         struct lpfc_sli *psli = &phba->sli;
5058         struct lpfc_sli_ring *pring;
5059
5060         /* Check the pmbox pointer first.  There is a race condition
5061          * between the mbox timeout handler getting executed in the
5062          * worklist and the mailbox actually completing. When this
5063          * race condition occurs, the mbox_active will be NULL.
5064          */
5065         spin_lock_irq(&phba->hbalock);
5066         if (pmbox == NULL) {
5067                 lpfc_printf_log(phba, KERN_WARNING,
5068                                 LOG_MBOX | LOG_SLI,
5069                                 "0353 Active Mailbox cleared - mailbox timeout "
5070                                 "exiting\n");
5071                 spin_unlock_irq(&phba->hbalock);
5072                 return;
5073         }
5074
5075         /* Mbox cmd <mbxCommand> timeout */
5076         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5077                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
5078                         mb->mbxCommand,
5079                         phba->pport->port_state,
5080                         phba->sli.sli_flag,
5081                         phba->sli.mbox_active);
5082         spin_unlock_irq(&phba->hbalock);
5083
5084         /* Setting state unknown so lpfc_sli_abort_iocb_ring
5085          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
5086          * it to fail all oustanding SCSI IO.
5087          */
5088         spin_lock_irq(&phba->pport->work_port_lock);
5089         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5090         spin_unlock_irq(&phba->pport->work_port_lock);
5091         spin_lock_irq(&phba->hbalock);
5092         phba->link_state = LPFC_LINK_UNKNOWN;
5093         psli->sli_flag &= ~LPFC_SLI_ACTIVE;
5094         spin_unlock_irq(&phba->hbalock);
5095
5096         pring = &psli->ring[psli->fcp_ring];
5097         lpfc_sli_abort_iocb_ring(phba, pring);
5098
5099         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5100                         "0345 Resetting board due to mailbox timeout\n");
5101
5102         /* Reset the HBA device */
5103         lpfc_reset_hba(phba);
5104 }
5105
5106 /**
5107  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
5108  * @phba: Pointer to HBA context object.
5109  * @pmbox: Pointer to mailbox object.
5110  * @flag: Flag indicating how the mailbox need to be processed.
5111  *
5112  * This function is called by discovery code and HBA management code
5113  * to submit a mailbox command to firmware with SLI-3 interface spec. This
5114  * function gets the hbalock to protect the data structures.
5115  * The mailbox command can be submitted in polling mode, in which case
5116  * this function will wait in a polling loop for the completion of the
5117  * mailbox.
5118  * If the mailbox is submitted in no_wait mode (not polling) the
5119  * function will submit the command and returns immediately without waiting
5120  * for the mailbox completion. The no_wait is supported only when HBA
5121  * is in SLI2/SLI3 mode - interrupts are enabled.
5122  * The SLI interface allows only one mailbox pending at a time. If the
5123  * mailbox is issued in polling mode and there is already a mailbox
5124  * pending, then the function will return an error. If the mailbox is issued
5125  * in NO_WAIT mode and there is a mailbox pending already, the function
5126  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
5127  * The sli layer owns the mailbox object until the completion of mailbox
5128  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
5129  * return codes the caller owns the mailbox command after the return of
5130  * the function.
5131  **/
5132 static int
5133 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
5134                        uint32_t flag)
5135 {
5136         MAILBOX_t *mb;
5137         struct lpfc_sli *psli = &phba->sli;
5138         uint32_t status, evtctr;
5139         uint32_t ha_copy;
5140         int i;
5141         unsigned long timeout;
5142         unsigned long drvr_flag = 0;
5143         uint32_t word0, ldata;
5144         void __iomem *to_slim;
5145         int processing_queue = 0;
5146
5147         spin_lock_irqsave(&phba->hbalock, drvr_flag);
5148         if (!pmbox) {
5149                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5150                 /* processing mbox queue from intr_handler */
5151                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5152                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5153                         return MBX_SUCCESS;
5154                 }
5155                 processing_queue = 1;
5156                 pmbox = lpfc_mbox_get(phba);
5157                 if (!pmbox) {
5158                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5159                         return MBX_SUCCESS;
5160                 }
5161         }
5162
5163         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
5164                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
5165                 if(!pmbox->vport) {
5166                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5167                         lpfc_printf_log(phba, KERN_ERR,
5168                                         LOG_MBOX | LOG_VPORT,
5169                                         "1806 Mbox x%x failed. No vport\n",
5170                                         pmbox->u.mb.mbxCommand);
5171                         dump_stack();
5172                         goto out_not_finished;
5173                 }
5174         }
5175
5176         /* If the PCI channel is in offline state, do not post mbox. */
5177         if (unlikely(pci_channel_offline(phba->pcidev))) {
5178                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5179                 goto out_not_finished;
5180         }
5181
5182         /* If HBA has a deferred error attention, fail the iocb. */
5183         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
5184                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5185                 goto out_not_finished;
5186         }
5187
5188         psli = &phba->sli;
5189
5190         mb = &pmbox->u.mb;
5191         status = MBX_SUCCESS;
5192
5193         if (phba->link_state == LPFC_HBA_ERROR) {
5194                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5195
5196                 /* Mbox command <mbxCommand> cannot issue */
5197                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5198                                 "(%d):0311 Mailbox command x%x cannot "
5199                                 "issue Data: x%x x%x\n",
5200                                 pmbox->vport ? pmbox->vport->vpi : 0,
5201                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5202                 goto out_not_finished;
5203         }
5204
5205         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
5206             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
5207                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5208                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5209                                 "(%d):2528 Mailbox command x%x cannot "
5210                                 "issue Data: x%x x%x\n",
5211                                 pmbox->vport ? pmbox->vport->vpi : 0,
5212                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
5213                 goto out_not_finished;
5214         }
5215
5216         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5217                 /* Polling for a mbox command when another one is already active
5218                  * is not allowed in SLI. Also, the driver must have established
5219                  * SLI2 mode to queue and process multiple mbox commands.
5220                  */
5221
5222                 if (flag & MBX_POLL) {
5223                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5224
5225                         /* Mbox command <mbxCommand> cannot issue */
5226                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5227                                         "(%d):2529 Mailbox command x%x "
5228                                         "cannot issue Data: x%x x%x\n",
5229                                         pmbox->vport ? pmbox->vport->vpi : 0,
5230                                         pmbox->u.mb.mbxCommand,
5231                                         psli->sli_flag, flag);
5232                         goto out_not_finished;
5233                 }
5234
5235                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
5236                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5237                         /* Mbox command <mbxCommand> cannot issue */
5238                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5239                                         "(%d):2530 Mailbox command x%x "
5240                                         "cannot issue Data: x%x x%x\n",
5241                                         pmbox->vport ? pmbox->vport->vpi : 0,
5242                                         pmbox->u.mb.mbxCommand,
5243                                         psli->sli_flag, flag);
5244                         goto out_not_finished;
5245                 }
5246
5247                 /* Another mailbox command is still being processed, queue this
5248                  * command to be processed later.
5249                  */
5250                 lpfc_mbox_put(phba, pmbox);
5251
5252                 /* Mbox cmd issue - BUSY */
5253                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5254                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
5255                                 "x%x x%x x%x x%x\n",
5256                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
5257                                 mb->mbxCommand, phba->pport->port_state,
5258                                 psli->sli_flag, flag);
5259
5260                 psli->slistat.mbox_busy++;
5261                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5262
5263                 if (pmbox->vport) {
5264                         lpfc_debugfs_disc_trc(pmbox->vport,
5265                                 LPFC_DISC_TRC_MBOX_VPORT,
5266                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
5267                                 (uint32_t)mb->mbxCommand,
5268                                 mb->un.varWords[0], mb->un.varWords[1]);
5269                 }
5270                 else {
5271                         lpfc_debugfs_disc_trc(phba->pport,
5272                                 LPFC_DISC_TRC_MBOX,
5273                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
5274                                 (uint32_t)mb->mbxCommand,
5275                                 mb->un.varWords[0], mb->un.varWords[1]);
5276                 }
5277
5278                 return MBX_BUSY;
5279         }
5280
5281         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5282
5283         /* If we are not polling, we MUST be in SLI2 mode */
5284         if (flag != MBX_POLL) {
5285                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
5286                     (mb->mbxCommand != MBX_KILL_BOARD)) {
5287                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5288                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5289                         /* Mbox command <mbxCommand> cannot issue */
5290                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5291                                         "(%d):2531 Mailbox command x%x "
5292                                         "cannot issue Data: x%x x%x\n",
5293                                         pmbox->vport ? pmbox->vport->vpi : 0,
5294                                         pmbox->u.mb.mbxCommand,
5295                                         psli->sli_flag, flag);
5296                         goto out_not_finished;
5297                 }
5298                 /* timeout active mbox command */
5299                 mod_timer(&psli->mbox_tmo, (jiffies +
5300                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
5301         }
5302
5303         /* Mailbox cmd <cmd> issue */
5304         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5305                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
5306                         "x%x\n",
5307                         pmbox->vport ? pmbox->vport->vpi : 0,
5308                         mb->mbxCommand, phba->pport->port_state,
5309                         psli->sli_flag, flag);
5310
5311         if (mb->mbxCommand != MBX_HEARTBEAT) {
5312                 if (pmbox->vport) {
5313                         lpfc_debugfs_disc_trc(pmbox->vport,
5314                                 LPFC_DISC_TRC_MBOX_VPORT,
5315                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5316                                 (uint32_t)mb->mbxCommand,
5317                                 mb->un.varWords[0], mb->un.varWords[1]);
5318                 }
5319                 else {
5320                         lpfc_debugfs_disc_trc(phba->pport,
5321                                 LPFC_DISC_TRC_MBOX,
5322                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
5323                                 (uint32_t)mb->mbxCommand,
5324                                 mb->un.varWords[0], mb->un.varWords[1]);
5325                 }
5326         }
5327
5328         psli->slistat.mbox_cmd++;
5329         evtctr = psli->slistat.mbox_event;
5330
5331         /* next set own bit for the adapter and copy over command word */
5332         mb->mbxOwner = OWN_CHIP;
5333
5334         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5335                 /* Populate mbox extension offset word. */
5336                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
5337                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5338                                 = (uint8_t *)phba->mbox_ext
5339                                   - (uint8_t *)phba->mbox;
5340                 }
5341
5342                 /* Copy the mailbox extension data */
5343                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5344                         lpfc_sli_pcimem_bcopy(pmbox->context2,
5345                                 (uint8_t *)phba->mbox_ext,
5346                                 pmbox->in_ext_byte_len);
5347                 }
5348                 /* Copy command data to host SLIM area */
5349                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5350         } else {
5351                 /* Populate mbox extension offset word. */
5352                 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
5353                         *(((uint32_t *)mb) + pmbox->mbox_offset_word)
5354                                 = MAILBOX_HBA_EXT_OFFSET;
5355
5356                 /* Copy the mailbox extension data */
5357                 if (pmbox->in_ext_byte_len && pmbox->context2) {
5358                         lpfc_memcpy_to_slim(phba->MBslimaddr +
5359                                 MAILBOX_HBA_EXT_OFFSET,
5360                                 pmbox->context2, pmbox->in_ext_byte_len);
5361
5362                 }
5363                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5364                         /* copy command data into host mbox for cmpl */
5365                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
5366                 }
5367
5368                 /* First copy mbox command data to HBA SLIM, skip past first
5369                    word */
5370                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5371                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5372                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
5373
5374                 /* Next copy over first word, with mbxOwner set */
5375                 ldata = *((uint32_t *)mb);
5376                 to_slim = phba->MBslimaddr;
5377                 writel(ldata, to_slim);
5378                 readl(to_slim); /* flush */
5379
5380                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5381                         /* switch over to host mailbox */
5382                         psli->sli_flag |= LPFC_SLI_ACTIVE;
5383                 }
5384         }
5385
5386         wmb();
5387
5388         switch (flag) {
5389         case MBX_NOWAIT:
5390                 /* Set up reference to mailbox command */
5391                 psli->mbox_active = pmbox;
5392                 /* Interrupt board to do it */
5393                 writel(CA_MBATT, phba->CAregaddr);
5394                 readl(phba->CAregaddr); /* flush */
5395                 /* Don't wait for it to finish, just return */
5396                 break;
5397
5398         case MBX_POLL:
5399                 /* Set up null reference to mailbox command */
5400                 psli->mbox_active = NULL;
5401                 /* Interrupt board to do it */
5402                 writel(CA_MBATT, phba->CAregaddr);
5403                 readl(phba->CAregaddr); /* flush */
5404
5405                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5406                         /* First read mbox status word */
5407                         word0 = *((uint32_t *)phba->mbox);
5408                         word0 = le32_to_cpu(word0);
5409                 } else {
5410                         /* First read mbox status word */
5411                         word0 = readl(phba->MBslimaddr);
5412                 }
5413
5414                 /* Read the HBA Host Attention Register */
5415                 ha_copy = readl(phba->HAregaddr);
5416                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5417                                                              mb->mbxCommand) *
5418                                            1000) + jiffies;
5419                 i = 0;
5420                 /* Wait for command to complete */
5421                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5422                        (!(ha_copy & HA_MBATT) &&
5423                         (phba->link_state > LPFC_WARM_START))) {
5424                         if (time_after(jiffies, timeout)) {
5425                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5426                                 spin_unlock_irqrestore(&phba->hbalock,
5427                                                        drvr_flag);
5428                                 goto out_not_finished;
5429                         }
5430
5431                         /* Check if we took a mbox interrupt while we were
5432                            polling */
5433                         if (((word0 & OWN_CHIP) != OWN_CHIP)
5434                             && (evtctr != psli->slistat.mbox_event))
5435                                 break;
5436
5437                         if (i++ > 10) {
5438                                 spin_unlock_irqrestore(&phba->hbalock,
5439                                                        drvr_flag);
5440                                 msleep(1);
5441                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5442                         }
5443
5444                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5445                                 /* First copy command data */
5446                                 word0 = *((uint32_t *)phba->mbox);
5447                                 word0 = le32_to_cpu(word0);
5448                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5449                                         MAILBOX_t *slimmb;
5450                                         uint32_t slimword0;
5451                                         /* Check real SLIM for any errors */
5452                                         slimword0 = readl(phba->MBslimaddr);
5453                                         slimmb = (MAILBOX_t *) & slimword0;
5454                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5455                                             && slimmb->mbxStatus) {
5456                                                 psli->sli_flag &=
5457                                                     ~LPFC_SLI_ACTIVE;
5458                                                 word0 = slimword0;
5459                                         }
5460                                 }
5461                         } else {
5462                                 /* First copy command data */
5463                                 word0 = readl(phba->MBslimaddr);
5464                         }
5465                         /* Read the HBA Host Attention Register */
5466                         ha_copy = readl(phba->HAregaddr);
5467                 }
5468
5469                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
5470                         /* copy results back to user */
5471                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
5472                         /* Copy the mailbox extension data */
5473                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5474                                 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5475                                                       pmbox->context2,
5476                                                       pmbox->out_ext_byte_len);
5477                         }
5478                 } else {
5479                         /* First copy command data */
5480                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5481                                                         MAILBOX_CMD_SIZE);
5482                         /* Copy the mailbox extension data */
5483                         if (pmbox->out_ext_byte_len && pmbox->context2) {
5484                                 lpfc_memcpy_from_slim(pmbox->context2,
5485                                         phba->MBslimaddr +
5486                                         MAILBOX_HBA_EXT_OFFSET,
5487                                         pmbox->out_ext_byte_len);
5488                         }
5489                 }
5490
5491                 writel(HA_MBATT, phba->HAregaddr);
5492                 readl(phba->HAregaddr); /* flush */
5493
5494                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5495                 status = mb->mbxStatus;
5496         }
5497
5498         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5499         return status;
5500
5501 out_not_finished:
5502         if (processing_queue) {
5503                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5504                 lpfc_mbox_cmpl_put(phba, pmbox);
5505         }
5506         return MBX_NOT_FINISHED;
5507 }
5508
5509 /**
5510  * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5511  * @phba: Pointer to HBA context object.
5512  *
5513  * The function blocks the posting of SLI4 asynchronous mailbox commands from
5514  * the driver internal pending mailbox queue. It will then try to wait out the
5515  * possible outstanding mailbox command before return.
5516  *
5517  * Returns:
5518  *      0 - the outstanding mailbox command completed; otherwise, the wait for
5519  *      the outstanding mailbox command timed out.
5520  **/
5521 static int
5522 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5523 {
5524         struct lpfc_sli *psli = &phba->sli;
5525         uint8_t actcmd = MBX_HEARTBEAT;
5526         int rc = 0;
5527         unsigned long timeout;
5528
5529         /* Mark the asynchronous mailbox command posting as blocked */
5530         spin_lock_irq(&phba->hbalock);
5531         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5532         if (phba->sli.mbox_active)
5533                 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5534         spin_unlock_irq(&phba->hbalock);
5535         /* Determine how long we might wait for the active mailbox
5536          * command to be gracefully completed by firmware.
5537          */
5538         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5539                                    jiffies;
5540         /* Wait for the outstnading mailbox command to complete */
5541         while (phba->sli.mbox_active) {
5542                 /* Check active mailbox complete status every 2ms */
5543                 msleep(2);
5544                 if (time_after(jiffies, timeout)) {
5545                         /* Timeout, marked the outstanding cmd not complete */
5546                         rc = 1;
5547                         break;
5548                 }
5549         }
5550
5551         /* Can not cleanly block async mailbox command, fails it */
5552         if (rc) {
5553                 spin_lock_irq(&phba->hbalock);
5554                 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5555                 spin_unlock_irq(&phba->hbalock);
5556         }
5557         return rc;
5558 }
5559
5560 /**
5561  * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5562  * @phba: Pointer to HBA context object.
5563  *
5564  * The function unblocks and resume posting of SLI4 asynchronous mailbox
5565  * commands from the driver internal pending mailbox queue. It makes sure
5566  * that there is no outstanding mailbox command before resuming posting
5567  * asynchronous mailbox commands. If, for any reason, there is outstanding
5568  * mailbox command, it will try to wait it out before resuming asynchronous
5569  * mailbox command posting.
5570  **/
5571 static void
5572 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5573 {
5574         struct lpfc_sli *psli = &phba->sli;
5575
5576         spin_lock_irq(&phba->hbalock);
5577         if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5578                 /* Asynchronous mailbox posting is not blocked, do nothing */
5579                 spin_unlock_irq(&phba->hbalock);
5580                 return;
5581         }
5582
5583         /* Outstanding synchronous mailbox command is guaranteed to be done,
5584          * successful or timeout, after timing-out the outstanding mailbox
5585          * command shall always be removed, so just unblock posting async
5586          * mailbox command and resume
5587          */
5588         psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5589         spin_unlock_irq(&phba->hbalock);
5590
5591         /* wake up worker thread to post asynchronlous mailbox command */
5592         lpfc_worker_wake_up(phba);
5593 }
5594
5595 /**
5596  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5597  * @phba: Pointer to HBA context object.
5598  * @mboxq: Pointer to mailbox object.
5599  *
5600  * The function posts a mailbox to the port.  The mailbox is expected
5601  * to be comletely filled in and ready for the port to operate on it.
5602  * This routine executes a synchronous completion operation on the
5603  * mailbox by polling for its completion.
5604  *
5605  * The caller must not be holding any locks when calling this routine.
5606  *
5607  * Returns:
5608  *      MBX_SUCCESS - mailbox posted successfully
5609  *      Any of the MBX error values.
5610  **/
5611 static int
5612 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5613 {
5614         int rc = MBX_SUCCESS;
5615         unsigned long iflag;
5616         uint32_t db_ready;
5617         uint32_t mcqe_status;
5618         uint32_t mbx_cmnd;
5619         unsigned long timeout;
5620         struct lpfc_sli *psli = &phba->sli;
5621         struct lpfc_mqe *mb = &mboxq->u.mqe;
5622         struct lpfc_bmbx_create *mbox_rgn;
5623         struct dma_address *dma_address;
5624         struct lpfc_register bmbx_reg;
5625
5626         /*
5627          * Only one mailbox can be active to the bootstrap mailbox region
5628          * at a time and there is no queueing provided.
5629          */
5630         spin_lock_irqsave(&phba->hbalock, iflag);
5631         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5632                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5633                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5634                                 "(%d):2532 Mailbox command x%x (x%x) "
5635                                 "cannot issue Data: x%x x%x\n",
5636                                 mboxq->vport ? mboxq->vport->vpi : 0,
5637                                 mboxq->u.mb.mbxCommand,
5638                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5639                                 psli->sli_flag, MBX_POLL);
5640                 return MBXERR_ERROR;
5641         }
5642         /* The server grabs the token and owns it until release */
5643         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5644         phba->sli.mbox_active = mboxq;
5645         spin_unlock_irqrestore(&phba->hbalock, iflag);
5646
5647         /*
5648          * Initialize the bootstrap memory region to avoid stale data areas
5649          * in the mailbox post.  Then copy the caller's mailbox contents to
5650          * the bmbx mailbox region.
5651          */
5652         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5653         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5654         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5655                               sizeof(struct lpfc_mqe));
5656
5657         /* Post the high mailbox dma address to the port and wait for ready. */
5658         dma_address = &phba->sli4_hba.bmbx.dma_address;
5659         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5660
5661         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5662                                    * 1000) + jiffies;
5663         do {
5664                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5665                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5666                 if (!db_ready)
5667                         msleep(2);
5668
5669                 if (time_after(jiffies, timeout)) {
5670                         rc = MBXERR_ERROR;
5671                         goto exit;
5672                 }
5673         } while (!db_ready);
5674
5675         /* Post the low mailbox dma address to the port. */
5676         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5677         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5678                                    * 1000) + jiffies;
5679         do {
5680                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5681                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5682                 if (!db_ready)
5683                         msleep(2);
5684
5685                 if (time_after(jiffies, timeout)) {
5686                         rc = MBXERR_ERROR;
5687                         goto exit;
5688                 }
5689         } while (!db_ready);
5690
5691         /*
5692          * Read the CQ to ensure the mailbox has completed.
5693          * If so, update the mailbox status so that the upper layers
5694          * can complete the request normally.
5695          */
5696         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5697                               sizeof(struct lpfc_mqe));
5698         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5699         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5700                               sizeof(struct lpfc_mcqe));
5701         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5702
5703         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5704         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5705                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5706                 rc = MBXERR_ERROR;
5707         } else
5708                 lpfc_sli4_swap_str(phba, mboxq);
5709
5710         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5711                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5712                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5713                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5714                         mboxq->vport ? mboxq->vport->vpi : 0,
5715                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5716                         bf_get(lpfc_mqe_status, mb),
5717                         mb->un.mb_words[0], mb->un.mb_words[1],
5718                         mb->un.mb_words[2], mb->un.mb_words[3],
5719                         mb->un.mb_words[4], mb->un.mb_words[5],
5720                         mb->un.mb_words[6], mb->un.mb_words[7],
5721                         mb->un.mb_words[8], mb->un.mb_words[9],
5722                         mb->un.mb_words[10], mb->un.mb_words[11],
5723                         mb->un.mb_words[12], mboxq->mcqe.word0,
5724                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5725                         mboxq->mcqe.trailer);
5726 exit:
5727         /* We are holding the token, no needed for lock when release */
5728         spin_lock_irqsave(&phba->hbalock, iflag);
5729         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5730         phba->sli.mbox_active = NULL;
5731         spin_unlock_irqrestore(&phba->hbalock, iflag);
5732         return rc;
5733 }
5734
5735 /**
5736  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5737  * @phba: Pointer to HBA context object.
5738  * @pmbox: Pointer to mailbox object.
5739  * @flag: Flag indicating how the mailbox need to be processed.
5740  *
5741  * This function is called by discovery code and HBA management code to submit
5742  * a mailbox command to firmware with SLI-4 interface spec.
5743  *
5744  * Return codes the caller owns the mailbox command after the return of the
5745  * function.
5746  **/
5747 static int
5748 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5749                        uint32_t flag)
5750 {
5751         struct lpfc_sli *psli = &phba->sli;
5752         unsigned long iflags;
5753         int rc;
5754
5755         rc = lpfc_mbox_dev_check(phba);
5756         if (unlikely(rc)) {
5757                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5758                                 "(%d):2544 Mailbox command x%x (x%x) "
5759                                 "cannot issue Data: x%x x%x\n",
5760                                 mboxq->vport ? mboxq->vport->vpi : 0,
5761                                 mboxq->u.mb.mbxCommand,
5762                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5763                                 psli->sli_flag, flag);
5764                 goto out_not_finished;
5765         }
5766
5767         /* Detect polling mode and jump to a handler */
5768         if (!phba->sli4_hba.intr_enable) {
5769                 if (flag == MBX_POLL)
5770                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5771                 else
5772                         rc = -EIO;
5773                 if (rc != MBX_SUCCESS)
5774                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5775                                         "(%d):2541 Mailbox command x%x "
5776                                         "(x%x) cannot issue Data: x%x x%x\n",
5777                                         mboxq->vport ? mboxq->vport->vpi : 0,
5778                                         mboxq->u.mb.mbxCommand,
5779                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5780                                         psli->sli_flag, flag);
5781                 return rc;
5782         } else if (flag == MBX_POLL) {
5783                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5784                                 "(%d):2542 Try to issue mailbox command "
5785                                 "x%x (x%x) synchronously ahead of async"
5786                                 "mailbox command queue: x%x x%x\n",
5787                                 mboxq->vport ? mboxq->vport->vpi : 0,
5788                                 mboxq->u.mb.mbxCommand,
5789                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5790                                 psli->sli_flag, flag);
5791                 /* Try to block the asynchronous mailbox posting */
5792                 rc = lpfc_sli4_async_mbox_block(phba);
5793                 if (!rc) {
5794                         /* Successfully blocked, now issue sync mbox cmd */
5795                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5796                         if (rc != MBX_SUCCESS)
5797                                 lpfc_printf_log(phba, KERN_ERR,
5798                                                 LOG_MBOX | LOG_SLI,
5799                                                 "(%d):2597 Mailbox command "
5800                                                 "x%x (x%x) cannot issue "
5801                                                 "Data: x%x x%x\n",
5802                                                 mboxq->vport ?
5803                                                 mboxq->vport->vpi : 0,
5804                                                 mboxq->u.mb.mbxCommand,
5805                                                 lpfc_sli4_mbox_opcode_get(phba,
5806                                                                 mboxq),
5807                                                 psli->sli_flag, flag);
5808                         /* Unblock the async mailbox posting afterward */
5809                         lpfc_sli4_async_mbox_unblock(phba);
5810                 }
5811                 return rc;
5812         }
5813
5814         /* Now, interrupt mode asynchrous mailbox command */
5815         rc = lpfc_mbox_cmd_check(phba, mboxq);
5816         if (rc) {
5817                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5818                                 "(%d):2543 Mailbox command x%x (x%x) "
5819                                 "cannot issue Data: x%x x%x\n",
5820                                 mboxq->vport ? mboxq->vport->vpi : 0,
5821                                 mboxq->u.mb.mbxCommand,
5822                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5823                                 psli->sli_flag, flag);
5824                 goto out_not_finished;
5825         }
5826
5827         /* Put the mailbox command to the driver internal FIFO */
5828         psli->slistat.mbox_busy++;
5829         spin_lock_irqsave(&phba->hbalock, iflags);
5830         lpfc_mbox_put(phba, mboxq);
5831         spin_unlock_irqrestore(&phba->hbalock, iflags);
5832         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5833                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5834                         "x%x (x%x) x%x x%x x%x\n",
5835                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5836                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5837                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5838                         phba->pport->port_state,
5839                         psli->sli_flag, MBX_NOWAIT);
5840         /* Wake up worker thread to transport mailbox command from head */
5841         lpfc_worker_wake_up(phba);
5842
5843         return MBX_BUSY;
5844
5845 out_not_finished:
5846         return MBX_NOT_FINISHED;
5847 }
5848
5849 /**
5850  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5851  * @phba: Pointer to HBA context object.
5852  *
5853  * This function is called by worker thread to send a mailbox command to
5854  * SLI4 HBA firmware.
5855  *
5856  **/
5857 int
5858 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5859 {
5860         struct lpfc_sli *psli = &phba->sli;
5861         LPFC_MBOXQ_t *mboxq;
5862         int rc = MBX_SUCCESS;
5863         unsigned long iflags;
5864         struct lpfc_mqe *mqe;
5865         uint32_t mbx_cmnd;
5866
5867         /* Check interrupt mode before post async mailbox command */
5868         if (unlikely(!phba->sli4_hba.intr_enable))
5869                 return MBX_NOT_FINISHED;
5870
5871         /* Check for mailbox command service token */
5872         spin_lock_irqsave(&phba->hbalock, iflags);
5873         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5874                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5875                 return MBX_NOT_FINISHED;
5876         }
5877         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5878                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5879                 return MBX_NOT_FINISHED;
5880         }
5881         if (unlikely(phba->sli.mbox_active)) {
5882                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5883                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5884                                 "0384 There is pending active mailbox cmd\n");
5885                 return MBX_NOT_FINISHED;
5886         }
5887         /* Take the mailbox command service token */
5888         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5889
5890         /* Get the next mailbox command from head of queue */
5891         mboxq = lpfc_mbox_get(phba);
5892
5893         /* If no more mailbox command waiting for post, we're done */
5894         if (!mboxq) {
5895                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5896                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5897                 return MBX_SUCCESS;
5898         }
5899         phba->sli.mbox_active = mboxq;
5900         spin_unlock_irqrestore(&phba->hbalock, iflags);
5901
5902         /* Check device readiness for posting mailbox command */
5903         rc = lpfc_mbox_dev_check(phba);
5904         if (unlikely(rc))
5905                 /* Driver clean routine will clean up pending mailbox */
5906                 goto out_not_finished;
5907
5908         /* Prepare the mbox command to be posted */
5909         mqe = &mboxq->u.mqe;
5910         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5911
5912         /* Start timer for the mbox_tmo and log some mailbox post messages */
5913         mod_timer(&psli->mbox_tmo, (jiffies +
5914                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5915
5916         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5917                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5918                         "x%x x%x\n",
5919                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5920                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5921                         phba->pport->port_state, psli->sli_flag);
5922
5923         if (mbx_cmnd != MBX_HEARTBEAT) {
5924                 if (mboxq->vport) {
5925                         lpfc_debugfs_disc_trc(mboxq->vport,
5926                                 LPFC_DISC_TRC_MBOX_VPORT,
5927                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5928                                 mbx_cmnd, mqe->un.mb_words[0],
5929                                 mqe->un.mb_words[1]);
5930                 } else {
5931                         lpfc_debugfs_disc_trc(phba->pport,
5932                                 LPFC_DISC_TRC_MBOX,
5933                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5934                                 mbx_cmnd, mqe->un.mb_words[0],
5935                                 mqe->un.mb_words[1]);
5936                 }
5937         }
5938         psli->slistat.mbox_cmd++;
5939
5940         /* Post the mailbox command to the port */
5941         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5942         if (rc != MBX_SUCCESS) {
5943                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5944                                 "(%d):2533 Mailbox command x%x (x%x) "
5945                                 "cannot issue Data: x%x x%x\n",
5946                                 mboxq->vport ? mboxq->vport->vpi : 0,
5947                                 mboxq->u.mb.mbxCommand,
5948                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5949                                 psli->sli_flag, MBX_NOWAIT);
5950                 goto out_not_finished;
5951         }
5952
5953         return rc;
5954
5955 out_not_finished:
5956         spin_lock_irqsave(&phba->hbalock, iflags);
5957         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5958         __lpfc_mbox_cmpl_put(phba, mboxq);
5959         /* Release the token */
5960         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5961         phba->sli.mbox_active = NULL;
5962         spin_unlock_irqrestore(&phba->hbalock, iflags);
5963
5964         return MBX_NOT_FINISHED;
5965 }
5966
5967 /**
5968  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5969  * @phba: Pointer to HBA context object.
5970  * @pmbox: Pointer to mailbox object.
5971  * @flag: Flag indicating how the mailbox need to be processed.
5972  *
5973  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5974  * the API jump table function pointer from the lpfc_hba struct.
5975  *
5976  * Return codes the caller owns the mailbox command after the return of the
5977  * function.
5978  **/
5979 int
5980 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5981 {
5982         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5983 }
5984
5985 /**
5986  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5987  * @phba: The hba struct for which this call is being executed.
5988  * @dev_grp: The HBA PCI-Device group number.
5989  *
5990  * This routine sets up the mbox interface API function jump table in @phba
5991  * struct.
5992  * Returns: 0 - success, -ENODEV - failure.
5993  **/
5994 int
5995 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5996 {
5997
5998         switch (dev_grp) {
5999         case LPFC_PCI_DEV_LP:
6000                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
6001                 phba->lpfc_sli_handle_slow_ring_event =
6002                                 lpfc_sli_handle_slow_ring_event_s3;
6003                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
6004                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
6005                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
6006                 break;
6007         case LPFC_PCI_DEV_OC:
6008                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
6009                 phba->lpfc_sli_handle_slow_ring_event =
6010                                 lpfc_sli_handle_slow_ring_event_s4;
6011                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
6012                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
6013                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
6014                 break;
6015         default:
6016                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6017                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
6018                                 dev_grp);
6019                 return -ENODEV;
6020                 break;
6021         }
6022         return 0;
6023 }
6024
6025 /**
6026  * __lpfc_sli_ringtx_put - Add an iocb to the txq
6027  * @phba: Pointer to HBA context object.
6028  * @pring: Pointer to driver SLI ring object.
6029  * @piocb: Pointer to address of newly added command iocb.
6030  *
6031  * This function is called with hbalock held to add a command
6032  * iocb to the txq when SLI layer cannot submit the command iocb
6033  * to the ring.
6034  **/
6035 void
6036 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6037                     struct lpfc_iocbq *piocb)
6038 {
6039         /* Insert the caller's iocb in the txq tail for later processing. */
6040         list_add_tail(&piocb->list, &pring->txq);
6041         pring->txq_cnt++;
6042 }
6043
6044 /**
6045  * lpfc_sli_next_iocb - Get the next iocb in the txq
6046  * @phba: Pointer to HBA context object.
6047  * @pring: Pointer to driver SLI ring object.
6048  * @piocb: Pointer to address of newly added command iocb.
6049  *
6050  * This function is called with hbalock held before a new
6051  * iocb is submitted to the firmware. This function checks
6052  * txq to flush the iocbs in txq to Firmware before
6053  * submitting new iocbs to the Firmware.
6054  * If there are iocbs in the txq which need to be submitted
6055  * to firmware, lpfc_sli_next_iocb returns the first element
6056  * of the txq after dequeuing it from txq.
6057  * If there is no iocb in the txq then the function will return
6058  * *piocb and *piocb is set to NULL. Caller needs to check
6059  * *piocb to find if there are more commands in the txq.
6060  **/
6061 static struct lpfc_iocbq *
6062 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6063                    struct lpfc_iocbq **piocb)
6064 {
6065         struct lpfc_iocbq * nextiocb;
6066
6067         nextiocb = lpfc_sli_ringtx_get(phba, pring);
6068         if (!nextiocb) {
6069                 nextiocb = *piocb;
6070                 *piocb = NULL;
6071         }
6072
6073         return nextiocb;
6074 }
6075
6076 /**
6077  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
6078  * @phba: Pointer to HBA context object.
6079  * @ring_number: SLI ring number to issue iocb on.
6080  * @piocb: Pointer to command iocb.
6081  * @flag: Flag indicating if this command can be put into txq.
6082  *
6083  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
6084  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
6085  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
6086  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
6087  * this function allows only iocbs for posting buffers. This function finds
6088  * next available slot in the command ring and posts the command to the
6089  * available slot and writes the port attention register to request HBA start
6090  * processing new iocb. If there is no slot available in the ring and
6091  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
6092  * the function returns IOCB_BUSY.
6093  *
6094  * This function is called with hbalock held. The function will return success
6095  * after it successfully submit the iocb to firmware or after adding to the
6096  * txq.
6097  **/
6098 static int
6099 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
6100                     struct lpfc_iocbq *piocb, uint32_t flag)
6101 {
6102         struct lpfc_iocbq *nextiocb;
6103         IOCB_t *iocb;
6104         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6105
6106         if (piocb->iocb_cmpl && (!piocb->vport) &&
6107            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
6108            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
6109                 lpfc_printf_log(phba, KERN_ERR,
6110                                 LOG_SLI | LOG_VPORT,
6111                                 "1807 IOCB x%x failed. No vport\n",
6112                                 piocb->iocb.ulpCommand);
6113                 dump_stack();
6114                 return IOCB_ERROR;
6115         }
6116
6117
6118         /* If the PCI channel is in offline state, do not post iocbs. */
6119         if (unlikely(pci_channel_offline(phba->pcidev)))
6120                 return IOCB_ERROR;
6121
6122         /* If HBA has a deferred error attention, fail the iocb. */
6123         if (unlikely(phba->hba_flag & DEFER_ERATT))
6124                 return IOCB_ERROR;
6125
6126         /*
6127          * We should never get an IOCB if we are in a < LINK_DOWN state
6128          */
6129         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
6130                 return IOCB_ERROR;
6131
6132         /*
6133          * Check to see if we are blocking IOCB processing because of a
6134          * outstanding event.
6135          */
6136         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
6137                 goto iocb_busy;
6138
6139         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
6140                 /*
6141                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
6142                  * can be issued if the link is not up.
6143                  */
6144                 switch (piocb->iocb.ulpCommand) {
6145                 case CMD_GEN_REQUEST64_CR:
6146                 case CMD_GEN_REQUEST64_CX:
6147                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
6148                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6149                                         FC_RCTL_DD_UNSOL_CMD) ||
6150                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
6151                                         MENLO_TRANSPORT_TYPE))
6152
6153                                 goto iocb_busy;
6154                         break;
6155                 case CMD_QUE_RING_BUF_CN:
6156                 case CMD_QUE_RING_BUF64_CN:
6157                         /*
6158                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
6159                          * completion, iocb_cmpl MUST be 0.
6160                          */
6161                         if (piocb->iocb_cmpl)
6162                                 piocb->iocb_cmpl = NULL;
6163                         /*FALLTHROUGH*/
6164                 case CMD_CREATE_XRI_CR:
6165                 case CMD_CLOSE_XRI_CN:
6166                 case CMD_CLOSE_XRI_CX:
6167                         break;
6168                 default:
6169                         goto iocb_busy;
6170                 }
6171
6172         /*
6173          * For FCP commands, we must be in a state where we can process link
6174          * attention events.
6175          */
6176         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
6177                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
6178                 goto iocb_busy;
6179         }
6180
6181         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
6182                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
6183                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
6184
6185         if (iocb)
6186                 lpfc_sli_update_ring(phba, pring);
6187         else
6188                 lpfc_sli_update_full_ring(phba, pring);
6189
6190         if (!piocb)
6191                 return IOCB_SUCCESS;
6192
6193         goto out_busy;
6194
6195  iocb_busy:
6196         pring->stats.iocb_cmd_delay++;
6197
6198  out_busy:
6199
6200         if (!(flag & SLI_IOCB_RET_IOCB)) {
6201                 __lpfc_sli_ringtx_put(phba, pring, piocb);
6202                 return IOCB_SUCCESS;
6203         }
6204
6205         return IOCB_BUSY;
6206 }
6207
6208 /**
6209  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
6210  * @phba: Pointer to HBA context object.
6211  * @piocb: Pointer to command iocb.
6212  * @sglq: Pointer to the scatter gather queue object.
6213  *
6214  * This routine converts the bpl or bde that is in the IOCB
6215  * to a sgl list for the sli4 hardware. The physical address
6216  * of the bpl/bde is converted back to a virtual address.
6217  * If the IOCB contains a BPL then the list of BDE's is
6218  * converted to sli4_sge's. If the IOCB contains a single
6219  * BDE then it is converted to a single sli_sge.
6220  * The IOCB is still in cpu endianess so the contents of
6221  * the bpl can be used without byte swapping.
6222  *
6223  * Returns valid XRI = Success, NO_XRI = Failure.
6224 **/
6225 static uint16_t
6226 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
6227                 struct lpfc_sglq *sglq)
6228 {
6229         uint16_t xritag = NO_XRI;
6230         struct ulp_bde64 *bpl = NULL;
6231         struct ulp_bde64 bde;
6232         struct sli4_sge *sgl  = NULL;
6233         IOCB_t *icmd;
6234         int numBdes = 0;
6235         int i = 0;
6236         uint32_t offset = 0; /* accumulated offset in the sg request list */
6237         int inbound = 0; /* number of sg reply entries inbound from firmware */
6238
6239         if (!piocbq || !sglq)
6240                 return xritag;
6241
6242         sgl  = (struct sli4_sge *)sglq->sgl;
6243         icmd = &piocbq->iocb;
6244         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6245                 numBdes = icmd->un.genreq64.bdl.bdeSize /
6246                                 sizeof(struct ulp_bde64);
6247                 /* The addrHigh and addrLow fields within the IOCB
6248                  * have not been byteswapped yet so there is no
6249                  * need to swap them back.
6250                  */
6251                 bpl  = (struct ulp_bde64 *)
6252                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
6253
6254                 if (!bpl)
6255                         return xritag;
6256
6257                 for (i = 0; i < numBdes; i++) {
6258                         /* Should already be byte swapped. */
6259                         sgl->addr_hi = bpl->addrHigh;
6260                         sgl->addr_lo = bpl->addrLow;
6261
6262                         if ((i+1) == numBdes)
6263                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
6264                         else
6265                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
6266                         sgl->word2 = cpu_to_le32(sgl->word2);
6267                         /* swap the size field back to the cpu so we
6268                          * can assign it to the sgl.
6269                          */
6270                         bde.tus.w = le32_to_cpu(bpl->tus.w);
6271                         sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
6272                         /* The offsets in the sgl need to be accumulated
6273                          * separately for the request and reply lists.
6274                          * The request is always first, the reply follows.
6275                          */
6276                         if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
6277                                 /* add up the reply sg entries */
6278                                 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
6279                                         inbound++;
6280                                 /* first inbound? reset the offset */
6281                                 if (inbound == 1)
6282                                         offset = 0;
6283                                 bf_set(lpfc_sli4_sge_offset, sgl, offset);
6284                                 offset += bde.tus.f.bdeSize;
6285                         }
6286                         bpl++;
6287                         sgl++;
6288                 }
6289         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
6290                         /* The addrHigh and addrLow fields of the BDE have not
6291                          * been byteswapped yet so they need to be swapped
6292                          * before putting them in the sgl.
6293                          */
6294                         sgl->addr_hi =
6295                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
6296                         sgl->addr_lo =
6297                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
6298                         bf_set(lpfc_sli4_sge_last, sgl, 1);
6299                         sgl->word2 = cpu_to_le32(sgl->word2);
6300                         sgl->sge_len =
6301                                 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
6302         }
6303         return sglq->sli4_xritag;
6304 }
6305
6306 /**
6307  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
6308  * @phba: Pointer to HBA context object.
6309  *
6310  * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
6311  * distribution.  This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
6312  * held.
6313  *
6314  * Return: index into SLI4 fast-path FCP queue index.
6315  **/
6316 static uint32_t
6317 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
6318 {
6319         ++phba->fcp_qidx;
6320         if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
6321                 phba->fcp_qidx = 0;
6322
6323         return phba->fcp_qidx;
6324 }
6325
6326 /**
6327  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
6328  * @phba: Pointer to HBA context object.
6329  * @piocb: Pointer to command iocb.
6330  * @wqe: Pointer to the work queue entry.
6331  *
6332  * This routine converts the iocb command to its Work Queue Entry
6333  * equivalent. The wqe pointer should not have any fields set when
6334  * this routine is called because it will memcpy over them.
6335  * This routine does not set the CQ_ID or the WQEC bits in the
6336  * wqe.
6337  *
6338  * Returns: 0 = Success, IOCB_ERROR = Failure.
6339  **/
6340 static int
6341 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
6342                 union lpfc_wqe *wqe)
6343 {
6344         uint32_t xmit_len = 0, total_len = 0;
6345         uint8_t ct = 0;
6346         uint32_t fip;
6347         uint32_t abort_tag;
6348         uint8_t command_type = ELS_COMMAND_NON_FIP;
6349         uint8_t cmnd;
6350         uint16_t xritag;
6351         uint16_t abrt_iotag;
6352         struct lpfc_iocbq *abrtiocbq;
6353         struct ulp_bde64 *bpl = NULL;
6354         uint32_t els_id = LPFC_ELS_ID_DEFAULT;
6355         int numBdes, i;
6356         struct ulp_bde64 bde;
6357
6358         fip = phba->hba_flag & HBA_FIP_SUPPORT;
6359         /* The fcp commands will set command type */
6360         if (iocbq->iocb_flag &  LPFC_IO_FCP)
6361                 command_type = FCP_COMMAND;
6362         else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
6363                 command_type = ELS_COMMAND_FIP;
6364         else
6365                 command_type = ELS_COMMAND_NON_FIP;
6366
6367         /* Some of the fields are in the right position already */
6368         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
6369         abort_tag = (uint32_t) iocbq->iotag;
6370         xritag = iocbq->sli4_xritag;
6371         wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
6372         /* words0-2 bpl convert bde */
6373         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
6374                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6375                                 sizeof(struct ulp_bde64);
6376                 bpl  = (struct ulp_bde64 *)
6377                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6378                 if (!bpl)
6379                         return IOCB_ERROR;
6380
6381                 /* Should already be byte swapped. */
6382                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
6383                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
6384                 /* swap the size field back to the cpu so we
6385                  * can assign it to the sgl.
6386                  */
6387                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
6388                 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6389                 total_len = 0;
6390                 for (i = 0; i < numBdes; i++) {
6391                         bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
6392                         total_len += bde.tus.f.bdeSize;
6393                 }
6394         } else
6395                 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
6396
6397         iocbq->iocb.ulpIoTag = iocbq->iotag;
6398         cmnd = iocbq->iocb.ulpCommand;
6399
6400         switch (iocbq->iocb.ulpCommand) {
6401         case CMD_ELS_REQUEST64_CR:
6402                 if (!iocbq->iocb.ulpLe) {
6403                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6404                                 "2007 Only Limited Edition cmd Format"
6405                                 " supported 0x%x\n",
6406                                 iocbq->iocb.ulpCommand);
6407                         return IOCB_ERROR;
6408                 }
6409                 wqe->els_req.payload_len = xmit_len;
6410                 /* Els_reguest64 has a TMO */
6411                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6412                         iocbq->iocb.ulpTimeout);
6413                 /* Need a VF for word 4 set the vf bit*/
6414                 bf_set(els_req64_vf, &wqe->els_req, 0);
6415                 /* And a VFID for word 12 */
6416                 bf_set(els_req64_vfid, &wqe->els_req, 0);
6417                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6418                 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6419                        iocbq->iocb.ulpContext);
6420                 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6421                 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
6422                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
6423                 if (command_type == ELS_COMMAND_FIP) {
6424                         els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6425                                         >> LPFC_FIP_ELS_ID_SHIFT);
6426                 }
6427                 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6428                 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6429                 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6430                 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6431                 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6432                 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
6433         break;
6434         case CMD_XMIT_SEQUENCE64_CX:
6435                 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6436                        iocbq->iocb.un.ulpWord[3]);
6437                 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6438                        iocbq->iocb.ulpContext);
6439                 /* The entire sequence is transmitted for this IOCB */
6440                 xmit_len = total_len;
6441                 cmnd = CMD_XMIT_SEQUENCE64_CR;
6442         case CMD_XMIT_SEQUENCE64_CR:
6443                 /* word3 iocb=io_tag32 wqe=reserved */
6444                 wqe->xmit_sequence.rsvd3 = 0;
6445                 /* word4 relative_offset memcpy */
6446                 /* word5 r_ctl/df_ctl memcpy */
6447                 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6448                 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6449                 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6450                        LPFC_WQE_IOD_WRITE);
6451                 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6452                        LPFC_WQE_LENLOC_WORD12);
6453                 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
6454                 wqe->xmit_sequence.xmit_len = xmit_len;
6455                 command_type = OTHER_COMMAND;
6456         break;
6457         case CMD_XMIT_BCAST64_CN:
6458                 /* word3 iocb=iotag32 wqe=seq_payload_len */
6459                 wqe->xmit_bcast64.seq_payload_len = xmit_len;
6460                 /* word4 iocb=rsvd wqe=rsvd */
6461                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6462                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6463                 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
6464                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6465                 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6466                 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6467                 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6468                        LPFC_WQE_LENLOC_WORD3);
6469                 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
6470         break;
6471         case CMD_FCP_IWRITE64_CR:
6472                 command_type = FCP_COMMAND_DATA_OUT;
6473                 /* word3 iocb=iotag wqe=payload_offset_len */
6474                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6475                 wqe->fcp_iwrite.payload_offset_len =
6476                         xmit_len + sizeof(struct fcp_rsp);
6477                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6478                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6479                 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6480                        iocbq->iocb.ulpFCP2Rcvy);
6481                 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6482                 /* Always open the exchange */
6483                 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6484                 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6485                 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6486                 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6487                        LPFC_WQE_LENLOC_WORD4);
6488                 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6489                 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6490         break;
6491         case CMD_FCP_IREAD64_CR:
6492                 /* word3 iocb=iotag wqe=payload_offset_len */
6493                 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6494                 wqe->fcp_iread.payload_offset_len =
6495                         xmit_len + sizeof(struct fcp_rsp);
6496                 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6497                 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6498                 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6499                        iocbq->iocb.ulpFCP2Rcvy);
6500                 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
6501                 /* Always open the exchange */
6502                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6503                 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6504                 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6505                 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6506                        LPFC_WQE_LENLOC_WORD4);
6507                 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6508                 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6509         break;
6510         case CMD_FCP_ICMND64_CR:
6511                 /* word3 iocb=IO_TAG wqe=reserved */
6512                 wqe->fcp_icmd.rsrvd3 = 0;
6513                 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
6514                 /* Always open the exchange */
6515                 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6516                 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6517                 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6518                 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6519                 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6520                        LPFC_WQE_LENLOC_NONE);
6521                 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
6522         break;
6523         case CMD_GEN_REQUEST64_CR:
6524                 /* For this command calculate the xmit length of the
6525                  * request bde.
6526                  */
6527                 xmit_len = 0;
6528                 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6529                         sizeof(struct ulp_bde64);
6530                 for (i = 0; i < numBdes; i++) {
6531                         if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6532                                 break;
6533                         bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6534                         xmit_len += bde.tus.f.bdeSize;
6535                 }
6536                 /* word3 iocb=IO_TAG wqe=request_payload_len */
6537                 wqe->gen_req.request_payload_len = xmit_len;
6538                 /* word4 iocb=parameter wqe=relative_offset memcpy */
6539                 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
6540                 /* word6 context tag copied in memcpy */
6541                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
6542                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6543                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6544                                 "2015 Invalid CT %x command 0x%x\n",
6545                                 ct, iocbq->iocb.ulpCommand);
6546                         return IOCB_ERROR;
6547                 }
6548                 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6549                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6550                 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6551                 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6552                 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6553                 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6554                 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6555                 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
6556                 command_type = OTHER_COMMAND;
6557         break;
6558         case CMD_XMIT_ELS_RSP64_CX:
6559                 /* words0-2 BDE memcpy */
6560                 /* word3 iocb=iotag32 wqe=response_payload_len */
6561                 wqe->xmit_els_rsp.response_payload_len = xmit_len;
6562                 /* word4 iocb=did wge=rsvd. */
6563                 wqe->xmit_els_rsp.rsvd4 = 0;
6564                 /* word5 iocb=rsvd wge=did */
6565                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6566                          iocbq->iocb.un.elsreq64.remoteID);
6567                 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6568                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6569                 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6570                 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6571                        iocbq->iocb.ulpContext);
6572                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6573                         bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6574                                iocbq->vport->vpi + phba->vpi_base);
6575                 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6576                 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6577                 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6578                 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6579                        LPFC_WQE_LENLOC_WORD3);
6580                 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6581                 command_type = OTHER_COMMAND;
6582         break;
6583         case CMD_CLOSE_XRI_CN:
6584         case CMD_ABORT_XRI_CN:
6585         case CMD_ABORT_XRI_CX:
6586                 /* words 0-2 memcpy should be 0 rserved */
6587                 /* port will send abts */
6588                 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6589                 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6590                         abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6591                         fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6592                 } else
6593                         fip = 0;
6594
6595                 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
6596                         /*
6597                          * The link is down, or the command was ELS_FIP
6598                          * so the fw does not need to send abts
6599                          * on the wire.
6600                          */
6601                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6602                 else
6603                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6604                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6605                 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6606                 wqe->abort_cmd.rsrvd5 = 0;
6607                 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
6608                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6609                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6610                 /*
6611                  * The abort handler will send us CMD_ABORT_XRI_CN or
6612                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6613                  */
6614                 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6615                 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6616                 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6617                        LPFC_WQE_LENLOC_NONE);
6618                 cmnd = CMD_ABORT_XRI_CX;
6619                 command_type = OTHER_COMMAND;
6620                 xritag = 0;
6621         break;
6622         case CMD_XMIT_BLS_RSP64_CX:
6623                 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6624                  * we re-construct this WQE here based on information in
6625                  * iocbq from scratch.
6626                  */
6627                 memset(wqe, 0, sizeof(union lpfc_wqe));
6628                 /* OX_ID is invariable to who sent ABTS to CT exchange */
6629                 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6630                        bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6631                 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6632                     LPFC_ABTS_UNSOL_INT) {
6633                         /* ABTS sent by initiator to CT exchange, the
6634                          * RX_ID field will be filled with the newly
6635                          * allocated responder XRI.
6636                          */
6637                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6638                                iocbq->sli4_xritag);
6639                 } else {
6640                         /* ABTS sent by responder to CT exchange, the
6641                          * RX_ID field will be filled with the responder
6642                          * RX_ID from ABTS.
6643                          */
6644                         bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6645                                bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6646                 }
6647                 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6648                 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6649                 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6650                        iocbq->iocb.ulpContext);
6651                 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6652                 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6653                        LPFC_WQE_LENLOC_NONE);
6654                 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6655                 command_type = OTHER_COMMAND;
6656         break;
6657         case CMD_XRI_ABORTED_CX:
6658         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6659         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6660         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6661         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6662         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6663         default:
6664                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6665                                 "2014 Invalid command 0x%x\n",
6666                                 iocbq->iocb.ulpCommand);
6667                 return IOCB_ERROR;
6668         break;
6669         }
6670         bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6671         bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6672         wqe->generic.wqe_com.abort_tag = abort_tag;
6673         bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6674         bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6675         bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6676         bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
6677         return 0;
6678 }
6679
6680 /**
6681  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6682  * @phba: Pointer to HBA context object.
6683  * @ring_number: SLI ring number to issue iocb on.
6684  * @piocb: Pointer to command iocb.
6685  * @flag: Flag indicating if this command can be put into txq.
6686  *
6687  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6688  * an iocb command to an HBA with SLI-4 interface spec.
6689  *
6690  * This function is called with hbalock held. The function will return success
6691  * after it successfully submit the iocb to firmware or after adding to the
6692  * txq.
6693  **/
6694 static int
6695 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6696                          struct lpfc_iocbq *piocb, uint32_t flag)
6697 {
6698         struct lpfc_sglq *sglq;
6699         union lpfc_wqe wqe;
6700         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6701
6702         if (piocb->sli4_xritag == NO_XRI) {
6703                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6704                     piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6705                         sglq = NULL;
6706                 else {
6707                         if (pring->txq_cnt) {
6708                                 if (!(flag & SLI_IOCB_RET_IOCB)) {
6709                                         __lpfc_sli_ringtx_put(phba,
6710                                                 pring, piocb);
6711                                         return IOCB_SUCCESS;
6712                                 } else {
6713                                         return IOCB_BUSY;
6714                                 }
6715                         } else {
6716                         sglq = __lpfc_sli_get_sglq(phba, piocb);
6717                                 if (!sglq) {
6718                                         if (!(flag & SLI_IOCB_RET_IOCB)) {
6719                                                 __lpfc_sli_ringtx_put(phba,
6720                                                                 pring,
6721                                                                 piocb);
6722                                                 return IOCB_SUCCESS;
6723                                         } else
6724                                                 return IOCB_BUSY;
6725                                 }
6726                         }
6727                 }
6728         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6729                 sglq = NULL; /* These IO's already have an XRI and
6730                               * a mapped sgl.
6731                               */
6732         } else {
6733                 /* This is a continuation of a commandi,(CX) so this
6734                  * sglq is on the active list
6735                  */
6736                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6737                 if (!sglq)
6738                         return IOCB_ERROR;
6739         }
6740
6741         if (sglq) {
6742                 piocb->sli4_xritag = sglq->sli4_xritag;
6743
6744                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
6745                         return IOCB_ERROR;
6746         }
6747
6748         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6749                 return IOCB_ERROR;
6750
6751         if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6752                 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
6753                 /*
6754                  * For FCP command IOCB, get a new WQ index to distribute
6755                  * WQE across the WQsr. On the other hand, for abort IOCB,
6756                  * it carries the same WQ index to the original command
6757                  * IOCB.
6758                  */
6759                 if (piocb->iocb_flag & LPFC_IO_FCP)
6760                         piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6761                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6762                                      &wqe))
6763                         return IOCB_ERROR;
6764         } else {
6765                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6766                         return IOCB_ERROR;
6767         }
6768         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6769
6770         return 0;
6771 }
6772
6773 /**
6774  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6775  *
6776  * This routine wraps the actual lockless version for issusing IOCB function
6777  * pointer from the lpfc_hba struct.
6778  *
6779  * Return codes:
6780  *      IOCB_ERROR - Error
6781  *      IOCB_SUCCESS - Success
6782  *      IOCB_BUSY - Busy
6783  **/
6784 int
6785 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6786                 struct lpfc_iocbq *piocb, uint32_t flag)
6787 {
6788         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6789 }
6790
6791 /**
6792  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6793  * @phba: The hba struct for which this call is being executed.
6794  * @dev_grp: The HBA PCI-Device group number.
6795  *
6796  * This routine sets up the SLI interface API function jump table in @phba
6797  * struct.
6798  * Returns: 0 - success, -ENODEV - failure.
6799  **/
6800 int
6801 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6802 {
6803
6804         switch (dev_grp) {
6805         case LPFC_PCI_DEV_LP:
6806                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6807                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6808                 break;
6809         case LPFC_PCI_DEV_OC:
6810                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6811                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6812                 break;
6813         default:
6814                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6815                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6816                                 dev_grp);
6817                 return -ENODEV;
6818                 break;
6819         }
6820         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6821         return 0;
6822 }
6823
6824 /**
6825  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6826  * @phba: Pointer to HBA context object.
6827  * @pring: Pointer to driver SLI ring object.
6828  * @piocb: Pointer to command iocb.
6829  * @flag: Flag indicating if this command can be put into txq.
6830  *
6831  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6832  * function. This function gets the hbalock and calls
6833  * __lpfc_sli_issue_iocb function and will return the error returned
6834  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6835  * functions which do not hold hbalock.
6836  **/
6837 int
6838 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6839                     struct lpfc_iocbq *piocb, uint32_t flag)
6840 {
6841         unsigned long iflags;
6842         int rc;
6843
6844         spin_lock_irqsave(&phba->hbalock, iflags);
6845         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6846         spin_unlock_irqrestore(&phba->hbalock, iflags);
6847
6848         return rc;
6849 }
6850
6851 /**
6852  * lpfc_extra_ring_setup - Extra ring setup function
6853  * @phba: Pointer to HBA context object.
6854  *
6855  * This function is called while driver attaches with the
6856  * HBA to setup the extra ring. The extra ring is used
6857  * only when driver needs to support target mode functionality
6858  * or IP over FC functionalities.
6859  *
6860  * This function is called with no lock held.
6861  **/
6862 static int
6863 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6864 {
6865         struct lpfc_sli *psli;
6866         struct lpfc_sli_ring *pring;
6867
6868         psli = &phba->sli;
6869
6870         /* Adjust cmd/rsp ring iocb entries more evenly */
6871
6872         /* Take some away from the FCP ring */
6873         pring = &psli->ring[psli->fcp_ring];
6874         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6875         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6876         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6877         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6878
6879         /* and give them to the extra ring */
6880         pring = &psli->ring[psli->extra_ring];
6881
6882         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6883         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6884         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6885         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6886
6887         /* Setup default profile for this ring */
6888         pring->iotag_max = 4096;
6889         pring->num_mask = 1;
6890         pring->prt[0].profile = 0;      /* Mask 0 */
6891         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6892         pring->prt[0].type = phba->cfg_multi_ring_type;
6893         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6894         return 0;
6895 }
6896
6897 /**
6898  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6899  * @phba: Pointer to HBA context object.
6900  * @pring: Pointer to driver SLI ring object.
6901  * @iocbq: Pointer to iocb object.
6902  *
6903  * This function is called by the slow ring event handler
6904  * function when there is an ASYNC event iocb in the ring.
6905  * This function is called with no lock held.
6906  * Currently this function handles only temperature related
6907  * ASYNC events. The function decodes the temperature sensor
6908  * event message and posts events for the management applications.
6909  **/
6910 static void
6911 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6912         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6913 {
6914         IOCB_t *icmd;
6915         uint16_t evt_code;
6916         uint16_t temp;
6917         struct temp_event temp_event_data;
6918         struct Scsi_Host *shost;
6919         uint32_t *iocb_w;
6920
6921         icmd = &iocbq->iocb;
6922         evt_code = icmd->un.asyncstat.evt_code;
6923         temp = icmd->ulpContext;
6924
6925         if ((evt_code != ASYNC_TEMP_WARN) &&
6926                 (evt_code != ASYNC_TEMP_SAFE)) {
6927                 iocb_w = (uint32_t *) icmd;
6928                 lpfc_printf_log(phba,
6929                         KERN_ERR,
6930                         LOG_SLI,
6931                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6932                         " evt_code 0x%x\n"
6933                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6934                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6935                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6936                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6937                         pring->ringno,
6938                         icmd->un.asyncstat.evt_code,
6939                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6940                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6941                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6942                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6943
6944                 return;
6945         }
6946         temp_event_data.data = (uint32_t)temp;
6947         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6948         if (evt_code == ASYNC_TEMP_WARN) {
6949                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6950                 lpfc_printf_log(phba,
6951                                 KERN_ERR,
6952                                 LOG_TEMP,
6953                                 "0347 Adapter is very hot, please take "
6954                                 "corrective action. temperature : %d Celsius\n",
6955                                 temp);
6956         }
6957         if (evt_code == ASYNC_TEMP_SAFE) {
6958                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6959                 lpfc_printf_log(phba,
6960                                 KERN_ERR,
6961                                 LOG_TEMP,
6962                                 "0340 Adapter temperature is OK now. "
6963                                 "temperature : %d Celsius\n",
6964                                 temp);
6965         }
6966
6967         /* Send temperature change event to applications */
6968         shost = lpfc_shost_from_vport(phba->pport);
6969         fc_host_post_vendor_event(shost, fc_get_event_number(),
6970                 sizeof(temp_event_data), (char *) &temp_event_data,
6971                 LPFC_NL_VENDOR_ID);
6972
6973 }
6974
6975
6976 /**
6977  * lpfc_sli_setup - SLI ring setup function
6978  * @phba: Pointer to HBA context object.
6979  *
6980  * lpfc_sli_setup sets up rings of the SLI interface with
6981  * number of iocbs per ring and iotags. This function is
6982  * called while driver attach to the HBA and before the
6983  * interrupts are enabled. So there is no need for locking.
6984  *
6985  * This function always returns 0.
6986  **/
6987 int
6988 lpfc_sli_setup(struct lpfc_hba *phba)
6989 {
6990         int i, totiocbsize = 0;
6991         struct lpfc_sli *psli = &phba->sli;
6992         struct lpfc_sli_ring *pring;
6993
6994         psli->num_rings = MAX_CONFIGURED_RINGS;
6995         psli->sli_flag = 0;
6996         psli->fcp_ring = LPFC_FCP_RING;
6997         psli->next_ring = LPFC_FCP_NEXT_RING;
6998         psli->extra_ring = LPFC_EXTRA_RING;
6999
7000         psli->iocbq_lookup = NULL;
7001         psli->iocbq_lookup_len = 0;
7002         psli->last_iotag = 0;
7003
7004         for (i = 0; i < psli->num_rings; i++) {
7005                 pring = &psli->ring[i];
7006                 switch (i) {
7007                 case LPFC_FCP_RING:     /* ring 0 - FCP */
7008                         /* numCiocb and numRiocb are used in config_port */
7009                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
7010                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
7011                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
7012                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
7013                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
7014                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
7015                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7016                                                         SLI3_IOCB_CMD_SIZE :
7017                                                         SLI2_IOCB_CMD_SIZE;
7018                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7019                                                         SLI3_IOCB_RSP_SIZE :
7020                                                         SLI2_IOCB_RSP_SIZE;
7021                         pring->iotag_ctr = 0;
7022                         pring->iotag_max =
7023                             (phba->cfg_hba_queue_depth * 2);
7024                         pring->fast_iotag = pring->iotag_max;
7025                         pring->num_mask = 0;
7026                         break;
7027                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
7028                         /* numCiocb and numRiocb are used in config_port */
7029                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
7030                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
7031                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7032                                                         SLI3_IOCB_CMD_SIZE :
7033                                                         SLI2_IOCB_CMD_SIZE;
7034                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7035                                                         SLI3_IOCB_RSP_SIZE :
7036                                                         SLI2_IOCB_RSP_SIZE;
7037                         pring->iotag_max = phba->cfg_hba_queue_depth;
7038                         pring->num_mask = 0;
7039                         break;
7040                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
7041                         /* numCiocb and numRiocb are used in config_port */
7042                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
7043                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
7044                         pring->sizeCiocb = (phba->sli_rev == 3) ?
7045                                                         SLI3_IOCB_CMD_SIZE :
7046                                                         SLI2_IOCB_CMD_SIZE;
7047                         pring->sizeRiocb = (phba->sli_rev == 3) ?
7048                                                         SLI3_IOCB_RSP_SIZE :
7049                                                         SLI2_IOCB_RSP_SIZE;
7050                         pring->fast_iotag = 0;
7051                         pring->iotag_ctr = 0;
7052                         pring->iotag_max = 4096;
7053                         pring->lpfc_sli_rcv_async_status =
7054                                 lpfc_sli_async_event_handler;
7055                         pring->num_mask = LPFC_MAX_RING_MASK;
7056                         pring->prt[0].profile = 0;      /* Mask 0 */
7057                         pring->prt[0].rctl = FC_RCTL_ELS_REQ;
7058                         pring->prt[0].type = FC_TYPE_ELS;
7059                         pring->prt[0].lpfc_sli_rcv_unsol_event =
7060                             lpfc_els_unsol_event;
7061                         pring->prt[1].profile = 0;      /* Mask 1 */
7062                         pring->prt[1].rctl = FC_RCTL_ELS_REP;
7063                         pring->prt[1].type = FC_TYPE_ELS;
7064                         pring->prt[1].lpfc_sli_rcv_unsol_event =
7065                             lpfc_els_unsol_event;
7066                         pring->prt[2].profile = 0;      /* Mask 2 */
7067                         /* NameServer Inquiry */
7068                         pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
7069                         /* NameServer */
7070                         pring->prt[2].type = FC_TYPE_CT;
7071                         pring->prt[2].lpfc_sli_rcv_unsol_event =
7072                             lpfc_ct_unsol_event;
7073                         pring->prt[3].profile = 0;      /* Mask 3 */
7074                         /* NameServer response */
7075                         pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
7076                         /* NameServer */
7077                         pring->prt[3].type = FC_TYPE_CT;
7078                         pring->prt[3].lpfc_sli_rcv_unsol_event =
7079                             lpfc_ct_unsol_event;
7080                         /* abort unsolicited sequence */
7081                         pring->prt[4].profile = 0;      /* Mask 4 */
7082                         pring->prt[4].rctl = FC_RCTL_BA_ABTS;
7083                         pring->prt[4].type = FC_TYPE_BLS;
7084                         pring->prt[4].lpfc_sli_rcv_unsol_event =
7085                             lpfc_sli4_ct_abort_unsol_event;
7086                         break;
7087                 }
7088                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
7089                                 (pring->numRiocb * pring->sizeRiocb);
7090         }
7091         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
7092                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
7093                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
7094                        "SLI2 SLIM Data: x%x x%lx\n",
7095                        phba->brd_no, totiocbsize,
7096                        (unsigned long) MAX_SLIM_IOCB_SIZE);
7097         }
7098         if (phba->cfg_multi_ring_support == 2)
7099                 lpfc_extra_ring_setup(phba);
7100
7101         return 0;
7102 }
7103
7104 /**
7105  * lpfc_sli_queue_setup - Queue initialization function
7106  * @phba: Pointer to HBA context object.
7107  *
7108  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
7109  * ring. This function also initializes ring indices of each ring.
7110  * This function is called during the initialization of the SLI
7111  * interface of an HBA.
7112  * This function is called with no lock held and always returns
7113  * 1.
7114  **/
7115 int
7116 lpfc_sli_queue_setup(struct lpfc_hba *phba)
7117 {
7118         struct lpfc_sli *psli;
7119         struct lpfc_sli_ring *pring;
7120         int i;
7121
7122         psli = &phba->sli;
7123         spin_lock_irq(&phba->hbalock);
7124         INIT_LIST_HEAD(&psli->mboxq);
7125         INIT_LIST_HEAD(&psli->mboxq_cmpl);
7126         /* Initialize list headers for txq and txcmplq as double linked lists */
7127         for (i = 0; i < psli->num_rings; i++) {
7128                 pring = &psli->ring[i];
7129                 pring->ringno = i;
7130                 pring->next_cmdidx  = 0;
7131                 pring->local_getidx = 0;
7132                 pring->cmdidx = 0;
7133                 INIT_LIST_HEAD(&pring->txq);
7134                 INIT_LIST_HEAD(&pring->txcmplq);
7135                 INIT_LIST_HEAD(&pring->iocb_continueq);
7136                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
7137                 INIT_LIST_HEAD(&pring->postbufq);
7138         }
7139         spin_unlock_irq(&phba->hbalock);
7140         return 1;
7141 }
7142
7143 /**
7144  * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
7145  * @phba: Pointer to HBA context object.
7146  *
7147  * This routine flushes the mailbox command subsystem. It will unconditionally
7148  * flush all the mailbox commands in the three possible stages in the mailbox
7149  * command sub-system: pending mailbox command queue; the outstanding mailbox
7150  * command; and completed mailbox command queue. It is caller's responsibility
7151  * to make sure that the driver is in the proper state to flush the mailbox
7152  * command sub-system. Namely, the posting of mailbox commands into the
7153  * pending mailbox command queue from the various clients must be stopped;
7154  * either the HBA is in a state that it will never works on the outstanding
7155  * mailbox command (such as in EEH or ERATT conditions) or the outstanding
7156  * mailbox command has been completed.
7157  **/
7158 static void
7159 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
7160 {
7161         LIST_HEAD(completions);
7162         struct lpfc_sli *psli = &phba->sli;
7163         LPFC_MBOXQ_t *pmb;
7164         unsigned long iflag;
7165
7166         /* Flush all the mailbox commands in the mbox system */
7167         spin_lock_irqsave(&phba->hbalock, iflag);
7168         /* The pending mailbox command queue */
7169         list_splice_init(&phba->sli.mboxq, &completions);
7170         /* The outstanding active mailbox command */
7171         if (psli->mbox_active) {
7172                 list_add_tail(&psli->mbox_active->list, &completions);
7173                 psli->mbox_active = NULL;
7174                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7175         }
7176         /* The completed mailbox command queue */
7177         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
7178         spin_unlock_irqrestore(&phba->hbalock, iflag);
7179
7180         /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
7181         while (!list_empty(&completions)) {
7182                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
7183                 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
7184                 if (pmb->mbox_cmpl)
7185                         pmb->mbox_cmpl(phba, pmb);
7186         }
7187 }
7188
7189 /**
7190  * lpfc_sli_host_down - Vport cleanup function
7191  * @vport: Pointer to virtual port object.
7192  *
7193  * lpfc_sli_host_down is called to clean up the resources
7194  * associated with a vport before destroying virtual
7195  * port data structures.
7196  * This function does following operations:
7197  * - Free discovery resources associated with this virtual
7198  *   port.
7199  * - Free iocbs associated with this virtual port in
7200  *   the txq.
7201  * - Send abort for all iocb commands associated with this
7202  *   vport in txcmplq.
7203  *
7204  * This function is called with no lock held and always returns 1.
7205  **/
7206 int
7207 lpfc_sli_host_down(struct lpfc_vport *vport)
7208 {
7209         LIST_HEAD(completions);
7210         struct lpfc_hba *phba = vport->phba;
7211         struct lpfc_sli *psli = &phba->sli;
7212         struct lpfc_sli_ring *pring;
7213         struct lpfc_iocbq *iocb, *next_iocb;
7214         int i;
7215         unsigned long flags = 0;
7216         uint16_t prev_pring_flag;
7217
7218         lpfc_cleanup_discovery_resources(vport);
7219
7220         spin_lock_irqsave(&phba->hbalock, flags);
7221         for (i = 0; i < psli->num_rings; i++) {
7222                 pring = &psli->ring[i];
7223                 prev_pring_flag = pring->flag;
7224                 /* Only slow rings */
7225                 if (pring->ringno == LPFC_ELS_RING) {
7226                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
7227                         /* Set the lpfc data pending flag */
7228                         set_bit(LPFC_DATA_READY, &phba->data_flags);
7229                 }
7230                 /*
7231                  * Error everything on the txq since these iocbs have not been
7232                  * given to the FW yet.
7233                  */
7234                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
7235                         if (iocb->vport != vport)
7236                                 continue;
7237                         list_move_tail(&iocb->list, &completions);
7238                         pring->txq_cnt--;
7239                 }
7240
7241                 /* Next issue ABTS for everything on the txcmplq */
7242                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
7243                                                                         list) {
7244                         if (iocb->vport != vport)
7245                                 continue;
7246                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
7247                 }
7248
7249                 pring->flag = prev_pring_flag;
7250         }
7251
7252         spin_unlock_irqrestore(&phba->hbalock, flags);
7253
7254         /* Cancel all the IOCBs from the completions list */
7255         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7256                               IOERR_SLI_DOWN);
7257         return 1;
7258 }
7259
7260 /**
7261  * lpfc_sli_hba_down - Resource cleanup function for the HBA
7262  * @phba: Pointer to HBA context object.
7263  *
7264  * This function cleans up all iocb, buffers, mailbox commands
7265  * while shutting down the HBA. This function is called with no
7266  * lock held and always returns 1.
7267  * This function does the following to cleanup driver resources:
7268  * - Free discovery resources for each virtual port
7269  * - Cleanup any pending fabric iocbs
7270  * - Iterate through the iocb txq and free each entry
7271  *   in the list.
7272  * - Free up any buffer posted to the HBA
7273  * - Free mailbox commands in the mailbox queue.
7274  **/
7275 int
7276 lpfc_sli_hba_down(struct lpfc_hba *phba)
7277 {
7278         LIST_HEAD(completions);
7279         struct lpfc_sli *psli = &phba->sli;
7280         struct lpfc_sli_ring *pring;
7281         struct lpfc_dmabuf *buf_ptr;
7282         unsigned long flags = 0;
7283         int i;
7284
7285         /* Shutdown the mailbox command sub-system */
7286         lpfc_sli_mbox_sys_shutdown(phba);
7287
7288         lpfc_hba_down_prep(phba);
7289
7290         lpfc_fabric_abort_hba(phba);
7291
7292         spin_lock_irqsave(&phba->hbalock, flags);
7293         for (i = 0; i < psli->num_rings; i++) {
7294                 pring = &psli->ring[i];
7295                 /* Only slow rings */
7296                 if (pring->ringno == LPFC_ELS_RING) {
7297                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
7298                         /* Set the lpfc data pending flag */
7299                         set_bit(LPFC_DATA_READY, &phba->data_flags);
7300                 }
7301
7302                 /*
7303                  * Error everything on the txq since these iocbs have not been
7304                  * given to the FW yet.
7305                  */
7306                 list_splice_init(&pring->txq, &completions);
7307                 pring->txq_cnt = 0;
7308
7309         }
7310         spin_unlock_irqrestore(&phba->hbalock, flags);
7311
7312         /* Cancel all the IOCBs from the completions list */
7313         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7314                               IOERR_SLI_DOWN);
7315
7316         spin_lock_irqsave(&phba->hbalock, flags);
7317         list_splice_init(&phba->elsbuf, &completions);
7318         phba->elsbuf_cnt = 0;
7319         phba->elsbuf_prev_cnt = 0;
7320         spin_unlock_irqrestore(&phba->hbalock, flags);
7321
7322         while (!list_empty(&completions)) {
7323                 list_remove_head(&completions, buf_ptr,
7324                         struct lpfc_dmabuf, list);
7325                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
7326                 kfree(buf_ptr);
7327         }
7328
7329         /* Return any active mbox cmds */
7330         del_timer_sync(&psli->mbox_tmo);
7331
7332         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
7333         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7334         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
7335
7336         return 1;
7337 }
7338
7339 /**
7340  * lpfc_sli_pcimem_bcopy - SLI memory copy function
7341  * @srcp: Source memory pointer.
7342  * @destp: Destination memory pointer.
7343  * @cnt: Number of words required to be copied.
7344  *
7345  * This function is used for copying data between driver memory
7346  * and the SLI memory. This function also changes the endianness
7347  * of each word if native endianness is different from SLI
7348  * endianness. This function can be called with or without
7349  * lock.
7350  **/
7351 void
7352 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
7353 {
7354         uint32_t *src = srcp;
7355         uint32_t *dest = destp;
7356         uint32_t ldata;
7357         int i;
7358
7359         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
7360                 ldata = *src;
7361                 ldata = le32_to_cpu(ldata);
7362                 *dest = ldata;
7363                 src++;
7364                 dest++;
7365         }
7366 }
7367
7368
7369 /**
7370  * lpfc_sli_bemem_bcopy - SLI memory copy function
7371  * @srcp: Source memory pointer.
7372  * @destp: Destination memory pointer.
7373  * @cnt: Number of words required to be copied.
7374  *
7375  * This function is used for copying data between a data structure
7376  * with big endian representation to local endianness.
7377  * This function can be called with or without lock.
7378  **/
7379 void
7380 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7381 {
7382         uint32_t *src = srcp;
7383         uint32_t *dest = destp;
7384         uint32_t ldata;
7385         int i;
7386
7387         for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7388                 ldata = *src;
7389                 ldata = be32_to_cpu(ldata);
7390                 *dest = ldata;
7391                 src++;
7392                 dest++;
7393         }
7394 }
7395
7396 /**
7397  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
7398  * @phba: Pointer to HBA context object.
7399  * @pring: Pointer to driver SLI ring object.
7400  * @mp: Pointer to driver buffer object.
7401  *
7402  * This function is called with no lock held.
7403  * It always return zero after adding the buffer to the postbufq
7404  * buffer list.
7405  **/
7406 int
7407 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7408                          struct lpfc_dmabuf *mp)
7409 {
7410         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7411            later */
7412         spin_lock_irq(&phba->hbalock);
7413         list_add_tail(&mp->list, &pring->postbufq);
7414         pring->postbufq_cnt++;
7415         spin_unlock_irq(&phba->hbalock);
7416         return 0;
7417 }
7418
7419 /**
7420  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
7421  * @phba: Pointer to HBA context object.
7422  *
7423  * When HBQ is enabled, buffers are searched based on tags. This function
7424  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7425  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7426  * does not conflict with tags of buffer posted for unsolicited events.
7427  * The function returns the allocated tag. The function is called with
7428  * no locks held.
7429  **/
7430 uint32_t
7431 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7432 {
7433         spin_lock_irq(&phba->hbalock);
7434         phba->buffer_tag_count++;
7435         /*
7436          * Always set the QUE_BUFTAG_BIT to distiguish between
7437          * a tag assigned by HBQ.
7438          */
7439         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7440         spin_unlock_irq(&phba->hbalock);
7441         return phba->buffer_tag_count;
7442 }
7443
7444 /**
7445  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
7446  * @phba: Pointer to HBA context object.
7447  * @pring: Pointer to driver SLI ring object.
7448  * @tag: Buffer tag.
7449  *
7450  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7451  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7452  * iocb is posted to the response ring with the tag of the buffer.
7453  * This function searches the pring->postbufq list using the tag
7454  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7455  * iocb. If the buffer is found then lpfc_dmabuf object of the
7456  * buffer is returned to the caller else NULL is returned.
7457  * This function is called with no lock held.
7458  **/
7459 struct lpfc_dmabuf *
7460 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7461                         uint32_t tag)
7462 {
7463         struct lpfc_dmabuf *mp, *next_mp;
7464         struct list_head *slp = &pring->postbufq;
7465
7466         /* Search postbufq, from the begining, looking for a match on tag */
7467         spin_lock_irq(&phba->hbalock);
7468         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7469                 if (mp->buffer_tag == tag) {
7470                         list_del_init(&mp->list);
7471                         pring->postbufq_cnt--;
7472                         spin_unlock_irq(&phba->hbalock);
7473                         return mp;
7474                 }
7475         }
7476
7477         spin_unlock_irq(&phba->hbalock);
7478         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7479                         "0402 Cannot find virtual addr for buffer tag on "
7480                         "ring %d Data x%lx x%p x%p x%x\n",
7481                         pring->ringno, (unsigned long) tag,
7482                         slp->next, slp->prev, pring->postbufq_cnt);
7483
7484         return NULL;
7485 }
7486
7487 /**
7488  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
7489  * @phba: Pointer to HBA context object.
7490  * @pring: Pointer to driver SLI ring object.
7491  * @phys: DMA address of the buffer.
7492  *
7493  * This function searches the buffer list using the dma_address
7494  * of unsolicited event to find the driver's lpfc_dmabuf object
7495  * corresponding to the dma_address. The function returns the
7496  * lpfc_dmabuf object if a buffer is found else it returns NULL.
7497  * This function is called by the ct and els unsolicited event
7498  * handlers to get the buffer associated with the unsolicited
7499  * event.
7500  *
7501  * This function is called with no lock held.
7502  **/
7503 struct lpfc_dmabuf *
7504 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7505                          dma_addr_t phys)
7506 {
7507         struct lpfc_dmabuf *mp, *next_mp;
7508         struct list_head *slp = &pring->postbufq;
7509
7510         /* Search postbufq, from the begining, looking for a match on phys */
7511         spin_lock_irq(&phba->hbalock);
7512         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7513                 if (mp->phys == phys) {
7514                         list_del_init(&mp->list);
7515                         pring->postbufq_cnt--;
7516                         spin_unlock_irq(&phba->hbalock);
7517                         return mp;
7518                 }
7519         }
7520
7521         spin_unlock_irq(&phba->hbalock);
7522         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7523                         "0410 Cannot find virtual addr for mapped buf on "
7524                         "ring %d Data x%llx x%p x%p x%x\n",
7525                         pring->ringno, (unsigned long long)phys,
7526                         slp->next, slp->prev, pring->postbufq_cnt);
7527         return NULL;
7528 }
7529
7530 /**
7531  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
7532  * @phba: Pointer to HBA context object.
7533  * @cmdiocb: Pointer to driver command iocb object.
7534  * @rspiocb: Pointer to driver response iocb object.
7535  *
7536  * This function is the completion handler for the abort iocbs for
7537  * ELS commands. This function is called from the ELS ring event
7538  * handler with no lock held. This function frees memory resources
7539  * associated with the abort iocb.
7540  **/
7541 static void
7542 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7543                         struct lpfc_iocbq *rspiocb)
7544 {
7545         IOCB_t *irsp = &rspiocb->iocb;
7546         uint16_t abort_iotag, abort_context;
7547         struct lpfc_iocbq *abort_iocb;
7548         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7549
7550         abort_iocb = NULL;
7551
7552         if (irsp->ulpStatus) {
7553                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7554                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7555
7556                 spin_lock_irq(&phba->hbalock);
7557                 if (phba->sli_rev < LPFC_SLI_REV4) {
7558                         if (abort_iotag != 0 &&
7559                                 abort_iotag <= phba->sli.last_iotag)
7560                                 abort_iocb =
7561                                         phba->sli.iocbq_lookup[abort_iotag];
7562                 } else
7563                         /* For sli4 the abort_tag is the XRI,
7564                          * so the abort routine puts the iotag  of the iocb
7565                          * being aborted in the context field of the abort
7566                          * IOCB.
7567                          */
7568                         abort_iocb = phba->sli.iocbq_lookup[abort_context];
7569
7570                 /*
7571                  *  If the iocb is not found in Firmware queue the iocb
7572                  *  might have completed already. Do not free it again.
7573                  */
7574                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
7575                         if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7576                                 spin_unlock_irq(&phba->hbalock);
7577                                 lpfc_sli_release_iocbq(phba, cmdiocb);
7578                                 return;
7579                         }
7580                         /* For SLI4 the ulpContext field for abort IOCB
7581                          * holds the iotag of the IOCB being aborted so
7582                          * the local abort_context needs to be reset to
7583                          * match the aborted IOCBs ulpContext.
7584                          */
7585                         if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7586                                 abort_context = abort_iocb->iocb.ulpContext;
7587                 }
7588
7589                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7590                                 "0327 Cannot abort els iocb %p "
7591                                 "with tag %x context %x, abort status %x, "
7592                                 "abort code %x\n",
7593                                 abort_iocb, abort_iotag, abort_context,
7594                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
7595                 /*
7596                  * make sure we have the right iocbq before taking it
7597                  * off the txcmplq and try to call completion routine.
7598                  */
7599                 if (!abort_iocb ||
7600                     abort_iocb->iocb.ulpContext != abort_context ||
7601                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7602                         spin_unlock_irq(&phba->hbalock);
7603                 else if (phba->sli_rev < LPFC_SLI_REV4) {
7604                         /*
7605                          * leave the SLI4 aborted command on the txcmplq
7606                          * list and the command complete WCQE's XB bit
7607                          * will tell whether the SGL (XRI) can be released
7608                          * immediately or to the aborted SGL list for the
7609                          * following abort XRI from the HBA.
7610                          */
7611                         list_del_init(&abort_iocb->list);
7612                         if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7613                                 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7614                                 pring->txcmplq_cnt--;
7615                         }
7616
7617                         /* Firmware could still be in progress of DMAing
7618                          * payload, so don't free data buffer till after
7619                          * a hbeat.
7620                          */
7621                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7622                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7623                         spin_unlock_irq(&phba->hbalock);
7624
7625                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7626                         abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
7627                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
7628                 } else
7629                         spin_unlock_irq(&phba->hbalock);
7630         }
7631
7632         lpfc_sli_release_iocbq(phba, cmdiocb);
7633         return;
7634 }
7635
7636 /**
7637  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
7638  * @phba: Pointer to HBA context object.
7639  * @cmdiocb: Pointer to driver command iocb object.
7640  * @rspiocb: Pointer to driver response iocb object.
7641  *
7642  * The function is called from SLI ring event handler with no
7643  * lock held. This function is the completion handler for ELS commands
7644  * which are aborted. The function frees memory resources used for
7645  * the aborted ELS commands.
7646  **/
7647 static void
7648 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7649                      struct lpfc_iocbq *rspiocb)
7650 {
7651         IOCB_t *irsp = &rspiocb->iocb;
7652
7653         /* ELS cmd tag <ulpIoTag> completes */
7654         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
7655                         "0139 Ignoring ELS cmd tag x%x completion Data: "
7656                         "x%x x%x x%x\n",
7657                         irsp->ulpIoTag, irsp->ulpStatus,
7658                         irsp->un.ulpWord[4], irsp->ulpTimeout);
7659         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7660                 lpfc_ct_free_iocb(phba, cmdiocb);
7661         else
7662                 lpfc_els_free_iocb(phba, cmdiocb);
7663         return;
7664 }
7665
7666 /**
7667  * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
7668  * @phba: Pointer to HBA context object.
7669  * @pring: Pointer to driver SLI ring object.
7670  * @cmdiocb: Pointer to driver command iocb object.
7671  *
7672  * This function issues an abort iocb for the provided command iocb down to
7673  * the port. Other than the case the outstanding command iocb is an abort
7674  * request, this function issues abort out unconditionally. This function is
7675  * called with hbalock held. The function returns 0 when it fails due to
7676  * memory allocation failure or when the command iocb is an abort request.
7677  **/
7678 static int
7679 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7680                            struct lpfc_iocbq *cmdiocb)
7681 {
7682         struct lpfc_vport *vport = cmdiocb->vport;
7683         struct lpfc_iocbq *abtsiocbp;
7684         IOCB_t *icmd = NULL;
7685         IOCB_t *iabt = NULL;
7686         int retval;
7687
7688         /*
7689          * There are certain command types we don't want to abort.  And we
7690          * don't want to abort commands that are already in the process of
7691          * being aborted.
7692          */
7693         icmd = &cmdiocb->iocb;
7694         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7695             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7696             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7697                 return 0;
7698
7699         /* issue ABTS for this IOCB based on iotag */
7700         abtsiocbp = __lpfc_sli_get_iocbq(phba);
7701         if (abtsiocbp == NULL)
7702                 return 0;
7703
7704         /* This signals the response to set the correct status
7705          * before calling the completion handler
7706          */
7707         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7708
7709         iabt = &abtsiocbp->iocb;
7710         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7711         iabt->un.acxri.abortContextTag = icmd->ulpContext;
7712         if (phba->sli_rev == LPFC_SLI_REV4) {
7713                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7714                 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7715         }
7716         else
7717                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
7718         iabt->ulpLe = 1;
7719         iabt->ulpClass = icmd->ulpClass;
7720
7721         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7722         abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
7723         if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7724                 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
7725
7726         if (phba->link_state >= LPFC_LINK_UP)
7727                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7728         else
7729                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7730
7731         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
7732
7733         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7734                          "0339 Abort xri x%x, original iotag x%x, "
7735                          "abort cmd iotag x%x\n",
7736                          iabt->un.acxri.abortIoTag,
7737                          iabt->un.acxri.abortContextTag,
7738                          abtsiocbp->iotag);
7739         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
7740
7741         if (retval)
7742                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
7743
7744         /*
7745          * Caller to this routine should check for IOCB_ERROR
7746          * and handle it properly.  This routine no longer removes
7747          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7748          */
7749         return retval;
7750 }
7751
7752 /**
7753  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7754  * @phba: Pointer to HBA context object.
7755  * @pring: Pointer to driver SLI ring object.
7756  * @cmdiocb: Pointer to driver command iocb object.
7757  *
7758  * This function issues an abort iocb for the provided command iocb. In case
7759  * of unloading, the abort iocb will not be issued to commands on the ELS
7760  * ring. Instead, the callback function shall be changed to those commands
7761  * so that nothing happens when them finishes. This function is called with
7762  * hbalock held. The function returns 0 when the command iocb is an abort
7763  * request.
7764  **/
7765 int
7766 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7767                            struct lpfc_iocbq *cmdiocb)
7768 {
7769         struct lpfc_vport *vport = cmdiocb->vport;
7770         int retval = IOCB_ERROR;
7771         IOCB_t *icmd = NULL;
7772
7773         /*
7774          * There are certain command types we don't want to abort.  And we
7775          * don't want to abort commands that are already in the process of
7776          * being aborted.
7777          */
7778         icmd = &cmdiocb->iocb;
7779         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7780             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7781             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7782                 return 0;
7783
7784         /*
7785          * If we're unloading, don't abort iocb on the ELS ring, but change
7786          * the callback so that nothing happens when it finishes.
7787          */
7788         if ((vport->load_flag & FC_UNLOADING) &&
7789             (pring->ringno == LPFC_ELS_RING)) {
7790                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7791                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7792                 else
7793                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7794                 goto abort_iotag_exit;
7795         }
7796
7797         /* Now, we try to issue the abort to the cmdiocb out */
7798         retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7799
7800 abort_iotag_exit:
7801         /*
7802          * Caller to this routine should check for IOCB_ERROR
7803          * and handle it properly.  This routine no longer removes
7804          * iocb off txcmplq and call compl in case of IOCB_ERROR.
7805          */
7806         return retval;
7807 }
7808
7809 /**
7810  * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7811  * @phba: Pointer to HBA context object.
7812  * @pring: Pointer to driver SLI ring object.
7813  *
7814  * This function aborts all iocbs in the given ring and frees all the iocb
7815  * objects in txq. This function issues abort iocbs unconditionally for all
7816  * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7817  * to complete before the return of this function. The caller is not required
7818  * to hold any locks.
7819  **/
7820 static void
7821 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7822 {
7823         LIST_HEAD(completions);
7824         struct lpfc_iocbq *iocb, *next_iocb;
7825
7826         if (pring->ringno == LPFC_ELS_RING)
7827                 lpfc_fabric_abort_hba(phba);
7828
7829         spin_lock_irq(&phba->hbalock);
7830
7831         /* Take off all the iocbs on txq for cancelling */
7832         list_splice_init(&pring->txq, &completions);
7833         pring->txq_cnt = 0;
7834
7835         /* Next issue ABTS for everything on the txcmplq */
7836         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7837                 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7838
7839         spin_unlock_irq(&phba->hbalock);
7840
7841         /* Cancel all the IOCBs from the completions list */
7842         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7843                               IOERR_SLI_ABORTED);
7844 }
7845
7846 /**
7847  * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7848  * @phba: pointer to lpfc HBA data structure.
7849  *
7850  * This routine will abort all pending and outstanding iocbs to an HBA.
7851  **/
7852 void
7853 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7854 {
7855         struct lpfc_sli *psli = &phba->sli;
7856         struct lpfc_sli_ring *pring;
7857         int i;
7858
7859         for (i = 0; i < psli->num_rings; i++) {
7860                 pring = &psli->ring[i];
7861                 lpfc_sli_iocb_ring_abort(phba, pring);
7862         }
7863 }
7864
7865 /**
7866  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
7867  * @iocbq: Pointer to driver iocb object.
7868  * @vport: Pointer to driver virtual port object.
7869  * @tgt_id: SCSI ID of the target.
7870  * @lun_id: LUN ID of the scsi device.
7871  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7872  *
7873  * This function acts as an iocb filter for functions which abort or count
7874  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7875  * 0 if the filtering criteria is met for the given iocb and will return
7876  * 1 if the filtering criteria is not met.
7877  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7878  * given iocb is for the SCSI device specified by vport, tgt_id and
7879  * lun_id parameter.
7880  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
7881  * given iocb is for the SCSI target specified by vport and tgt_id
7882  * parameters.
7883  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7884  * given iocb is for the SCSI host associated with the given vport.
7885  * This function is called with no locks held.
7886  **/
7887 static int
7888 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7889                            uint16_t tgt_id, uint64_t lun_id,
7890                            lpfc_ctx_cmd ctx_cmd)
7891 {
7892         struct lpfc_scsi_buf *lpfc_cmd;
7893         int rc = 1;
7894
7895         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
7896                 return rc;
7897
7898         if (iocbq->vport != vport)
7899                 return rc;
7900
7901         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
7902
7903         if (lpfc_cmd->pCmd == NULL)
7904                 return rc;
7905
7906         switch (ctx_cmd) {
7907         case LPFC_CTX_LUN:
7908                 if ((lpfc_cmd->rdata->pnode) &&
7909                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7910                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7911                         rc = 0;
7912                 break;
7913         case LPFC_CTX_TGT:
7914                 if ((lpfc_cmd->rdata->pnode) &&
7915                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7916                         rc = 0;
7917                 break;
7918         case LPFC_CTX_HOST:
7919                 rc = 0;
7920                 break;
7921         default:
7922                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7923                         __func__, ctx_cmd);
7924                 break;
7925         }
7926
7927         return rc;
7928 }
7929
7930 /**
7931  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7932  * @vport: Pointer to virtual port.
7933  * @tgt_id: SCSI ID of the target.
7934  * @lun_id: LUN ID of the scsi device.
7935  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7936  *
7937  * This function returns number of FCP commands pending for the vport.
7938  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7939  * commands pending on the vport associated with SCSI device specified
7940  * by tgt_id and lun_id parameters.
7941  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7942  * commands pending on the vport associated with SCSI target specified
7943  * by tgt_id parameter.
7944  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7945  * commands pending on the vport.
7946  * This function returns the number of iocbs which satisfy the filter.
7947  * This function is called without any lock held.
7948  **/
7949 int
7950 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7951                   lpfc_ctx_cmd ctx_cmd)
7952 {
7953         struct lpfc_hba *phba = vport->phba;
7954         struct lpfc_iocbq *iocbq;
7955         int sum, i;
7956
7957         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7958                 iocbq = phba->sli.iocbq_lookup[i];
7959
7960                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7961                                                 ctx_cmd) == 0)
7962                         sum++;
7963         }
7964
7965         return sum;
7966 }
7967
7968 /**
7969  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7970  * @phba: Pointer to HBA context object
7971  * @cmdiocb: Pointer to command iocb object.
7972  * @rspiocb: Pointer to response iocb object.
7973  *
7974  * This function is called when an aborted FCP iocb completes. This
7975  * function is called by the ring event handler with no lock held.
7976  * This function frees the iocb.
7977  **/
7978 void
7979 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7980                         struct lpfc_iocbq *rspiocb)
7981 {
7982         lpfc_sli_release_iocbq(phba, cmdiocb);
7983         return;
7984 }
7985
7986 /**
7987  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7988  * @vport: Pointer to virtual port.
7989  * @pring: Pointer to driver SLI ring object.
7990  * @tgt_id: SCSI ID of the target.
7991  * @lun_id: LUN ID of the scsi device.
7992  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7993  *
7994  * This function sends an abort command for every SCSI command
7995  * associated with the given virtual port pending on the ring
7996  * filtered by lpfc_sli_validate_fcp_iocb function.
7997  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7998  * FCP iocbs associated with lun specified by tgt_id and lun_id
7999  * parameters
8000  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
8001  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
8002  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
8003  * FCP iocbs associated with virtual port.
8004  * This function returns number of iocbs it failed to abort.
8005  * This function is called with no locks held.
8006  **/
8007 int
8008 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
8009                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
8010 {
8011         struct lpfc_hba *phba = vport->phba;
8012         struct lpfc_iocbq *iocbq;
8013         struct lpfc_iocbq *abtsiocb;
8014         IOCB_t *cmd = NULL;
8015         int errcnt = 0, ret_val = 0;
8016         int i;
8017
8018         for (i = 1; i <= phba->sli.last_iotag; i++) {
8019                 iocbq = phba->sli.iocbq_lookup[i];
8020
8021                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
8022                                                abort_cmd) != 0)
8023                         continue;
8024
8025                 /* issue ABTS for this IOCB based on iotag */
8026                 abtsiocb = lpfc_sli_get_iocbq(phba);
8027                 if (abtsiocb == NULL) {
8028                         errcnt++;
8029                         continue;
8030                 }
8031
8032                 cmd = &iocbq->iocb;
8033                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
8034                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
8035                 if (phba->sli_rev == LPFC_SLI_REV4)
8036                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
8037                 else
8038                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
8039                 abtsiocb->iocb.ulpLe = 1;
8040                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
8041                 abtsiocb->vport = phba->pport;
8042
8043                 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8044                 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
8045                 if (iocbq->iocb_flag & LPFC_IO_FCP)
8046                         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
8047
8048                 if (lpfc_is_link_up(phba))
8049                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
8050                 else
8051                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
8052
8053                 /* Setup callback routine and issue the command. */
8054                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
8055                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
8056                                               abtsiocb, 0);
8057                 if (ret_val == IOCB_ERROR) {
8058                         lpfc_sli_release_iocbq(phba, abtsiocb);
8059                         errcnt++;
8060                         continue;
8061                 }
8062         }
8063
8064         return errcnt;
8065 }
8066
8067 /**
8068  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
8069  * @phba: Pointer to HBA context object.
8070  * @cmdiocbq: Pointer to command iocb.
8071  * @rspiocbq: Pointer to response iocb.
8072  *
8073  * This function is the completion handler for iocbs issued using
8074  * lpfc_sli_issue_iocb_wait function. This function is called by the
8075  * ring event handler function without any lock held. This function
8076  * can be called from both worker thread context and interrupt
8077  * context. This function also can be called from other thread which
8078  * cleans up the SLI layer objects.
8079  * This function copy the contents of the response iocb to the
8080  * response iocb memory object provided by the caller of
8081  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
8082  * sleeps for the iocb completion.
8083  **/
8084 static void
8085 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
8086                         struct lpfc_iocbq *cmdiocbq,
8087                         struct lpfc_iocbq *rspiocbq)
8088 {
8089         wait_queue_head_t *pdone_q;
8090         unsigned long iflags;
8091         struct lpfc_scsi_buf *lpfc_cmd;
8092
8093         spin_lock_irqsave(&phba->hbalock, iflags);
8094         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
8095         if (cmdiocbq->context2 && rspiocbq)
8096                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
8097                        &rspiocbq->iocb, sizeof(IOCB_t));
8098
8099         /* Set the exchange busy flag for task management commands */
8100         if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
8101                 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
8102                 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
8103                         cur_iocbq);
8104                 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
8105         }
8106
8107         pdone_q = cmdiocbq->context_un.wait_queue;
8108         if (pdone_q)
8109                 wake_up(pdone_q);
8110         spin_unlock_irqrestore(&phba->hbalock, iflags);
8111         return;
8112 }
8113
8114 /**
8115  * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
8116  * @phba: Pointer to HBA context object..
8117  * @piocbq: Pointer to command iocb.
8118  * @flag: Flag to test.
8119  *
8120  * This routine grabs the hbalock and then test the iocb_flag to
8121  * see if the passed in flag is set.
8122  * Returns:
8123  * 1 if flag is set.
8124  * 0 if flag is not set.
8125  **/
8126 static int
8127 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
8128                  struct lpfc_iocbq *piocbq, uint32_t flag)
8129 {
8130         unsigned long iflags;
8131         int ret;
8132
8133         spin_lock_irqsave(&phba->hbalock, iflags);
8134         ret = piocbq->iocb_flag & flag;
8135         spin_unlock_irqrestore(&phba->hbalock, iflags);
8136         return ret;
8137
8138 }
8139
8140 /**
8141  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
8142  * @phba: Pointer to HBA context object..
8143  * @pring: Pointer to sli ring.
8144  * @piocb: Pointer to command iocb.
8145  * @prspiocbq: Pointer to response iocb.
8146  * @timeout: Timeout in number of seconds.
8147  *
8148  * This function issues the iocb to firmware and waits for the
8149  * iocb to complete. If the iocb command is not
8150  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
8151  * Caller should not free the iocb resources if this function
8152  * returns IOCB_TIMEDOUT.
8153  * The function waits for the iocb completion using an
8154  * non-interruptible wait.
8155  * This function will sleep while waiting for iocb completion.
8156  * So, this function should not be called from any context which
8157  * does not allow sleeping. Due to the same reason, this function
8158  * cannot be called with interrupt disabled.
8159  * This function assumes that the iocb completions occur while
8160  * this function sleep. So, this function cannot be called from
8161  * the thread which process iocb completion for this ring.
8162  * This function clears the iocb_flag of the iocb object before
8163  * issuing the iocb and the iocb completion handler sets this
8164  * flag and wakes this thread when the iocb completes.
8165  * The contents of the response iocb will be copied to prspiocbq
8166  * by the completion handler when the command completes.
8167  * This function returns IOCB_SUCCESS when success.
8168  * This function is called with no lock held.
8169  **/
8170 int
8171 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
8172                          uint32_t ring_number,
8173                          struct lpfc_iocbq *piocb,
8174                          struct lpfc_iocbq *prspiocbq,
8175                          uint32_t timeout)
8176 {
8177         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
8178         long timeleft, timeout_req = 0;
8179         int retval = IOCB_SUCCESS;
8180         uint32_t creg_val;
8181         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8182         /*
8183          * If the caller has provided a response iocbq buffer, then context2
8184          * is NULL or its an error.
8185          */
8186         if (prspiocbq) {
8187                 if (piocb->context2)
8188                         return IOCB_ERROR;
8189                 piocb->context2 = prspiocbq;
8190         }
8191
8192         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
8193         piocb->context_un.wait_queue = &done_q;
8194         piocb->iocb_flag &= ~LPFC_IO_WAKE;
8195
8196         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8197                 creg_val = readl(phba->HCregaddr);
8198                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
8199                 writel(creg_val, phba->HCregaddr);
8200                 readl(phba->HCregaddr); /* flush */
8201         }
8202
8203         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
8204                                      SLI_IOCB_RET_IOCB);
8205         if (retval == IOCB_SUCCESS) {
8206                 timeout_req = timeout * HZ;
8207                 timeleft = wait_event_timeout(done_q,
8208                                 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
8209                                 timeout_req);
8210
8211                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
8212                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8213                                         "0331 IOCB wake signaled\n");
8214                 } else if (timeleft == 0) {
8215                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8216                                         "0338 IOCB wait timeout error - no "
8217                                         "wake response Data x%x\n", timeout);
8218                         retval = IOCB_TIMEDOUT;
8219                 } else {
8220                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8221                                         "0330 IOCB wake NOT set, "
8222                                         "Data x%x x%lx\n",
8223                                         timeout, (timeleft / jiffies));
8224                         retval = IOCB_TIMEDOUT;
8225                 }
8226         } else if (retval == IOCB_BUSY) {
8227                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8228                         "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
8229                         phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
8230                 return retval;
8231         } else {
8232                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8233                                 "0332 IOCB wait issue failed, Data x%x\n",
8234                                 retval);
8235                 retval = IOCB_ERROR;
8236         }
8237
8238         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
8239                 creg_val = readl(phba->HCregaddr);
8240                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
8241                 writel(creg_val, phba->HCregaddr);
8242                 readl(phba->HCregaddr); /* flush */
8243         }
8244
8245         if (prspiocbq)
8246                 piocb->context2 = NULL;
8247
8248         piocb->context_un.wait_queue = NULL;
8249         piocb->iocb_cmpl = NULL;
8250         return retval;
8251 }
8252
8253 /**
8254  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
8255  * @phba: Pointer to HBA context object.
8256  * @pmboxq: Pointer to driver mailbox object.
8257  * @timeout: Timeout in number of seconds.
8258  *
8259  * This function issues the mailbox to firmware and waits for the
8260  * mailbox command to complete. If the mailbox command is not
8261  * completed within timeout seconds, it returns MBX_TIMEOUT.
8262  * The function waits for the mailbox completion using an
8263  * interruptible wait. If the thread is woken up due to a
8264  * signal, MBX_TIMEOUT error is returned to the caller. Caller
8265  * should not free the mailbox resources, if this function returns
8266  * MBX_TIMEOUT.
8267  * This function will sleep while waiting for mailbox completion.
8268  * So, this function should not be called from any context which
8269  * does not allow sleeping. Due to the same reason, this function
8270  * cannot be called with interrupt disabled.
8271  * This function assumes that the mailbox completion occurs while
8272  * this function sleep. So, this function cannot be called from
8273  * the worker thread which processes mailbox completion.
8274  * This function is called in the context of HBA management
8275  * applications.
8276  * This function returns MBX_SUCCESS when successful.
8277  * This function is called with no lock held.
8278  **/
8279 int
8280 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
8281                          uint32_t timeout)
8282 {
8283         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
8284         int retval;
8285         unsigned long flag;
8286
8287         /* The caller must leave context1 empty. */
8288         if (pmboxq->context1)
8289                 return MBX_NOT_FINISHED;
8290
8291         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
8292         /* setup wake call as IOCB callback */
8293         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
8294         /* setup context field to pass wait_queue pointer to wake function  */
8295         pmboxq->context1 = &done_q;
8296
8297         /* now issue the command */
8298         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
8299
8300         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
8301                 wait_event_interruptible_timeout(done_q,
8302                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
8303                                 timeout * HZ);
8304
8305                 spin_lock_irqsave(&phba->hbalock, flag);
8306                 pmboxq->context1 = NULL;
8307                 /*
8308                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
8309                  * else do not free the resources.
8310                  */
8311                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
8312                         retval = MBX_SUCCESS;
8313                         lpfc_sli4_swap_str(phba, pmboxq);
8314                 } else {
8315                         retval = MBX_TIMEOUT;
8316                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8317                 }
8318                 spin_unlock_irqrestore(&phba->hbalock, flag);
8319         }
8320
8321         return retval;
8322 }
8323
8324 /**
8325  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
8326  * @phba: Pointer to HBA context.
8327  *
8328  * This function is called to shutdown the driver's mailbox sub-system.
8329  * It first marks the mailbox sub-system is in a block state to prevent
8330  * the asynchronous mailbox command from issued off the pending mailbox
8331  * command queue. If the mailbox command sub-system shutdown is due to
8332  * HBA error conditions such as EEH or ERATT, this routine shall invoke
8333  * the mailbox sub-system flush routine to forcefully bring down the
8334  * mailbox sub-system. Otherwise, if it is due to normal condition (such
8335  * as with offline or HBA function reset), this routine will wait for the
8336  * outstanding mailbox command to complete before invoking the mailbox
8337  * sub-system flush routine to gracefully bring down mailbox sub-system.
8338  **/
8339 void
8340 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
8341 {
8342         struct lpfc_sli *psli = &phba->sli;
8343         uint8_t actcmd = MBX_HEARTBEAT;
8344         unsigned long timeout;
8345
8346         spin_lock_irq(&phba->hbalock);
8347         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8348         spin_unlock_irq(&phba->hbalock);
8349
8350         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
8351                 spin_lock_irq(&phba->hbalock);
8352                 if (phba->sli.mbox_active)
8353                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
8354                 spin_unlock_irq(&phba->hbalock);
8355                 /* Determine how long we might wait for the active mailbox
8356                  * command to be gracefully completed by firmware.
8357                  */
8358                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
8359                                            1000) + jiffies;
8360                 while (phba->sli.mbox_active) {
8361                         /* Check active mailbox complete status every 2ms */
8362                         msleep(2);
8363                         if (time_after(jiffies, timeout))
8364                                 /* Timeout, let the mailbox flush routine to
8365                                  * forcefully release active mailbox command
8366                                  */
8367                                 break;
8368                 }
8369         }
8370         lpfc_sli_mbox_sys_flush(phba);
8371 }
8372
8373 /**
8374  * lpfc_sli_eratt_read - read sli-3 error attention events
8375  * @phba: Pointer to HBA context.
8376  *
8377  * This function is called to read the SLI3 device error attention registers
8378  * for possible error attention events. The caller must hold the hostlock
8379  * with spin_lock_irq().
8380  *
8381  * This fucntion returns 1 when there is Error Attention in the Host Attention
8382  * Register and returns 0 otherwise.
8383  **/
8384 static int
8385 lpfc_sli_eratt_read(struct lpfc_hba *phba)
8386 {
8387         uint32_t ha_copy;
8388
8389         /* Read chip Host Attention (HA) register */
8390         ha_copy = readl(phba->HAregaddr);
8391         if (ha_copy & HA_ERATT) {
8392                 /* Read host status register to retrieve error event */
8393                 lpfc_sli_read_hs(phba);
8394
8395                 /* Check if there is a deferred error condition is active */
8396                 if ((HS_FFER1 & phba->work_hs) &&
8397                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8398                       HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
8399                         phba->hba_flag |= DEFER_ERATT;
8400                         /* Clear all interrupt enable conditions */
8401                         writel(0, phba->HCregaddr);
8402                         readl(phba->HCregaddr);
8403                 }
8404
8405                 /* Set the driver HA work bitmap */
8406                 phba->work_ha |= HA_ERATT;
8407                 /* Indicate polling handles this ERATT */
8408                 phba->hba_flag |= HBA_ERATT_HANDLED;
8409                 return 1;
8410         }
8411         return 0;
8412 }
8413
8414 /**
8415  * lpfc_sli4_eratt_read - read sli-4 error attention events
8416  * @phba: Pointer to HBA context.
8417  *
8418  * This function is called to read the SLI4 device error attention registers
8419  * for possible error attention events. The caller must hold the hostlock
8420  * with spin_lock_irq().
8421  *
8422  * This fucntion returns 1 when there is Error Attention in the Host Attention
8423  * Register and returns 0 otherwise.
8424  **/
8425 static int
8426 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8427 {
8428         uint32_t uerr_sta_hi, uerr_sta_lo;
8429         uint32_t if_type, portsmphr;
8430         struct lpfc_register portstat_reg;
8431
8432         /*
8433          * For now, use the SLI4 device internal unrecoverable error
8434          * registers for error attention. This can be changed later.
8435          */
8436         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8437         switch (if_type) {
8438         case LPFC_SLI_INTF_IF_TYPE_0:
8439                 uerr_sta_lo = readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
8440                 uerr_sta_hi = readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
8441                 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8442                     (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8443                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8444                                         "1423 HBA Unrecoverable error: "
8445                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8446                                         "ue_mask_lo_reg=0x%x, "
8447                                         "ue_mask_hi_reg=0x%x\n",
8448                                         uerr_sta_lo, uerr_sta_hi,
8449                                         phba->sli4_hba.ue_mask_lo,
8450                                         phba->sli4_hba.ue_mask_hi);
8451                         phba->work_status[0] = uerr_sta_lo;
8452                         phba->work_status[1] = uerr_sta_hi;
8453                         phba->work_ha |= HA_ERATT;
8454                         phba->hba_flag |= HBA_ERATT_HANDLED;
8455                         return 1;
8456                 }
8457                 break;
8458         case LPFC_SLI_INTF_IF_TYPE_2:
8459                 portstat_reg.word0 =
8460                         readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
8461                 portsmphr = readl(phba->sli4_hba.PSMPHRregaddr);
8462                 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
8463                         phba->work_status[0] =
8464                                 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
8465                         phba->work_status[1] =
8466                                 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
8467                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8468                                         "2885 Port Error Detected: "
8469                                         "port status reg 0x%x, "
8470                                         "port smphr reg 0x%x, "
8471                                         "error 1=0x%x, error 2=0x%x\n",
8472                                         portstat_reg.word0,
8473                                         portsmphr,
8474                                         phba->work_status[0],
8475                                         phba->work_status[1]);
8476                         phba->work_ha |= HA_ERATT;
8477                         phba->hba_flag |= HBA_ERATT_HANDLED;
8478                         return 1;
8479                 }
8480                 break;
8481         case LPFC_SLI_INTF_IF_TYPE_1:
8482         default:
8483                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8484                                 "2886 HBA Error Attention on unsupported "
8485                                 "if type %d.", if_type);
8486                 return 1;
8487         }
8488
8489         return 0;
8490 }
8491
8492 /**
8493  * lpfc_sli_check_eratt - check error attention events
8494  * @phba: Pointer to HBA context.
8495  *
8496  * This function is called from timer soft interrupt context to check HBA's
8497  * error attention register bit for error attention events.
8498  *
8499  * This fucntion returns 1 when there is Error Attention in the Host Attention
8500  * Register and returns 0 otherwise.
8501  **/
8502 int
8503 lpfc_sli_check_eratt(struct lpfc_hba *phba)
8504 {
8505         uint32_t ha_copy;
8506
8507         /* If somebody is waiting to handle an eratt, don't process it
8508          * here. The brdkill function will do this.
8509          */
8510         if (phba->link_flag & LS_IGNORE_ERATT)
8511                 return 0;
8512
8513         /* Check if interrupt handler handles this ERATT */
8514         spin_lock_irq(&phba->hbalock);
8515         if (phba->hba_flag & HBA_ERATT_HANDLED) {
8516                 /* Interrupt handler has handled ERATT */
8517                 spin_unlock_irq(&phba->hbalock);
8518                 return 0;
8519         }
8520
8521         /*
8522          * If there is deferred error attention, do not check for error
8523          * attention
8524          */
8525         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8526                 spin_unlock_irq(&phba->hbalock);
8527                 return 0;
8528         }
8529
8530         /* If PCI channel is offline, don't process it */
8531         if (unlikely(pci_channel_offline(phba->pcidev))) {
8532                 spin_unlock_irq(&phba->hbalock);
8533                 return 0;
8534         }
8535
8536         switch (phba->sli_rev) {
8537         case LPFC_SLI_REV2:
8538         case LPFC_SLI_REV3:
8539                 /* Read chip Host Attention (HA) register */
8540                 ha_copy = lpfc_sli_eratt_read(phba);
8541                 break;
8542         case LPFC_SLI_REV4:
8543                 /* Read device Uncoverable Error (UERR) registers */
8544                 ha_copy = lpfc_sli4_eratt_read(phba);
8545                 break;
8546         default:
8547                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548                                 "0299 Invalid SLI revision (%d)\n",
8549                                 phba->sli_rev);
8550                 ha_copy = 0;
8551                 break;
8552         }
8553         spin_unlock_irq(&phba->hbalock);
8554
8555         return ha_copy;
8556 }
8557
8558 /**
8559  * lpfc_intr_state_check - Check device state for interrupt handling
8560  * @phba: Pointer to HBA context.
8561  *
8562  * This inline routine checks whether a device or its PCI slot is in a state
8563  * that the interrupt should be handled.
8564  *
8565  * This function returns 0 if the device or the PCI slot is in a state that
8566  * interrupt should be handled, otherwise -EIO.
8567  */
8568 static inline int
8569 lpfc_intr_state_check(struct lpfc_hba *phba)
8570 {
8571         /* If the pci channel is offline, ignore all the interrupts */
8572         if (unlikely(pci_channel_offline(phba->pcidev)))
8573                 return -EIO;
8574
8575         /* Update device level interrupt statistics */
8576         phba->sli.slistat.sli_intr++;
8577
8578         /* Ignore all interrupts during initialization. */
8579         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8580                 return -EIO;
8581
8582         return 0;
8583 }
8584
8585 /**
8586  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
8587  * @irq: Interrupt number.
8588  * @dev_id: The device context pointer.
8589  *
8590  * This function is directly called from the PCI layer as an interrupt
8591  * service routine when device with SLI-3 interface spec is enabled with
8592  * MSI-X multi-message interrupt mode and there are slow-path events in
8593  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8594  * interrupt mode, this function is called as part of the device-level
8595  * interrupt handler. When the PCI slot is in error recovery or the HBA
8596  * is undergoing initialization, the interrupt handler will not process
8597  * the interrupt. The link attention and ELS ring attention events are
8598  * handled by the worker thread. The interrupt handler signals the worker
8599  * thread and returns for these events. This function is called without
8600  * any lock held. It gets the hbalock to access and update SLI data
8601  * structures.
8602  *
8603  * This function returns IRQ_HANDLED when interrupt is handled else it
8604  * returns IRQ_NONE.
8605  **/
8606 irqreturn_t
8607 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
8608 {
8609         struct lpfc_hba  *phba;
8610         uint32_t ha_copy, hc_copy;
8611         uint32_t work_ha_copy;
8612         unsigned long status;
8613         unsigned long iflag;
8614         uint32_t control;
8615
8616         MAILBOX_t *mbox, *pmbox;
8617         struct lpfc_vport *vport;
8618         struct lpfc_nodelist *ndlp;
8619         struct lpfc_dmabuf *mp;
8620         LPFC_MBOXQ_t *pmb;
8621         int rc;
8622
8623         /*
8624          * Get the driver's phba structure from the dev_id and
8625          * assume the HBA is not interrupting.
8626          */
8627         phba = (struct lpfc_hba *)dev_id;
8628
8629         if (unlikely(!phba))
8630                 return IRQ_NONE;
8631
8632         /*
8633          * Stuff needs to be attented to when this function is invoked as an
8634          * individual interrupt handler in MSI-X multi-message interrupt mode
8635          */
8636         if (phba->intr_type == MSIX) {
8637                 /* Check device state for handling interrupt */
8638                 if (lpfc_intr_state_check(phba))
8639                         return IRQ_NONE;
8640                 /* Need to read HA REG for slow-path events */
8641                 spin_lock_irqsave(&phba->hbalock, iflag);
8642                 ha_copy = readl(phba->HAregaddr);
8643                 /* If somebody is waiting to handle an eratt don't process it
8644                  * here. The brdkill function will do this.
8645                  */
8646                 if (phba->link_flag & LS_IGNORE_ERATT)
8647                         ha_copy &= ~HA_ERATT;
8648                 /* Check the need for handling ERATT in interrupt handler */
8649                 if (ha_copy & HA_ERATT) {
8650                         if (phba->hba_flag & HBA_ERATT_HANDLED)
8651                                 /* ERATT polling has handled ERATT */
8652                                 ha_copy &= ~HA_ERATT;
8653                         else
8654                                 /* Indicate interrupt handler handles ERATT */
8655                                 phba->hba_flag |= HBA_ERATT_HANDLED;
8656                 }
8657
8658                 /*
8659                  * If there is deferred error attention, do not check for any
8660                  * interrupt.
8661                  */
8662                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8663                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8664                         return IRQ_NONE;
8665                 }
8666
8667                 /* Clear up only attention source related to slow-path */
8668                 hc_copy = readl(phba->HCregaddr);
8669                 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8670                         HC_LAINT_ENA | HC_ERINT_ENA),
8671                         phba->HCregaddr);
8672                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8673                         phba->HAregaddr);
8674                 writel(hc_copy, phba->HCregaddr);
8675                 readl(phba->HAregaddr); /* flush */
8676                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8677         } else
8678                 ha_copy = phba->ha_copy;
8679
8680         work_ha_copy = ha_copy & phba->work_ha_mask;
8681
8682         if (work_ha_copy) {
8683                 if (work_ha_copy & HA_LATT) {
8684                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8685                                 /*
8686                                  * Turn off Link Attention interrupts
8687                                  * until CLEAR_LA done
8688                                  */
8689                                 spin_lock_irqsave(&phba->hbalock, iflag);
8690                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8691                                 control = readl(phba->HCregaddr);
8692                                 control &= ~HC_LAINT_ENA;
8693                                 writel(control, phba->HCregaddr);
8694                                 readl(phba->HCregaddr); /* flush */
8695                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8696                         }
8697                         else
8698                                 work_ha_copy &= ~HA_LATT;
8699                 }
8700
8701                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
8702                         /*
8703                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8704                          * the only slow ring.
8705                          */
8706                         status = (work_ha_copy &
8707                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
8708                         status >>= (4*LPFC_ELS_RING);
8709                         if (status & HA_RXMASK) {
8710                                 spin_lock_irqsave(&phba->hbalock, iflag);
8711                                 control = readl(phba->HCregaddr);
8712
8713                                 lpfc_debugfs_slow_ring_trc(phba,
8714                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
8715                                 control, status,
8716                                 (uint32_t)phba->sli.slistat.sli_intr);
8717
8718                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
8719                                         lpfc_debugfs_slow_ring_trc(phba,
8720                                                 "ISR Disable ring:"
8721                                                 "pwork:x%x hawork:x%x wait:x%x",
8722                                                 phba->work_ha, work_ha_copy,
8723                                                 (uint32_t)((unsigned long)
8724                                                 &phba->work_waitq));
8725
8726                                         control &=
8727                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
8728                                         writel(control, phba->HCregaddr);
8729                                         readl(phba->HCregaddr); /* flush */
8730                                 }
8731                                 else {
8732                                         lpfc_debugfs_slow_ring_trc(phba,
8733                                                 "ISR slow ring:   pwork:"
8734                                                 "x%x hawork:x%x wait:x%x",
8735                                                 phba->work_ha, work_ha_copy,
8736                                                 (uint32_t)((unsigned long)
8737                                                 &phba->work_waitq));
8738                                 }
8739                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8740                         }
8741                 }
8742                 spin_lock_irqsave(&phba->hbalock, iflag);
8743                 if (work_ha_copy & HA_ERATT) {
8744                         lpfc_sli_read_hs(phba);
8745                         /*
8746                          * Check if there is a deferred error condition
8747                          * is active
8748                          */
8749                         if ((HS_FFER1 & phba->work_hs) &&
8750                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8751                                   HS_FFER6 | HS_FFER7 | HS_FFER8) &
8752                                   phba->work_hs)) {
8753                                 phba->hba_flag |= DEFER_ERATT;
8754                                 /* Clear all interrupt enable conditions */
8755                                 writel(0, phba->HCregaddr);
8756                                 readl(phba->HCregaddr);
8757                         }
8758                 }
8759
8760                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
8761                         pmb = phba->sli.mbox_active;
8762                         pmbox = &pmb->u.mb;
8763                         mbox = phba->mbox;
8764                         vport = pmb->vport;
8765
8766                         /* First check out the status word */
8767                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8768                         if (pmbox->mbxOwner != OWN_HOST) {
8769                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8770                                 /*
8771                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
8772                                  * mbxStatus <status>
8773                                  */
8774                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8775                                                 LOG_SLI,
8776                                                 "(%d):0304 Stray Mailbox "
8777                                                 "Interrupt mbxCommand x%x "
8778                                                 "mbxStatus x%x\n",
8779                                                 (vport ? vport->vpi : 0),
8780                                                 pmbox->mbxCommand,
8781                                                 pmbox->mbxStatus);
8782                                 /* clear mailbox attention bit */
8783                                 work_ha_copy &= ~HA_MBATT;
8784                         } else {
8785                                 phba->sli.mbox_active = NULL;
8786                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8787                                 phba->last_completion_time = jiffies;
8788                                 del_timer(&phba->sli.mbox_tmo);
8789                                 if (pmb->mbox_cmpl) {
8790                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
8791                                                         MAILBOX_CMD_SIZE);
8792                                         if (pmb->out_ext_byte_len &&
8793                                                 pmb->context2)
8794                                                 lpfc_sli_pcimem_bcopy(
8795                                                 phba->mbox_ext,
8796                                                 pmb->context2,
8797                                                 pmb->out_ext_byte_len);
8798                                 }
8799                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8800                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8801
8802                                         lpfc_debugfs_disc_trc(vport,
8803                                                 LPFC_DISC_TRC_MBOX_VPORT,
8804                                                 "MBOX dflt rpi: : "
8805                                                 "status:x%x rpi:x%x",
8806                                                 (uint32_t)pmbox->mbxStatus,
8807                                                 pmbox->un.varWords[0], 0);
8808
8809                                         if (!pmbox->mbxStatus) {
8810                                                 mp = (struct lpfc_dmabuf *)
8811                                                         (pmb->context1);
8812                                                 ndlp = (struct lpfc_nodelist *)
8813                                                         pmb->context2;
8814
8815                                                 /* Reg_LOGIN of dflt RPI was
8816                                                  * successful. new lets get
8817                                                  * rid of the RPI using the
8818                                                  * same mbox buffer.
8819                                                  */
8820                                                 lpfc_unreg_login(phba,
8821                                                         vport->vpi,
8822                                                         pmbox->un.varWords[0],
8823                                                         pmb);
8824                                                 pmb->mbox_cmpl =
8825                                                         lpfc_mbx_cmpl_dflt_rpi;
8826                                                 pmb->context1 = mp;
8827                                                 pmb->context2 = ndlp;
8828                                                 pmb->vport = vport;
8829                                                 rc = lpfc_sli_issue_mbox(phba,
8830                                                                 pmb,
8831                                                                 MBX_NOWAIT);
8832                                                 if (rc != MBX_BUSY)
8833                                                         lpfc_printf_log(phba,
8834                                                         KERN_ERR,
8835                                                         LOG_MBOX | LOG_SLI,
8836                                                         "0350 rc should have"
8837                                                         "been MBX_BUSY\n");
8838                                                 if (rc != MBX_NOT_FINISHED)
8839                                                         goto send_current_mbox;
8840                                         }
8841                                 }
8842                                 spin_lock_irqsave(
8843                                                 &phba->pport->work_port_lock,
8844                                                 iflag);
8845                                 phba->pport->work_port_events &=
8846                                         ~WORKER_MBOX_TMO;
8847                                 spin_unlock_irqrestore(
8848                                                 &phba->pport->work_port_lock,
8849                                                 iflag);
8850                                 lpfc_mbox_cmpl_put(phba, pmb);
8851                         }
8852                 } else
8853                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8854
8855                 if ((work_ha_copy & HA_MBATT) &&
8856                     (phba->sli.mbox_active == NULL)) {
8857 send_current_mbox:
8858                         /* Process next mailbox command if there is one */
8859                         do {
8860                                 rc = lpfc_sli_issue_mbox(phba, NULL,
8861                                                          MBX_NOWAIT);
8862                         } while (rc == MBX_NOT_FINISHED);
8863                         if (rc != MBX_SUCCESS)
8864                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8865                                                 LOG_SLI, "0349 rc should be "
8866                                                 "MBX_SUCCESS\n");
8867                 }
8868
8869                 spin_lock_irqsave(&phba->hbalock, iflag);
8870                 phba->work_ha |= work_ha_copy;
8871                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8872                 lpfc_worker_wake_up(phba);
8873         }
8874         return IRQ_HANDLED;
8875
8876 } /* lpfc_sli_sp_intr_handler */
8877
8878 /**
8879  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
8880  * @irq: Interrupt number.
8881  * @dev_id: The device context pointer.
8882  *
8883  * This function is directly called from the PCI layer as an interrupt
8884  * service routine when device with SLI-3 interface spec is enabled with
8885  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8886  * ring event in the HBA. However, when the device is enabled with either
8887  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8888  * device-level interrupt handler. When the PCI slot is in error recovery
8889  * or the HBA is undergoing initialization, the interrupt handler will not
8890  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8891  * the intrrupt context. This function is called without any lock held.
8892  * It gets the hbalock to access and update SLI data structures.
8893  *
8894  * This function returns IRQ_HANDLED when interrupt is handled else it
8895  * returns IRQ_NONE.
8896  **/
8897 irqreturn_t
8898 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
8899 {
8900         struct lpfc_hba  *phba;
8901         uint32_t ha_copy;
8902         unsigned long status;
8903         unsigned long iflag;
8904
8905         /* Get the driver's phba structure from the dev_id and
8906          * assume the HBA is not interrupting.
8907          */
8908         phba = (struct lpfc_hba *) dev_id;
8909
8910         if (unlikely(!phba))
8911                 return IRQ_NONE;
8912
8913         /*
8914          * Stuff needs to be attented to when this function is invoked as an
8915          * individual interrupt handler in MSI-X multi-message interrupt mode
8916          */
8917         if (phba->intr_type == MSIX) {
8918                 /* Check device state for handling interrupt */
8919                 if (lpfc_intr_state_check(phba))
8920                         return IRQ_NONE;
8921                 /* Need to read HA REG for FCP ring and other ring events */
8922                 ha_copy = readl(phba->HAregaddr);
8923                 /* Clear up only attention source related to fast-path */
8924                 spin_lock_irqsave(&phba->hbalock, iflag);
8925                 /*
8926                  * If there is deferred error attention, do not check for
8927                  * any interrupt.
8928                  */
8929                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8930                         spin_unlock_irqrestore(&phba->hbalock, iflag);
8931                         return IRQ_NONE;
8932                 }
8933                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8934                         phba->HAregaddr);
8935                 readl(phba->HAregaddr); /* flush */
8936                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8937         } else
8938                 ha_copy = phba->ha_copy;
8939
8940         /*
8941          * Process all events on FCP ring. Take the optimized path for FCP IO.
8942          */
8943         ha_copy &= ~(phba->work_ha_mask);
8944
8945         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8946         status >>= (4*LPFC_FCP_RING);
8947         if (status & HA_RXMASK)
8948                 lpfc_sli_handle_fast_ring_event(phba,
8949                                                 &phba->sli.ring[LPFC_FCP_RING],
8950                                                 status);
8951
8952         if (phba->cfg_multi_ring_support == 2) {
8953                 /*
8954                  * Process all events on extra ring. Take the optimized path
8955                  * for extra ring IO.
8956                  */
8957                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8958                 status >>= (4*LPFC_EXTRA_RING);
8959                 if (status & HA_RXMASK) {
8960                         lpfc_sli_handle_fast_ring_event(phba,
8961                                         &phba->sli.ring[LPFC_EXTRA_RING],
8962                                         status);
8963                 }
8964         }
8965         return IRQ_HANDLED;
8966 }  /* lpfc_sli_fp_intr_handler */
8967
8968 /**
8969  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
8970  * @irq: Interrupt number.
8971  * @dev_id: The device context pointer.
8972  *
8973  * This function is the HBA device-level interrupt handler to device with
8974  * SLI-3 interface spec, called from the PCI layer when either MSI or
8975  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8976  * requires driver attention. This function invokes the slow-path interrupt
8977  * attention handling function and fast-path interrupt attention handling
8978  * function in turn to process the relevant HBA attention events. This
8979  * function is called without any lock held. It gets the hbalock to access
8980  * and update SLI data structures.
8981  *
8982  * This function returns IRQ_HANDLED when interrupt is handled, else it
8983  * returns IRQ_NONE.
8984  **/
8985 irqreturn_t
8986 lpfc_sli_intr_handler(int irq, void *dev_id)
8987 {
8988         struct lpfc_hba  *phba;
8989         irqreturn_t sp_irq_rc, fp_irq_rc;
8990         unsigned long status1, status2;
8991         uint32_t hc_copy;
8992
8993         /*
8994          * Get the driver's phba structure from the dev_id and
8995          * assume the HBA is not interrupting.
8996          */
8997         phba = (struct lpfc_hba *) dev_id;
8998
8999         if (unlikely(!phba))
9000                 return IRQ_NONE;
9001
9002         /* Check device state for handling interrupt */
9003         if (lpfc_intr_state_check(phba))
9004                 return IRQ_NONE;
9005
9006         spin_lock(&phba->hbalock);
9007         phba->ha_copy = readl(phba->HAregaddr);
9008         if (unlikely(!phba->ha_copy)) {
9009                 spin_unlock(&phba->hbalock);
9010                 return IRQ_NONE;
9011         } else if (phba->ha_copy & HA_ERATT) {
9012                 if (phba->hba_flag & HBA_ERATT_HANDLED)
9013                         /* ERATT polling has handled ERATT */
9014                         phba->ha_copy &= ~HA_ERATT;
9015                 else
9016                         /* Indicate interrupt handler handles ERATT */
9017                         phba->hba_flag |= HBA_ERATT_HANDLED;
9018         }
9019
9020         /*
9021          * If there is deferred error attention, do not check for any interrupt.
9022          */
9023         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9024                 spin_unlock(&phba->hbalock);
9025                 return IRQ_NONE;
9026         }
9027
9028         /* Clear attention sources except link and error attentions */
9029         hc_copy = readl(phba->HCregaddr);
9030         writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
9031                 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
9032                 phba->HCregaddr);
9033         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
9034         writel(hc_copy, phba->HCregaddr);
9035         readl(phba->HAregaddr); /* flush */
9036         spin_unlock(&phba->hbalock);
9037
9038         /*
9039          * Invokes slow-path host attention interrupt handling as appropriate.
9040          */
9041
9042         /* status of events with mailbox and link attention */
9043         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
9044
9045         /* status of events with ELS ring */
9046         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
9047         status2 >>= (4*LPFC_ELS_RING);
9048
9049         if (status1 || (status2 & HA_RXMASK))
9050                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9051         else
9052                 sp_irq_rc = IRQ_NONE;
9053
9054         /*
9055          * Invoke fast-path host attention interrupt handling as appropriate.
9056          */
9057
9058         /* status of events with FCP ring */
9059         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
9060         status1 >>= (4*LPFC_FCP_RING);
9061
9062         /* status of events with extra ring */
9063         if (phba->cfg_multi_ring_support == 2) {
9064                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
9065                 status2 >>= (4*LPFC_EXTRA_RING);
9066         } else
9067                 status2 = 0;
9068
9069         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
9070                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9071         else
9072                 fp_irq_rc = IRQ_NONE;
9073
9074         /* Return device-level interrupt handling status */
9075         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
9076 }  /* lpfc_sli_intr_handler */
9077
9078 /**
9079  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
9080  * @phba: pointer to lpfc hba data structure.
9081  *
9082  * This routine is invoked by the worker thread to process all the pending
9083  * SLI4 FCP abort XRI events.
9084  **/
9085 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
9086 {
9087         struct lpfc_cq_event *cq_event;
9088
9089         /* First, declare the fcp xri abort event has been handled */
9090         spin_lock_irq(&phba->hbalock);
9091         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
9092         spin_unlock_irq(&phba->hbalock);
9093         /* Now, handle all the fcp xri abort events */
9094         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
9095                 /* Get the first event from the head of the event queue */
9096                 spin_lock_irq(&phba->hbalock);
9097                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
9098                                  cq_event, struct lpfc_cq_event, list);
9099                 spin_unlock_irq(&phba->hbalock);
9100                 /* Notify aborted XRI for FCP work queue */
9101                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9102                 /* Free the event processed back to the free pool */
9103                 lpfc_sli4_cq_event_release(phba, cq_event);
9104         }
9105 }
9106
9107 /**
9108  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
9109  * @phba: pointer to lpfc hba data structure.
9110  *
9111  * This routine is invoked by the worker thread to process all the pending
9112  * SLI4 els abort xri events.
9113  **/
9114 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
9115 {
9116         struct lpfc_cq_event *cq_event;
9117
9118         /* First, declare the els xri abort event has been handled */
9119         spin_lock_irq(&phba->hbalock);
9120         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
9121         spin_unlock_irq(&phba->hbalock);
9122         /* Now, handle all the els xri abort events */
9123         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
9124                 /* Get the first event from the head of the event queue */
9125                 spin_lock_irq(&phba->hbalock);
9126                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
9127                                  cq_event, struct lpfc_cq_event, list);
9128                 spin_unlock_irq(&phba->hbalock);
9129                 /* Notify aborted XRI for ELS work queue */
9130                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
9131                 /* Free the event processed back to the free pool */
9132                 lpfc_sli4_cq_event_release(phba, cq_event);
9133         }
9134 }
9135
9136 /**
9137  * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
9138  * @phba: pointer to lpfc hba data structure
9139  * @pIocbIn: pointer to the rspiocbq
9140  * @pIocbOut: pointer to the cmdiocbq
9141  * @wcqe: pointer to the complete wcqe
9142  *
9143  * This routine transfers the fields of a command iocbq to a response iocbq
9144  * by copying all the IOCB fields from command iocbq and transferring the
9145  * completion status information from the complete wcqe.
9146  **/
9147 static void
9148 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
9149                               struct lpfc_iocbq *pIocbIn,
9150                               struct lpfc_iocbq *pIocbOut,
9151                               struct lpfc_wcqe_complete *wcqe)
9152 {
9153         unsigned long iflags;
9154         size_t offset = offsetof(struct lpfc_iocbq, iocb);
9155
9156         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
9157                sizeof(struct lpfc_iocbq) - offset);
9158         /* Map WCQE parameters into irspiocb parameters */
9159         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
9160         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
9161                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
9162                         pIocbIn->iocb.un.fcpi.fcpi_parm =
9163                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
9164                                         wcqe->total_data_placed;
9165                 else
9166                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
9167         else {
9168                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
9169                 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
9170         }
9171
9172         /* Pick up HBA exchange busy condition */
9173         if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
9174                 spin_lock_irqsave(&phba->hbalock, iflags);
9175                 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
9176                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9177         }
9178 }
9179
9180 /**
9181  * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
9182  * @phba: Pointer to HBA context object.
9183  * @wcqe: Pointer to work-queue completion queue entry.
9184  *
9185  * This routine handles an ELS work-queue completion event and construct
9186  * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
9187  * discovery engine to handle.
9188  *
9189  * Return: Pointer to the receive IOCBQ, NULL otherwise.
9190  **/
9191 static struct lpfc_iocbq *
9192 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
9193                                struct lpfc_iocbq *irspiocbq)
9194 {
9195         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9196         struct lpfc_iocbq *cmdiocbq;
9197         struct lpfc_wcqe_complete *wcqe;
9198         unsigned long iflags;
9199
9200         wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
9201         spin_lock_irqsave(&phba->hbalock, iflags);
9202         pring->stats.iocb_event++;
9203         /* Look up the ELS command IOCB and create pseudo response IOCB */
9204         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9205                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9206         spin_unlock_irqrestore(&phba->hbalock, iflags);
9207
9208         if (unlikely(!cmdiocbq)) {
9209                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9210                                 "0386 ELS complete with no corresponding "
9211                                 "cmdiocb: iotag (%d)\n",
9212                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9213                 lpfc_sli_release_iocbq(phba, irspiocbq);
9214                 return NULL;
9215         }
9216
9217         /* Fake the irspiocbq and copy necessary response information */
9218         lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
9219
9220         return irspiocbq;
9221 }
9222
9223 /**
9224  * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
9225  * @phba: Pointer to HBA context object.
9226  * @cqe: Pointer to mailbox completion queue entry.
9227  *
9228  * This routine process a mailbox completion queue entry with asynchrous
9229  * event.
9230  *
9231  * Return: true if work posted to worker thread, otherwise false.
9232  **/
9233 static bool
9234 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9235 {
9236         struct lpfc_cq_event *cq_event;
9237         unsigned long iflags;
9238
9239         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9240                         "0392 Async Event: word0:x%x, word1:x%x, "
9241                         "word2:x%x, word3:x%x\n", mcqe->word0,
9242                         mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
9243
9244         /* Allocate a new internal CQ_EVENT entry */
9245         cq_event = lpfc_sli4_cq_event_alloc(phba);
9246         if (!cq_event) {
9247                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9248                                 "0394 Failed to allocate CQ_EVENT entry\n");
9249                 return false;
9250         }
9251
9252         /* Move the CQE into an asynchronous event entry */
9253         memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
9254         spin_lock_irqsave(&phba->hbalock, iflags);
9255         list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
9256         /* Set the async event flag */
9257         phba->hba_flag |= ASYNC_EVENT;
9258         spin_unlock_irqrestore(&phba->hbalock, iflags);
9259
9260         return true;
9261 }
9262
9263 /**
9264  * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
9265  * @phba: Pointer to HBA context object.
9266  * @cqe: Pointer to mailbox completion queue entry.
9267  *
9268  * This routine process a mailbox completion queue entry with mailbox
9269  * completion event.
9270  *
9271  * Return: true if work posted to worker thread, otherwise false.
9272  **/
9273 static bool
9274 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
9275 {
9276         uint32_t mcqe_status;
9277         MAILBOX_t *mbox, *pmbox;
9278         struct lpfc_mqe *mqe;
9279         struct lpfc_vport *vport;
9280         struct lpfc_nodelist *ndlp;
9281         struct lpfc_dmabuf *mp;
9282         unsigned long iflags;
9283         LPFC_MBOXQ_t *pmb;
9284         bool workposted = false;
9285         int rc;
9286
9287         /* If not a mailbox complete MCQE, out by checking mailbox consume */
9288         if (!bf_get(lpfc_trailer_completed, mcqe))
9289                 goto out_no_mqe_complete;
9290
9291         /* Get the reference to the active mbox command */
9292         spin_lock_irqsave(&phba->hbalock, iflags);
9293         pmb = phba->sli.mbox_active;
9294         if (unlikely(!pmb)) {
9295                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9296                                 "1832 No pending MBOX command to handle\n");
9297                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9298                 goto out_no_mqe_complete;
9299         }
9300         spin_unlock_irqrestore(&phba->hbalock, iflags);
9301         mqe = &pmb->u.mqe;
9302         pmbox = (MAILBOX_t *)&pmb->u.mqe;
9303         mbox = phba->mbox;
9304         vport = pmb->vport;
9305
9306         /* Reset heartbeat timer */
9307         phba->last_completion_time = jiffies;
9308         del_timer(&phba->sli.mbox_tmo);
9309
9310         /* Move mbox data to caller's mailbox region, do endian swapping */
9311         if (pmb->mbox_cmpl && mbox)
9312                 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
9313         /* Set the mailbox status with SLI4 range 0x4000 */
9314         mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
9315         if (mcqe_status != MB_CQE_STATUS_SUCCESS)
9316                 bf_set(lpfc_mqe_status, mqe,
9317                        (LPFC_MBX_ERROR_RANGE | mcqe_status));
9318
9319         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
9320                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
9321                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
9322                                       "MBOX dflt rpi: status:x%x rpi:x%x",
9323                                       mcqe_status,
9324                                       pmbox->un.varWords[0], 0);
9325                 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
9326                         mp = (struct lpfc_dmabuf *)(pmb->context1);
9327                         ndlp = (struct lpfc_nodelist *)pmb->context2;
9328                         /* Reg_LOGIN of dflt RPI was successful. Now lets get
9329                          * RID of the PPI using the same mbox buffer.
9330                          */
9331                         lpfc_unreg_login(phba, vport->vpi,
9332                                          pmbox->un.varWords[0], pmb);
9333                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
9334                         pmb->context1 = mp;
9335                         pmb->context2 = ndlp;
9336                         pmb->vport = vport;
9337                         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
9338                         if (rc != MBX_BUSY)
9339                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
9340                                                 LOG_SLI, "0385 rc should "
9341                                                 "have been MBX_BUSY\n");
9342                         if (rc != MBX_NOT_FINISHED)
9343                                 goto send_current_mbox;
9344                 }
9345         }
9346         spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9347         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9348         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9349
9350         /* There is mailbox completion work to do */
9351         spin_lock_irqsave(&phba->hbalock, iflags);
9352         __lpfc_mbox_cmpl_put(phba, pmb);
9353         phba->work_ha |= HA_MBATT;
9354         spin_unlock_irqrestore(&phba->hbalock, iflags);
9355         workposted = true;
9356
9357 send_current_mbox:
9358         spin_lock_irqsave(&phba->hbalock, iflags);
9359         /* Release the mailbox command posting token */
9360         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9361         /* Setting active mailbox pointer need to be in sync to flag clear */
9362         phba->sli.mbox_active = NULL;
9363         spin_unlock_irqrestore(&phba->hbalock, iflags);
9364         /* Wake up worker thread to post the next pending mailbox command */
9365         lpfc_worker_wake_up(phba);
9366 out_no_mqe_complete:
9367         if (bf_get(lpfc_trailer_consumed, mcqe))
9368                 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
9369         return workposted;
9370 }
9371
9372 /**
9373  * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
9374  * @phba: Pointer to HBA context object.
9375  * @cqe: Pointer to mailbox completion queue entry.
9376  *
9377  * This routine process a mailbox completion queue entry, it invokes the
9378  * proper mailbox complete handling or asynchrous event handling routine
9379  * according to the MCQE's async bit.
9380  *
9381  * Return: true if work posted to worker thread, otherwise false.
9382  **/
9383 static bool
9384 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
9385 {
9386         struct lpfc_mcqe mcqe;
9387         bool workposted;
9388
9389         /* Copy the mailbox MCQE and convert endian order as needed */
9390         lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
9391
9392         /* Invoke the proper event handling routine */
9393         if (!bf_get(lpfc_trailer_async, &mcqe))
9394                 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
9395         else
9396                 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
9397         return workposted;
9398 }
9399
9400 /**
9401  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9402  * @phba: Pointer to HBA context object.
9403  * @wcqe: Pointer to work-queue completion queue entry.
9404  *
9405  * This routine handles an ELS work-queue completion event.
9406  *
9407  * Return: true if work posted to worker thread, otherwise false.
9408  **/
9409 static bool
9410 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9411                              struct lpfc_wcqe_complete *wcqe)
9412 {
9413         struct lpfc_iocbq *irspiocbq;
9414         unsigned long iflags;
9415         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9416
9417         /* Get an irspiocbq for later ELS response processing use */
9418         irspiocbq = lpfc_sli_get_iocbq(phba);
9419         if (!irspiocbq) {
9420                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9421                         "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9422                         "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9423                         pring->txq_cnt, phba->iocb_cnt,
9424                         phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9425                         phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
9426                 return false;
9427         }
9428
9429         /* Save off the slow-path queue event for work thread to process */
9430         memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
9431         spin_lock_irqsave(&phba->hbalock, iflags);
9432         list_add_tail(&irspiocbq->cq_event.list,
9433                       &phba->sli4_hba.sp_queue_event);
9434         phba->hba_flag |= HBA_SP_QUEUE_EVT;
9435         spin_unlock_irqrestore(&phba->hbalock, iflags);
9436
9437         return true;
9438 }
9439
9440 /**
9441  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9442  * @phba: Pointer to HBA context object.
9443  * @wcqe: Pointer to work-queue completion queue entry.
9444  *
9445  * This routine handles slow-path WQ entry comsumed event by invoking the
9446  * proper WQ release routine to the slow-path WQ.
9447  **/
9448 static void
9449 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9450                              struct lpfc_wcqe_release *wcqe)
9451 {
9452         /* Check for the slow-path ELS work queue */
9453         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9454                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9455                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9456         else
9457                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9458                                 "2579 Slow-path wqe consume event carries "
9459                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9460                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9461                                 phba->sli4_hba.els_wq->queue_id);
9462 }
9463
9464 /**
9465  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9466  * @phba: Pointer to HBA context object.
9467  * @cq: Pointer to a WQ completion queue.
9468  * @wcqe: Pointer to work-queue completion queue entry.
9469  *
9470  * This routine handles an XRI abort event.
9471  *
9472  * Return: true if work posted to worker thread, otherwise false.
9473  **/
9474 static bool
9475 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9476                                    struct lpfc_queue *cq,
9477                                    struct sli4_wcqe_xri_aborted *wcqe)
9478 {
9479         bool workposted = false;
9480         struct lpfc_cq_event *cq_event;
9481         unsigned long iflags;
9482
9483         /* Allocate a new internal CQ_EVENT entry */
9484         cq_event = lpfc_sli4_cq_event_alloc(phba);
9485         if (!cq_event) {
9486                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9487                                 "0602 Failed to allocate CQ_EVENT entry\n");
9488                 return false;
9489         }
9490
9491         /* Move the CQE into the proper xri abort event list */
9492         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9493         switch (cq->subtype) {
9494         case LPFC_FCP:
9495                 spin_lock_irqsave(&phba->hbalock, iflags);
9496                 list_add_tail(&cq_event->list,
9497                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9498                 /* Set the fcp xri abort event flag */
9499                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9500                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9501                 workposted = true;
9502                 break;
9503         case LPFC_ELS:
9504                 spin_lock_irqsave(&phba->hbalock, iflags);
9505                 list_add_tail(&cq_event->list,
9506                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9507                 /* Set the els xri abort event flag */
9508                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9509                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9510                 workposted = true;
9511                 break;
9512         default:
9513                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9514                                 "0603 Invalid work queue CQE subtype (x%x)\n",
9515                                 cq->subtype);
9516                 workposted = false;
9517                 break;
9518         }
9519         return workposted;
9520 }
9521
9522 /**
9523  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
9524  * @phba: Pointer to HBA context object.
9525  * @rcqe: Pointer to receive-queue completion queue entry.
9526  *
9527  * This routine process a receive-queue completion queue entry.
9528  *
9529  * Return: true if work posted to worker thread, otherwise false.
9530  **/
9531 static bool
9532 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
9533 {
9534         bool workposted = false;
9535         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9536         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9537         struct hbq_dmabuf *dma_buf;
9538         uint32_t status;
9539         unsigned long iflags;
9540
9541         if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
9542                 goto out;
9543
9544         status = bf_get(lpfc_rcqe_status, rcqe);
9545         switch (status) {
9546         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9547                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9548                                 "2537 Receive Frame Truncated!!\n");
9549         case FC_STATUS_RQ_SUCCESS:
9550                 lpfc_sli4_rq_release(hrq, drq);
9551                 spin_lock_irqsave(&phba->hbalock, iflags);
9552                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9553                 if (!dma_buf) {
9554                         spin_unlock_irqrestore(&phba->hbalock, iflags);
9555                         goto out;
9556                 }
9557                 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
9558                 /* save off the frame for the word thread to process */
9559                 list_add_tail(&dma_buf->cq_event.list,
9560                               &phba->sli4_hba.sp_queue_event);
9561                 /* Frame received */
9562                 phba->hba_flag |= HBA_SP_QUEUE_EVT;
9563                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9564                 workposted = true;
9565                 break;
9566         case FC_STATUS_INSUFF_BUF_NEED_BUF:
9567         case FC_STATUS_INSUFF_BUF_FRM_DISC:
9568                 /* Post more buffers if possible */
9569                 spin_lock_irqsave(&phba->hbalock, iflags);
9570                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9571                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9572                 workposted = true;
9573                 break;
9574         }
9575 out:
9576         return workposted;
9577 }
9578
9579 /**
9580  * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9581  * @phba: Pointer to HBA context object.
9582  * @cq: Pointer to the completion queue.
9583  * @wcqe: Pointer to a completion queue entry.
9584  *
9585  * This routine process a slow-path work-queue or recieve queue completion queue
9586  * entry.
9587  *
9588  * Return: true if work posted to worker thread, otherwise false.
9589  **/
9590 static bool
9591 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9592                          struct lpfc_cqe *cqe)
9593 {
9594         struct lpfc_cqe cqevt;
9595         bool workposted = false;
9596
9597         /* Copy the work queue CQE and convert endian order if needed */
9598         lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
9599
9600         /* Check and process for different type of WCQE and dispatch */
9601         switch (bf_get(lpfc_cqe_code, &cqevt)) {
9602         case CQE_CODE_COMPL_WQE:
9603                 /* Process the WQ/RQ complete event */
9604                 phba->last_completion_time = jiffies;
9605                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
9606                                 (struct lpfc_wcqe_complete *)&cqevt);
9607                 break;
9608         case CQE_CODE_RELEASE_WQE:
9609                 /* Process the WQ release event */
9610                 lpfc_sli4_sp_handle_rel_wcqe(phba,
9611                                 (struct lpfc_wcqe_release *)&cqevt);
9612                 break;
9613         case CQE_CODE_XRI_ABORTED:
9614                 /* Process the WQ XRI abort event */
9615                 phba->last_completion_time = jiffies;
9616                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9617                                 (struct sli4_wcqe_xri_aborted *)&cqevt);
9618                 break;
9619         case CQE_CODE_RECEIVE:
9620                 /* Process the RQ event */
9621                 phba->last_completion_time = jiffies;
9622                 workposted = lpfc_sli4_sp_handle_rcqe(phba,
9623                                 (struct lpfc_rcqe *)&cqevt);
9624                 break;
9625         default:
9626                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9627                                 "0388 Not a valid WCQE code: x%x\n",
9628                                 bf_get(lpfc_cqe_code, &cqevt));
9629                 break;
9630         }
9631         return workposted;
9632 }
9633
9634 /**
9635  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9636  * @phba: Pointer to HBA context object.
9637  * @eqe: Pointer to fast-path event queue entry.
9638  *
9639  * This routine process a event queue entry from the slow-path event queue.
9640  * It will check the MajorCode and MinorCode to determine this is for a
9641  * completion event on a completion queue, if not, an error shall be logged
9642  * and just return. Otherwise, it will get to the corresponding completion
9643  * queue and process all the entries on that completion queue, rearm the
9644  * completion queue, and then return.
9645  *
9646  **/
9647 static void
9648 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9649 {
9650         struct lpfc_queue *cq = NULL, *childq, *speq;
9651         struct lpfc_cqe *cqe;
9652         bool workposted = false;
9653         int ecount = 0;
9654         uint16_t cqid;
9655
9656         if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
9657                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9658                                 "0359 Not a valid slow-path completion "
9659                                 "event: majorcode=x%x, minorcode=x%x\n",
9660                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9661                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9662                 return;
9663         }
9664
9665         /* Get the reference to the corresponding CQ */
9666         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9667
9668         /* Search for completion queue pointer matching this cqid */
9669         speq = phba->sli4_hba.sp_eq;
9670         list_for_each_entry(childq, &speq->child_list, list) {
9671                 if (childq->queue_id == cqid) {
9672                         cq = childq;
9673                         break;
9674                 }
9675         }
9676         if (unlikely(!cq)) {
9677                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9678                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9679                                         "0365 Slow-path CQ identifier "
9680                                         "(%d) does not exist\n", cqid);
9681                 return;
9682         }
9683
9684         /* Process all the entries to the CQ */
9685         switch (cq->type) {
9686         case LPFC_MCQ:
9687                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9688                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9689                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9690                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9691                 }
9692                 break;
9693         case LPFC_WCQ:
9694                 while ((cqe = lpfc_sli4_cq_get(cq))) {
9695                         workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
9696                         if (!(++ecount % LPFC_GET_QE_REL_INT))
9697                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9698                 }
9699                 break;
9700         default:
9701                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9702                                 "0370 Invalid completion queue type (%d)\n",
9703                                 cq->type);
9704                 return;
9705         }
9706
9707         /* Catch the no cq entry condition, log an error */
9708         if (unlikely(ecount == 0))
9709                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9710                                 "0371 No entry from the CQ: identifier "
9711                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9712
9713         /* In any case, flash and re-arm the RCQ */
9714         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9715
9716         /* wake up worker thread if there are works to be done */
9717         if (workposted)
9718                 lpfc_worker_wake_up(phba);
9719 }
9720
9721 /**
9722  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9723  * @eqe: Pointer to fast-path completion queue entry.
9724  *
9725  * This routine process a fast-path work queue completion entry from fast-path
9726  * event queue for FCP command response completion.
9727  **/
9728 static void
9729 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9730                              struct lpfc_wcqe_complete *wcqe)
9731 {
9732         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9733         struct lpfc_iocbq *cmdiocbq;
9734         struct lpfc_iocbq irspiocbq;
9735         unsigned long iflags;
9736
9737         spin_lock_irqsave(&phba->hbalock, iflags);
9738         pring->stats.iocb_event++;
9739         spin_unlock_irqrestore(&phba->hbalock, iflags);
9740
9741         /* Check for response status */
9742         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9743                 /* If resource errors reported from HBA, reduce queue
9744                  * depth of the SCSI device.
9745                  */
9746                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9747                      IOSTAT_LOCAL_REJECT) &&
9748                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
9749                         phba->lpfc_rampdown_queue_depth(phba);
9750                 }
9751                 /* Log the error status */
9752                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9753                                 "0373 FCP complete error: status=x%x, "
9754                                 "hw_status=x%x, total_data_specified=%d, "
9755                                 "parameter=x%x, word3=x%x\n",
9756                                 bf_get(lpfc_wcqe_c_status, wcqe),
9757                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9758                                 wcqe->total_data_placed, wcqe->parameter,
9759                                 wcqe->word3);
9760         }
9761
9762         /* Look up the FCP command IOCB and create pseudo response IOCB */
9763         spin_lock_irqsave(&phba->hbalock, iflags);
9764         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9765                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9766         spin_unlock_irqrestore(&phba->hbalock, iflags);
9767         if (unlikely(!cmdiocbq)) {
9768                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9769                                 "0374 FCP complete with no corresponding "
9770                                 "cmdiocb: iotag (%d)\n",
9771                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9772                 return;
9773         }
9774         if (unlikely(!cmdiocbq->iocb_cmpl)) {
9775                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9776                                 "0375 FCP cmdiocb not callback function "
9777                                 "iotag: (%d)\n",
9778                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9779                 return;
9780         }
9781
9782         /* Fake the irspiocb and copy necessary response information */
9783         lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
9784
9785         if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9786                 spin_lock_irqsave(&phba->hbalock, iflags);
9787                 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9788                 spin_unlock_irqrestore(&phba->hbalock, iflags);
9789         }
9790
9791         /* Pass the cmd_iocb and the rsp state to the upper layer */
9792         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9793 }
9794
9795 /**
9796  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9797  * @phba: Pointer to HBA context object.
9798  * @cq: Pointer to completion queue.
9799  * @wcqe: Pointer to work-queue completion queue entry.
9800  *
9801  * This routine handles an fast-path WQ entry comsumed event by invoking the
9802  * proper WQ release routine to the slow-path WQ.
9803  **/
9804 static void
9805 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9806                              struct lpfc_wcqe_release *wcqe)
9807 {
9808         struct lpfc_queue *childwq;
9809         bool wqid_matched = false;
9810         uint16_t fcp_wqid;
9811
9812         /* Check for fast-path FCP work queue release */
9813         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9814         list_for_each_entry(childwq, &cq->child_list, list) {
9815                 if (childwq->queue_id == fcp_wqid) {
9816                         lpfc_sli4_wq_release(childwq,
9817                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9818                         wqid_matched = true;
9819                         break;
9820                 }
9821         }
9822         /* Report warning log message if no match found */
9823         if (wqid_matched != true)
9824                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9825                                 "2580 Fast-path wqe consume event carries "
9826                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9827 }
9828
9829 /**
9830  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9831  * @cq: Pointer to the completion queue.
9832  * @eqe: Pointer to fast-path completion queue entry.
9833  *
9834  * This routine process a fast-path work queue completion entry from fast-path
9835  * event queue for FCP command response completion.
9836  **/
9837 static int
9838 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9839                          struct lpfc_cqe *cqe)
9840 {
9841         struct lpfc_wcqe_release wcqe;
9842         bool workposted = false;
9843
9844         /* Copy the work queue CQE and convert endian order if needed */
9845         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9846
9847         /* Check and process for different type of WCQE and dispatch */
9848         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9849         case CQE_CODE_COMPL_WQE:
9850                 /* Process the WQ complete event */
9851                 phba->last_completion_time = jiffies;
9852                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9853                                 (struct lpfc_wcqe_complete *)&wcqe);
9854                 break;
9855         case CQE_CODE_RELEASE_WQE:
9856                 /* Process the WQ release event */
9857                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9858                                 (struct lpfc_wcqe_release *)&wcqe);
9859                 break;
9860         case CQE_CODE_XRI_ABORTED:
9861                 /* Process the WQ XRI abort event */
9862                 phba->last_completion_time = jiffies;
9863                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9864                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
9865                 break;
9866         default:
9867                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9868                                 "0144 Not a valid WCQE code: x%x\n",
9869                                 bf_get(lpfc_wcqe_c_code, &wcqe));
9870                 break;
9871         }
9872         return workposted;
9873 }
9874
9875 /**
9876  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9877  * @phba: Pointer to HBA context object.
9878  * @eqe: Pointer to fast-path event queue entry.
9879  *
9880  * This routine process a event queue entry from the fast-path event queue.
9881  * It will check the MajorCode and MinorCode to determine this is for a
9882  * completion event on a completion queue, if not, an error shall be logged
9883  * and just return. Otherwise, it will get to the corresponding completion
9884  * queue and process all the entries on the completion queue, rearm the
9885  * completion queue, and then return.
9886  **/
9887 static void
9888 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9889                         uint32_t fcp_cqidx)
9890 {
9891         struct lpfc_queue *cq;
9892         struct lpfc_cqe *cqe;
9893         bool workposted = false;
9894         uint16_t cqid;
9895         int ecount = 0;
9896
9897         if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
9898                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9899                                 "0366 Not a valid fast-path completion "
9900                                 "event: majorcode=x%x, minorcode=x%x\n",
9901                                 bf_get_le32(lpfc_eqe_major_code, eqe),
9902                                 bf_get_le32(lpfc_eqe_minor_code, eqe));
9903                 return;
9904         }
9905
9906         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9907         if (unlikely(!cq)) {
9908                 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9909                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9910                                         "0367 Fast-path completion queue "
9911                                         "does not exist\n");
9912                 return;
9913         }
9914
9915         /* Get the reference to the corresponding CQ */
9916         cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
9917         if (unlikely(cqid != cq->queue_id)) {
9918                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9919                                 "0368 Miss-matched fast-path completion "
9920                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9921                                 cqid, cq->queue_id);
9922                 return;
9923         }
9924
9925         /* Process all the entries to the CQ */
9926         while ((cqe = lpfc_sli4_cq_get(cq))) {
9927                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9928                 if (!(++ecount % LPFC_GET_QE_REL_INT))
9929                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9930         }
9931
9932         /* Catch the no cq entry condition */
9933         if (unlikely(ecount == 0))
9934                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9935                                 "0369 No entry from fast-path completion "
9936                                 "queue fcpcqid=%d\n", cq->queue_id);
9937
9938         /* In any case, flash and re-arm the CQ */
9939         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9940
9941         /* wake up worker thread if there are works to be done */
9942         if (workposted)
9943                 lpfc_worker_wake_up(phba);
9944 }
9945
9946 static void
9947 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9948 {
9949         struct lpfc_eqe *eqe;
9950
9951         /* walk all the EQ entries and drop on the floor */
9952         while ((eqe = lpfc_sli4_eq_get(eq)))
9953                 ;
9954
9955         /* Clear and re-arm the EQ */
9956         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9957 }
9958
9959 /**
9960  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9961  * @irq: Interrupt number.
9962  * @dev_id: The device context pointer.
9963  *
9964  * This function is directly called from the PCI layer as an interrupt
9965  * service routine when device with SLI-4 interface spec is enabled with
9966  * MSI-X multi-message interrupt mode and there are slow-path events in
9967  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9968  * interrupt mode, this function is called as part of the device-level
9969  * interrupt handler. When the PCI slot is in error recovery or the HBA is
9970  * undergoing initialization, the interrupt handler will not process the
9971  * interrupt. The link attention and ELS ring attention events are handled
9972  * by the worker thread. The interrupt handler signals the worker thread
9973  * and returns for these events. This function is called without any lock
9974  * held. It gets the hbalock to access and update SLI data structures.
9975  *
9976  * This function returns IRQ_HANDLED when interrupt is handled else it
9977  * returns IRQ_NONE.
9978  **/
9979 irqreturn_t
9980 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9981 {
9982         struct lpfc_hba *phba;
9983         struct lpfc_queue *speq;
9984         struct lpfc_eqe *eqe;
9985         unsigned long iflag;
9986         int ecount = 0;
9987
9988         /*
9989          * Get the driver's phba structure from the dev_id
9990          */
9991         phba = (struct lpfc_hba *)dev_id;
9992
9993         if (unlikely(!phba))
9994                 return IRQ_NONE;
9995
9996         /* Get to the EQ struct associated with this vector */
9997         speq = phba->sli4_hba.sp_eq;
9998
9999         /* Check device state for handling interrupt */
10000         if (unlikely(lpfc_intr_state_check(phba))) {
10001                 /* Check again for link_state with lock held */
10002                 spin_lock_irqsave(&phba->hbalock, iflag);
10003                 if (phba->link_state < LPFC_LINK_DOWN)
10004                         /* Flush, clear interrupt, and rearm the EQ */
10005                         lpfc_sli4_eq_flush(phba, speq);
10006                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10007                 return IRQ_NONE;
10008         }
10009
10010         /*
10011          * Process all the event on FCP slow-path EQ
10012          */
10013         while ((eqe = lpfc_sli4_eq_get(speq))) {
10014                 lpfc_sli4_sp_handle_eqe(phba, eqe);
10015                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10016                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
10017         }
10018
10019         /* Always clear and re-arm the slow-path EQ */
10020         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
10021
10022         /* Catch the no cq entry condition */
10023         if (unlikely(ecount == 0)) {
10024                 if (phba->intr_type == MSIX)
10025                         /* MSI-X treated interrupt served as no EQ share INT */
10026                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10027                                         "0357 MSI-X interrupt with no EQE\n");
10028                 else
10029                         /* Non MSI-X treated on interrupt as EQ share INT */
10030                         return IRQ_NONE;
10031         }
10032
10033         return IRQ_HANDLED;
10034 } /* lpfc_sli4_sp_intr_handler */
10035
10036 /**
10037  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
10038  * @irq: Interrupt number.
10039  * @dev_id: The device context pointer.
10040  *
10041  * This function is directly called from the PCI layer as an interrupt
10042  * service routine when device with SLI-4 interface spec is enabled with
10043  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10044  * ring event in the HBA. However, when the device is enabled with either
10045  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10046  * device-level interrupt handler. When the PCI slot is in error recovery
10047  * or the HBA is undergoing initialization, the interrupt handler will not
10048  * process the interrupt. The SCSI FCP fast-path ring event are handled in
10049  * the intrrupt context. This function is called without any lock held.
10050  * It gets the hbalock to access and update SLI data structures. Note that,
10051  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
10052  * equal to that of FCP CQ index.
10053  *
10054  * This function returns IRQ_HANDLED when interrupt is handled else it
10055  * returns IRQ_NONE.
10056  **/
10057 irqreturn_t
10058 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
10059 {
10060         struct lpfc_hba *phba;
10061         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
10062         struct lpfc_queue *fpeq;
10063         struct lpfc_eqe *eqe;
10064         unsigned long iflag;
10065         int ecount = 0;
10066         uint32_t fcp_eqidx;
10067
10068         /* Get the driver's phba structure from the dev_id */
10069         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
10070         phba = fcp_eq_hdl->phba;
10071         fcp_eqidx = fcp_eq_hdl->idx;
10072
10073         if (unlikely(!phba))
10074                 return IRQ_NONE;
10075
10076         /* Get to the EQ struct associated with this vector */
10077         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
10078
10079         /* Check device state for handling interrupt */
10080         if (unlikely(lpfc_intr_state_check(phba))) {
10081                 /* Check again for link_state with lock held */
10082                 spin_lock_irqsave(&phba->hbalock, iflag);
10083                 if (phba->link_state < LPFC_LINK_DOWN)
10084                         /* Flush, clear interrupt, and rearm the EQ */
10085                         lpfc_sli4_eq_flush(phba, fpeq);
10086                 spin_unlock_irqrestore(&phba->hbalock, iflag);
10087                 return IRQ_NONE;
10088         }
10089
10090         /*
10091          * Process all the event on FCP fast-path EQ
10092          */
10093         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10094                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
10095                 if (!(++ecount % LPFC_GET_QE_REL_INT))
10096                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
10097         }
10098
10099         /* Always clear and re-arm the fast-path EQ */
10100         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
10101
10102         if (unlikely(ecount == 0)) {
10103                 if (phba->intr_type == MSIX)
10104                         /* MSI-X treated interrupt served as no EQ share INT */
10105                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10106                                         "0358 MSI-X interrupt with no EQE\n");
10107                 else
10108                         /* Non MSI-X treated on interrupt as EQ share INT */
10109                         return IRQ_NONE;
10110         }
10111
10112         return IRQ_HANDLED;
10113 } /* lpfc_sli4_fp_intr_handler */
10114
10115 /**
10116  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
10117  * @irq: Interrupt number.
10118  * @dev_id: The device context pointer.
10119  *
10120  * This function is the device-level interrupt handler to device with SLI-4
10121  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
10122  * interrupt mode is enabled and there is an event in the HBA which requires
10123  * driver attention. This function invokes the slow-path interrupt attention
10124  * handling function and fast-path interrupt attention handling function in
10125  * turn to process the relevant HBA attention events. This function is called
10126  * without any lock held. It gets the hbalock to access and update SLI data
10127  * structures.
10128  *
10129  * This function returns IRQ_HANDLED when interrupt is handled, else it
10130  * returns IRQ_NONE.
10131  **/
10132 irqreturn_t
10133 lpfc_sli4_intr_handler(int irq, void *dev_id)
10134 {
10135         struct lpfc_hba  *phba;
10136         irqreturn_t sp_irq_rc, fp_irq_rc;
10137         bool fp_handled = false;
10138         uint32_t fcp_eqidx;
10139
10140         /* Get the driver's phba structure from the dev_id */
10141         phba = (struct lpfc_hba *)dev_id;
10142
10143         if (unlikely(!phba))
10144                 return IRQ_NONE;
10145
10146         /*
10147          * Invokes slow-path host attention interrupt handling as appropriate.
10148          */
10149         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
10150
10151         /*
10152          * Invoke fast-path host attention interrupt handling as appropriate.
10153          */
10154         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
10155                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
10156                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
10157                 if (fp_irq_rc == IRQ_HANDLED)
10158                         fp_handled |= true;
10159         }
10160
10161         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
10162 } /* lpfc_sli4_intr_handler */
10163
10164 /**
10165  * lpfc_sli4_queue_free - free a queue structure and associated memory
10166  * @queue: The queue structure to free.
10167  *
10168  * This function frees a queue structure and the DMAable memory used for
10169  * the host resident queue. This function must be called after destroying the
10170  * queue on the HBA.
10171  **/
10172 void
10173 lpfc_sli4_queue_free(struct lpfc_queue *queue)
10174 {
10175         struct lpfc_dmabuf *dmabuf;
10176
10177         if (!queue)
10178                 return;
10179
10180         while (!list_empty(&queue->page_list)) {
10181                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
10182                                  list);
10183                 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
10184                                   dmabuf->virt, dmabuf->phys);
10185                 kfree(dmabuf);
10186         }
10187         kfree(queue);
10188         return;
10189 }
10190
10191 /**
10192  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
10193  * @phba: The HBA that this queue is being created on.
10194  * @entry_size: The size of each queue entry for this queue.
10195  * @entry count: The number of entries that this queue will handle.
10196  *
10197  * This function allocates a queue structure and the DMAable memory used for
10198  * the host resident queue. This function must be called before creating the
10199  * queue on the HBA.
10200  **/
10201 struct lpfc_queue *
10202 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
10203                       uint32_t entry_count)
10204 {
10205         struct lpfc_queue *queue;
10206         struct lpfc_dmabuf *dmabuf;
10207         int x, total_qe_count;
10208         void *dma_pointer;
10209         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10210
10211         if (!phba->sli4_hba.pc_sli4_params.supported)
10212                 hw_page_size = SLI4_PAGE_SIZE;
10213
10214         queue = kzalloc(sizeof(struct lpfc_queue) +
10215                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
10216         if (!queue)
10217                 return NULL;
10218         queue->page_count = (ALIGN(entry_size * entry_count,
10219                         hw_page_size))/hw_page_size;
10220         INIT_LIST_HEAD(&queue->list);
10221         INIT_LIST_HEAD(&queue->page_list);
10222         INIT_LIST_HEAD(&queue->child_list);
10223         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
10224                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
10225                 if (!dmabuf)
10226                         goto out_fail;
10227                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10228                                                   hw_page_size, &dmabuf->phys,
10229                                                   GFP_KERNEL);
10230                 if (!dmabuf->virt) {
10231                         kfree(dmabuf);
10232                         goto out_fail;
10233                 }
10234                 memset(dmabuf->virt, 0, hw_page_size);
10235                 dmabuf->buffer_tag = x;
10236                 list_add_tail(&dmabuf->list, &queue->page_list);
10237                 /* initialize queue's entry array */
10238                 dma_pointer = dmabuf->virt;
10239                 for (; total_qe_count < entry_count &&
10240                      dma_pointer < (hw_page_size + dmabuf->virt);
10241                      total_qe_count++, dma_pointer += entry_size) {
10242                         queue->qe[total_qe_count].address = dma_pointer;
10243                 }
10244         }
10245         queue->entry_size = entry_size;
10246         queue->entry_count = entry_count;
10247         queue->phba = phba;
10248
10249         return queue;
10250 out_fail:
10251         lpfc_sli4_queue_free(queue);
10252         return NULL;
10253 }
10254
10255 /**
10256  * lpfc_eq_create - Create an Event Queue on the HBA
10257  * @phba: HBA structure that indicates port to create a queue on.
10258  * @eq: The queue structure to use to create the event queue.
10259  * @imax: The maximum interrupt per second limit.
10260  *
10261  * This function creates an event queue, as detailed in @eq, on a port,
10262  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
10263  *
10264  * The @phba struct is used to send mailbox command to HBA. The @eq struct
10265  * is used to get the entry count and entry size that are necessary to
10266  * determine the number of pages to allocate and use for this queue. This
10267  * function will send the EQ_CREATE mailbox command to the HBA to setup the
10268  * event queue. This function is asynchronous and will wait for the mailbox
10269  * command to finish before continuing.
10270  *
10271  * On success this function will return a zero. If unable to allocate enough
10272  * memory this function will return -ENOMEM. If the queue create mailbox command
10273  * fails this function will return -ENXIO.
10274  **/
10275 uint32_t
10276 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
10277 {
10278         struct lpfc_mbx_eq_create *eq_create;
10279         LPFC_MBOXQ_t *mbox;
10280         int rc, length, status = 0;
10281         struct lpfc_dmabuf *dmabuf;
10282         uint32_t shdr_status, shdr_add_status;
10283         union lpfc_sli4_cfg_shdr *shdr;
10284         uint16_t dmult;
10285         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10286
10287         if (!phba->sli4_hba.pc_sli4_params.supported)
10288                 hw_page_size = SLI4_PAGE_SIZE;
10289
10290         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10291         if (!mbox)
10292                 return -ENOMEM;
10293         length = (sizeof(struct lpfc_mbx_eq_create) -
10294                   sizeof(struct lpfc_sli4_cfg_mhdr));
10295         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10296                          LPFC_MBOX_OPCODE_EQ_CREATE,
10297                          length, LPFC_SLI4_MBX_EMBED);
10298         eq_create = &mbox->u.mqe.un.eq_create;
10299         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
10300                eq->page_count);
10301         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
10302                LPFC_EQE_SIZE);
10303         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
10304         /* Calculate delay multiper from maximum interrupt per second */
10305         dmult = LPFC_DMULT_CONST/imax - 1;
10306         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
10307                dmult);
10308         switch (eq->entry_count) {
10309         default:
10310                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10311                                 "0360 Unsupported EQ count. (%d)\n",
10312                                 eq->entry_count);
10313                 if (eq->entry_count < 256)
10314                         return -EINVAL;
10315                 /* otherwise default to smallest count (drop through) */
10316         case 256:
10317                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10318                        LPFC_EQ_CNT_256);
10319                 break;
10320         case 512:
10321                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10322                        LPFC_EQ_CNT_512);
10323                 break;
10324         case 1024:
10325                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10326                        LPFC_EQ_CNT_1024);
10327                 break;
10328         case 2048:
10329                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10330                        LPFC_EQ_CNT_2048);
10331                 break;
10332         case 4096:
10333                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
10334                        LPFC_EQ_CNT_4096);
10335                 break;
10336         }
10337         list_for_each_entry(dmabuf, &eq->page_list, list) {
10338                 memset(dmabuf->virt, 0, hw_page_size);
10339                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10340                                         putPaddrLow(dmabuf->phys);
10341                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10342                                         putPaddrHigh(dmabuf->phys);
10343         }
10344         mbox->vport = phba->pport;
10345         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10346         mbox->context1 = NULL;
10347         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10348         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
10349         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10350         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10351         if (shdr_status || shdr_add_status || rc) {
10352                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10353                                 "2500 EQ_CREATE mailbox failed with "
10354                                 "status x%x add_status x%x, mbx status x%x\n",
10355                                 shdr_status, shdr_add_status, rc);
10356                 status = -ENXIO;
10357         }
10358         eq->type = LPFC_EQ;
10359         eq->subtype = LPFC_NONE;
10360         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
10361         if (eq->queue_id == 0xFFFF)
10362                 status = -ENXIO;
10363         eq->host_index = 0;
10364         eq->hba_index = 0;
10365
10366         mempool_free(mbox, phba->mbox_mem_pool);
10367         return status;
10368 }
10369
10370 /**
10371  * lpfc_cq_create - Create a Completion Queue on the HBA
10372  * @phba: HBA structure that indicates port to create a queue on.
10373  * @cq: The queue structure to use to create the completion queue.
10374  * @eq: The event queue to bind this completion queue to.
10375  *
10376  * This function creates a completion queue, as detailed in @wq, on a port,
10377  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
10378  *
10379  * The @phba struct is used to send mailbox command to HBA. The @cq struct
10380  * is used to get the entry count and entry size that are necessary to
10381  * determine the number of pages to allocate and use for this queue. The @eq
10382  * is used to indicate which event queue to bind this completion queue to. This
10383  * function will send the CQ_CREATE mailbox command to the HBA to setup the
10384  * completion queue. This function is asynchronous and will wait for the mailbox
10385  * command to finish before continuing.
10386  *
10387  * On success this function will return a zero. If unable to allocate enough
10388  * memory this function will return -ENOMEM. If the queue create mailbox command
10389  * fails this function will return -ENXIO.
10390  **/
10391 uint32_t
10392 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
10393                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
10394 {
10395         struct lpfc_mbx_cq_create *cq_create;
10396         struct lpfc_dmabuf *dmabuf;
10397         LPFC_MBOXQ_t *mbox;
10398         int rc, length, status = 0;
10399         uint32_t shdr_status, shdr_add_status;
10400         union lpfc_sli4_cfg_shdr *shdr;
10401         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10402
10403         if (!phba->sli4_hba.pc_sli4_params.supported)
10404                 hw_page_size = SLI4_PAGE_SIZE;
10405
10406
10407         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10408         if (!mbox)
10409                 return -ENOMEM;
10410         length = (sizeof(struct lpfc_mbx_cq_create) -
10411                   sizeof(struct lpfc_sli4_cfg_mhdr));
10412         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10413                          LPFC_MBOX_OPCODE_CQ_CREATE,
10414                          length, LPFC_SLI4_MBX_EMBED);
10415         cq_create = &mbox->u.mqe.un.cq_create;
10416         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10417                     cq->page_count);
10418         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10419         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10420         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10421         switch (cq->entry_count) {
10422         default:
10423                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10424                                 "0361 Unsupported CQ count. (%d)\n",
10425                                 cq->entry_count);
10426                 if (cq->entry_count < 256)
10427                         return -EINVAL;
10428                 /* otherwise default to smallest count (drop through) */
10429         case 256:
10430                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10431                        LPFC_CQ_CNT_256);
10432                 break;
10433         case 512:
10434                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10435                        LPFC_CQ_CNT_512);
10436                 break;
10437         case 1024:
10438                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10439                        LPFC_CQ_CNT_1024);
10440                 break;
10441         }
10442         list_for_each_entry(dmabuf, &cq->page_list, list) {
10443                 memset(dmabuf->virt, 0, hw_page_size);
10444                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10445                                         putPaddrLow(dmabuf->phys);
10446                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10447                                         putPaddrHigh(dmabuf->phys);
10448         }
10449         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10450
10451         /* The IOCTL status is embedded in the mailbox subheader. */
10452         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10453         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10454         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10455         if (shdr_status || shdr_add_status || rc) {
10456                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10457                                 "2501 CQ_CREATE mailbox failed with "
10458                                 "status x%x add_status x%x, mbx status x%x\n",
10459                                 shdr_status, shdr_add_status, rc);
10460                 status = -ENXIO;
10461                 goto out;
10462         }
10463         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10464         if (cq->queue_id == 0xFFFF) {
10465                 status = -ENXIO;
10466                 goto out;
10467         }
10468         /* link the cq onto the parent eq child list */
10469         list_add_tail(&cq->list, &eq->child_list);
10470         /* Set up completion queue's type and subtype */
10471         cq->type = type;
10472         cq->subtype = subtype;
10473         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10474         cq->assoc_qid = eq->queue_id;
10475         cq->host_index = 0;
10476         cq->hba_index = 0;
10477
10478 out:
10479         mempool_free(mbox, phba->mbox_mem_pool);
10480         return status;
10481 }
10482
10483 /**
10484  * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
10485  * @phba: HBA structure that indicates port to create a queue on.
10486  * @mq: The queue structure to use to create the mailbox queue.
10487  * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10488  * @cq: The completion queue to associate with this cq.
10489  *
10490  * This function provides failback (fb) functionality when the
10491  * mq_create_ext fails on older FW generations.  It's purpose is identical
10492  * to mq_create_ext otherwise.
10493  *
10494  * This routine cannot fail as all attributes were previously accessed and
10495  * initialized in mq_create_ext.
10496  **/
10497 static void
10498 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10499                        LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
10500 {
10501         struct lpfc_mbx_mq_create *mq_create;
10502         struct lpfc_dmabuf *dmabuf;
10503         int length;
10504
10505         length = (sizeof(struct lpfc_mbx_mq_create) -
10506                   sizeof(struct lpfc_sli4_cfg_mhdr));
10507         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10508                          LPFC_MBOX_OPCODE_MQ_CREATE,
10509                          length, LPFC_SLI4_MBX_EMBED);
10510         mq_create = &mbox->u.mqe.un.mq_create;
10511         bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
10512                mq->page_count);
10513         bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
10514                cq->queue_id);
10515         bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10516         switch (mq->entry_count) {
10517         case 16:
10518                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10519                        LPFC_MQ_CNT_16);
10520                 break;
10521         case 32:
10522                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10523                        LPFC_MQ_CNT_32);
10524                 break;
10525         case 64:
10526                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10527                        LPFC_MQ_CNT_64);
10528                 break;
10529         case 128:
10530                 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10531                        LPFC_MQ_CNT_128);
10532                 break;
10533         }
10534         list_for_each_entry(dmabuf, &mq->page_list, list) {
10535                 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10536                         putPaddrLow(dmabuf->phys);
10537                 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10538                         putPaddrHigh(dmabuf->phys);
10539         }
10540 }
10541
10542 /**
10543  * lpfc_mq_create - Create a mailbox Queue on the HBA
10544  * @phba: HBA structure that indicates port to create a queue on.
10545  * @mq: The queue structure to use to create the mailbox queue.
10546  * @cq: The completion queue to associate with this cq.
10547  * @subtype: The queue's subtype.
10548  *
10549  * This function creates a mailbox queue, as detailed in @mq, on a port,
10550  * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10551  *
10552  * The @phba struct is used to send mailbox command to HBA. The @cq struct
10553  * is used to get the entry count and entry size that are necessary to
10554  * determine the number of pages to allocate and use for this queue. This
10555  * function will send the MQ_CREATE mailbox command to the HBA to setup the
10556  * mailbox queue. This function is asynchronous and will wait for the mailbox
10557  * command to finish before continuing.
10558  *
10559  * On success this function will return a zero. If unable to allocate enough
10560  * memory this function will return -ENOMEM. If the queue create mailbox command
10561  * fails this function will return -ENXIO.
10562  **/
10563 int32_t
10564 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10565                struct lpfc_queue *cq, uint32_t subtype)
10566 {
10567         struct lpfc_mbx_mq_create *mq_create;
10568         struct lpfc_mbx_mq_create_ext *mq_create_ext;
10569         struct lpfc_dmabuf *dmabuf;
10570         LPFC_MBOXQ_t *mbox;
10571         int rc, length, status = 0;
10572         uint32_t shdr_status, shdr_add_status;
10573         union lpfc_sli4_cfg_shdr *shdr;
10574         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10575
10576         if (!phba->sli4_hba.pc_sli4_params.supported)
10577                 hw_page_size = SLI4_PAGE_SIZE;
10578
10579         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10580         if (!mbox)
10581                 return -ENOMEM;
10582         length = (sizeof(struct lpfc_mbx_mq_create_ext) -
10583                   sizeof(struct lpfc_sli4_cfg_mhdr));
10584         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10585                          LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
10586                          length, LPFC_SLI4_MBX_EMBED);
10587
10588         mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10589         bf_set(lpfc_mbx_mq_create_ext_num_pages,
10590                &mq_create_ext->u.request, mq->page_count);
10591         bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
10592                &mq_create_ext->u.request, 1);
10593         bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
10594                &mq_create_ext->u.request, 1);
10595         bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10596                &mq_create_ext->u.request, 1);
10597         bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
10598                &mq_create_ext->u.request, 1);
10599         bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
10600                &mq_create_ext->u.request, 1);
10601         bf_set(lpfc_mq_context_cq_id,
10602                &mq_create_ext->u.request.context, cq->queue_id);
10603         bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
10604         switch (mq->entry_count) {
10605         default:
10606                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10607                                 "0362 Unsupported MQ count. (%d)\n",
10608                                 mq->entry_count);
10609                 if (mq->entry_count < 16)
10610                         return -EINVAL;
10611                 /* otherwise default to smallest count (drop through) */
10612         case 16:
10613                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10614                        LPFC_MQ_CNT_16);
10615                 break;
10616         case 32:
10617                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10618                        LPFC_MQ_CNT_32);
10619                 break;
10620         case 64:
10621                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10622                        LPFC_MQ_CNT_64);
10623                 break;
10624         case 128:
10625                 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
10626                        LPFC_MQ_CNT_128);
10627                 break;
10628         }
10629         list_for_each_entry(dmabuf, &mq->page_list, list) {
10630                 memset(dmabuf->virt, 0, hw_page_size);
10631                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
10632                                         putPaddrLow(dmabuf->phys);
10633                 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
10634                                         putPaddrHigh(dmabuf->phys);
10635         }
10636         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10637         shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10638         mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10639                               &mq_create_ext->u.response);
10640         if (rc != MBX_SUCCESS) {
10641                 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10642                                 "2795 MQ_CREATE_EXT failed with "
10643                                 "status x%x. Failback to MQ_CREATE.\n",
10644                                 rc);
10645                 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10646                 mq_create = &mbox->u.mqe.un.mq_create;
10647                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10648                 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10649                 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10650                                       &mq_create->u.response);
10651         }
10652
10653         /* The IOCTL status is embedded in the mailbox subheader. */
10654         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10655         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10656         if (shdr_status || shdr_add_status || rc) {
10657                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10658                                 "2502 MQ_CREATE mailbox failed with "
10659                                 "status x%x add_status x%x, mbx status x%x\n",
10660                                 shdr_status, shdr_add_status, rc);
10661                 status = -ENXIO;
10662                 goto out;
10663         }
10664         if (mq->queue_id == 0xFFFF) {
10665                 status = -ENXIO;
10666                 goto out;
10667         }
10668         mq->type = LPFC_MQ;
10669         mq->assoc_qid = cq->queue_id;
10670         mq->subtype = subtype;
10671         mq->host_index = 0;
10672         mq->hba_index = 0;
10673
10674         /* link the mq onto the parent cq child list */
10675         list_add_tail(&mq->list, &cq->child_list);
10676 out:
10677         mempool_free(mbox, phba->mbox_mem_pool);
10678         return status;
10679 }
10680
10681 /**
10682  * lpfc_wq_create - Create a Work Queue on the HBA
10683  * @phba: HBA structure that indicates port to create a queue on.
10684  * @wq: The queue structure to use to create the work queue.
10685  * @cq: The completion queue to bind this work queue to.
10686  * @subtype: The subtype of the work queue indicating its functionality.
10687  *
10688  * This function creates a work queue, as detailed in @wq, on a port, described
10689  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10690  *
10691  * The @phba struct is used to send mailbox command to HBA. The @wq struct
10692  * is used to get the entry count and entry size that are necessary to
10693  * determine the number of pages to allocate and use for this queue. The @cq
10694  * is used to indicate which completion queue to bind this work queue to. This
10695  * function will send the WQ_CREATE mailbox command to the HBA to setup the
10696  * work queue. This function is asynchronous and will wait for the mailbox
10697  * command to finish before continuing.
10698  *
10699  * On success this function will return a zero. If unable to allocate enough
10700  * memory this function will return -ENOMEM. If the queue create mailbox command
10701  * fails this function will return -ENXIO.
10702  **/
10703 uint32_t
10704 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10705                struct lpfc_queue *cq, uint32_t subtype)
10706 {
10707         struct lpfc_mbx_wq_create *wq_create;
10708         struct lpfc_dmabuf *dmabuf;
10709         LPFC_MBOXQ_t *mbox;
10710         int rc, length, status = 0;
10711         uint32_t shdr_status, shdr_add_status;
10712         union lpfc_sli4_cfg_shdr *shdr;
10713         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10714
10715         if (!phba->sli4_hba.pc_sli4_params.supported)
10716                 hw_page_size = SLI4_PAGE_SIZE;
10717
10718         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10719         if (!mbox)
10720                 return -ENOMEM;
10721         length = (sizeof(struct lpfc_mbx_wq_create) -
10722                   sizeof(struct lpfc_sli4_cfg_mhdr));
10723         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10724                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10725                          length, LPFC_SLI4_MBX_EMBED);
10726         wq_create = &mbox->u.mqe.un.wq_create;
10727         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10728                     wq->page_count);
10729         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10730                     cq->queue_id);
10731         list_for_each_entry(dmabuf, &wq->page_list, list) {
10732                 memset(dmabuf->virt, 0, hw_page_size);
10733                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10734                                         putPaddrLow(dmabuf->phys);
10735                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10736                                         putPaddrHigh(dmabuf->phys);
10737         }
10738         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10739         /* The IOCTL status is embedded in the mailbox subheader. */
10740         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10741         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10742         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10743         if (shdr_status || shdr_add_status || rc) {
10744                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10745                                 "2503 WQ_CREATE mailbox failed with "
10746                                 "status x%x add_status x%x, mbx status x%x\n",
10747                                 shdr_status, shdr_add_status, rc);
10748                 status = -ENXIO;
10749                 goto out;
10750         }
10751         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10752         if (wq->queue_id == 0xFFFF) {
10753                 status = -ENXIO;
10754                 goto out;
10755         }
10756         wq->type = LPFC_WQ;
10757         wq->assoc_qid = cq->queue_id;
10758         wq->subtype = subtype;
10759         wq->host_index = 0;
10760         wq->hba_index = 0;
10761
10762         /* link the wq onto the parent cq child list */
10763         list_add_tail(&wq->list, &cq->child_list);
10764 out:
10765         mempool_free(mbox, phba->mbox_mem_pool);
10766         return status;
10767 }
10768
10769 /**
10770  * lpfc_rq_create - Create a Receive Queue on the HBA
10771  * @phba: HBA structure that indicates port to create a queue on.
10772  * @hrq: The queue structure to use to create the header receive queue.
10773  * @drq: The queue structure to use to create the data receive queue.
10774  * @cq: The completion queue to bind this work queue to.
10775  *
10776  * This function creates a receive buffer queue pair , as detailed in @hrq and
10777  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10778  * to the HBA.
10779  *
10780  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10781  * struct is used to get the entry count that is necessary to determine the
10782  * number of pages to use for this queue. The @cq is used to indicate which
10783  * completion queue to bind received buffers that are posted to these queues to.
10784  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10785  * receive queue pair. This function is asynchronous and will wait for the
10786  * mailbox command to finish before continuing.
10787  *
10788  * On success this function will return a zero. If unable to allocate enough
10789  * memory this function will return -ENOMEM. If the queue create mailbox command
10790  * fails this function will return -ENXIO.
10791  **/
10792 uint32_t
10793 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10794                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10795 {
10796         struct lpfc_mbx_rq_create *rq_create;
10797         struct lpfc_dmabuf *dmabuf;
10798         LPFC_MBOXQ_t *mbox;
10799         int rc, length, status = 0;
10800         uint32_t shdr_status, shdr_add_status;
10801         union lpfc_sli4_cfg_shdr *shdr;
10802         uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10803
10804         if (!phba->sli4_hba.pc_sli4_params.supported)
10805                 hw_page_size = SLI4_PAGE_SIZE;
10806
10807         if (hrq->entry_count != drq->entry_count)
10808                 return -EINVAL;
10809         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10810         if (!mbox)
10811                 return -ENOMEM;
10812         length = (sizeof(struct lpfc_mbx_rq_create) -
10813                   sizeof(struct lpfc_sli4_cfg_mhdr));
10814         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10815                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10816                          length, LPFC_SLI4_MBX_EMBED);
10817         rq_create = &mbox->u.mqe.un.rq_create;
10818         switch (hrq->entry_count) {
10819         default:
10820                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10821                                 "2535 Unsupported RQ count. (%d)\n",
10822                                 hrq->entry_count);
10823                 if (hrq->entry_count < 512)
10824                         return -EINVAL;
10825                 /* otherwise default to smallest count (drop through) */
10826         case 512:
10827                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10828                        LPFC_RQ_RING_SIZE_512);
10829                 break;
10830         case 1024:
10831                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10832                        LPFC_RQ_RING_SIZE_1024);
10833                 break;
10834         case 2048:
10835                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10836                        LPFC_RQ_RING_SIZE_2048);
10837                 break;
10838         case 4096:
10839                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10840                        LPFC_RQ_RING_SIZE_4096);
10841                 break;
10842         }
10843         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10844                cq->queue_id);
10845         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10846                hrq->page_count);
10847         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10848                LPFC_HDR_BUF_SIZE);
10849         list_for_each_entry(dmabuf, &hrq->page_list, list) {
10850                 memset(dmabuf->virt, 0, hw_page_size);
10851                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10852                                         putPaddrLow(dmabuf->phys);
10853                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10854                                         putPaddrHigh(dmabuf->phys);
10855         }
10856         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10857         /* The IOCTL status is embedded in the mailbox subheader. */
10858         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10859         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10860         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10861         if (shdr_status || shdr_add_status || rc) {
10862                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10863                                 "2504 RQ_CREATE mailbox failed with "
10864                                 "status x%x add_status x%x, mbx status x%x\n",
10865                                 shdr_status, shdr_add_status, rc);
10866                 status = -ENXIO;
10867                 goto out;
10868         }
10869         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10870         if (hrq->queue_id == 0xFFFF) {
10871                 status = -ENXIO;
10872                 goto out;
10873         }
10874         hrq->type = LPFC_HRQ;
10875         hrq->assoc_qid = cq->queue_id;
10876         hrq->subtype = subtype;
10877         hrq->host_index = 0;
10878         hrq->hba_index = 0;
10879
10880         /* now create the data queue */
10881         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10882                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10883                          length, LPFC_SLI4_MBX_EMBED);
10884         switch (drq->entry_count) {
10885         default:
10886                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10887                                 "2536 Unsupported RQ count. (%d)\n",
10888                                 drq->entry_count);
10889                 if (drq->entry_count < 512)
10890                         return -EINVAL;
10891                 /* otherwise default to smallest count (drop through) */
10892         case 512:
10893                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10894                        LPFC_RQ_RING_SIZE_512);
10895                 break;
10896         case 1024:
10897                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10898                        LPFC_RQ_RING_SIZE_1024);
10899                 break;
10900         case 2048:
10901                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10902                        LPFC_RQ_RING_SIZE_2048);
10903                 break;
10904         case 4096:
10905                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10906                        LPFC_RQ_RING_SIZE_4096);
10907                 break;
10908         }
10909         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10910                cq->queue_id);
10911         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10912                drq->page_count);
10913         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10914                LPFC_DATA_BUF_SIZE);
10915         list_for_each_entry(dmabuf, &drq->page_list, list) {
10916                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10917                                         putPaddrLow(dmabuf->phys);
10918                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10919                                         putPaddrHigh(dmabuf->phys);
10920         }
10921         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10922         /* The IOCTL status is embedded in the mailbox subheader. */
10923         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10924         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10925         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10926         if (shdr_status || shdr_add_status || rc) {
10927                 status = -ENXIO;
10928                 goto out;
10929         }
10930         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10931         if (drq->queue_id == 0xFFFF) {
10932                 status = -ENXIO;
10933                 goto out;
10934         }
10935         drq->type = LPFC_DRQ;
10936         drq->assoc_qid = cq->queue_id;
10937         drq->subtype = subtype;
10938         drq->host_index = 0;
10939         drq->hba_index = 0;
10940
10941         /* link the header and data RQs onto the parent cq child list */
10942         list_add_tail(&hrq->list, &cq->child_list);
10943         list_add_tail(&drq->list, &cq->child_list);
10944
10945 out:
10946         mempool_free(mbox, phba->mbox_mem_pool);
10947         return status;
10948 }
10949
10950 /**
10951  * lpfc_eq_destroy - Destroy an event Queue on the HBA
10952  * @eq: The queue structure associated with the queue to destroy.
10953  *
10954  * This function destroys a queue, as detailed in @eq by sending an mailbox
10955  * command, specific to the type of queue, to the HBA.
10956  *
10957  * The @eq struct is used to get the queue ID of the queue to destroy.
10958  *
10959  * On success this function will return a zero. If the queue destroy mailbox
10960  * command fails this function will return -ENXIO.
10961  **/
10962 uint32_t
10963 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10964 {
10965         LPFC_MBOXQ_t *mbox;
10966         int rc, length, status = 0;
10967         uint32_t shdr_status, shdr_add_status;
10968         union lpfc_sli4_cfg_shdr *shdr;
10969
10970         if (!eq)
10971                 return -ENODEV;
10972         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10973         if (!mbox)
10974                 return -ENOMEM;
10975         length = (sizeof(struct lpfc_mbx_eq_destroy) -
10976                   sizeof(struct lpfc_sli4_cfg_mhdr));
10977         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10978                          LPFC_MBOX_OPCODE_EQ_DESTROY,
10979                          length, LPFC_SLI4_MBX_EMBED);
10980         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10981                eq->queue_id);
10982         mbox->vport = eq->phba->pport;
10983         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10984
10985         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10986         /* The IOCTL status is embedded in the mailbox subheader. */
10987         shdr = (union lpfc_sli4_cfg_shdr *)
10988                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10989         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10990         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10991         if (shdr_status || shdr_add_status || rc) {
10992                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10993                                 "2505 EQ_DESTROY mailbox failed with "
10994                                 "status x%x add_status x%x, mbx status x%x\n",
10995                                 shdr_status, shdr_add_status, rc);
10996                 status = -ENXIO;
10997         }
10998
10999         /* Remove eq from any list */
11000         list_del_init(&eq->list);
11001         mempool_free(mbox, eq->phba->mbox_mem_pool);
11002         return status;
11003 }
11004
11005 /**
11006  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
11007  * @cq: The queue structure associated with the queue to destroy.
11008  *
11009  * This function destroys a queue, as detailed in @cq by sending an mailbox
11010  * command, specific to the type of queue, to the HBA.
11011  *
11012  * The @cq struct is used to get the queue ID of the queue to destroy.
11013  *
11014  * On success this function will return a zero. If the queue destroy mailbox
11015  * command fails this function will return -ENXIO.
11016  **/
11017 uint32_t
11018 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
11019 {
11020         LPFC_MBOXQ_t *mbox;
11021         int rc, length, status = 0;
11022         uint32_t shdr_status, shdr_add_status;
11023         union lpfc_sli4_cfg_shdr *shdr;
11024
11025         if (!cq)
11026                 return -ENODEV;
11027         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
11028         if (!mbox)
11029                 return -ENOMEM;
11030         length = (sizeof(struct lpfc_mbx_cq_destroy) -
11031                   sizeof(struct lpfc_sli4_cfg_mhdr));
11032         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11033                          LPFC_MBOX_OPCODE_CQ_DESTROY,
11034                          length, LPFC_SLI4_MBX_EMBED);
11035         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
11036                cq->queue_id);
11037         mbox->vport = cq->phba->pport;
11038         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11039         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
11040         /* The IOCTL status is embedded in the mailbox subheader. */
11041         shdr = (union lpfc_sli4_cfg_shdr *)
11042                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
11043         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11044         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11045         if (shdr_status || shdr_add_status || rc) {
11046                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11047                                 "2506 CQ_DESTROY mailbox failed with "
11048                                 "status x%x add_status x%x, mbx status x%x\n",
11049                                 shdr_status, shdr_add_status, rc);
11050                 status = -ENXIO;
11051         }
11052         /* Remove cq from any list */
11053         list_del_init(&cq->list);
11054         mempool_free(mbox, cq->phba->mbox_mem_pool);
11055         return status;
11056 }
11057
11058 /**
11059  * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
11060  * @qm: The queue structure associated with the queue to destroy.
11061  *
11062  * This function destroys a queue, as detailed in @mq by sending an mailbox
11063  * command, specific to the type of queue, to the HBA.
11064  *
11065  * The @mq struct is used to get the queue ID of the queue to destroy.
11066  *
11067  * On success this function will return a zero. If the queue destroy mailbox
11068  * command fails this function will return -ENXIO.
11069  **/
11070 uint32_t
11071 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
11072 {
11073         LPFC_MBOXQ_t *mbox;
11074         int rc, length, status = 0;
11075         uint32_t shdr_status, shdr_add_status;
11076         union lpfc_sli4_cfg_shdr *shdr;
11077
11078         if (!mq)
11079                 return -ENODEV;
11080         mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
11081         if (!mbox)
11082                 return -ENOMEM;
11083         length = (sizeof(struct lpfc_mbx_mq_destroy) -
11084                   sizeof(struct lpfc_sli4_cfg_mhdr));
11085         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11086                          LPFC_MBOX_OPCODE_MQ_DESTROY,
11087                          length, LPFC_SLI4_MBX_EMBED);
11088         bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
11089                mq->queue_id);
11090         mbox->vport = mq->phba->pport;
11091         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11092         rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
11093         /* The IOCTL status is embedded in the mailbox subheader. */
11094         shdr = (union lpfc_sli4_cfg_shdr *)
11095                 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
11096         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11097         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11098         if (shdr_status || shdr_add_status || rc) {
11099                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11100                                 "2507 MQ_DESTROY mailbox failed with "
11101                                 "status x%x add_status x%x, mbx status x%x\n",
11102                                 shdr_status, shdr_add_status, rc);
11103                 status = -ENXIO;
11104         }
11105         /* Remove mq from any list */
11106         list_del_init(&mq->list);
11107         mempool_free(mbox, mq->phba->mbox_mem_pool);
11108         return status;
11109 }
11110
11111 /**
11112  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
11113  * @wq: The queue structure associated with the queue to destroy.
11114  *
11115  * This function destroys a queue, as detailed in @wq by sending an mailbox
11116  * command, specific to the type of queue, to the HBA.
11117  *
11118  * The @wq struct is used to get the queue ID of the queue to destroy.
11119  *
11120  * On success this function will return a zero. If the queue destroy mailbox
11121  * command fails this function will return -ENXIO.
11122  **/
11123 uint32_t
11124 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
11125 {
11126         LPFC_MBOXQ_t *mbox;
11127         int rc, length, status = 0;
11128         uint32_t shdr_status, shdr_add_status;
11129         union lpfc_sli4_cfg_shdr *shdr;
11130
11131         if (!wq)
11132                 return -ENODEV;
11133         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
11134         if (!mbox)
11135                 return -ENOMEM;
11136         length = (sizeof(struct lpfc_mbx_wq_destroy) -
11137                   sizeof(struct lpfc_sli4_cfg_mhdr));
11138         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11139                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
11140                          length, LPFC_SLI4_MBX_EMBED);
11141         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
11142                wq->queue_id);
11143         mbox->vport = wq->phba->pport;
11144         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11145         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
11146         shdr = (union lpfc_sli4_cfg_shdr *)
11147                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
11148         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11149         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11150         if (shdr_status || shdr_add_status || rc) {
11151                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11152                                 "2508 WQ_DESTROY mailbox failed with "
11153                                 "status x%x add_status x%x, mbx status x%x\n",
11154                                 shdr_status, shdr_add_status, rc);
11155                 status = -ENXIO;
11156         }
11157         /* Remove wq from any list */
11158         list_del_init(&wq->list);
11159         mempool_free(mbox, wq->phba->mbox_mem_pool);
11160         return status;
11161 }
11162
11163 /**
11164  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
11165  * @rq: The queue structure associated with the queue to destroy.
11166  *
11167  * This function destroys a queue, as detailed in @rq by sending an mailbox
11168  * command, specific to the type of queue, to the HBA.
11169  *
11170  * The @rq struct is used to get the queue ID of the queue to destroy.
11171  *
11172  * On success this function will return a zero. If the queue destroy mailbox
11173  * command fails this function will return -ENXIO.
11174  **/
11175 uint32_t
11176 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
11177                 struct lpfc_queue *drq)
11178 {
11179         LPFC_MBOXQ_t *mbox;
11180         int rc, length, status = 0;
11181         uint32_t shdr_status, shdr_add_status;
11182         union lpfc_sli4_cfg_shdr *shdr;
11183
11184         if (!hrq || !drq)
11185                 return -ENODEV;
11186         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
11187         if (!mbox)
11188                 return -ENOMEM;
11189         length = (sizeof(struct lpfc_mbx_rq_destroy) -
11190                   sizeof(struct lpfc_sli4_cfg_mhdr));
11191         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11192                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
11193                          length, LPFC_SLI4_MBX_EMBED);
11194         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11195                hrq->queue_id);
11196         mbox->vport = hrq->phba->pport;
11197         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11198         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
11199         /* The IOCTL status is embedded in the mailbox subheader. */
11200         shdr = (union lpfc_sli4_cfg_shdr *)
11201                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11202         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11203         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11204         if (shdr_status || shdr_add_status || rc) {
11205                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11206                                 "2509 RQ_DESTROY mailbox failed with "
11207                                 "status x%x add_status x%x, mbx status x%x\n",
11208                                 shdr_status, shdr_add_status, rc);
11209                 if (rc != MBX_TIMEOUT)
11210                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
11211                 return -ENXIO;
11212         }
11213         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
11214                drq->queue_id);
11215         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
11216         shdr = (union lpfc_sli4_cfg_shdr *)
11217                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
11218         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11219         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11220         if (shdr_status || shdr_add_status || rc) {
11221                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11222                                 "2510 RQ_DESTROY mailbox failed with "
11223                                 "status x%x add_status x%x, mbx status x%x\n",
11224                                 shdr_status, shdr_add_status, rc);
11225                 status = -ENXIO;
11226         }
11227         list_del_init(&hrq->list);
11228         list_del_init(&drq->list);
11229         mempool_free(mbox, hrq->phba->mbox_mem_pool);
11230         return status;
11231 }
11232
11233 /**
11234  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
11235  * @phba: The virtual port for which this call being executed.
11236  * @pdma_phys_addr0: Physical address of the 1st SGL page.
11237  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
11238  * @xritag: the xritag that ties this io to the SGL pages.
11239  *
11240  * This routine will post the sgl pages for the IO that has the xritag
11241  * that is in the iocbq structure. The xritag is assigned during iocbq
11242  * creation and persists for as long as the driver is loaded.
11243  * if the caller has fewer than 256 scatter gather segments to map then
11244  * pdma_phys_addr1 should be 0.
11245  * If the caller needs to map more than 256 scatter gather segment then
11246  * pdma_phys_addr1 should be a valid physical address.
11247  * physical address for SGLs must be 64 byte aligned.
11248  * If you are going to map 2 SGL's then the first one must have 256 entries
11249  * the second sgl can have between 1 and 256 entries.
11250  *
11251  * Return codes:
11252  *      0 - Success
11253  *      -ENXIO, -ENOMEM - Failure
11254  **/
11255 int
11256 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
11257                 dma_addr_t pdma_phys_addr0,
11258                 dma_addr_t pdma_phys_addr1,
11259                 uint16_t xritag)
11260 {
11261         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
11262         LPFC_MBOXQ_t *mbox;
11263         int rc;
11264         uint32_t shdr_status, shdr_add_status;
11265         union lpfc_sli4_cfg_shdr *shdr;
11266
11267         if (xritag == NO_XRI) {
11268                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11269                                 "0364 Invalid param:\n");
11270                 return -EINVAL;
11271         }
11272
11273         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11274         if (!mbox)
11275                 return -ENOMEM;
11276
11277         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11278                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
11279                         sizeof(struct lpfc_mbx_post_sgl_pages) -
11280                         sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
11281
11282         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
11283                                 &mbox->u.mqe.un.post_sgl_pages;
11284         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
11285         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
11286
11287         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
11288                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
11289         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
11290                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
11291
11292         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
11293                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
11294         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
11295                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
11296         if (!phba->sli4_hba.intr_enable)
11297                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11298         else
11299                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
11300         /* The IOCTL status is embedded in the mailbox subheader. */
11301         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
11302         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11303         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11304         if (rc != MBX_TIMEOUT)
11305                 mempool_free(mbox, phba->mbox_mem_pool);
11306         if (shdr_status || shdr_add_status || rc) {
11307                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11308                                 "2511 POST_SGL mailbox failed with "
11309                                 "status x%x add_status x%x, mbx status x%x\n",
11310                                 shdr_status, shdr_add_status, rc);
11311                 rc = -ENXIO;
11312         }
11313         return 0;
11314 }
11315
11316 /**
11317  * lpfc_sli4_next_xritag - Get an xritag for the io
11318  * @phba: Pointer to HBA context object.
11319  *
11320  * This function gets an xritag for the iocb. If there is no unused xritag
11321  * it will return 0xffff.
11322  * The function returns the allocated xritag if successful, else returns zero.
11323  * Zero is not a valid xritag.
11324  * The caller is not required to hold any lock.
11325  **/
11326 uint16_t
11327 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
11328 {
11329         uint16_t xritag;
11330
11331         spin_lock_irq(&phba->hbalock);
11332         xritag = phba->sli4_hba.next_xri;
11333         if ((xritag != (uint16_t) -1) && xritag <
11334                 (phba->sli4_hba.max_cfg_param.max_xri
11335                         + phba->sli4_hba.max_cfg_param.xri_base)) {
11336                 phba->sli4_hba.next_xri++;
11337                 phba->sli4_hba.max_cfg_param.xri_used++;
11338                 spin_unlock_irq(&phba->hbalock);
11339                 return xritag;
11340         }
11341         spin_unlock_irq(&phba->hbalock);
11342         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11343                         "2004 Failed to allocate XRI.last XRITAG is %d"
11344                         " Max XRI is %d, Used XRI is %d\n",
11345                         phba->sli4_hba.next_xri,
11346                         phba->sli4_hba.max_cfg_param.max_xri,
11347                         phba->sli4_hba.max_cfg_param.xri_used);
11348         return -1;
11349 }
11350
11351 /**
11352  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
11353  * @phba: pointer to lpfc hba data structure.
11354  *
11355  * This routine is invoked to post a block of driver's sgl pages to the
11356  * HBA using non-embedded mailbox command. No Lock is held. This routine
11357  * is only called when the driver is loading and after all IO has been
11358  * stopped.
11359  **/
11360 int
11361 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
11362 {
11363         struct lpfc_sglq *sglq_entry;
11364         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11365         struct sgl_page_pairs *sgl_pg_pairs;
11366         void *viraddr;
11367         LPFC_MBOXQ_t *mbox;
11368         uint32_t reqlen, alloclen, pg_pairs;
11369         uint32_t mbox_tmo;
11370         uint16_t xritag_start = 0;
11371         int els_xri_cnt, rc = 0;
11372         uint32_t shdr_status, shdr_add_status;
11373         union lpfc_sli4_cfg_shdr *shdr;
11374
11375         /* The number of sgls to be posted */
11376         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
11377
11378         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
11379                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
11380         if (reqlen > SLI4_PAGE_SIZE) {
11381                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11382                                 "2559 Block sgl registration required DMA "
11383                                 "size (%d) great than a page\n", reqlen);
11384                 return -ENOMEM;
11385         }
11386         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11387         if (!mbox) {
11388                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11389                                 "2560 Failed to allocate mbox cmd memory\n");
11390                 return -ENOMEM;
11391         }
11392
11393         /* Allocate DMA memory and set up the non-embedded mailbox command */
11394         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11395                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11396                          LPFC_SLI4_MBX_NEMBED);
11397
11398         if (alloclen < reqlen) {
11399                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11400                                 "0285 Allocated DMA memory size (%d) is "
11401                                 "less than the requested DMA memory "
11402                                 "size (%d)\n", alloclen, reqlen);
11403                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11404                 return -ENOMEM;
11405         }
11406         /* Get the first SGE entry from the non-embedded DMA memory */
11407         viraddr = mbox->sge_array->addr[0];
11408
11409         /* Set up the SGL pages in the non-embedded DMA pages */
11410         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11411         sgl_pg_pairs = &sgl->sgl_pg_pairs;
11412
11413         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11414                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11415                 /* Set up the sge entry */
11416                 sgl_pg_pairs->sgl_pg0_addr_lo =
11417                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11418                 sgl_pg_pairs->sgl_pg0_addr_hi =
11419                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11420                 sgl_pg_pairs->sgl_pg1_addr_lo =
11421                                 cpu_to_le32(putPaddrLow(0));
11422                 sgl_pg_pairs->sgl_pg1_addr_hi =
11423                                 cpu_to_le32(putPaddrHigh(0));
11424                 /* Keep the first xritag on the list */
11425                 if (pg_pairs == 0)
11426                         xritag_start = sglq_entry->sli4_xritag;
11427                 sgl_pg_pairs++;
11428         }
11429         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11430         bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
11431         /* Perform endian conversion if necessary */
11432         sgl->word0 = cpu_to_le32(sgl->word0);
11433
11434         if (!phba->sli4_hba.intr_enable)
11435                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11436         else {
11437                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11438                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11439         }
11440         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11441         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11442         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11443         if (rc != MBX_TIMEOUT)
11444                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11445         if (shdr_status || shdr_add_status || rc) {
11446                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11447                                 "2513 POST_SGL_BLOCK mailbox command failed "
11448                                 "status x%x add_status x%x mbx status x%x\n",
11449                                 shdr_status, shdr_add_status, rc);
11450                 rc = -ENXIO;
11451         }
11452         return rc;
11453 }
11454
11455 /**
11456  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11457  * @phba: pointer to lpfc hba data structure.
11458  * @sblist: pointer to scsi buffer list.
11459  * @count: number of scsi buffers on the list.
11460  *
11461  * This routine is invoked to post a block of @count scsi sgl pages from a
11462  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11463  * No Lock is held.
11464  *
11465  **/
11466 int
11467 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11468                               int cnt)
11469 {
11470         struct lpfc_scsi_buf *psb;
11471         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11472         struct sgl_page_pairs *sgl_pg_pairs;
11473         void *viraddr;
11474         LPFC_MBOXQ_t *mbox;
11475         uint32_t reqlen, alloclen, pg_pairs;
11476         uint32_t mbox_tmo;
11477         uint16_t xritag_start = 0;
11478         int rc = 0;
11479         uint32_t shdr_status, shdr_add_status;
11480         dma_addr_t pdma_phys_bpl1;
11481         union lpfc_sli4_cfg_shdr *shdr;
11482
11483         /* Calculate the requested length of the dma memory */
11484         reqlen = cnt * sizeof(struct sgl_page_pairs) +
11485                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
11486         if (reqlen > SLI4_PAGE_SIZE) {
11487                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11488                                 "0217 Block sgl registration required DMA "
11489                                 "size (%d) great than a page\n", reqlen);
11490                 return -ENOMEM;
11491         }
11492         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11493         if (!mbox) {
11494                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11495                                 "0283 Failed to allocate mbox cmd memory\n");
11496                 return -ENOMEM;
11497         }
11498
11499         /* Allocate DMA memory and set up the non-embedded mailbox command */
11500         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11501                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11502                                 LPFC_SLI4_MBX_NEMBED);
11503
11504         if (alloclen < reqlen) {
11505                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11506                                 "2561 Allocated DMA memory size (%d) is "
11507                                 "less than the requested DMA memory "
11508                                 "size (%d)\n", alloclen, reqlen);
11509                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11510                 return -ENOMEM;
11511         }
11512         /* Get the first SGE entry from the non-embedded DMA memory */
11513         viraddr = mbox->sge_array->addr[0];
11514
11515         /* Set up the SGL pages in the non-embedded DMA pages */
11516         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11517         sgl_pg_pairs = &sgl->sgl_pg_pairs;
11518
11519         pg_pairs = 0;
11520         list_for_each_entry(psb, sblist, list) {
11521                 /* Set up the sge entry */
11522                 sgl_pg_pairs->sgl_pg0_addr_lo =
11523                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11524                 sgl_pg_pairs->sgl_pg0_addr_hi =
11525                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11526                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11527                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11528                 else
11529                         pdma_phys_bpl1 = 0;
11530                 sgl_pg_pairs->sgl_pg1_addr_lo =
11531                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11532                 sgl_pg_pairs->sgl_pg1_addr_hi =
11533                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11534                 /* Keep the first xritag on the list */
11535                 if (pg_pairs == 0)
11536                         xritag_start = psb->cur_iocbq.sli4_xritag;
11537                 sgl_pg_pairs++;
11538                 pg_pairs++;
11539         }
11540         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11541         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11542         /* Perform endian conversion if necessary */
11543         sgl->word0 = cpu_to_le32(sgl->word0);
11544
11545         if (!phba->sli4_hba.intr_enable)
11546                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11547         else {
11548                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11549                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11550         }
11551         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11552         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11553         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11554         if (rc != MBX_TIMEOUT)
11555                 lpfc_sli4_mbox_cmd_free(phba, mbox);
11556         if (shdr_status || shdr_add_status || rc) {
11557                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11558                                 "2564 POST_SGL_BLOCK mailbox command failed "
11559                                 "status x%x add_status x%x mbx status x%x\n",
11560                                 shdr_status, shdr_add_status, rc);
11561                 rc = -ENXIO;
11562         }
11563         return rc;
11564 }
11565
11566 /**
11567  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11568  * @phba: pointer to lpfc_hba struct that the frame was received on
11569  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11570  *
11571  * This function checks the fields in the @fc_hdr to see if the FC frame is a
11572  * valid type of frame that the LPFC driver will handle. This function will
11573  * return a zero if the frame is a valid frame or a non zero value when the
11574  * frame does not pass the check.
11575  **/
11576 static int
11577 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11578 {
11579         /*  make rctl_names static to save stack space */
11580         static char *rctl_names[] = FC_RCTL_NAMES_INIT;
11581         char *type_names[] = FC_TYPE_NAMES_INIT;
11582         struct fc_vft_header *fc_vft_hdr;
11583
11584         switch (fc_hdr->fh_r_ctl) {
11585         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
11586         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
11587         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
11588         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
11589         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
11590         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
11591         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
11592         case FC_RCTL_DD_CMD_STATUS:     /* command status */
11593         case FC_RCTL_ELS_REQ:   /* extended link services request */
11594         case FC_RCTL_ELS_REP:   /* extended link services reply */
11595         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
11596         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
11597         case FC_RCTL_BA_NOP:    /* basic link service NOP */
11598         case FC_RCTL_BA_ABTS:   /* basic link service abort */
11599         case FC_RCTL_BA_RMC:    /* remove connection */
11600         case FC_RCTL_BA_ACC:    /* basic accept */
11601         case FC_RCTL_BA_RJT:    /* basic reject */
11602         case FC_RCTL_BA_PRMT:
11603         case FC_RCTL_ACK_1:     /* acknowledge_1 */
11604         case FC_RCTL_ACK_0:     /* acknowledge_0 */
11605         case FC_RCTL_P_RJT:     /* port reject */
11606         case FC_RCTL_F_RJT:     /* fabric reject */
11607         case FC_RCTL_P_BSY:     /* port busy */
11608         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
11609         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
11610         case FC_RCTL_LCR:       /* link credit reset */
11611         case FC_RCTL_END:       /* end */
11612                 break;
11613         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
11614                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11615                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11616                 return lpfc_fc_frame_check(phba, fc_hdr);
11617         default:
11618                 goto drop;
11619         }
11620         switch (fc_hdr->fh_type) {
11621         case FC_TYPE_BLS:
11622         case FC_TYPE_ELS:
11623         case FC_TYPE_FCP:
11624         case FC_TYPE_CT:
11625                 break;
11626         case FC_TYPE_IP:
11627         case FC_TYPE_ILS:
11628         default:
11629                 goto drop;
11630         }
11631         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11632                         "2538 Received frame rctl:%s type:%s\n",
11633                         rctl_names[fc_hdr->fh_r_ctl],
11634                         type_names[fc_hdr->fh_type]);
11635         return 0;
11636 drop:
11637         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11638                         "2539 Dropped frame rctl:%s type:%s\n",
11639                         rctl_names[fc_hdr->fh_r_ctl],
11640                         type_names[fc_hdr->fh_type]);
11641         return 1;
11642 }
11643
11644 /**
11645  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11646  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11647  *
11648  * This function processes the FC header to retrieve the VFI from the VF
11649  * header, if one exists. This function will return the VFI if one exists
11650  * or 0 if no VSAN Header exists.
11651  **/
11652 static uint32_t
11653 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11654 {
11655         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11656
11657         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11658                 return 0;
11659         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11660 }
11661
11662 /**
11663  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11664  * @phba: Pointer to the HBA structure to search for the vport on
11665  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11666  * @fcfi: The FC Fabric ID that the frame came from
11667  *
11668  * This function searches the @phba for a vport that matches the content of the
11669  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11670  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11671  * returns the matching vport pointer or NULL if unable to match frame to a
11672  * vport.
11673  **/
11674 static struct lpfc_vport *
11675 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11676                        uint16_t fcfi)
11677 {
11678         struct lpfc_vport **vports;
11679         struct lpfc_vport *vport = NULL;
11680         int i;
11681         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11682                         fc_hdr->fh_d_id[1] << 8 |
11683                         fc_hdr->fh_d_id[2]);
11684
11685         vports = lpfc_create_vport_work_array(phba);
11686         if (vports != NULL)
11687                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11688                         if (phba->fcf.fcfi == fcfi &&
11689                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11690                             vports[i]->fc_myDID == did) {
11691                                 vport = vports[i];
11692                                 break;
11693                         }
11694                 }
11695         lpfc_destroy_vport_work_array(phba, vports);
11696         return vport;
11697 }
11698
11699 /**
11700  * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11701  * @vport: The vport to work on.
11702  *
11703  * This function updates the receive sequence time stamp for this vport. The
11704  * receive sequence time stamp indicates the time that the last frame of the
11705  * the sequence that has been idle for the longest amount of time was received.
11706  * the driver uses this time stamp to indicate if any received sequences have
11707  * timed out.
11708  **/
11709 void
11710 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11711 {
11712         struct lpfc_dmabuf *h_buf;
11713         struct hbq_dmabuf *dmabuf = NULL;
11714
11715         /* get the oldest sequence on the rcv list */
11716         h_buf = list_get_first(&vport->rcv_buffer_list,
11717                                struct lpfc_dmabuf, list);
11718         if (!h_buf)
11719                 return;
11720         dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11721         vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11722 }
11723
11724 /**
11725  * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11726  * @vport: The vport that the received sequences were sent to.
11727  *
11728  * This function cleans up all outstanding received sequences. This is called
11729  * by the driver when a link event or user action invalidates all the received
11730  * sequences.
11731  **/
11732 void
11733 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11734 {
11735         struct lpfc_dmabuf *h_buf, *hnext;
11736         struct lpfc_dmabuf *d_buf, *dnext;
11737         struct hbq_dmabuf *dmabuf = NULL;
11738
11739         /* start with the oldest sequence on the rcv list */
11740         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11741                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11742                 list_del_init(&dmabuf->hbuf.list);
11743                 list_for_each_entry_safe(d_buf, dnext,
11744                                          &dmabuf->dbuf.list, list) {
11745                         list_del_init(&d_buf->list);
11746                         lpfc_in_buf_free(vport->phba, d_buf);
11747                 }
11748                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11749         }
11750 }
11751
11752 /**
11753  * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11754  * @vport: The vport that the received sequences were sent to.
11755  *
11756  * This function determines whether any received sequences have timed out by
11757  * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11758  * indicates that there is at least one timed out sequence this routine will
11759  * go through the received sequences one at a time from most inactive to most
11760  * active to determine which ones need to be cleaned up. Once it has determined
11761  * that a sequence needs to be cleaned up it will simply free up the resources
11762  * without sending an abort.
11763  **/
11764 void
11765 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11766 {
11767         struct lpfc_dmabuf *h_buf, *hnext;
11768         struct lpfc_dmabuf *d_buf, *dnext;
11769         struct hbq_dmabuf *dmabuf = NULL;
11770         unsigned long timeout;
11771         int abort_count = 0;
11772
11773         timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11774                    vport->rcv_buffer_time_stamp);
11775         if (list_empty(&vport->rcv_buffer_list) ||
11776             time_before(jiffies, timeout))
11777                 return;
11778         /* start with the oldest sequence on the rcv list */
11779         list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11780                 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11781                 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11782                            dmabuf->time_stamp);
11783                 if (time_before(jiffies, timeout))
11784                         break;
11785                 abort_count++;
11786                 list_del_init(&dmabuf->hbuf.list);
11787                 list_for_each_entry_safe(d_buf, dnext,
11788                                          &dmabuf->dbuf.list, list) {
11789                         list_del_init(&d_buf->list);
11790                         lpfc_in_buf_free(vport->phba, d_buf);
11791                 }
11792                 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11793         }
11794         if (abort_count)
11795                 lpfc_update_rcv_time_stamp(vport);
11796 }
11797
11798 /**
11799  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11800  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11801  *
11802  * This function searches through the existing incomplete sequences that have
11803  * been sent to this @vport. If the frame matches one of the incomplete
11804  * sequences then the dbuf in the @dmabuf is added to the list of frames that
11805  * make up that sequence. If no sequence is found that matches this frame then
11806  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11807  * This function returns a pointer to the first dmabuf in the sequence list that
11808  * the frame was linked to.
11809  **/
11810 static struct hbq_dmabuf *
11811 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11812 {
11813         struct fc_frame_header *new_hdr;
11814         struct fc_frame_header *temp_hdr;
11815         struct lpfc_dmabuf *d_buf;
11816         struct lpfc_dmabuf *h_buf;
11817         struct hbq_dmabuf *seq_dmabuf = NULL;
11818         struct hbq_dmabuf *temp_dmabuf = NULL;
11819
11820         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11821         dmabuf->time_stamp = jiffies;
11822         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11823         /* Use the hdr_buf to find the sequence that this frame belongs to */
11824         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11825                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11826                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11827                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11828                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11829                         continue;
11830                 /* found a pending sequence that matches this frame */
11831                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11832                 break;
11833         }
11834         if (!seq_dmabuf) {
11835                 /*
11836                  * This indicates first frame received for this sequence.
11837                  * Queue the buffer on the vport's rcv_buffer_list.
11838                  */
11839                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11840                 lpfc_update_rcv_time_stamp(vport);
11841                 return dmabuf;
11842         }
11843         temp_hdr = seq_dmabuf->hbuf.virt;
11844         if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11845                 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11846                 list_del_init(&seq_dmabuf->hbuf.list);
11847                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11848                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11849                 lpfc_update_rcv_time_stamp(vport);
11850                 return dmabuf;
11851         }
11852         /* move this sequence to the tail to indicate a young sequence */
11853         list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11854         seq_dmabuf->time_stamp = jiffies;
11855         lpfc_update_rcv_time_stamp(vport);
11856         if (list_empty(&seq_dmabuf->dbuf.list)) {
11857                 temp_hdr = dmabuf->hbuf.virt;
11858                 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11859                 return seq_dmabuf;
11860         }
11861         /* find the correct place in the sequence to insert this frame */
11862         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11863                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11864                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11865                 /*
11866                  * If the frame's sequence count is greater than the frame on
11867                  * the list then insert the frame right after this frame
11868                  */
11869                 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11870                         be16_to_cpu(temp_hdr->fh_seq_cnt)) {
11871                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11872                         return seq_dmabuf;
11873                 }
11874         }
11875         return NULL;
11876 }
11877
11878 /**
11879  * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11880  * @vport: pointer to a vitural port
11881  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11882  *
11883  * This function tries to abort from the partially assembed sequence, described
11884  * by the information from basic abbort @dmabuf. It checks to see whether such
11885  * partially assembled sequence held by the driver. If so, it shall free up all
11886  * the frames from the partially assembled sequence.
11887  *
11888  * Return
11889  * true  -- if there is matching partially assembled sequence present and all
11890  *          the frames freed with the sequence;
11891  * false -- if there is no matching partially assembled sequence present so
11892  *          nothing got aborted in the lower layer driver
11893  **/
11894 static bool
11895 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11896                             struct hbq_dmabuf *dmabuf)
11897 {
11898         struct fc_frame_header *new_hdr;
11899         struct fc_frame_header *temp_hdr;
11900         struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11901         struct hbq_dmabuf *seq_dmabuf = NULL;
11902
11903         /* Use the hdr_buf to find the sequence that matches this frame */
11904         INIT_LIST_HEAD(&dmabuf->dbuf.list);
11905         INIT_LIST_HEAD(&dmabuf->hbuf.list);
11906         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11907         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11908                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11909                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11910                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11911                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11912                         continue;
11913                 /* found a pending sequence that matches this frame */
11914                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11915                 break;
11916         }
11917
11918         /* Free up all the frames from the partially assembled sequence */
11919         if (seq_dmabuf) {
11920                 list_for_each_entry_safe(d_buf, n_buf,
11921                                          &seq_dmabuf->dbuf.list, list) {
11922                         list_del_init(&d_buf->list);
11923                         lpfc_in_buf_free(vport->phba, d_buf);
11924                 }
11925                 return true;
11926         }
11927         return false;
11928 }
11929
11930 /**
11931  * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11932  * @phba: Pointer to HBA context object.
11933  * @cmd_iocbq: pointer to the command iocbq structure.
11934  * @rsp_iocbq: pointer to the response iocbq structure.
11935  *
11936  * This function handles the sequence abort accept iocb command complete
11937  * event. It properly releases the memory allocated to the sequence abort
11938  * accept iocb.
11939  **/
11940 static void
11941 lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11942                              struct lpfc_iocbq *cmd_iocbq,
11943                              struct lpfc_iocbq *rsp_iocbq)
11944 {
11945         if (cmd_iocbq)
11946                 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11947 }
11948
11949 /**
11950  * lpfc_sli4_seq_abort_acc - Accept sequence abort
11951  * @phba: Pointer to HBA context object.
11952  * @fc_hdr: pointer to a FC frame header.
11953  *
11954  * This function sends a basic accept to a previous unsol sequence abort
11955  * event after aborting the sequence handling.
11956  **/
11957 static void
11958 lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11959                         struct fc_frame_header *fc_hdr)
11960 {
11961         struct lpfc_iocbq *ctiocb = NULL;
11962         struct lpfc_nodelist *ndlp;
11963         uint16_t oxid, rxid;
11964         uint32_t sid, fctl;
11965         IOCB_t *icmd;
11966
11967         if (!lpfc_is_link_up(phba))
11968                 return;
11969
11970         sid = sli4_sid_from_fc_hdr(fc_hdr);
11971         oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11972         rxid = be16_to_cpu(fc_hdr->fh_rx_id);
11973
11974         ndlp = lpfc_findnode_did(phba->pport, sid);
11975         if (!ndlp) {
11976                 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11977                                 "1268 Find ndlp returned NULL for oxid:x%x "
11978                                 "SID:x%x\n", oxid, sid);
11979                 return;
11980         }
11981         if (rxid >= phba->sli4_hba.max_cfg_param.xri_base
11982                 && rxid <= (phba->sli4_hba.max_cfg_param.max_xri
11983                 + phba->sli4_hba.max_cfg_param.xri_base))
11984                 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
11985
11986         /* Allocate buffer for acc iocb */
11987         ctiocb = lpfc_sli_get_iocbq(phba);
11988         if (!ctiocb)
11989                 return;
11990
11991         /* Extract the F_CTL field from FC_HDR */
11992         fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11993
11994         icmd = &ctiocb->iocb;
11995         icmd->un.xseq64.bdl.bdeSize = 0;
11996         icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11997         icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11998         icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11999         icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
12000
12001         /* Fill in the rest of iocb fields */
12002         icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
12003         icmd->ulpBdeCount = 0;
12004         icmd->ulpLe = 1;
12005         icmd->ulpClass = CLASS3;
12006         icmd->ulpContext = ndlp->nlp_rpi;
12007         ctiocb->context1 = ndlp;
12008
12009         ctiocb->iocb_cmpl = NULL;
12010         ctiocb->vport = phba->pport;
12011         ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
12012
12013         if (fctl & FC_FC_EX_CTX) {
12014                 /* ABTS sent by responder to CT exchange, construction
12015                  * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
12016                  * field and RX_ID from ABTS for RX_ID field.
12017                  */
12018                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
12019                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
12020                 ctiocb->sli4_xritag = oxid;
12021         } else {
12022                 /* ABTS sent by initiator to CT exchange, construction
12023                  * of BA_ACC will need to allocate a new XRI as for the
12024                  * XRI_TAG and RX_ID fields.
12025                  */
12026                 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
12027                 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
12028                 ctiocb->sli4_xritag = NO_XRI;
12029         }
12030         bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
12031
12032         /* Xmit CT abts accept on exchange <xid> */
12033         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12034                         "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
12035                         CMD_XMIT_BLS_RSP64_CX, phba->link_state);
12036         lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
12037 }
12038
12039 /**
12040  * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
12041  * @vport: Pointer to the vport on which this sequence was received
12042  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12043  *
12044  * This function handles an SLI-4 unsolicited abort event. If the unsolicited
12045  * receive sequence is only partially assembed by the driver, it shall abort
12046  * the partially assembled frames for the sequence. Otherwise, if the
12047  * unsolicited receive sequence has been completely assembled and passed to
12048  * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
12049  * unsolicited sequence has been aborted. After that, it will issue a basic
12050  * accept to accept the abort.
12051  **/
12052 void
12053 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
12054                              struct hbq_dmabuf *dmabuf)
12055 {
12056         struct lpfc_hba *phba = vport->phba;
12057         struct fc_frame_header fc_hdr;
12058         uint32_t fctl;
12059         bool abts_par;
12060
12061         /* Make a copy of fc_hdr before the dmabuf being released */
12062         memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
12063         fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
12064
12065         if (fctl & FC_FC_EX_CTX) {
12066                 /*
12067                  * ABTS sent by responder to exchange, just free the buffer
12068                  */
12069                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12070         } else {
12071                 /*
12072                  * ABTS sent by initiator to exchange, need to do cleanup
12073                  */
12074                 /* Try to abort partially assembled seq */
12075                 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
12076
12077                 /* Send abort to ULP if partially seq abort failed */
12078                 if (abts_par == false)
12079                         lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
12080                 else
12081                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
12082         }
12083         /* Send basic accept (BA_ACC) to the abort requester */
12084         lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
12085 }
12086
12087 /**
12088  * lpfc_seq_complete - Indicates if a sequence is complete
12089  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12090  *
12091  * This function checks the sequence, starting with the frame described by
12092  * @dmabuf, to see if all the frames associated with this sequence are present.
12093  * the frames associated with this sequence are linked to the @dmabuf using the
12094  * dbuf list. This function looks for two major things. 1) That the first frame
12095  * has a sequence count of zero. 2) There is a frame with last frame of sequence
12096  * set. 3) That there are no holes in the sequence count. The function will
12097  * return 1 when the sequence is complete, otherwise it will return 0.
12098  **/
12099 static int
12100 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
12101 {
12102         struct fc_frame_header *hdr;
12103         struct lpfc_dmabuf *d_buf;
12104         struct hbq_dmabuf *seq_dmabuf;
12105         uint32_t fctl;
12106         int seq_count = 0;
12107
12108         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12109         /* make sure first fame of sequence has a sequence count of zero */
12110         if (hdr->fh_seq_cnt != seq_count)
12111                 return 0;
12112         fctl = (hdr->fh_f_ctl[0] << 16 |
12113                 hdr->fh_f_ctl[1] << 8 |
12114                 hdr->fh_f_ctl[2]);
12115         /* If last frame of sequence we can return success. */
12116         if (fctl & FC_FC_END_SEQ)
12117                 return 1;
12118         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
12119                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
12120                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12121                 /* If there is a hole in the sequence count then fail. */
12122                 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
12123                         return 0;
12124                 fctl = (hdr->fh_f_ctl[0] << 16 |
12125                         hdr->fh_f_ctl[1] << 8 |
12126                         hdr->fh_f_ctl[2]);
12127                 /* If last frame of sequence we can return success. */
12128                 if (fctl & FC_FC_END_SEQ)
12129                         return 1;
12130         }
12131         return 0;
12132 }
12133
12134 /**
12135  * lpfc_prep_seq - Prep sequence for ULP processing
12136  * @vport: Pointer to the vport on which this sequence was received
12137  * @dmabuf: pointer to a dmabuf that describes the FC sequence
12138  *
12139  * This function takes a sequence, described by a list of frames, and creates
12140  * a list of iocbq structures to describe the sequence. This iocbq list will be
12141  * used to issue to the generic unsolicited sequence handler. This routine
12142  * returns a pointer to the first iocbq in the list. If the function is unable
12143  * to allocate an iocbq then it throw out the received frames that were not
12144  * able to be described and return a pointer to the first iocbq. If unable to
12145  * allocate any iocbqs (including the first) this function will return NULL.
12146  **/
12147 static struct lpfc_iocbq *
12148 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
12149 {
12150         struct lpfc_dmabuf *d_buf, *n_buf;
12151         struct lpfc_iocbq *first_iocbq, *iocbq;
12152         struct fc_frame_header *fc_hdr;
12153         uint32_t sid;
12154         struct ulp_bde64 *pbde;
12155
12156         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12157         /* remove from receive buffer list */
12158         list_del_init(&seq_dmabuf->hbuf.list);
12159         lpfc_update_rcv_time_stamp(vport);
12160         /* get the Remote Port's SID */
12161         sid = sli4_sid_from_fc_hdr(fc_hdr);
12162         /* Get an iocbq struct to fill in. */
12163         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
12164         if (first_iocbq) {
12165                 /* Initialize the first IOCB. */
12166                 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
12167                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
12168                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
12169                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
12170                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
12171                                         vport->vpi + vport->phba->vpi_base;
12172                 /* put the first buffer into the first IOCBq */
12173                 first_iocbq->context2 = &seq_dmabuf->dbuf;
12174                 first_iocbq->context3 = NULL;
12175                 first_iocbq->iocb.ulpBdeCount = 1;
12176                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12177                                                         LPFC_DATA_BUF_SIZE;
12178                 first_iocbq->iocb.un.rcvels.remoteID = sid;
12179                 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12180                                 bf_get(lpfc_rcqe_length,
12181                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12182         }
12183         iocbq = first_iocbq;
12184         /*
12185          * Each IOCBq can have two Buffers assigned, so go through the list
12186          * of buffers for this sequence and save two buffers in each IOCBq
12187          */
12188         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
12189                 if (!iocbq) {
12190                         lpfc_in_buf_free(vport->phba, d_buf);
12191                         continue;
12192                 }
12193                 if (!iocbq->context3) {
12194                         iocbq->context3 = d_buf;
12195                         iocbq->iocb.ulpBdeCount++;
12196                         pbde = (struct ulp_bde64 *)
12197                                         &iocbq->iocb.unsli3.sli3Words[4];
12198                         pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
12199                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12200                                 bf_get(lpfc_rcqe_length,
12201                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12202                 } else {
12203                         iocbq = lpfc_sli_get_iocbq(vport->phba);
12204                         if (!iocbq) {
12205                                 if (first_iocbq) {
12206                                         first_iocbq->iocb.ulpStatus =
12207                                                         IOSTAT_FCP_RSP_ERROR;
12208                                         first_iocbq->iocb.un.ulpWord[4] =
12209                                                         IOERR_NO_RESOURCES;
12210                                 }
12211                                 lpfc_in_buf_free(vport->phba, d_buf);
12212                                 continue;
12213                         }
12214                         iocbq->context2 = d_buf;
12215                         iocbq->context3 = NULL;
12216                         iocbq->iocb.ulpBdeCount = 1;
12217                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
12218                                                         LPFC_DATA_BUF_SIZE;
12219                         first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
12220                                 bf_get(lpfc_rcqe_length,
12221                                        &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
12222                         iocbq->iocb.un.rcvels.remoteID = sid;
12223                         list_add_tail(&iocbq->list, &first_iocbq->list);
12224                 }
12225         }
12226         return first_iocbq;
12227 }
12228
12229 static void
12230 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
12231                           struct hbq_dmabuf *seq_dmabuf)
12232 {
12233         struct fc_frame_header *fc_hdr;
12234         struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
12235         struct lpfc_hba *phba = vport->phba;
12236
12237         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
12238         iocbq = lpfc_prep_seq(vport, seq_dmabuf);
12239         if (!iocbq) {
12240                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12241                                 "2707 Ring %d handler: Failed to allocate "
12242                                 "iocb Rctl x%x Type x%x received\n",
12243                                 LPFC_ELS_RING,
12244                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12245                 return;
12246         }
12247         if (!lpfc_complete_unsol_iocb(phba,
12248                                       &phba->sli.ring[LPFC_ELS_RING],
12249                                       iocbq, fc_hdr->fh_r_ctl,
12250                                       fc_hdr->fh_type))
12251                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12252                                 "2540 Ring %d handler: unexpected Rctl "
12253                                 "x%x Type x%x received\n",
12254                                 LPFC_ELS_RING,
12255                                 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
12256
12257         /* Free iocb created in lpfc_prep_seq */
12258         list_for_each_entry_safe(curr_iocb, next_iocb,
12259                 &iocbq->list, list) {
12260                 list_del_init(&curr_iocb->list);
12261                 lpfc_sli_release_iocbq(phba, curr_iocb);
12262         }
12263         lpfc_sli_release_iocbq(phba, iocbq);
12264 }
12265
12266 /**
12267  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
12268  * @phba: Pointer to HBA context object.
12269  *
12270  * This function is called with no lock held. This function processes all
12271  * the received buffers and gives it to upper layers when a received buffer
12272  * indicates that it is the final frame in the sequence. The interrupt
12273  * service routine processes received buffers at interrupt contexts and adds
12274  * received dma buffers to the rb_pend_list queue and signals the worker thread.
12275  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
12276  * appropriate receive function when the final frame in a sequence is received.
12277  **/
12278 void
12279 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
12280                                  struct hbq_dmabuf *dmabuf)
12281 {
12282         struct hbq_dmabuf *seq_dmabuf;
12283         struct fc_frame_header *fc_hdr;
12284         struct lpfc_vport *vport;
12285         uint32_t fcfi;
12286
12287         /* Process each received buffer */
12288         fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
12289         /* check to see if this a valid type of frame */
12290         if (lpfc_fc_frame_check(phba, fc_hdr)) {
12291                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12292                 return;
12293         }
12294         fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
12295         vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
12296         if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
12297                 /* throw out the frame */
12298                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12299                 return;
12300         }
12301         /* Handle the basic abort sequence (BA_ABTS) event */
12302         if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
12303                 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
12304                 return;
12305         }
12306
12307         /* Link this frame */
12308         seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
12309         if (!seq_dmabuf) {
12310                 /* unable to add frame to vport - throw it out */
12311                 lpfc_in_buf_free(phba, &dmabuf->dbuf);
12312                 return;
12313         }
12314         /* If not last frame in sequence continue processing frames. */
12315         if (!lpfc_seq_complete(seq_dmabuf))
12316                 return;
12317
12318         /* Send the complete sequence to the upper layer protocol */
12319         lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
12320 }
12321
12322 /**
12323  * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
12324  * @phba: pointer to lpfc hba data structure.
12325  *
12326  * This routine is invoked to post rpi header templates to the
12327  * HBA consistent with the SLI-4 interface spec.  This routine
12328  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12329  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12330  *
12331  * This routine does not require any locks.  It's usage is expected
12332  * to be driver load or reset recovery when the driver is
12333  * sequential.
12334  *
12335  * Return codes
12336  *      0 - successful
12337  *      -EIO - The mailbox failed to complete successfully.
12338  *      When this error occurs, the driver is not guaranteed
12339  *      to have any rpi regions posted to the device and
12340  *      must either attempt to repost the regions or take a
12341  *      fatal error.
12342  **/
12343 int
12344 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
12345 {
12346         struct lpfc_rpi_hdr *rpi_page;
12347         uint32_t rc = 0;
12348
12349         /* Post all rpi memory regions to the port. */
12350         list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
12351                 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
12352                 if (rc != MBX_SUCCESS) {
12353                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12354                                         "2008 Error %d posting all rpi "
12355                                         "headers\n", rc);
12356                         rc = -EIO;
12357                         break;
12358                 }
12359         }
12360
12361         return rc;
12362 }
12363
12364 /**
12365  * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
12366  * @phba: pointer to lpfc hba data structure.
12367  * @rpi_page:  pointer to the rpi memory region.
12368  *
12369  * This routine is invoked to post a single rpi header to the
12370  * HBA consistent with the SLI-4 interface spec.  This memory region
12371  * maps up to 64 rpi context regions.
12372  *
12373  * Return codes
12374  *      0 - successful
12375  *      -ENOMEM - No available memory
12376  *      -EIO - The mailbox failed to complete successfully.
12377  **/
12378 int
12379 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
12380 {
12381         LPFC_MBOXQ_t *mboxq;
12382         struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
12383         uint32_t rc = 0;
12384         uint32_t mbox_tmo;
12385         uint32_t shdr_status, shdr_add_status;
12386         union lpfc_sli4_cfg_shdr *shdr;
12387
12388         /* The port is notified of the header region via a mailbox command. */
12389         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12390         if (!mboxq) {
12391                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12392                                 "2001 Unable to allocate memory for issuing "
12393                                 "SLI_CONFIG_SPECIAL mailbox command\n");
12394                 return -ENOMEM;
12395         }
12396
12397         /* Post all rpi memory regions to the port. */
12398         hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
12399         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
12400         lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12401                          LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
12402                          sizeof(struct lpfc_mbx_post_hdr_tmpl) -
12403                          sizeof(struct lpfc_sli4_cfg_mhdr),
12404                          LPFC_SLI4_MBX_EMBED);
12405         bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
12406                hdr_tmpl, rpi_page->page_count);
12407         bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
12408                rpi_page->start_rpi);
12409         hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
12410         hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
12411         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
12412         shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
12413         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12414         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12415         if (rc != MBX_TIMEOUT)
12416                 mempool_free(mboxq, phba->mbox_mem_pool);
12417         if (shdr_status || shdr_add_status || rc) {
12418                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12419                                 "2514 POST_RPI_HDR mailbox failed with "
12420                                 "status x%x add_status x%x, mbx status x%x\n",
12421                                 shdr_status, shdr_add_status, rc);
12422                 rc = -ENXIO;
12423         }
12424         return rc;
12425 }
12426
12427 /**
12428  * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12429  * @phba: pointer to lpfc hba data structure.
12430  *
12431  * This routine is invoked to post rpi header templates to the
12432  * HBA consistent with the SLI-4 interface spec.  This routine
12433  * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12434  * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12435  *
12436  * Returns
12437  *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12438  *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
12439  **/
12440 int
12441 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12442 {
12443         int rpi;
12444         uint16_t max_rpi, rpi_base, rpi_limit;
12445         uint16_t rpi_remaining;
12446         struct lpfc_rpi_hdr *rpi_hdr;
12447
12448         max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12449         rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12450         rpi_limit = phba->sli4_hba.next_rpi;
12451
12452         /*
12453          * The valid rpi range is not guaranteed to be zero-based.  Start
12454          * the search at the rpi_base as reported by the port.
12455          */
12456         spin_lock_irq(&phba->hbalock);
12457         rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12458         if (rpi >= rpi_limit || rpi < rpi_base)
12459                 rpi = LPFC_RPI_ALLOC_ERROR;
12460         else {
12461                 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12462                 phba->sli4_hba.max_cfg_param.rpi_used++;
12463                 phba->sli4_hba.rpi_count++;
12464         }
12465
12466         /*
12467          * Don't try to allocate more rpi header regions if the device limit
12468          * on available rpis max has been exhausted.
12469          */
12470         if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12471             (phba->sli4_hba.rpi_count >= max_rpi)) {
12472                 spin_unlock_irq(&phba->hbalock);
12473                 return rpi;
12474         }
12475
12476         /*
12477          * If the driver is running low on rpi resources, allocate another
12478          * page now.  Note that the next_rpi value is used because
12479          * it represents how many are actually in use whereas max_rpi notes
12480          * how many are supported max by the device.
12481          */
12482         rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12483                 phba->sli4_hba.rpi_count;
12484         spin_unlock_irq(&phba->hbalock);
12485         if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12486                 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12487                 if (!rpi_hdr) {
12488                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12489                                         "2002 Error Could not grow rpi "
12490                                         "count\n");
12491                 } else {
12492                         lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12493                 }
12494         }
12495
12496         return rpi;
12497 }
12498
12499 /**
12500  * lpfc_sli4_free_rpi - Release an rpi for reuse.
12501  * @phba: pointer to lpfc hba data structure.
12502  *
12503  * This routine is invoked to release an rpi to the pool of
12504  * available rpis maintained by the driver.
12505  **/
12506 void
12507 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12508 {
12509         if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12510                 phba->sli4_hba.rpi_count--;
12511                 phba->sli4_hba.max_cfg_param.rpi_used--;
12512         }
12513 }
12514
12515 /**
12516  * lpfc_sli4_free_rpi - Release an rpi for reuse.
12517  * @phba: pointer to lpfc hba data structure.
12518  *
12519  * This routine is invoked to release an rpi to the pool of
12520  * available rpis maintained by the driver.
12521  **/
12522 void
12523 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12524 {
12525         spin_lock_irq(&phba->hbalock);
12526         __lpfc_sli4_free_rpi(phba, rpi);
12527         spin_unlock_irq(&phba->hbalock);
12528 }
12529
12530 /**
12531  * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12532  * @phba: pointer to lpfc hba data structure.
12533  *
12534  * This routine is invoked to remove the memory region that
12535  * provided rpi via a bitmask.
12536  **/
12537 void
12538 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12539 {
12540         kfree(phba->sli4_hba.rpi_bmask);
12541 }
12542
12543 /**
12544  * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12545  * @phba: pointer to lpfc hba data structure.
12546  *
12547  * This routine is invoked to remove the memory region that
12548  * provided rpi via a bitmask.
12549  **/
12550 int
12551 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12552 {
12553         LPFC_MBOXQ_t *mboxq;
12554         struct lpfc_hba *phba = ndlp->phba;
12555         int rc;
12556
12557         /* The port is notified of the header region via a mailbox command. */
12558         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12559         if (!mboxq)
12560                 return -ENOMEM;
12561
12562         /* Post all rpi memory regions to the port. */
12563         lpfc_resume_rpi(mboxq, ndlp);
12564         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12565         if (rc == MBX_NOT_FINISHED) {
12566                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12567                                 "2010 Resume RPI Mailbox failed "
12568                                 "status %d, mbxStatus x%x\n", rc,
12569                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12570                 mempool_free(mboxq, phba->mbox_mem_pool);
12571                 return -EIO;
12572         }
12573         return 0;
12574 }
12575
12576 /**
12577  * lpfc_sli4_init_vpi - Initialize a vpi with the port
12578  * @vport: Pointer to the vport for which the vpi is being initialized
12579  *
12580  * This routine is invoked to activate a vpi with the port.
12581  *
12582  * Returns:
12583  *    0 success
12584  *    -Evalue otherwise
12585  **/
12586 int
12587 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
12588 {
12589         LPFC_MBOXQ_t *mboxq;
12590         int rc = 0;
12591         int retval = MBX_SUCCESS;
12592         uint32_t mbox_tmo;
12593         struct lpfc_hba *phba = vport->phba;
12594         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12595         if (!mboxq)
12596                 return -ENOMEM;
12597         lpfc_init_vpi(phba, mboxq, vport->vpi);
12598         mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12599         rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
12600         if (rc != MBX_SUCCESS) {
12601                 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
12602                                 "2022 INIT VPI Mailbox failed "
12603                                 "status %d, mbxStatus x%x\n", rc,
12604                                 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12605                 retval = -EIO;
12606         }
12607         if (rc != MBX_TIMEOUT)
12608                 mempool_free(mboxq, vport->phba->mbox_mem_pool);
12609
12610         return retval;
12611 }
12612
12613 /**
12614  * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12615  * @phba: pointer to lpfc hba data structure.
12616  * @mboxq: Pointer to mailbox object.
12617  *
12618  * This routine is invoked to manually add a single FCF record. The caller
12619  * must pass a completely initialized FCF_Record.  This routine takes
12620  * care of the nonembedded mailbox operations.
12621  **/
12622 static void
12623 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12624 {
12625         void *virt_addr;
12626         union lpfc_sli4_cfg_shdr *shdr;
12627         uint32_t shdr_status, shdr_add_status;
12628
12629         virt_addr = mboxq->sge_array->addr[0];
12630         /* The IOCTL status is embedded in the mailbox subheader. */
12631         shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12632         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12633         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12634
12635         if ((shdr_status || shdr_add_status) &&
12636                 (shdr_status != STATUS_FCF_IN_USE))
12637                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12638                         "2558 ADD_FCF_RECORD mailbox failed with "
12639                         "status x%x add_status x%x\n",
12640                         shdr_status, shdr_add_status);
12641
12642         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12643 }
12644
12645 /**
12646  * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12647  * @phba: pointer to lpfc hba data structure.
12648  * @fcf_record:  pointer to the initialized fcf record to add.
12649  *
12650  * This routine is invoked to manually add a single FCF record. The caller
12651  * must pass a completely initialized FCF_Record.  This routine takes
12652  * care of the nonembedded mailbox operations.
12653  **/
12654 int
12655 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12656 {
12657         int rc = 0;
12658         LPFC_MBOXQ_t *mboxq;
12659         uint8_t *bytep;
12660         void *virt_addr;
12661         dma_addr_t phys_addr;
12662         struct lpfc_mbx_sge sge;
12663         uint32_t alloc_len, req_len;
12664         uint32_t fcfindex;
12665
12666         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12667         if (!mboxq) {
12668                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12669                         "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12670                 return -ENOMEM;
12671         }
12672
12673         req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12674                   sizeof(uint32_t);
12675
12676         /* Allocate DMA memory and set up the non-embedded mailbox command */
12677         alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12678                                      LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12679                                      req_len, LPFC_SLI4_MBX_NEMBED);
12680         if (alloc_len < req_len) {
12681                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12682                         "2523 Allocated DMA memory size (x%x) is "
12683                         "less than the requested DMA memory "
12684                         "size (x%x)\n", alloc_len, req_len);
12685                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12686                 return -ENOMEM;
12687         }
12688
12689         /*
12690          * Get the first SGE entry from the non-embedded DMA memory.  This
12691          * routine only uses a single SGE.
12692          */
12693         lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12694         phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
12695         virt_addr = mboxq->sge_array->addr[0];
12696         /*
12697          * Configure the FCF record for FCFI 0.  This is the driver's
12698          * hardcoded default and gets used in nonFIP mode.
12699          */
12700         fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12701         bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12702         lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12703
12704         /*
12705          * Copy the fcf_index and the FCF Record Data. The data starts after
12706          * the FCoE header plus word10. The data copy needs to be endian
12707          * correct.
12708          */
12709         bytep += sizeof(uint32_t);
12710         lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12711         mboxq->vport = phba->pport;
12712         mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12713         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12714         if (rc == MBX_NOT_FINISHED) {
12715                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12716                         "2515 ADD_FCF_RECORD mailbox failed with "
12717                         "status 0x%x\n", rc);
12718                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12719                 rc = -EIO;
12720         } else
12721                 rc = 0;
12722
12723         return rc;
12724 }
12725
12726 /**
12727  * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12728  * @phba: pointer to lpfc hba data structure.
12729  * @fcf_record:  pointer to the fcf record to write the default data.
12730  * @fcf_index: FCF table entry index.
12731  *
12732  * This routine is invoked to build the driver's default FCF record.  The
12733  * values used are hardcoded.  This routine handles memory initialization.
12734  *
12735  **/
12736 void
12737 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12738                                 struct fcf_record *fcf_record,
12739                                 uint16_t fcf_index)
12740 {
12741         memset(fcf_record, 0, sizeof(struct fcf_record));
12742         fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12743         fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12744         fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12745         bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12746         bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12747         bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12748         bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12749         bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12750         bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12751         bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12752         bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12753         bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12754         bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
12755         bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
12756         bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12757         bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12758                 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12759         /* Set the VLAN bit map */
12760         if (phba->valid_vlan) {
12761                 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12762                         = 1 << (phba->vlan_id % 8);
12763         }
12764 }
12765
12766 /**
12767  * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
12768  * @phba: pointer to lpfc hba data structure.
12769  * @fcf_index: FCF table entry offset.
12770  *
12771  * This routine is invoked to scan the entire FCF table by reading FCF
12772  * record and processing it one at a time starting from the @fcf_index
12773  * for initial FCF discovery or fast FCF failover rediscovery.
12774  *
12775  * Return 0 if the mailbox command is submitted sucessfully, none 0
12776  * otherwise.
12777  **/
12778 int
12779 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12780 {
12781         int rc = 0, error;
12782         LPFC_MBOXQ_t *mboxq;
12783
12784         phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
12785         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12786         if (!mboxq) {
12787                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12788                                 "2000 Failed to allocate mbox for "
12789                                 "READ_FCF cmd\n");
12790                 error = -ENOMEM;
12791                 goto fail_fcf_scan;
12792         }
12793         /* Construct the read FCF record mailbox command */
12794         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12795         if (rc) {
12796                 error = -EINVAL;
12797                 goto fail_fcf_scan;
12798         }
12799         /* Issue the mailbox command asynchronously */
12800         mboxq->vport = phba->pport;
12801         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
12802
12803         spin_lock_irq(&phba->hbalock);
12804         phba->hba_flag |= FCF_TS_INPROG;
12805         spin_unlock_irq(&phba->hbalock);
12806
12807         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12808         if (rc == MBX_NOT_FINISHED)
12809                 error = -EIO;
12810         else {
12811                 /* Reset eligible FCF count for new scan */
12812                 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
12813                         phba->fcf.eligible_fcf_cnt = 0;
12814                 error = 0;
12815         }
12816 fail_fcf_scan:
12817         if (error) {
12818                 if (mboxq)
12819                         lpfc_sli4_mbox_cmd_free(phba, mboxq);
12820                 /* FCF scan failed, clear FCF_TS_INPROG flag */
12821                 spin_lock_irq(&phba->hbalock);
12822                 phba->hba_flag &= ~FCF_TS_INPROG;
12823                 spin_unlock_irq(&phba->hbalock);
12824         }
12825         return error;
12826 }
12827
12828 /**
12829  * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
12830  * @phba: pointer to lpfc hba data structure.
12831  * @fcf_index: FCF table entry offset.
12832  *
12833  * This routine is invoked to read an FCF record indicated by @fcf_index
12834  * and to use it for FLOGI roundrobin FCF failover.
12835  *
12836  * Return 0 if the mailbox command is submitted sucessfully, none 0
12837  * otherwise.
12838  **/
12839 int
12840 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12841 {
12842         int rc = 0, error;
12843         LPFC_MBOXQ_t *mboxq;
12844
12845         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12846         if (!mboxq) {
12847                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12848                                 "2763 Failed to allocate mbox for "
12849                                 "READ_FCF cmd\n");
12850                 error = -ENOMEM;
12851                 goto fail_fcf_read;
12852         }
12853         /* Construct the read FCF record mailbox command */
12854         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12855         if (rc) {
12856                 error = -EINVAL;
12857                 goto fail_fcf_read;
12858         }
12859         /* Issue the mailbox command asynchronously */
12860         mboxq->vport = phba->pport;
12861         mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12862         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12863         if (rc == MBX_NOT_FINISHED)
12864                 error = -EIO;
12865         else
12866                 error = 0;
12867
12868 fail_fcf_read:
12869         if (error && mboxq)
12870                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12871         return error;
12872 }
12873
12874 /**
12875  * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12876  * @phba: pointer to lpfc hba data structure.
12877  * @fcf_index: FCF table entry offset.
12878  *
12879  * This routine is invoked to read an FCF record indicated by @fcf_index to
12880  * determine whether it's eligible for FLOGI roundrobin failover list.
12881  *
12882  * Return 0 if the mailbox command is submitted sucessfully, none 0
12883  * otherwise.
12884  **/
12885 int
12886 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12887 {
12888         int rc = 0, error;
12889         LPFC_MBOXQ_t *mboxq;
12890
12891         mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12892         if (!mboxq) {
12893                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12894                                 "2758 Failed to allocate mbox for "
12895                                 "READ_FCF cmd\n");
12896                                 error = -ENOMEM;
12897                                 goto fail_fcf_read;
12898         }
12899         /* Construct the read FCF record mailbox command */
12900         rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12901         if (rc) {
12902                 error = -EINVAL;
12903                 goto fail_fcf_read;
12904         }
12905         /* Issue the mailbox command asynchronously */
12906         mboxq->vport = phba->pport;
12907         mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12908         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12909         if (rc == MBX_NOT_FINISHED)
12910                 error = -EIO;
12911         else
12912                 error = 0;
12913
12914 fail_fcf_read:
12915         if (error && mboxq)
12916                 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12917         return error;
12918 }
12919
12920 /**
12921  * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12922  * @phba: pointer to lpfc hba data structure.
12923  *
12924  * This routine is to get the next eligible FCF record index in a round
12925  * robin fashion. If the next eligible FCF record index equals to the
12926  * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12927  * shall be returned, otherwise, the next eligible FCF record's index
12928  * shall be returned.
12929  **/
12930 uint16_t
12931 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12932 {
12933         uint16_t next_fcf_index;
12934
12935         /* Search start from next bit of currently registered FCF index */
12936         next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12937                                         LPFC_SLI4_FCF_TBL_INDX_MAX;
12938         next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12939                                        LPFC_SLI4_FCF_TBL_INDX_MAX,
12940                                        next_fcf_index);
12941
12942         /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12943         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12944                 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12945                                                LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12946
12947         /* Check roundrobin failover list empty condition */
12948         if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12949                 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12950                                 "2844 No roundrobin failover FCF available\n");
12951                 return LPFC_FCOE_FCF_NEXT_NONE;
12952         }
12953
12954         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12955                         "2845 Get next roundrobin failover FCF (x%x)\n",
12956                         next_fcf_index);
12957
12958         return next_fcf_index;
12959 }
12960
12961 /**
12962  * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12963  * @phba: pointer to lpfc hba data structure.
12964  *
12965  * This routine sets the FCF record index in to the eligible bmask for
12966  * roundrobin failover search. It checks to make sure that the index
12967  * does not go beyond the range of the driver allocated bmask dimension
12968  * before setting the bit.
12969  *
12970  * Returns 0 if the index bit successfully set, otherwise, it returns
12971  * -EINVAL.
12972  **/
12973 int
12974 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12975 {
12976         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12977                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12978                                 "2610 FCF (x%x) reached driver's book "
12979                                 "keeping dimension:x%x\n",
12980                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12981                 return -EINVAL;
12982         }
12983         /* Set the eligible FCF record index bmask */
12984         set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12985
12986         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12987                         "2790 Set FCF (x%x) to roundrobin FCF failover "
12988                         "bmask\n", fcf_index);
12989
12990         return 0;
12991 }
12992
12993 /**
12994  * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
12995  * @phba: pointer to lpfc hba data structure.
12996  *
12997  * This routine clears the FCF record index from the eligible bmask for
12998  * roundrobin failover search. It checks to make sure that the index
12999  * does not go beyond the range of the driver allocated bmask dimension
13000  * before clearing the bit.
13001  **/
13002 void
13003 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
13004 {
13005         if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
13006                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13007                                 "2762 FCF (x%x) reached driver's book "
13008                                 "keeping dimension:x%x\n",
13009                                 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
13010                 return;
13011         }
13012         /* Clear the eligible FCF record index bmask */
13013         clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
13014
13015         lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13016                         "2791 Clear FCF (x%x) from roundrobin failover "
13017                         "bmask\n", fcf_index);
13018 }
13019
13020 /**
13021  * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
13022  * @phba: pointer to lpfc hba data structure.
13023  *
13024  * This routine is the completion routine for the rediscover FCF table mailbox
13025  * command. If the mailbox command returned failure, it will try to stop the
13026  * FCF rediscover wait timer.
13027  **/
13028 void
13029 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
13030 {
13031         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13032         uint32_t shdr_status, shdr_add_status;
13033
13034         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13035
13036         shdr_status = bf_get(lpfc_mbox_hdr_status,
13037                              &redisc_fcf->header.cfg_shdr.response);
13038         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13039                              &redisc_fcf->header.cfg_shdr.response);
13040         if (shdr_status || shdr_add_status) {
13041                 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
13042                                 "2746 Requesting for FCF rediscovery failed "
13043                                 "status x%x add_status x%x\n",
13044                                 shdr_status, shdr_add_status);
13045                 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
13046                         spin_lock_irq(&phba->hbalock);
13047                         phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
13048                         spin_unlock_irq(&phba->hbalock);
13049                         /*
13050                          * CVL event triggered FCF rediscover request failed,
13051                          * last resort to re-try current registered FCF entry.
13052                          */
13053                         lpfc_retry_pport_discovery(phba);
13054                 } else {
13055                         spin_lock_irq(&phba->hbalock);
13056                         phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
13057                         spin_unlock_irq(&phba->hbalock);
13058                         /*
13059                          * DEAD FCF event triggered FCF rediscover request
13060                          * failed, last resort to fail over as a link down
13061                          * to FCF registration.
13062                          */
13063                         lpfc_sli4_fcf_dead_failthrough(phba);
13064                 }
13065         } else {
13066                 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
13067                                 "2775 Start FCF rediscover quiescent timer\n");
13068                 /*
13069                  * Start FCF rediscovery wait timer for pending FCF
13070                  * before rescan FCF record table.
13071                  */
13072                 lpfc_fcf_redisc_wait_start_timer(phba);
13073         }
13074
13075         mempool_free(mbox, phba->mbox_mem_pool);
13076 }
13077
13078 /**
13079  * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
13080  * @phba: pointer to lpfc hba data structure.
13081  *
13082  * This routine is invoked to request for rediscovery of the entire FCF table
13083  * by the port.
13084  **/
13085 int
13086 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
13087 {
13088         LPFC_MBOXQ_t *mbox;
13089         struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
13090         int rc, length;
13091
13092         /* Cancel retry delay timers to all vports before FCF rediscover */
13093         lpfc_cancel_all_vport_retry_delay_timer(phba);
13094
13095         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13096         if (!mbox) {
13097                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13098                                 "2745 Failed to allocate mbox for "
13099                                 "requesting FCF rediscover.\n");
13100                 return -ENOMEM;
13101         }
13102
13103         length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
13104                   sizeof(struct lpfc_sli4_cfg_mhdr));
13105         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13106                          LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
13107                          length, LPFC_SLI4_MBX_EMBED);
13108
13109         redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
13110         /* Set count to 0 for invalidating the entire FCF database */
13111         bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
13112
13113         /* Issue the mailbox command asynchronously */
13114         mbox->vport = phba->pport;
13115         mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
13116         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
13117
13118         if (rc == MBX_NOT_FINISHED) {
13119                 mempool_free(mbox, phba->mbox_mem_pool);
13120                 return -EIO;
13121         }
13122         return 0;
13123 }
13124
13125 /**
13126  * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
13127  * @phba: pointer to lpfc hba data structure.
13128  *
13129  * This function is the failover routine as a last resort to the FCF DEAD
13130  * event when driver failed to perform fast FCF failover.
13131  **/
13132 void
13133 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
13134 {
13135         uint32_t link_state;
13136
13137         /*
13138          * Last resort as FCF DEAD event failover will treat this as
13139          * a link down, but save the link state because we don't want
13140          * it to be changed to Link Down unless it is already down.
13141          */
13142         link_state = phba->link_state;
13143         lpfc_linkdown(phba);
13144         phba->link_state = link_state;
13145
13146         /* Unregister FCF if no devices connected to it */
13147         lpfc_unregister_unused_fcf(phba);
13148 }
13149
13150 /**
13151  * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
13152  * @phba: pointer to lpfc hba data structure.
13153  *
13154  * This function read region 23 and parse TLV for port status to
13155  * decide if the user disaled the port. If the TLV indicates the
13156  * port is disabled, the hba_flag is set accordingly.
13157  **/
13158 void
13159 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
13160 {
13161         LPFC_MBOXQ_t *pmb = NULL;
13162         MAILBOX_t *mb;
13163         uint8_t *rgn23_data = NULL;
13164         uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
13165         int rc;
13166
13167         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13168         if (!pmb) {
13169                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13170                         "2600 lpfc_sli_read_serdes_param failed to"
13171                         " allocate mailbox memory\n");
13172                 goto out;
13173         }
13174         mb = &pmb->u.mb;
13175
13176         /* Get adapter Region 23 data */
13177         rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
13178         if (!rgn23_data)
13179                 goto out;
13180
13181         do {
13182                 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
13183                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
13184
13185                 if (rc != MBX_SUCCESS) {
13186                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13187                                 "2601 lpfc_sli_read_link_ste failed to"
13188                                 " read config region 23 rc 0x%x Status 0x%x\n",
13189                                 rc, mb->mbxStatus);
13190                         mb->un.varDmp.word_cnt = 0;
13191                 }
13192                 /*
13193                  * dump mem may return a zero when finished or we got a
13194                  * mailbox error, either way we are done.
13195                  */
13196                 if (mb->un.varDmp.word_cnt == 0)
13197                         break;
13198                 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
13199                         mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
13200
13201                 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
13202                         rgn23_data + offset,
13203                         mb->un.varDmp.word_cnt);
13204                 offset += mb->un.varDmp.word_cnt;
13205         } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
13206
13207         data_size = offset;
13208         offset = 0;
13209
13210         if (!data_size)
13211                 goto out;
13212
13213         /* Check the region signature first */
13214         if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
13215                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13216                         "2619 Config region 23 has bad signature\n");
13217                         goto out;
13218         }
13219         offset += 4;
13220
13221         /* Check the data structure version */
13222         if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
13223                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13224                         "2620 Config region 23 has bad version\n");
13225                 goto out;
13226         }
13227         offset += 4;
13228
13229         /* Parse TLV entries in the region */
13230         while (offset < data_size) {
13231                 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
13232                         break;
13233                 /*
13234                  * If the TLV is not driver specific TLV or driver id is
13235                  * not linux driver id, skip the record.
13236                  */
13237                 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
13238                     (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
13239                     (rgn23_data[offset + 3] != 0)) {
13240                         offset += rgn23_data[offset + 1] * 4 + 4;
13241                         continue;
13242                 }
13243
13244                 /* Driver found a driver specific TLV in the config region */
13245                 sub_tlv_len = rgn23_data[offset + 1] * 4;
13246                 offset += 4;
13247                 tlv_offset = 0;
13248
13249                 /*
13250                  * Search for configured port state sub-TLV.
13251                  */
13252                 while ((offset < data_size) &&
13253                         (tlv_offset < sub_tlv_len)) {
13254                         if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
13255                                 offset += 4;
13256                                 tlv_offset += 4;
13257                                 break;
13258                         }
13259                         if (rgn23_data[offset] != PORT_STE_TYPE) {
13260                                 offset += rgn23_data[offset + 1] * 4 + 4;
13261                                 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
13262                                 continue;
13263                         }
13264
13265                         /* This HBA contains PORT_STE configured */
13266                         if (!rgn23_data[offset + 2])
13267                                 phba->hba_flag |= LINK_DISABLED;
13268
13269                         goto out;
13270                 }
13271         }
13272 out:
13273         if (pmb)
13274                 mempool_free(pmb, phba->mbox_mem_pool);
13275         kfree(rgn23_data);
13276         return;
13277 }
13278
13279 /**
13280  * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
13281  * @vport: pointer to vport data structure.
13282  *
13283  * This function iterate through the mailboxq and clean up all REG_LOGIN
13284  * and REG_VPI mailbox commands associated with the vport. This function
13285  * is called when driver want to restart discovery of the vport due to
13286  * a Clear Virtual Link event.
13287  **/
13288 void
13289 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
13290 {
13291         struct lpfc_hba *phba = vport->phba;
13292         LPFC_MBOXQ_t *mb, *nextmb;
13293         struct lpfc_dmabuf *mp;
13294         struct lpfc_nodelist *ndlp;
13295         struct lpfc_nodelist *act_mbx_ndlp = NULL;
13296         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
13297         LIST_HEAD(mbox_cmd_list);
13298         uint8_t restart_loop;
13299
13300         /* Clean up internally queued mailbox commands with the vport */
13301         spin_lock_irq(&phba->hbalock);
13302         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
13303                 if (mb->vport != vport)
13304                         continue;
13305
13306                 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13307                         (mb->u.mb.mbxCommand != MBX_REG_VPI))
13308                         continue;
13309
13310                 list_del(&mb->list);
13311                 list_add_tail(&mb->list, &mbox_cmd_list);
13312         }
13313         /* Clean up active mailbox command with the vport */
13314         mb = phba->sli.mbox_active;
13315         if (mb && (mb->vport == vport)) {
13316                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
13317                         (mb->u.mb.mbxCommand == MBX_REG_VPI))
13318                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13319                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13320                         act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
13321                         /* Put reference count for delayed processing */
13322                         act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
13323                         /* Unregister the RPI when mailbox complete */
13324                         mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13325                 }
13326         }
13327         /* Cleanup any mailbox completions which are not yet processed */
13328         do {
13329                 restart_loop = 0;
13330                 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
13331                         /*
13332                          * If this mailox is already processed or it is
13333                          * for another vport ignore it.
13334                          */
13335                         if ((mb->vport != vport) ||
13336                                 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
13337                                 continue;
13338
13339                         if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
13340                                 (mb->u.mb.mbxCommand != MBX_REG_VPI))
13341                                 continue;
13342
13343                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13344                         if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13345                                 ndlp = (struct lpfc_nodelist *)mb->context2;
13346                                 /* Unregister the RPI when mailbox complete */
13347                                 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
13348                                 restart_loop = 1;
13349                                 spin_unlock_irq(&phba->hbalock);
13350                                 spin_lock(shost->host_lock);
13351                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13352                                 spin_unlock(shost->host_lock);
13353                                 spin_lock_irq(&phba->hbalock);
13354                                 break;
13355                         }
13356                 }
13357         } while (restart_loop);
13358
13359         spin_unlock_irq(&phba->hbalock);
13360
13361         /* Release the cleaned-up mailbox commands */
13362         while (!list_empty(&mbox_cmd_list)) {
13363                 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
13364                 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
13365                         mp = (struct lpfc_dmabuf *) (mb->context1);
13366                         if (mp) {
13367                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
13368                                 kfree(mp);
13369                         }
13370                         ndlp = (struct lpfc_nodelist *) mb->context2;
13371                         mb->context2 = NULL;
13372                         if (ndlp) {
13373                                 spin_lock(shost->host_lock);
13374                                 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13375                                 spin_unlock(shost->host_lock);
13376                                 lpfc_nlp_put(ndlp);
13377                         }
13378                 }
13379                 mempool_free(mb, phba->mbox_mem_pool);
13380         }
13381
13382         /* Release the ndlp with the cleaned-up active mailbox command */
13383         if (act_mbx_ndlp) {
13384                 spin_lock(shost->host_lock);
13385                 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
13386                 spin_unlock(shost->host_lock);
13387                 lpfc_nlp_put(act_mbx_ndlp);
13388         }
13389 }
13390
13391 /**
13392  * lpfc_drain_txq - Drain the txq
13393  * @phba: Pointer to HBA context object.
13394  *
13395  * This function attempt to submit IOCBs on the txq
13396  * to the adapter.  For SLI4 adapters, the txq contains
13397  * ELS IOCBs that have been deferred because the there
13398  * are no SGLs.  This congestion can occur with large
13399  * vport counts during node discovery.
13400  **/
13401
13402 uint32_t
13403 lpfc_drain_txq(struct lpfc_hba *phba)
13404 {
13405         LIST_HEAD(completions);
13406         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
13407         struct lpfc_iocbq *piocbq = 0;
13408         unsigned long iflags = 0;
13409         char *fail_msg = NULL;
13410         struct lpfc_sglq *sglq;
13411         union lpfc_wqe wqe;
13412
13413         spin_lock_irqsave(&phba->hbalock, iflags);
13414         if (pring->txq_cnt > pring->txq_max)
13415                 pring->txq_max = pring->txq_cnt;
13416
13417         spin_unlock_irqrestore(&phba->hbalock, iflags);
13418
13419         while (pring->txq_cnt) {
13420                 spin_lock_irqsave(&phba->hbalock, iflags);
13421
13422                 piocbq = lpfc_sli_ringtx_get(phba, pring);
13423                 sglq = __lpfc_sli_get_sglq(phba, piocbq);
13424                 if (!sglq) {
13425                         __lpfc_sli_ringtx_put(phba, pring, piocbq);
13426                         spin_unlock_irqrestore(&phba->hbalock, iflags);
13427                         break;
13428                 } else {
13429                         if (!piocbq) {
13430                                 /* The txq_cnt out of sync. This should
13431                                  * never happen
13432                                  */
13433                                 sglq = __lpfc_clear_active_sglq(phba,
13434                                                  sglq->sli4_xritag);
13435                                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13436                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13437                                         "2823 txq empty and txq_cnt is %d\n ",
13438                                         pring->txq_cnt);
13439                                 break;
13440                         }
13441                 }
13442
13443                 /* The xri and iocb resources secured,
13444                  * attempt to issue request
13445                  */
13446                 piocbq->sli4_xritag = sglq->sli4_xritag;
13447                 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13448                         fail_msg = "to convert bpl to sgl";
13449                 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13450                         fail_msg = "to convert iocb to wqe";
13451                 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13452                         fail_msg = " - Wq is full";
13453                 else
13454                         lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13455
13456                 if (fail_msg) {
13457                         /* Failed means we can't issue and need to cancel */
13458                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13459                                         "2822 IOCB failed %s iotag 0x%x "
13460                                         "xri 0x%x\n",
13461                                         fail_msg,
13462                                         piocbq->iotag, piocbq->sli4_xritag);
13463                         list_add_tail(&piocbq->list, &completions);
13464                 }
13465                 spin_unlock_irqrestore(&phba->hbalock, iflags);
13466         }
13467
13468         /* Cancel all the IOCBs that cannot be issued */
13469         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13470                                 IOERR_SLI_ABORTED);
13471
13472         return pring->txq_cnt;
13473 }